Transcribed Text
Traffic Light Controller
Objective
This lab has these major objectives:
1) The understanding and implementing of indexed data structures;
2) Learning how to create a segmented software system;
3) The study of real-time synchronization by designing a finite state machine
controller.
Software skills you will learn include advanced indexed addressing, linked data
structures, creating fixed-time delays using the SysTick timer, and debugging realtime systems. Please read the entire lab before starting. Also, read the FSM section
in the textbook (or online book).
Introduction
Consider a 4-corner intersection as shown in Figure 1. There are two one-way streets
are labeled South (cars travel South) and West (cars travel West). There are three
inputs to your LaunchPad, two are car sensors, and one is a pedestrian sensor. The
South car sensor will be true (3.3V) if one or more cars are near the intersection on
the South road. Similarly, the West car sensor will be true (3.3V) if one or more cars
are near the intersection on the West road. The Walk sensor will be true (3.3V) if a
pedestrian is present and he or she wishes to cross in any direction. This walk sensor
is different from a walk button on most real intersections. This means when you are
testing the system, you must push and hold the walk sensor until the FSM recognizes
the presence of the pedestrian. Similarly, you will have to push and hold the car
sensor until the FSM recognizes the presence of the car. In this simple system, if the
walk sensor is +3.3V, there is pedestrian to service, and if the walk sensor is 0V, there
are no people who wish to walk. In a similar fashion, when a car sensor is 0V, it means
no cars are waiting to enter the intersection. You will interface 6 LEDs that represent
the two Red-Yellow-Green traffic lights, and you will use the PF3 green LED for the
“walk” light and the PF1 red LED for the “don’t walk” light. When the “walk”
condition is signified, pedestrians are allowed to cross. When the “don’t walk” light
flashes (and the two traffic signals are red), pedestrians should hurry up and finish
crossing. When the “don’t walk” condition is on steady, pedestrians should not enter
the intersection.
Figure 1: Traffic Light Intersection.
Traffic should not be allowed to crash. I.e., there should not be only a green or only a
yellow LED on one road at the same time there is only a green or only a yellow LED
on the other road. You should exercise common sense when assigning the length of
time that the traffic light will spend in each state. Each traffic light pattern must be
on for at least ½ second but for at most 5 seconds. Cars should not be allowed to hit
the pedestrians. The walk sequence should be realistic, showing three separate
conditions: 1) “walk”, 2) “hurry up” using a flashing LED, and 3) “don’t walk”. You
may assume the three sensors remain active for as long as service is required. The
“hurry up” flashing should occur at least twice but at most 4 times.
There is no single, “best” way to implement your system. A “good” solution will have
about 9 to 30 states in the finite state machine and provides for input dependence.
Try not to focus on the civil engineering issues. I.e., first build a quality computer
engineering solution that is easy to understand and easy to change. Because we have
three inputs, there will be 8 next state links. One way to draw the FSM graph to
make it easier to read is to use X to signify don’t care. For example, compare the two
FSM graphs in Figure 2. Drawing two arrows labeled 01 and 11 is the same as
drawing one arrow with the label X1.
South
West
R
G
Y
R Y G G
Walk
Don’t walk
PF3
PF1
R
Walk
Figure 2: FSM drawn with a short hand format (refer the textbook chapter 6 or online book).
Procedure
The basic approach to this lab will be to first develop and debug your system using
the simulator. After the software is debugged, you will interface actual lights and
switches to the LaunchPad and run your software on the real microcontroller. As you
have experienced, the simulator requires different amount of actual time as compared
to simulated time. On the other hand, the correct simulation time is maintained in
the SysTick timer, which is decremented every cycle of simulation time. The
simulator speed depends on the amount of information it needs to update into the
windows. Because we do not want to wait the minutes required for an actual
intersection, the cars in this traffic intersection travel much faster than “real” cars.
In other words, you are encouraged to adjust the time delays so that the operation of
your machine is convenient for you to debug
Part a:
1. First you will run the Keil Project file.
This lab starter project includes the template for all the functions that you will write.
The comments describe the desired operation of that function.
2. Decide which port pins you will use for the inputs and outputs. Avoid the pins
with hardware already connected. Run the starter code in the simulator to see
which ports are available for the lights and switches; these choices are listed
in Tables 1. The “don’t walk” and “walk” lights must be PF1 and PF3
respectively, but where to attach the others have some flexibility. Table 1
shows you three possibilities for how you can connect the six LEDs that form
goN
30
100001
00,10 01,11
waitN
5
100010
goE
30
001100
waitE
5
010100
00,01,
10,11 10,11
00,01
00,01,10,11
goN
30
100001
X0 X1
waitN
5
100010
goE
30
001100
waitE
5
010100
XX 1X
0X
XX
the traffic lights and three possibilities for how you can connect the three
positive logic switches that constitute the input sensors.
Table 1: Possible ports to interface lights and sensors.
Red east/west PB5
Yellow east/west PB4
Green east/west PB3
Red north/south PB2
Yellow north/south PB1
Green north/south PB0
Walk sensor PE2
North/south sensor PE1
East/west sensor PE0
Note: You might have to make hardware changes if you are using ports other than
the ones that are listed above. Also, make sure to use the correct resistor values for
the LEDs and switches.
Part b:
1. Design a finite state machine that implements a good traffic light system.
Draw a graphical picture of your finite state machine showing the various
states, inputs, outputs, wait times and transitions.
2. Write and debug the C code that implements the traffic light control system.
During the debugging phase with the simulator, use the logic analyzer to
visualize the input/output behavior of your system.
3. After you have debugged your system in simulation mode, you will implement
it on the real board. Use the same ports you used during simulation. The first
step is to interface three push button switches for the sensors. You should
implement positive logic switches. Please do not place or remove wires on the
protoboard while the power is on.
4. The next step is to build the six LED output circuits. Build the system
physically in a shape that matches a traffic intersection. You will use the PF3-
2-1 LED interface for the walk light (green for walk and red for don’t walk).
Write a simple main program to test the LED interface)
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
// ***** 1. Pre-processor Directives Section *****
#include "TExaS.h"
#include "tm4c123gh6pm.h"
// ***** 2. Global Declarations Section *****
struct State {
unsigned long OutB; // output for Port B
unsigned long OutF; // output for Port F
unsigned long Time; // duration of the state
unsigned long Next[8];}; // Next state
typedef const struct State SType;
#define goWest 0
#define waitWest 1
#define goSouth 2
#define waitSouth 3
#define walk 4
#define flash1 5
#define flash2 6
#define flash3 7
#define flash4 8
#define alloff 9
SType FSM[10] = {
{ 0x0C,0x02,100, { goWest,goWest,waitWest,waitWest,waitWest,waitWest,waitWest,waitWest } },
// goWest
{ 0x14,0x02,50, { goSouth,goSouth,goSouth,goSouth,walk,walk,goSouth,goSouth } },
// waitWest
{ 0x21,0x02,100, { goSouth,waitSouth,goSouth,waitSouth,waitSouth,waitSouth,waitSouth,waitSouth} },
// goSouth
{ 0x22,0x02,50, { goWest,goWest,goWest,goWest,walk,walk,walk,walk } },
// waitSouth
{ 0x24,0x01,100, { walk,flash1,flash1,flash1,walk,flash1,flash1,flash1 } },
// walk
{ 0x24,0x02,30, { flash2,flash2,flash2,flash2,flash2,flash2,flash2,flash2 } },
/ flash1
{ 0x24,0x00,30, { flash3,flash3,flash3,flash3,flash3,flash3,flash3,flash3 } },
// flash2
{ 0x24,0x02,30, { flash4,flash4,flash4,flash4,flash4,flash4,flash4,flash4 } },
// flash3
{ 0x24,0x00,30, { alloff,goWest,goSouth,goWest,walk,goWest,goSouth,goWest } },
// flash4
{ 0x00,0x00,50, { alloff,goWest,goSouth,goWest,walk,goWest,goSouth,goWest } }
// alloff
};...