Section 12.3 State Machine in Action
Traffic light systems are crucial for regulating traffic. These systems use carefully coordinated signals to ensure safety for both vehicles and pedestrians. In the previous section, the traffic light system was modeled in an overly simplistic way. This section adds complexity to account for pedestrian presence, ensuring safe crossings while maintaining smooth traffic flow.
Subsection 12.3.1 Traffic Light Controller: Problem Overview
Let’s design a traffic light system for a two-way road with pedestrian crossings. This system coordinates the movement of vehicles and pedestrians using lights to indicate when vehicles can proceed, slow down, or stop, and when pedestrians can cross safely. Vehicle traffic lights include three signals: Red, Yellow, and Green. For simplicity, the pedestrian lights also use three signals: red, yellow, and green. Signal transitions are governed by timers, as described in the previous section, with each timer triggering a transition event after a predefined duration.
The system must ensure safety and smooth traffic flow by coordinating appropriate traffic and pedestrian light configurations. Initially, vehicle traffic proceeds with a green light, while pedestrian crossing is prohibited with a red light.
Subsection 12.3.2 Elements of the FSM Model
The goal here is to define a state machine model that can control this traffic light system, construct it, then put it under test. This system has different configurations of lights: Red (
R
), Yellow (Y
), and Green (G
) for traffic, and red (r
), yellow (y
), and green (g
) for pedestrians. Note that not all possible combinations makes sense.For inputs, the system leverage four independent timers with different preset durations and triggering different use cases as follows:
- 30sec timer
t30s
: drives the traffic light transition fromG
toY
. The pedestrian light remainsr
and unchanged. - 5sec timer
t5s
: drives the traffic light transition fromY
toR
, and the pedestrian light transition fromr
tog
. - 20sec timer
t20s
: drives the pedestrian light transition fromg
toy
, while the traffic light remainsR
and unchanged. - 10sec timer
t10s
: drives the pedestrian light transition fromy
tor
, and the traffic light transition fromR
back toG
.
From the above timers and lights configurations, the following set of 4 distinct states emerges:
- State
Yr
: Where the traffic light is Yellow, Pedestrian light is red. - State
Rg
: Where the traffic light is Red, Pedestrian light is green. - State
Ry
: Where the traffic light is Red, Pedestrian light is yellow. - State
Gr
: Where the traffic light is Green, Pedestrian light is red.
Finally, the system’s outputs corresponding to each of the above are the light configurations and would be similar to the states:
(Y,r)
: Traffic light turning Yellow and the Pedestrian light remains red.(R,g)
: Traffic light turning Red and the Pedestrian light turning green.(R,y)
: Traffic light remains Red and the Pedestrian light turning yellow.(G,r)
: Traffic light turning Green and the Pedestrian light turning red.
The following table summarize the elements of the new traffic light FSM.
next | output | |||||||
---|---|---|---|---|---|---|---|---|
current | \(t_{5s}\) | \(t_{10s}\) | \(t_{20s}\) | \(t_{30s}\) | \(t_{5s}\) | \(t_{10s}\) | \(t_{20s}\) | \(t_{30s}\) |
\(Gr\) | \(-\) | \(-\) | \(-\) | \(Yr\) | \(-\) | \(-\) | \(-\) | \((Y,r)\) |
\(Yr\) | \(Rg\) | \(-\) | \(-\) | \(-\) | \((R,g)\) | \(-\) | \(-\) | \(-\) |
\(Rg\) | \(-\) | \(-\) | \(Ry\) | \(-\) | \(-\) | \(-\) | \((R,y)\) | \(-\) |
\(Ry\) | \(-\) | \(Gr\) | \(-\) | \(-\) | \(-\) | \((G,r)\) | \(-\) | \(-\) |
The symbol \(-\) indicates no state change, and no output change.
Subsection 12.3.3 Construct the FSM
Subsection 12.3.4 Display the State Transition Graph
The FSM is visualized as before (a directed graph with nodes representing states and edges showing transitions).
Subsection 12.3.5 Simulate a Full Cycle Run of the FSM
The simulation starts in the initial state (
Gr
) and transitions through all states.One limitation of using Sage built-in modules is that it fails handling transitions that were not defined in the FSM. For instance, in the previous example, if the timer durations pattern for the input does not match the defined transitions, the run will raise an exception, instead of gracefully handling the exception and defaulting to no transition (similarly, the same issue arise if attempting to run the FSM starting at state that is not part of the FSM definition).