In phase 6 of this workflow, you simulate the chart to test its behavior. During simulation, you can animate Stateflow® charts to highlight states and transitions as they execute.
To prepare the chart for simulation yourself, work through the exercises in this section. Otherwise, open the supplied model to see how simulation parameters should appear. Enter this command at the MATLAB® prompt:
addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage6Simulate
Before starting a simulation session, you should examine your chart to see if it conforms to recommended design practices:
A default transition must exist at every level of the Stateflow hierarchy that contains exclusive (OR) states (has exclusive [OR] decomposition). (See Deciding Where to Place Default Transitions.)
Whenever possible, input data objects should inherit
properties from the associated Simulink® input signal to ensure consistency, minimize data entry, and
simplify maintenance of your model. Recall that in Defining the Inputs and Outputs, you defined the input
temp
to inherit its size and type from the
Simulink output port temp
, which provides the input
value to the Air Controller chart.
Output data objects should
not inherit types and sizes because the values are back
propagated from Simulink blocks and may, therefore, be unpredictable. Recall that in
Defining the Inputs and Outputs, you
specified the data type as uint8
and the size as scalar
(the default). (See Avoid Inheriting Output Data Properties from Simulink Blocks in the Stateflow User's Guide.)
Tip
You can specify data types and sizes as expressions in which you call
functions that return property values of other variables already defined in
Stateflow, MATLAB, or Simulink software. Such functions include type and
fixdt
. For more information,
see Specify Data Properties by Using MATLAB Expressions in the
Stateflow User's Guide.
To specify the length of the simulation, follow these steps:
Open the model Stage5Trigger
— either the one
you created in the previous exercises or the supplied model for stage
5.
To open the supplied model, enter the following command at the MATLAB prompt:
addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage5Trigger
Save the model as Stage6Simulate
in your local work
folder.
Double-click Air Controller to open the chart.
Check the settings for simulation time:
In the Simulink Editor, in the Modeling tab, click Model Settings.
The following dialog box opens:
Click Solver in the left Select pane if it is not already selected.
Under Simulation time on the right, note that the start and stop times have been preset for you. You can adjust these times later as you become more familiar with the run-time behavior of the chart.
Keep the preset values for now and click OK to close the dialog box.
Leave the chart open for the next exercise.
When you simulate a Simulink model, Stateflow animates charts to highlight states and transitions as they execute. Animation provides visual verification that your chart behaves as you expect. Animation is enabled by default to Fast. Slowing it down gives you more time to view the execution order of objects. To configure animation for your simulation session, follow these steps:
Set the speed of animation to medium. In the Stateflow Editor, in the Debug tab, set the
animation speed to Medium
. This slows the
animation down.
Leave the Air Controller chart open for the next exercise.
In this exercise, you will learn how to set breakpoints to pause simulation during key run-time activities so you can observe the behavior of your chart in slow motion. You will set the following breakpoints:
Breakpoint | Description |
---|---|
Chart Entry | Simulation halts when the Stateflow chart wakes up. |
State Entry | Simulation halts when a state becomes active. |
You will also learn how to examine data values when simulation pauses.
Follow these steps:
Right click in the chart, and select Set Breakpoint on Chart Entry.
For each state PowerOn
and PowerOff
,
right click in the state, and select Set Breakpoints > On State Entry.
In this exercise, you will simulate the Air Controller chart. During simulation, you will change breakpoints and observe data values when execution pauses. Follow these steps.
In Stage6Simulate
, open the Scope block. Position the
Scope block and the Air Controller chart so they are visible on your
desktop.
Start simulation by clicking Run.
After the simulation target is built, the chart appears with a gray background, indicating that simulation has begun. Simulation continues until it reaches the first breakpoint, when the Air Controller chart wakes up.
Right click a transition in the state FAN1
, and select Add to watch > (Input) temp. This adds the variable temp
to the
Stateflow Breakpoints and Watch window.
Right click in the state SpeedValue
, and select Add to watch > (Output) airflow. This adds the variable airflow
to the
Stateflow Breakpoints and Watch window.
Tip
You can also view data values from the MATLAB command line at simulation breakpoints. Here's how to do it:
When simulation pauses at a breakpoint, click in the MATLAB command line and press the Enter key.
The MATLAB Command Window displays a
debug>>
prompt.
At the prompt, type the name of the data object.
The MATLAB Command Window displays the value of the data object.
View the values of temp
and
airflow
.
Note that temp
is 70 (below the threshold for turning
on FAN1
) and airflow
is 0 (indicating
that no fans are running).
Resume simulation by clicking the Continue button.
Simulation continues until the next breakpoint, activation of the
PowerOff
state, which appears highlighted in the
chart (as part of animation).
The default transition activates PowerOff
after the
chart wakes up.
In the Breakpoints tab of the Stateflow Breakpoints and Watch Data window, clear the breakpoint on
Chart Entry. Hover the cursor over the name of the breakpoint, and select
the delete button,. Continue simulation.
Simulation continues to the next breakpoint, the activation of the
PowerOn
state:
Note that temp
has risen to over 157 degrees. The Scope
displays the temperature pattern:
To speed through the rest of the simulation, clear all breakpoints, and continue simulation.
Notice that FAN1
continues to cycle on and off as
temp
fluctuates between 119 and 120 degrees until
power cycles off at 500 seconds. After power cycles off, the fans stop
running and temp
begins to rise unchecked until
simulation reaches stop time at 600 seconds.
The Scope captures this activity:
Note
This display should look the same as the Scope after running the prebuilt model in Running the Model.
Save Stage6Simulate
, and close all other windows and
dialog boxes.