Stateflow User's Guide Previous page   Next Page

Simulating a Stateflow Diagram

Stateflow allows you to observe the behavior of Stateflow diagrams in Simulink models during simulation of the model. Before you simulate a Stateflow diagram, you make sure that simulation is enabled for your model as you did in Setting Up Diagram Simulation.

When you simulate a Simulink model containing Stateflow blocks, Stateflow builds an application for each Stateflow block that graphically executes its Stateflow diagram by highlighting the active state and the executing transition. By simulating your Stateflow diagrams, you understand the behavior that you specify for them, and find out if they are behaving as you expect them to.

Simulate the SFcontrol1 model as described in the following steps:

  1. Before you begin simulating the SFcontrol1 model, position the Simulink model window and the Controller Stateflow diagram window so that they can both be seen at once with no overlap.
  2. From the Stateflow diagram editor Simulation menu, select Start to start simulation of the model and notice the following:
  1. Simulation commences when the Stateflow diagram editor background becomes shaded as shown.

    Stateflow begins in simulation mode. The Controller diagram is active, but nothing in it is.

  1. In the Simulink window, double-click the Manual Switch.
  1. This causes the Manual Switch to switch between its inputs, -1 and 1. Because the trigger event for the Controller block is set to Either, changing the switch in either direction (1 to -1 or -1 to 1) sends a falling edge or rising edge signal to the Controller block that recognizes it as the event event.

    The event causes the Stateflow diagram to execute and look for the first available transition, which is the default transition into the state Off. You see the following simulation sequence:

    The only available transition, the default transition, is taken.

    The destination state of the default transition, the state Off, is entered, becomes active, and stays active until another event occurs.

  1. In the Simulink window, double-click the Manual Switch.
  1. This sends another event event that causes the Stateflow diagram to look for an available transition. You see the following continued simulation behavior:

    The only available transition from the active state Off to another state is the transition to the state On. The transition to On is taken.

    The Off state is exited and the destination state On is entered, becomes active, and stays active.

  1. In the Simulink window, double-click the Manual Switch.
  1. This sends another event event that causes the Stateflow diagram to look for an available transition. You see the following continued simulation behavior:

    The only available transition from the active state On is the transition to the Off state. The transition to the state Off is taken.

    The On state is exited and the destination state Off, is entered, becomes active, and stays active.

  1. Continue sending events by double-clicking the Manual Switch in the Simulink window to repeat the sequence in the preceding steps 4 and 5.
  2. Choose Stop from the graphics editor Simulation menu to stop the simulation.
  1. Once the simulation stops, Stateflow resets the model to be editable.


Previous page  Setting Up Diagram Simulation Using the Debug Tool During Simulation Next page

© 1994-2005 The MathWorks, Inc.