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
inherit its size and type from the Simulink output port
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.)
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
To specify the length of the simulation, follow these steps:
Open the model
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
your local work folder.
Double-click Air Controller to open the chart.
Check the settings for simulation time:
In the Stateflow Editor, select Simulation > Model Configuration Parameters.
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 by selecting Simulation > Stateflow Animation > 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:
|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
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.
Stage6Simulate, open the Scope
block. Position the Scope block and the Air Controller chart so they
are visible on your desktop.
Start simulation by selecting Simulation > 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
and select Add to watch > (Input) temp. This adds the
temp to the Stateflow Breakpoints
and Watch window.
Right click in the state
and select Add to watch > (Output) airflow. This adds
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:
View the values of
temp is 70 (below the threshold
for turning on
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).
PowerOff after the chart wakes
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
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.
FAN1 continues to cycle on and
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.
Stage6Simulate, and close
all other windows and dialog boxes.