Synchronize Parallel States by Broadcasting Events
Local events enable you to coordinate parallel states by allowing one state to trigger a transition or an action in another state in the same Stateflow® chart. To broadcast an event from one state to another state, use the send
operator with the name of the event and the name of an active state:
send(eventName,stateName)
When you broadcast an event, the event takes effect in the receiving state and in any substates in the hierarchy of that state.
Model a Home Security System
This example 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 door sensor, a window 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 chart models each sensor by using one of these parallel states:
The parallel state
Door
models the door sensor. The input signalD_mode
selects between theActive
andDisabled
modes for this sensor. When the sensor is active, the input signalDoor_sens
indicates a possible intrusion.The parallel state
Win
models the window sensor. The input signalW_mode
selects between theActive
andDisabled
modes for this sensor. When the sensor is active, the input signalWin_sens
indicates a possible intrusion.The parallel state
Motion
models the motion detector. The input signalM_mode
selects between theActive
andDisabled
modes for this sensor. When the sensor is active, the input signalMot_sens
indicates a possible intrusion.
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 alert. In contrast, the door and window sensors interpret a single positive trigger signal as an intrusion and issue an immediate alert.
A fourth parallel state called Alarm
models the operating modes of the alarm system. The input signal Alarm_active
selects between the On
and Off
modes for the alarm. If a sensor detects an intrusion while the alarm subsystem is on, the sensor broadcasts the local event Alert
to the Alarm
state. In the On
substate of the state Alarm
, the event triggers the transition from the Idle
substate to the Pending
substate. When Pending
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 before returning to the Idle
mode.
Coordinate with Other Simulink Blocks
Stateflow charts can also use events to communicate with other blocks in a Simulink® model.
Output Events
An output event is an event that 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. For instance, in this example, the output events Sound
and call_police
drive external blocks that handle the warning sound and the call to the police. The chart broadcasts these events when the local event Alert
triggers the transition to the Pending
substate of the state Alarm
. In particular, in the Pending
substate, the entry action broadcasts the Sound
event. Similarly, the condition action on the transition from Pending
to Idle
broadcasts the call_police
event. In each case, the action that broadcasts the output event uses the send
operator with the name of the event:
send(eventName)
Each output event maps to an output port on the chart. Depending on the configuration, the corresponding signal can control a Triggered Subsystem or a Function-Call Subsystem. To configure an output event:
In the Modeling tab, under Design Data, select Symbols Pane and Property Inspector.
In the Symbols pane, select the output event.
In the Property Inspector, set Trigger to one of these options:
Either edge
— The output event broadcast causes the outgoing signal to toggle between zero and one.Function call
— The output event broadcast causes a Simulink function-call event.
In this example, the output events 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 is an event that 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. For instance, in this example, 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 triggers a transition after the chart receives the event some number of times.
An external Simulink block sends an input event through a signal connected to the trigger port on the Stateflow chart. Depending on the 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 Modeling tab, under Design Data, select Symbols Pane and Property Inspector.
In the Symbols pane, select the input event.
In the Property Inspector, set Trigger to one of these options:
Rising
— The chart activates when the input signal changes from either zero or a negative value to a positive value.Falling
— The chart activates when the input signal changes from a positive value to either zero or a negative value.Either
— The chart activates when the input signal crosses zero as it changes in either direction.Function
call — The chart activates with a function call from a Simulink block.
In this 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
In this example, the Stateflow 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.
For example, suppose that you switch the alarm and sensor subsystems on, switch the sensor triggers off, and start the simulation. During the simulation, you perform these actions:
At time seconds, you trigger the door sensor. The alarm begins to sound (
Sound = 1
) so you immediately disable the alarm system. You switch the door sensor trigger off and turn the alarm back on.At time 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 , the security system calls the police (call_police = 1
). TheSound
andcall_police
signals continue to toggle between zero and one every 80 seconds.At time seconds, you disable the alarm. The
Sound
andcall_police
signals stop toggling.
The Simulation Data Inspector shows the response of the Sound
and call_police
signals to your actions.