Synchronize Model Components by Broadcasting Events
An event is a Stateflow® object that can trigger actions in one of these objects:
A parallel state in a Stateflow chart
Another Stateflow chart
A Simulink® triggered or function-call subsystem
For simulation purposes, there is no limit to the number of events in a Stateflow chart. However, for code generation, the underlying C compiler enforces a theoretical limit of 231-1 events.
Types of Events
An implicit event is a built-in event that is broadcast during chart execution. These events are implicit because you do not define or trigger them explicitly. For more information, see Control Chart Behavior by Using Implicit Events.
An explicit event is an event that you define explicitly. Explicit events can have one of these types.
Type | Description |
---|---|
Input Event | Event that is broadcast to a Stateflow chart from outside the chart. For more information, see Activate a Stateflow Chart by Sending Input Events and Design Human-Machine Interface Logic by Using Stateflow Charts. |
Local Event | Event that can occur anywhere in a Stateflow chart but is visible only in the parent object and its descendants. Local events are not supported in standalone Stateflow charts in MATLAB®. For more information, see Broadcast Local Events to Synchronize Parallel States. |
Output Event | Event that occurs in a Stateflow chart but is broadcast to a Simulink block. Output events are not supported in standalone Stateflow charts in MATLAB. For more information, see Activate a Simulink Block by Sending Output Events. |
You can define local events at these levels of the Stateflow hierarchy.
Level of Hierarchy | Visibility |
---|---|
Chart | Local event is visible to the chart and all its states and substates. |
Subchart | Local event is visible to the subchart and all its states and substates. |
State | Local event is visible to the state and all its substates. |
Define Events in a Chart
You can add events to a Stateflow chart by using the Symbols pane, the Stateflow Editor menu, or the Model Explorer.
Add Events Through the Symbols Pane
In the Modeling tab, under Design Data, select Symbols Pane.
Click the Create Event icon .
In the row for the new event, under Type, click the icon and choose:
Input Event
Local Event
Output Event
Edit the name of the event.
For input and output events, click the Port field and choose a port number.
To specify properties for the event, open the Property Inspector. In the Symbols pane, right-click the row for the event and select Explore. For more information, see Set Properties for an Event.
Add Events by Using the Stateflow Editor Menu
In a Stateflow chart in a Simulink model, select the menu option corresponding to the type of the event that you want to add.
Type Menu Option Input Event In the Modeling tab, under Design Data, click Event Input. Output Event In the Modeling tab, under Design Data, click Event Output. Local Event In the Modeling tab, under Design Data, click Local Event. In the Event dialog box, specify data properties. For more information, see Set Properties for an Event.
Add Events Through the Model Explorer
In the Modeling tab, under Design Data, select Model Explorer.
In the Model Hierarchy pane, select the object in the Stateflow hierarchy where you want to make the new event visible. The object that you select becomes the parent of the new event.
In the Model Explorer menu, select Add > Event. The new event with a default definition appears in the Contents pane of the Model Explorer.
In the Event pane, specify the properties of the event. For more information, see Set Properties for an Event.
Access Event Information from a Stateflow Chart
You can display the properties of an input or local event, or open the destination of an output event directly from a Stateflow chart. Right-click the state or transition that contains the event of interest and select Explore. A context menu lists the names and scopes of all resolved symbols in the state or transition. Selecting an input or local event from the context menu displays its properties in the Model Explorer. Selecting an output event from the context menu opens the Simulink subsystem or Stateflow chart associated with the event.
Best Practices for Using Events in Stateflow Charts
Use the send
Command to Broadcast Explicit Events in Actions
To broadcast local or output events in state or transition actions, use the
send
operator. For example, to broadcast an output event when a
transition is valid, avoid using the name of the event as a condition
action.
{output_event;}
Instead, call the send
operator.
{send(output_event);}
Although both actions are valid, using the send
operator
enhances readability of a chart and ensures that explicit events are not
mistaken for data.
Avoid Using Explicit Events to Trigger Conditional Actions
Use conditions on transitions instead of events when you want to:
Represent conditional statements, for example,
[x < 1]
or[x == 0]
.Represent a change of data value, for example,
[hasChanged(x)]
.
Avoid Using the Implicit Event enter
to Check State Activity
To check state activity, use the in
operator instead of the implicit event enter
. For more information, see Check State Activity by Using the in Operator.
Do Not Mix Edge-Triggered and Function-Call Input Events in a Chart
Mixing input events that use edge triggers and function calls results in a compile-time error.