

Adding the Transitions
Your chart should look like this:
5 Save Stage4Transitions, but leave the chart open for the next exercise.
Adding Events to Guard Transitions
Events are nongraphical objects that trigger activities during the execution of a Stateflow chart. Depending on where and how events are defined, they can trigger a transition to occur, an action to be executed, and state status to be evaluated. In this exercise, you will define an event that triggers transitions.
As you learned in “Guarding the Transitions” on page 6-3, the control system should power on and off at regular intervals. You model this behavior by first defining an event that occurs at the rising or falling edge of an input signal, and then associating that event with the transitions between the PowerOn and PowerOff states.
6-13

6 Defining Transitions Between States
Follow these steps to define an edge-triggered event and associate it with the transitions:
1In the Stateflow Editor, add an input event by selecting Event > Input from Simulink from the Add menu.
The Event properties dialog box opens on your desktop:
Note that the event is assigned to trigger port 1.
2 Edit the following properties:
|
Property |
What to Specify |
|
|
Name |
Change the name to SWITCH. |
|
|
Trigger |
Select Either from the drop-down menu so the event |
|
|
|
can be triggered by either the rising edge or falling edge |
|
|
|
of a signal. |
|
6-14

Adding the Transitions
3Click OK to record the changes and close the dialog box.
4Look back at the Simulink model and notice that a trigger port appears at the top of the Stateflow block:
Trigger port
When you define one or more input events for a chart, Stateflow software adds a single trigger port to the block. External Simulink blocks can trigger the input events via a signal or vector of signals connected to the trigger port.
5Back in the Stateflow Editor, associate the input event SWITCH with the transitions:
aSelect the transition from PowerOff to PowerOn and click the question mark to get a text cursor.
bType the name of the event you just defined, SWITCH.
6-15

6 Defining Transitions Between States
You might need to reposition the event text for readability. If so, click outside the text string, left-click the text, and drag it to the desired location.
cRepeat these steps to add the same event, SWITCH, to the transition from
PowerOn to PowerOff.
Your chart should now look something like this:
Now that you have associated these transitions with the event SWITCH, the control system will alternately power on and off every time SWITCH occurs
— that is, every time the chart detects a rising or falling signal edge.
6-16

Adding the Transitions
Note that the sf_aircontrol model has already defined the pulse signal SWITCH in the Signal Builder block at the top level of the model hierarchy:
In the next phase of the workflow, you will connect your Stateflow chart to the SWITCH signal to trigger the transitions between power on and power off.
6 Save Stage4Transitions.
Where to go next. Now you are ready to begin phase 5 of the workflow: Chapter 7, “Triggering a Stateflow Chart”.
6-17

6 Defining Transitions Between States
6-18

7
Triggering a Stateflow
Chart
2
Define the states
for modeling each mode of operation
|
|
|
|
|
|
1 |
|
3 |
|
||
|
Define |
|
|||
Define the |
|
|
|||
|
state actions |
|
|||
interface |
|
|
|||
|
and variables |
|
|||
to Simulink |
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
6 Simulate the chart
5 |
|
|
|
|
|
7 |
|
|
|
Decide how to |
|
Debug |
||
trigger the chart |
|
the chart |
||
|
|
|
|
|
4 Define the transitions
between states
You have entered phase 5 of a basic workflow for building a Stateflow chart: deciding how to trigger the chart. This chapter presents the design questions that you must answer and guides you through exercises for adding triggers, based on your design decisions.
•“Design Considerations for Triggering Stateflow Charts” on page 7-2
•“Implementing the Triggers” on page 7-3

7 Triggering a Stateflow® Chart
Design Considerations for Triggering Stateflow Charts
A Simulink model can wake up a Stateflow chart by
•Sampling the chart at a specified or inherited rate
•Using a signal as a trigger
•Using one Stateflow chart to drive the activity of another
A signal trigger works best for the Air Controller chart because it needs to monitor the temperature of the physical plant at regular intervals. To meet this requirement, you will use a periodic signal to trigger the chart. The source is a square wave signal called CLOCK, provided by a Signal Builder block in the Simulink model, described in “How the Stateflow Chart Works with the Simulink Model” on page 2-6. To harness the signal, you will set up an edge trigger event that wakes the chart at the rising or falling edge of CLOCK.
The rationale for using an edge trigger in this case is that it uses the regularity and frequency of the signal to wake up the chart. When using edge triggers, keep in mind that there can be a delay from the time the trigger occurs to the time the chart begins executing. This is because an edge trigger causes the chart to execute at the beginning of the next simulation time step, regardless of when the edge trigger actually occurred during the previous time step. The Air Controller can tolerate this delay, as long as the edge occurs frequently enough. (For more information about triggering Stateflow charts, see “Implementing Update Interfaces to Simulink Models” in the online Stateflow User’s Guide.)
Recall that you already defined one edge-triggered event, SWITCH, to guard the transitions between PowerOff and PowerOn. You will now define a second edge-triggered event, CLOCK, to wake up the chart.
7-2

Implementing the Triggers
Implementing the Triggers
In this section...
“Defining the CLOCK Event” on page 7-3
“Connecting the Edge-Triggered Events to the Input Signals” on page 7-4
Defining the CLOCK Event
To define the CLOCK event, follow these steps:
1Open the model Stage4Transitions and save it as Stage5Trigger in the same folder.
2In Stage5Trigger, double-click the Air Controller block to open the Stateflow chart.
3In the Stateflow Editor, add an input event by selecting Event > Input from Simulink from the Add menu.
4In the Event properties dialog box, edit the following fields:
|
Property |
What to Specify |
|
|
Name |
Change the name to CLOCK. |
|
|
Trigger |
Select Either from the drop-down menu so the event |
|
|
|
can be triggered by either the rising edge or falling edge |
|
|
|
of a signal. |
|
Because the SWITCH event you created in “Adding Events to Guard Transitions” on page 6-13 was assigned to trigger port 1, the CLOCK event is assigned to trigger port 2. Nevertheless, only one trigger port appears at the top of the Air Controller block to receive trigger signals. This means that each signal must be indexed into an array, as described in “Connecting the Edge-Triggered Events to the Input Signals” on page 7-4.
5Click OK to record the changes and close the dialog box.
6Save Stage5Trigger, but leave it open for the next exercise.
7-3

7 Triggering a Stateflow® Chart
Connecting the Edge-Triggered Events to the Input Signals
You need to connect the edge-triggered events to the Simulink input signals in a way that
•Associates each event with the correct signal
•Indexes each signal into an array that can be received by the Air Controller trigger port
In Stage5Trigger, notice that the two input signals SWITCH and CLOCK feed into a Mux block where they are joined in an array to a single output. SWITCH is a pulse signal and CLOCK is a square wave. When you connect the Mux to the trigger port, the index of the signals in the array are associated with the like-numbered ports. Therefore, the SWITCH signal at the top input port of the Mux triggers the event SWITCH on trigger port 1. Likewise, the CLOCK signal at the second input port of the Mux triggers the event CLOCK on trigger port 2.
To connect the Mux to the trigger port, follow these steps:
1Click the Mux block, hold down the Ctrl key, and click the Air Controller block.
7-4