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");

2022-04-05_8-24-14.png

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.

2022-04-05_10-01-15.png

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.

2022-04-05_10-00-06.png

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.

2022-04-05_10-03-58.png

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

In the View Browser, select the PressDetection sequence diagram.

2022-04-05_8-28-24.png

Observe the PressDetection sequence diagram during model simulation using a Simulink.SimulationInput object for the sim function. The ObservedSequenceDiagrams model configuration parameter specifies which sequences diagram to observe. Use sim to set ObservedSequenceDiagrams for just the simulation.

simIn = Simulink.SimulationInput("TLExample");
simIn = setModelParameter(simIn,"ObservedSequenceDiagrams","PressDetection","ObservedSequenceDiagramsOut","sequenceDiagramOutput");
simOut = sim(simIn);
sequenceDiagramOut = simOut.sequenceDiagramOutput
sequenceDiagramOut = struct with fields:
         Name: 'PressDetection'
    Completed: 1
    NumErrors: 0

Messages where the conditions are met turn green with a checkmark.

2022-04-05_8-39-37.png

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.

TLExample_PedestrianCross.png

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.

TLExample_PedestrianCross2.png

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.

2022-06-16_14-04-31.png

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.

TLExample_PedestrianCross3.png

View the corresponding message events in the Sequence Viewer.

2022-06-16_14-07-36.png

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.

TLExample_PedestrianCross4.png

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

2022-06-16_14-09-44.png

Simulate and Detect Errors with SignalSequence Sequence Diagram

In the View Browser, select the SignalSequence sequence diagram.

2022-05-04_15-57-59.png

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

2022-05-04_16-16-01.png

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.

2022-05-04_16-21-26.png

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.

2022-05-04_16-24-41.png

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