Main Content

Simulate Sequence Diagrams for Traffic Light Example

This demonstrates how to simulate a System Composer™ architecture model of a traffic light and verify that the model simulation results match the interactions within the sequence diagrams of the model. The example model uses blocks from Stateflow®. If you do not have a Stateflow license, you can open and simulate the model but only make basic changes such as modifying block parameters.

This traffic light example uses sequence diagrams to describe the process of pedestrians crossing an intersection.

  1. The traffic signal cycles from red to green to yellow.

  2. When the pedestrian crossing button is pressed, if the traffic signal is green, the traffic signal transitions from yellow to red for a limited time.

  3. The pedestrians cross while the walk signal is active.

Open the model.

model = systemcomposer.openModel("TLExample");

The top architecture model for the traffic light example.

Open the Architecture Views Gallery to view the sequence diagrams.

openViews(model)

Simulate Inhibit Sequence Diagram

In the View Browser, select the Inhibit sequence diagram. For more information on how to construct this sequence diagram, see Author Sequence Diagram for Traffic Light Example.

Inhibit sequence diagram before execution.

To simulate the sequence diagram until the next message, select the Next Message option in the toolstrip. When the message event switchEvent occurs, the switch lifeline activates.

Inhibit sequence diagram with one message constraint satisfied.

Select Continue to continue until the end. Since the inhibit flag is equal to 0, the first operand of the Alt fragment activates. For more information on the Alt fragment, see Author Sequence Diagram Fragments. The switch lifeline sends a message to the controller lifeline to change the traffic lamp via the lampcontroller lifeline to stop traffic and allow the pedestrians to cross the intersection.

Fully executed inhibit sequence diagram.

If the inhibit flag is set to 1, the switch lifeline bypasses the controller and sends the signal directly to the lampcontroller lifeline. This action means that pedestrian crossing is not controlled by a walk signal on this intersection.

Simulate PressDetection Sequence Diagram Programmatically

In the View Browser, select the PressDetection sequence diagram.

Press detection sequence diagram before exectuion.

Observe the PressDetection sequence diagram during model simulation using a Simulink.SimulationInput object for the sim function. The ObservedSequenceDiagrams model configuration parameter specifies which sequence diagram to simulate. You can programmatically simulate only one sequence diagram at a time. To use sim to simulate a sequence diagram, set ObservedSequenceDiagrams for the simulation.

simIn = Simulink.SimulationInput("TLExample");
simIn = setModelParameter(simIn,"ObservedSequenceDiagrams","PressDetection",...
    "ObservedSequenceDiagramsOut","sequenceDiagramOutput");
simOut = sim(simIn);

ObservedSequenceDiagramsOut is a property of the Simulink.SimulationOutput object captured in simOut. The object contains these properties:

  • Name: Name of the sequence diagram being simulated.

  • Completed: Whether the simulation was completed.

  • NumErrors: Number of errors found, which counts the number of red exclamation marks next to messages in your sequence diagram after simulation. The red marks indicate that the message triggers were satisfied, but the constraints were not met.

For ease of use, you can rename the optional ObservedSequenceDiagramsOut parameter to sequenceDiagramOutput, or any valid MATLAB® variable name.

sequenceDiagramOut = simOut.sequenceDiagramOutput
sequenceDiagramOut = struct with fields:
         Name: 'PressDetection'
    Completed: 1
    NumErrors: 0

Messages where the trigger is satisfied and the constraints are met turn green with a check mark. Since all message constraints are met, the number of errors is zero.

Fully executed press detection sequence diagram.

When a pedestrian presses the crossing button, the value of the signal sw rises to 1. When this action happens, the poller lifeline sends the message switchEvent to the switch lifeline. This action alerts the switch lifeline that a pedestrian is waiting so the switch lifeline can alert the controller lifeline. The traffic light then turns red to stop traffic, and the walk signal turns on.

Simulate PedestrianCross Sequence Diagram

In the View Browser, select the PedestrianCross sequence diagram.

Pedestrian cross sequence diagram before execution.

To simulate the sequence diagram until the next message, select the Next Message option in the toolstrip. The value of the message traffic is 1, which indicates that the traffic light color is red.

Pedestrian cross sequence diagram with one message constrant satisfied.

The Sequence Viewer describes the simulation events as they occur in the model as the sequence diagram describes what is expected to occur. On the toolstrip, in the Simulation tab, select Log Events, then launch the Sequence Viewer from the same location. See that the simulation pauses when traffic is 1.

Pedestrian cross sequence diagram and model execution in the Sequence Viewer.

Select Next Message three more times to simulate until the traffic light completes one loop from green to yellow to red again. For more information on the Loop fragment, see Author Sequence Diagram Fragments.

Pedestrian cross sequence diagram with more message constrants satisfied.

View the corresponding message events in the Sequence Viewer.

Pedestrian cross sequence diagram and model execution in the Sequence Viewer.

Select Continue to continue until the end. The pedestrian crossing signal allows the pedestrians cross by turning the traffic light red. Then, the traffic light continues its cycle.

Fully executed pedestrian cross sequence diagram.

View the corresponding message events for the pedestrian crossing messages in the Sequence Viewer.

Pedestrian cross sequence diagram and model execution in the Sequence Viewer.

Simulate and Detect Errors with SignalSequence Sequence Diagram

In the View Browser, select the SignalSequence sequence diagram.

Signal sequence diagram before execution.

Click Run to simulate the sequence diagram to the end. Messages where the conditions are met turn green with a checkmark.

Fully executed Signal sequence diagram.

This step requires a Stateflow license.

Return to the TLExample model. Double-click the lampController component to view the state chart. In the ped subchart, introduce an error into model execution by changing pedColor=trafficColors.RED to pedColor=trafficColors.YELLOW. Save the TLExample model.

The Stateflow chart for the lampController component shows the ped subchart with pedColor=trafficColors.YELLOW circled in green.

Return to the View Browser. For the SignalSequence sequence diagram, click Clear Results to clear the green checkmarks and reset sequence diagram simulation. Click Run to simulate the SignalSequence sequence diagram again.

Signal sequence diagram fully executed with one message not met by model execution.

For the first message from the lampController lifeline to the ped lamp lifeline, the constraints specified by the sequence diagram are not met by the model execution.

See Also

| |

Related Topics