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.
The traffic signal cycles from red to green to yellow.
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.
The pedestrians cross while the walk signal is active.
Open the model.
model = systemcomposer.openModel("TLExample");
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 Diagrams Interactively.
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.
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.
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.
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.
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.
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.
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
.
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.
View the corresponding message events 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.
View the corresponding message events for the pedestrian crossing messages in the Sequence Viewer.
Simulate and Detect Errors with SignalSequence
Sequence Diagram
In the View Browser, select the SignalSequence
sequence diagram.
Click Run to simulate the sequence diagram to the end. Messages where the conditions are met turn green with a check mark.
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.
Return to the View Browser. For the SignalSequence
sequence diagram, click Clear Results to clear the green check marks and reset sequence diagram simulation. Click Run to simulate the SignalSequence
sequence diagram again.
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.