Main Content

Model Finite State Machines by Using Stateflow Charts

A finite state machine is a representation of an event-driven, reactive system that makes a transition from one operating mode to another when the condition defining the change is true. For example, you can use a state machine to represent the automatic transmission of a car. The transmission has operating modes, such as park, reverse, neutral, drive, and low. As the driver moves the gearshift, the system makes a transition from one operating mode to another.

Types of Stateflow Blocks

To represent the relationships between the inputs, outputs, and operating modes of a finite state machine, you can add Stateflow® blocks to a Simulink® model to create state transition diagrams, state transition tables, and truth tables:

Tip

To combine the advantages of state machine programming with the full functionality of MATLAB®, you can create a standalone Stateflow chart. You execute standalone charts as MATLAB objects directly through the Command Window or by using a script. You can also program a MATLAB app that controls the state of the chart through a graphical user interface. For more information, see Create Stateflow Charts for Execution as MATLAB Objects.

Program a Stateflow Chart

To create a Stateflow chart that models a finite state machine:

  1. Create a Simulink model that contains an empty Stateflow chart by calling the function sfnew.

    sfnew

  2. To open the Stateflow Editor, double-click the chart block. For more information on using the Stateflow Editor, see Stateflow Editor Operations.

  3. For each operating mode in your system, draw a state and implement the state actions by adding state labels, as described in Represent Operating Modes by Using States.

  4. To represent the direction of flow logic between states, draw transitions and implement the transition conditions by adding transition labels, as described in Transition Between Operating Modes.

  5. If your system has inputs or outputs, or depends on any state variables, add input, output, and local data, as described in Add Stateflow Data.

  6. If your system reacts to event triggers or must trigger actions in your chart or other blocks in your model, add input, output, or local events, as described in Synchronize Model Components by Broadcasting Events.

  7. If your chart has complex state actions or transition conditions, add reusable functions to your chart. Use the function format that is most natural for the type of calculation in the state action or transition condition by selecting from these functions:

    Alternatively, you can write your own C or C++ code for integration with your chart. For more information, see Reuse Custom Code in Stateflow Charts.

  8. Connect the chart to other blocks in the Simulink model by using input and output ports.

  9. To simulate the model, click Run . During the simulation, the Stateflow Editor highlights active states and transitions through chart animation.

For a tutorial that illustrates this workflow, see Construct and Run a Stateflow Chart.

References

[1] Harel, David. "Statecharts: A Visual Formalism for Complex Systems." Science of Computer Programming 8, no.3 (June 1987): 231-74.

[2] Hatley, Derek J. and Imtiaz A. Pirbhai. Strategies for Real-Time System Specification. New York, NY: Dorset House Publishing, 1988.

See Also

| | |

Related Topics