Skip to Main Content Skip to Search
Product Documentation

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:

run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage6Simulate.mdl')))

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:

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:

    run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage5Trigger.mdl')))
  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 > Configuration Parameters.

      The Configuration Parameters dialog box opens on your desktop:

    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, you can animate Stateflow charts to highlight states and transitions as they execute. Animation provides visual verification that your chart behaves as expected. Animation is enabled by default, but you set the speed. To configure animation for your simulation session, follow these steps:

  1. Verify that animation has been enabled for your chart:

    1. In the Stateflow Editor, select Tools > Open Simulation Target.

      The Simulation Target pane of the Configuration Parameters dialog box opens on your desktop.

        Note   This dialog box is used to configure Stateflow software for building targets. A target is a program that executes a Stateflow chart or a Simulink model that contains a Stateflow chart. Stateflow software builds a simulation target that you use to simulate your Stateflow application. For more information, see Building Targets in the Stateflow User's Guide.

    2. Verify that Enable debugging/animation is selected.

    3. Close the dialog box.

  2. Set the speed of animation:

    1. From the Stateflow Editor, open the Stateflow debugger by selecting Debug > Stateflow Debugger or clicking the Debug icon:

      The Stateflow debugger opens on your desktop:

      By default, animation is enabled at 0-second delay.

    2. Change the delay to 1 second so the animation will proceed at the slowest speed.

        Note   You can change the speed of animation at any time during simulation.

  3. Leave the Air Controller chart and the debugger open for the next exercise.

Setting Breakpoints to Observe Chart Behavior

In this exercise, you will learn how to set breakpoints in the debugger to pause simulation during key run-time activities so you can observe the behavior of your chart in slow motion. You can set the following breakpoints:

BreakpointDescription
Chart EntrySimulation halts when the Stateflow chart wakes up.
Event BroadcastSimulation halts when an event, such as SWITCH or CLOCK, occurs.
State EntrySimulation halts when a state becomes active.

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

Follow these steps:

  1. In the debugger, select Chart Entry and State Entry as breakpoints.

      Note   If you also set breakpoints at each event broadcast, simulation would pause at every rising or falling edge of the SWITCH and CLOCK signals. To keep simulation running at a reasonable pace, do not select Event Broadcast.

  2. Notice the Browse Data option in the menu bar just above the output display pane of the debugger:

    The Browse Data option appears grayed out, but becomes active when simulation pauses at a breakpoint. You will use this option in Simulating the Air Controller Chart.

  3. Leave the debugger open for the next exercise.

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:

The MATLAB command line displays status messages during each of these processes. You should see the following messages in your MATLAB Command Window, which indicate a successful build:

For more information, see Building Targets in the Stateflow User's Guide.

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, Air Controller chart, and debugger so all are visible on your desktop.

  2. In the debugger, start simulation by clicking the Start button.

    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. Notice that the status panel at the top of the debugger provides a snapshot of simulation activities at the breakpoint.

    DetailWhat It MeansWhat You See at First Breakpoint
    StoppedWhat executed at the breakpointEntry: Chart Air Controller
    ExecutingStateflow chart that is executingAir Controller
    Current EventEvent that is processed at this time stepInput event SWITCH
    Simulink TimeTime at which the simulation paused0.000000

    Note also that the Browse Data option is now enabled.

  3. Click the down arrow to the right of the Browse Data option and select Watched Data (Current Chart) from the submenu.

    By selecting this option, you can examine the values of the input temp and output airflow. Recall that you configured these objects as data to be watched in the debugger in the exercise Defining the Inputs and Outputs.

      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.

  4. Scroll down in the output display pane of the debugger to 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).

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

  6. Clear the breakpoint Chart Entry and continue simulation.

    Simulation continues to the next breakpoint, the activation of the PowerOn state:

    Note in the output display pane of the debugger that temp has risen to over 157 degrees. The Scope displays the temperature pattern:

  7. Continue simulation through the following breakpoints, noting chart animation, Scope display, and how data values change:

    BreakpointValue of temp
    (Degrees)
    Value of airflow
    Activation of FAN1> 1570
    Default transition to FAN1.Off> 1570
    Activation of FAN2> 1570
    Default transition to FAN2.Off> 1570
    Activation of SpeedValue> 1570
    Transition to FAN1.On
    (because temp >= 120 degrees)
    > 1570
    Transition to FAN2.On
    (because temp >= 150 degrees)
    > 1570
    Transition to FAN2.Off
    (because temp < 150 degrees)
    > 149 and < 150 2
    Transition to FAN1.Off
    (because temp < 120 degrees)
    > 119 and < 1201
    Transition to FAN1.On
    (because temp >= 120 degrees)
    > 1200
    Transition to FAN1.Off
    (because temp < 120 degrees)
    > 119 and < 1201

  8. To speed through the rest of the simulation, clear all breakpoints, change animation delay to 0, and click Continue.

    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.

Where to go next.  Now you are ready to use the debugger for detecting state inconsistencies and data range violations. See Debugging Common Modeling Errors.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS