How to Model Traffic Light Using Stateflow in Simulink
To model a traffic light using
Stateflow in Simulink, create states for each light color (Red, Green, Yellow) and define transitions with timing conditions. Use temporal logic like after() to control state durations and simulate the traffic light cycle.Syntax
In Stateflow, you define states and transitions to model behavior. For a traffic light:
- States: Represent light colors (Red, Green, Yellow).
- Transitions: Arrows between states with conditions like
after(time, sec)to control timing. - Actions: Use
entry:orduring:to set outputs (e.g., turning lights on/off).
stateflow
state Red {
entry: light = 'Red';
during: if after(10, sec) transition to Green;
}
state Green {
entry: light = 'Green';
during: if after(15, sec) transition to Yellow;
}
state Yellow {
entry: light = 'Yellow';
during: if after(3, sec) transition to Red;
}Example
This example shows a complete Stateflow chart for a traffic light with three states cycling automatically using after() timing conditions. Each state sets the light color output.
stateflow
chart TrafficLight {
output string light;
state Red {
entry: light = 'Red';
during: if after(10, sec) transition to Green;
}
state Green {
entry: light = 'Green';
during: if after(15, sec) transition to Yellow;
}
state Yellow {
entry: light = 'Yellow';
during: if after(3, sec) transition to Red;
}
initial state Red;
}Output
Simulink simulation cycles through states: Red (10s) → Green (15s) → Yellow (3s) → Red ...
Common Pitfalls
Common mistakes when modeling traffic lights in Stateflow include:
- Not setting an initial state, causing simulation errors.
- Using incorrect timing functions or units (e.g., forgetting
secinafter()). - Missing transitions or conditions, which can cause the model to get stuck.
- Not defining outputs properly in each state, so the light color does not update.
Always verify transitions and timing carefully.
stateflow
/* Wrong: Missing initial state and incorrect timing */ state Red { entry: light = 'Red'; during: if after(10) transition to Green; /* Missing 'sec' */ } /* Right: */ initial state Red; state Red { entry: light = 'Red'; during: if after(10, sec) transition to Green; }
Quick Reference
| Concept | Description | Example |
|---|---|---|
| State | Represents a traffic light color | state Red { entry: light = 'Red'; } |
| Transition | Moves between states based on condition | if after(10, sec) transition to Green; |
| Initial State | Starting point of the chart | initial state Red; |
| Temporal Logic | Controls timing in states | after(15, sec) |
| Output | Variable to show current light | output string light; |
Key Takeaways
Define states for each traffic light color with entry actions to set outputs.
Use temporal logic like after(time, sec) to control how long each light stays on.
Always specify an initial state to start the traffic light cycle.
Check transitions carefully to avoid the model getting stuck.
Test the model in Simulink to verify the traffic light cycles correctly.