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.
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
event_name is the name of the
event to be broadcast.
is an active state during the broadcast.
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 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
Off modes for the alarm, or between the
Disabled modes for each sensor. When enabled, each sensor
monitors a triggering input signal that indicates a possible intrusion.
|Subsystem||State||Enabling Signal||Triggering Signal|
If a sensor detects an intrusion while the alarm subsystem is on, then it broadcasts
Alert event with this command:
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
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
Stateflow charts can use events to communicate with other blocks in a Simulink® model. For instance, in the
The output events
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
The command for
Sound occurs as an entry
action in the
The command for
call_police occurs as an
action in the transition between the
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.
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 Trigger||Description|
|Output event broadcast causes the outgoing signal to toggle between zero and one.|
|Output event broadcast causes a Simulink function-call event.|
sf_security example, the output events
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.
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 Trigger||Description|
|Chart is activated when the input signal changes from either zero or a negative value to a positive value.|
|Chart is activated when the input signal changes from a positive value to either zero or a negative value.|
|Chart is activated when the input signal crosses zero as it changes in either direction.|
|Chart is activated with a function call from a Simulink block.|
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
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:
At time t = 250 seconds, you trigger the door sensor. The alarm begins to
Sound = 1) so you immediately disable the alarm system. You switch
off the trigger and turn the alarm back on.
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
call_police signals continue to toggle between zero
and one every 80 seconds.
At time t = 1400 seconds, you disable the alarm. The
call_police signals stop
The Simulation Data Inspector shows the response of the
call_police signals to your