|On this page…|
To implement the triggers yourself, work through the exercises in this section. Otherwise, open the supplied model to see how the triggers should appear in the chart. Enter this command at the MATLAB® prompt:
addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage5Trigger
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. 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 Implement Interfaces to Simulink Models in the 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.
To define the CLOCK event, follow these steps:
Open the model Stage4Transitions — either the one you created in the previous exercises or the supplied model for stage 4.
To open the supplied model, enter the following command at the MATLAB prompt:
addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage4Transitions
Save the model as Stage5Trigger in your local work folder.
In Stage5Trigger, double-click the Air Controller block to open the Stateflow chart.
In the Stateflow Editor, add an input event by selecting Chart > Add Inputs & Outputs > Event Input From Simulink.
In 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 that the rising or falling edge of a signal can trigger the event.|
Because the SWITCH event you created in Adding Events to Guard Transitions 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.
Click OK to record the changes and close the dialog box.
Save Stage5Trigger, but leave it open for the next exercise.
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:
Click the Mux block, hold down the Ctrl key, and click the Air Controller block.
The output signal of the Mux block connects to the input trigger port of the Stateflow block. Your model should look like this:
Where to go next. Now you are ready to simulate your chart. See Setting Simulation Parameters and Breakpoints.