Traffic Light SystemGithub

Final Solution Demonstration

Level 100

Design Solution

The Traffic Light System (TLS) project is a real-time system using the FreeRTOS real-time kernel operating system for microcontrollers. The FreeRTOS OS and the TLS project code are being executed on the STM32F4_DISCOVERY microcontroller. The TLS project uses 3 tasks, 4 queues, 2 helpers functions, 2 middleware functions, and a timer callback function to manage the system. The system is required to move cars as lit LEDs from left to right across 19 LEDs. Between the 7th and 8th LED, there will be an intersection where cars will be required to stop. A traffic light must be configured to allow traffic to pass through on a green light. On a yellow or red light, traffic must stop before the intersection but continue after/ in the middle of the intersection. The following is a demonstration of the final solution.

Manager Task

The Manager task stayed the same as the initial specification, continuously polling the ADC for the current potentiometer reading as set by the user. A flowRate queue is loaded with 1 of 5 flow rate settings that correspond to the 5 equal ranges for the 4096 values that the 12 bit ADC configuration can output. The Manager task generates a new car to be passed into the newCar queue (0-no car, 1-car) based on a probability proportional to the flow rate. This means that at the lowest flow rate of 1, the probability of generating a new car will be 1/5=20%, where the highest flow rate will always generate a new car with probability 5/5=100%.

TrafficLightState Task

The Traffic Light State task is different from the initial design as now it will retrieve the items from the flowRate queue to configure the next timer period along with updating the lightState queue but it will not communicate with the peripherals. It will set the duration of the red light to be inversely proportional to the duration of the green light and it will maintain a constant duration for the amber light. The callback will also simply receive the updated light state from the updatedLight queue and load it into the current lightState queue to be peeked from other tasks. This task only updates the lightState queue and the timer's duration once every time after the timer's callback function completes, based on how the queues are orchestrated.

Traffic Task

The purpose of this newly added task from the initial design document is to consistently retrieve the newest car placed in the nextCar queue by the Manager task in combination with peeking the current light state in the lightState queue. This will effectively lead to determining how the cars should be shifted on the display. This task manages a binary array where 0's represent no cars (inactive LEDs) in the display and 1's represent cars (active LEDs) in the display, prepending the next car to the array if a car is not currently in the spot. In the case that the current light state is green, the array is simply shifted by one to the right, where the tail is discarded and the new car is set to the first element in the array. In the case that the light state is amber or red, cars must be stopped at the position of the stop line in the intersection which is the 7th element in the array. The first 7 elements are shifted one by one to the right only if there is a “no car” space available. This effectively results in the cars accumulating before the intersection. The remaining 11 elements in the array are shifted to the right as per usual except for the fact that the first element in this subarray is set to a “no car” space. Since this TrafficTask is using an array of size 19 to represent the 19 car LEDs on the display, the microcontroller's shift register (SPC) is used on GPIOC by looping through the array, utilizing and adjusting the shift register to output the corresponding car to the display.

Traffic Timer

Using FreeRTOS, a one-shot timer was configured to delay between changing the state of the lights. A callback function, vCallbackFunction(), was configured for when the timer expires. Since the TLS task resets the timer and loads the lightState queue with the current value, when the timer has expired, the callback function retrieves the current state of the traffic light, updates it in the circular sequence: green → amber → red → green … After calculating the updated state of the light, the LEDs are reset/set directly on GPIOC and the updated light state is loaded into the updatedLight queue.

The following diagram outlines the program architecture and the communication streams between tasks and the peripherals:


AWS