Exercise: Traffic light simulation

From FBSwiki
Jump to: navigation, search
Intersection.png

In this problem you will build a finite state controller for a traffic light system at an intersection. Your controller should take as inputs the sensor signals on the road that detect whether a car is present and should have as outputs the colors of the various signals. For simplicity, assume that all traffic is two-way (i.e. don't worry about left turn lanes, etc.). Implement your solution as a MATLAB function, which you can assume is called every 1 second of intersection operation.

Controller Specification

Trafficlight.png

You are to implement a traffic light controller for a standard four-way intersection. This controller should be implemented as a MATLAB function, with the following 4 inputs and 2 outputs:

  • First input: North side vehicle sensor (number in the set {0, 1})
  • Second input: South side vehicle sensor (number in the set {0, 1})
  • Third input: West side vehicle sensor (number in the set {0, 1})
  • Fourth input: East side vehicle sensor (number in the set {0, 1})
  • First output: North-South light color (string in the set {'red','yellow',green'})
  • Second output: East-West light color (string in the set {'red','yellow',green'})

The diagram at the right gives a high level view of the operation of your controller.

Notes

  1. A MATLAB function is a text file containing MATLAB commands which begins with a MATLAB "function" declaration. Type "help function" at the MATLAB command prompt for information on how to construct a MATLAB function.
  2. Your MATLAB function will be called every second, and you will need some timing information for your intersection to run smoothly. Declare a variable as "persistent" that will represent time. See "help persistent" for why you will need to do this. Your function will be called every second, so this variable can be treated as an integer.
  3. Be sure to give the cars enough time to travel through the intersection after the light turns green, and make your yellow lights last long enough to give fair warning to approaching cars that the light is turning red.
  4. You can implement your controller with a series of if-else statements or with a "switch" statement, and/or any other means you see fit.
  5. The sensors in the road will not always trigger (e.g. for cars that stop short, motorcycles, etc.), so be sure to include timeouts on your red lights so no vehicle waits forever.