Setting Simulation Parameters and Breakpoints

Prepare the Chart Yourself or Use the Supplied Model

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

Checking That Your Chart Conforms to Best Practices

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 Enter Expressions and Parameters for Data Properties in the Stateflow User's Guide.

Setting the Length of the Simulation

To specify the length of the simulation, follow these steps:

  1. 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
  2. Save the model as Stage6Simulate in your local work folder.

  3. Double-click Air Controller to open the chart.

  4. Check the settings for simulation time:

    1. In the Stateflow Editor, select Simulation > Model Configuration Parameters.

      The following dialog box opens:

    2. 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.

    3. Keep the preset values for now and click OK to close the dialog box.

  5. Leave the chart open for the next exercise.

Configuring Animation for the Chart

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 Lightning 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:

  1. Set the speed of animation by selecting Simulation > Chart Animation > Chart Animation Speed > Medium. This slows the animation down.

  2. Leave the Air Controller chart open for the next exercise.

Setting Breakpoints to Observe Chart Behavior

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:

BreakpointDescription
Chart EntrySimulation halts when the Stateflow chart wakes up.
State EntrySimulation halts when a state becomes active.

You will also learn how to examine data values when simulation pauses.

Follow these steps:

  1. Right click in the chart, and select Set Breakpoint on Chart Entry.

  2. For each state PowerOn and PowerOff, right click in the state, and select Set Breakpoints > On State Entry.

Simulating the Air Controller Chart

In this exercise, you will simulate the Air Controller chart. If this is the first time you are simulating the chart in the Stage6Simulate model, Stateflow software builds the simulation target by performing the following actions before simulation starts:

  • Parses the chart for state inconsistency errors, like those mentioned in Checking That Your Chart Conforms to Best Practices

  • Generates C code that represents the behavior of the chart

  • Builds the generated code into an executable program for the simulation target

  • Creates a new folder called slprj in the folder where the chart resides to store the generated files that make up the simulation target

  • Creates a MEX (MATLAB executable) file that corresponds to the C source file

During simulation, you will change breakpoints and observe data values when execution pauses. Follow these steps:

  1. In Stage6Simulate, open the Scope block. Position the Scope block and the Air Controller chart so they are visible on your desktop.

  2. 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.

  3. 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.

  4. 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:

      1. 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.

      2. At the prompt, type the name of the data object.

        The MATLAB Command Window displays the value of the data object.

  5. 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).

  6. 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.

  7. 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:

  8. 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.

  9. Save Stage6Simulate, and close all other windows and dialog boxes.

Was this topic helpful?