This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Synchronize Parallel States by Broadcasting Events

Events help parallel states to coordinate with one another, allowing one state to trigger an action in another state. To synchronize parallel states in the same Stateflow® chart, broadcast events directly from one state to another. For more information on parallel states, see Model Synchronous Subsystems by Using Parallelism.

Broadcasting Local Events

A local event is a nongraphical object that can trigger transitions or actions in a parallel state of a Stateflow chart. When you broadcast an event to a state, the event takes effect in the receiving state and in any substates in the hierarchy of that state. To broadcast an event, use the send operator:

send(event_name,state_name)
event_name is the name of the event to be broadcast. state_name is an active state during the broadcast.

Example of Event Broadcasting

The Stateflow example sf_security uses local events as part of the design of a home security system.

The security system consists of an alarm and three anti-intrusion sensors (a window sensor, a door sensor, and a motion detector). After the system detects an intrusion, you have a small amount of time to disable the alarm. Otherwise, the system calls the police.

The Security System chart models each subsystem with a separate parallel state. An enabling input signal selects between the On and Off modes for the alarm, or between the Active and Disabled modes for each sensor. When enabled, each sensor monitors a triggering input signal that indicates a possible intrusion.

SubsystemStateEnabling SignalTriggering Signal
Door sensorDoorD_modeDoor_sens
Window sensorWinW_modeWin_sens
Motion detectorMotionM_modeMot_sens
AlarmAlarmAlarm_active 

If a sensor detects an intrusion while the alarm subsystem is on, then it broadcasts an Alert event with this command:

send(Alert,Alarm)
To mitigate the effect of sporadic false positives, the motion detector incorporates a debouncing design, so that only a sustained positive trigger signal produces an alarm. In contrast, the door and window sensors interpret a single positive trigger signal as an intrusion and issue an immediate alarm.

In the alarm subsystem, the Alert event causes a transition from the Idle substate to the Pending substate. When this state becomes active, a warning sound alerts occupants to the possible intrusion. If there is an accidental alarm, the occupants have a short time to disable the security system. If not disabled within that time period, the system calls the police for help.

Coordinate with Other Simulink Blocks

Stateflow charts can use events to communicate with other blocks in a Simulink® model. For instance, in the sf_security example:

  • The output events Sound and call_police drive external blocks that handle the warning sound and the call to the police. The commands for broadcasting these events occur in the Alarm.On state:

    • The command for Sound occurs as an entry action in the Pending substate.

    • The command for call_police occurs as an action in the transition between the Pending and Idle substates.

    In each case, the command to issue the output event is the name of the event.

  • The input event sl_call controls the timing of the motion detector debouncer and the short delay before the call to the police. In each instance, the event occurs inside a call to the temporal operator after, which results in a transition after the chart receives the event some number of times.

Output Events

An output event occurs in a Stateflow chart but is visible in Simulink blocks outside the chart. This type of event enables a chart to notify other blocks in a model about events that occur in the chart.

Each output event maps to an output port on the right side of the chart. Depending on its configuration, the corresponding signal can control a Triggered Subsystem or a Function-Call Subsystem. To configure an output event, in the Property Inspector, set the Trigger field to one of these options.

Type of TriggerDescription
Either EdgeOutput event broadcast causes the outgoing signal to toggle between zero and one.
Function callOutput event broadcast causes a Simulink function-call event.

In the sf_security example, the output events Sound and call_police use edge triggers to activate a pair of latch subsystems in the Simulink model. When each latch detects a change of value in its input signal, it briefly outputs a value of one before returning to an output of zero.

Input Events

An input event occurs in a Simulink block but is visible in a Stateflow chart. This type of event enables other Simulink blocks, including other Stateflow charts, to notify a specific chart of events that occur outside it.

An external Simulink block sends an input event through a signal connected to the trigger port on the top of the Stateflow chart. Depending on its configuration, an input event results from a change in signal value or through a function call from a Simulink block. To configure an input event, in the Property Inspector, set the Trigger field to one of these options.

Type of TriggerDescription
RisingChart is activated when the input signal changes from either zero or a negative value to a positive value.
FallingChart is activated when the input signal changes from a positive value to either zero or a negative value.
EitherChart is activated when the input signal crosses zero as it changes in either direction.
Function callChart is activated with a function call from a Simulink block.

In the sf_security example, a Simulink Function-Call Generator block controls the timing of the security system by triggering the input event sl_call through periodic function calls.

Explore the Example

The Security System chart has inputs from several Manual Switch blocks and outputs to a pair of latch subsystems that connect to Display blocks. During simulation, you can:

  • Enable the alarm and sensor subsystems and trigger intrusion detections by clicking the Switch blocks.

  • Watch the chart animation highlight the various active states in the chart.

  • View the output signals in the Scope block and in the Simulation Data Inspector.

To adjust the timing of the simulation, double-click the Function-Call Generator block and, in the dialog box, modify the Sample time field. For example, suppose that you set the sample time to 1 and start the simulation with all subsystems switched on and all sensor triggers switched off. During the simulation, you perform these actions:

  1. At time t = 250 seconds, you trigger the door sensor. The alarm begins to sound (Sound = 1) so you immediately disable the alarm system. You switch off the trigger and turn the alarm back on.

  2. At time t = 520 seconds, you trigger the window sensor and the alarm begins to sound (Sound = 0). This time, you do not disable the alarm. At around time t = 600, the security system calls the police (call_police = 1). The Sound and call_police signals continue to toggle between zero and one every 80 seconds.

  3. At time t = 1400 seconds, you disable the alarm. The Sound and call_police signals stop toggling.

The Simulation Data Inspector shows the response of the Sound and call_police signals to your actions.

Related Examples

More About