Main Content

Model Finite State Machines

Stateflow® is a graphical programming environment based on finite state machines. With Stateflow, you can test and debug your design, consider different simulation scenarios, and generate code from your state machine.

Finite state machines are representations of dynamic systems that transition from one mode of operation (state) to another. State machines:

  • Serve as a high-level starting point for a complex software design process.

  • Enable you to focus on the operating modes and the conditions required to pass from one mode to the next mode.

  • Help you to design models that remain clear and concise even as the level of model complexity increases.

Control systems design relies heavily on state machines to manage complex logic. Applications include designing aircraft, automobiles, and robotics control systems.

Example of a Stateflow Chart

In a Stateflow chart, you combine states, transitions, and data to implement a finite state machine. This Stateflow chart presents a simplified model of the logic to shift gears in a four-speed automatic transmission system of a car. The chart represents each gear position by a state, shown as a rectangle labeled first, second, third, or fourth. Like the gears they represent, these states are exclusive, so only one state is active at a time.

Stateflow chart with states labeled first, second, third, and fourth. The chart transitions between the states when the speed is higher or lower than a specified amount.

The arrow on the left of the diagram represents the default transition and indicates the first state to become active. When you execute the chart, this state is highlighted on the canvas. The other arrows indicate the possible transitions between the states. To define the dynamics of the state machine, you associate each transition with a Boolean condition or a trigger event. For example, this chart monitors the speed of the car and shifts to a different gear when the speed crosses a fixed threshold. During simulation, the highlighting in the chart changes as different states become active.

Chart animation showing states and transitions activating.

This chart offers a simple design that disregards important factors such as engine speed and torque. You can construct a more comprehensive and realistic model by linking this Stateflow chart with other components in MATLAB® or Simulink®. The following examples describe two possible approaches.

Simulate Chart as a Simulink Block with Local Events

This example provides a more complex design for an automatic transmission system. The Stateflow chart appears as a block in a Simulink model. The other blocks in the model represent related automotive components. The chart interfaces with the other blocks by sharing data through input and output connections. To open the chart, click the arrow in the bottom left corner of the shift_logic block.

This chart combines state hierarchy, parallelism, active state data, local events, and temporal logic.

  • Hierarchy: The state gear_state contains a modified version of the four-speed automatic transmission chart. The state selection_state contains substates that represent the steady state, upshifting, and downshifting modes of operation. When circumstances require a shift to a higher or lower gear, these states become active.

  • Parallelism: The parallel states gear_state and selection_state appear as rectangles with a dashed border. These states operate simultaneously, even as the substates inside them turn on and off.

  • Active State Data: The output value gear reflects the choice of gears during simulation. The chart generates this value from the active substate in gear_state.

  • Local Events: In place of Boolean conditions, this chart uses the local events UP and DOWN to trigger the transitions between gears. These events originate from the send commands in selection_state when the speed of the car goes outside the range of operation for the selected gear. The Simulink function calc_th determines the boundary values for the range of operation based on the selected gear and the engine speed.

  • Temporal Logic: To prevent a rapid succession of gear changes, selection_state uses the temporal logic operator after to delay the broadcasting of the UP and DOWN events. The state broadcasts one of these events only if a change of gears is required for longer than some predetermined time TWAIT.

To run a simulation of the model:

  1. Double-click the User Inputs block. In the Signal Editor dialog box, select a predefined brake-to-throttle profile from the Active Scenario list. The default profile is Passing Maneuver.

  2. Click Run. In the Stateflow Editor, chart animation highlights the active states during the simulation. To slow down the animation, in the Debug tab, select Slow from the Animation Speed drop-down list.

  3. In the Scope blocks, examine the results of the simulation. Each scope displays a graph of its input signals during simulation.

Simulate Chart as a Simulink Block with Temporal Conditions

This example provides another alternative for modeling the transmission system in a car. The Stateflow chart appears as a block in a Simulink model. The other blocks in the model represent related automotive components. The chart interfaces with the other blocks by sharing data through input and output connections. To open the chart, click the arrow in the bottom left corner of the Gear_logic block.

This chart combines state hierarchy, active state data, and temporal logic.

  • Hierarchy: This model places the four-speed automatic transmission chart inside a superstate gear. The superstate monitors the vehicle and engine speeds and triggers gear changes. The actions listed on the upper left corner of the state gear determine the operating thresholds for the selected gear and the values of the Boolean conditions up and down. The label en,du indicates that the state actions are executed when the state first becomes active (en = entry) and at every subsequent time step while the state is active (du = during).

  • Active State Data: The output value gear reflects the choice of gears during simulation. The chart generates this value from the active substate in gear.

  • Temporal Logic: To prevent a rapid succession of gear changes, the Boolean conditions up and down use the temporal logic operator duration to control the transition between gears. The conditions are valid when the speed of the car remains outside the range of operation for the selected gear longer than some predetermined time TWAIT (measured in seconds).

To run a simulation of the model:

  1. Double-click the User Inputs block. In the Signal Editor dialog box, select a predefined brake-to-throttle profile from the Active Scenario list. The default profile is Passing Maneuver.

  2. Click Run. In the Stateflow Editor, chart animation highlights the active states during the simulation. To slow down the animation, in the Debug tab, select Slow from the Animation Speed drop-down list.

  3. In the Scope block, examine the results of the simulation. The scope displays a graph of the gear selected during simulation.

Next Steps

To learn more about Stateflow charts, follow these steps in the tutorial: