Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Pedestrian Crossing Traffic Light in Stateflow

Design a traffic light with a pedestrian button in Stateflow: states, temporal logic, a parallel request memory, safety checks, tests and code generation, with runnable stand-ins.

A traffic light looks simple, but it contains everything that makes real state machines hard: timing, requests that arrive at any time, safety (never green for both directions) and fault handling. This project designs a pedestrian crossing controller in Stateflow, from requirements to tests. Stateflow does not run on this site, so each design decision has a Python stand-in that we use as the executable specification. The stand-ins are our own code, not Stateflow output.

Requirements

  1. REQ-1: The vehicle light cycles Green, Yellow, Red, then Green again.
  2. REQ-2: Green lasts at least 20 s before a pedestrian request is served.
  3. REQ-3: Yellow lasts 3 s; the all-red clearance before the walk signal lasts 2 s.
  4. REQ-4: A pedestrian request is remembered (button press) until served and may arrive at any time.
  5. REQ-5: Walk lasts 10 s, then Flashing Don't Walk 5 s, then Don't Walk.
  6. REQ-6: Vehicles and pedestrians shall never have go signals together (safety).
  7. REQ-7: On a fault input the lights flash yellow for vehicles and show Don't Walk.

Design with hierarchy and parallel states

stateDiagram-v2
    [*] --> Normal
    state Normal {
        [*] --> VehicleGreen
        VehicleGreen --> VehicleYellow: after(20 sec) and request
        VehicleYellow --> AllRed: after(3 sec)
        AllRed --> Walk: after(2 sec)
        Walk --> FlashDontWalk: after(10 sec)
        FlashDontWalk --> VehicleGreen: after(5 sec)
    }
    Normal --> Fault: fault
    Fault --> Normal: not fault

The Normal superstate holds the sequence; a transition from Normal to Fault leaves from every child at once.

Design points (see hierarchy and parallel states):

  • A parent Normal and a state Fault at the top level; the fault transition leaves the parent so it applies in every phase.
  • A parallel state Request next to the sequence stores the button press: Idle -> Pending [button], and the sequence clears it (request = false) when Walk starts. Because the button can be pressed in any phase, a flag set by a parallel machine is cleaner than sampling the button only in VehicleGreen.
  • All timing uses after(n, sec) in a chart with a discrete sample time of 0.1 s (temporal logic).
  • Outputs are enumerated types (VehicleLight: Green, Yellow, Red, FlashYellow; PedLight: Walk, FlashDW, DontWalk) defined as Simulink enumerations, which make code and logs readable.
  • Output assignment in each state's entry action, not in transition actions, so the lights always match the state.

The executable stand-in

Python Stand-in: the crossing controller as a time-stepped state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the log against the requirements: the request at 5 s is stored; the light stays green until it has been green for 20 s; then Yellow (3 s), AllRed (2 s), Walk (10 s), FlashDW (5 s) and Green again.

Checking the safety requirement

REQ-6 says the vehicles and pedestrians must never have go signals together. In Simulink you would add a Test Assessment block or a Requirements Table row that states: not (vehicle == Green && ped == Walk) for all time. The stand-in does the equivalent by exhaustively simulating many request and fault patterns:

Python Stand-in: random scenarios check that Green and Walk never coincide

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The stand-in found zero violations because the design only reaches Walk through AllRed. In Simulink, prove it with Design Verifier (formal verification) and add the assessment to the regression suite.

  1. Create a model crossing with a Chart block, inputs button and fault (boolean) and outputs vehicle and ped (enumerations). Define the enumeration classes in MATLAB (classdef VehicleLight < Simulink.IntEnumType) or in the data dictionary.
  2. Draw the top-level machine: Normal (exclusive decomposition) containing Green, Yellow, AllRed, Walk, FlashDW, and Fault. Add the parallel Request machine by making the top level parallel with two children: Lights (containing Normal and Fault) and Request.
  3. Put entry actions that set the outputs in each state, after(n, sec) conditions on the transitions, and the fault transition from Normal to Fault.
  4. Set the chart's Update method to Discrete, Sample time 0.1, and Initialize outputs every time chart wakes up off.
  5. Add a Pulse Generator for a button press (amplitude 1, period 60, pulse width 1) and a Step for the fault; add Scopes (display enumerations with a Stairs plot) and the Stateflow animation.
  6. Make the chart testable with a harness: Test Sequence presses the button at different times (during Green before 20 s, after 20 s, during Yellow), triggers a fault in each state, and releases it.

Test list

Test Expected
Button before 20 s of green Switch to Yellow only after 20 s
Button during Yellow Remembered, served in the same cycle (request flag stays set until Walk)
No button Stays green forever
Fault during Walk FlashYellow and DontWalk immediately
Fault released Goes through AllRed to restart
Two buttons within a cycle Only one Walk phase

Run with model coverage (decision, condition and execution coverage on the chart): the Fault -> AllRed and AllRed -> Green (no request) transitions are easy to forget.

Code generation notes

  • Enumerations become C enum types, and after(n, sec) becomes a per-state tick counter that is reset on entry.
  • With a sample time of 0.1 s the generated function crossing_step() is called every 100 ms by the controller's timer interrupt.
  • Use a watchdog and run PIL to verify timing on the target microcontroller.

Extensions

  • Add a cross traffic direction (two vehicle lights) with a shared all-red phase and a parallel safety monitor.
  • Add a night mode controlled by a real-time clock input.
  • Add an accessible pushbutton with an audible signal state.
  • Use a State Transition Table version of the chart and compare review effort.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the pedestrian request modeled in a parallel state?

    A separate always-active machine captures the press whatever the lights are doing.
  2. 2. Where should the light outputs be assigned?

    Entry actions keep outputs and states consistent.
  3. 3. How does the controller restart after a fault?

    All-red is the safe state that precedes any other phase.
  4. 4. Which type is best for the light outputs?

    Enumerations are readable in the model, in logs and in generated code.

Summary

  • Write requirements first; make safety requirements assessable (never Green and Walk together).
  • Use hierarchy for phases, a transition from the parent for faults, and a parallel state for requests that arrive at any time.
  • Use temporal logic for durations, enumerations for outputs, and entry actions for outputs.
  • Test every phase and fault, measure coverage, prove the safety property and generate code for a fixed 0.1 s step.

Next: Project: DC motor position control