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.

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.

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®. Following are three possible approaches.

Execute Chart as a MATLAB Object

This example presents a modified version of an automatic transmission system that incorporates state hierarchy, temporal logic, and input events.

Hierarchy: The chart consists of a superstate gear_logic that surrounds the four-speed automatic transmission chart in the previous example. This superstate controls the speed and acceleration of the car. During execution, gear_logic is always active.

Temporal Logic: In the state gear_logic, the action on every(0.25,sec) determines the speed of the car. The operator every creates a MATLAB timer that executes the chart and updates the chart data speed every 0.25 seconds.

Input Events: The input events SpeedUp, Cruise, and SlowDown reset the value of the chart data delta. This data determines whether the car accelerates or maintains its speed at each execution step.

You can execute this chart as an object in MATLAB 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 example, this user interface sends an input event to the chart when you click a button. In the chart, the MATLAB function widgets controls the values of the gauges and lamps on the interface.

The chart continues to run until you close the user interface window. For more information about executing Stateflow charts as MATLAB objects, see Execution in MATLAB.

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:

Double-click the User Inputs block. In the Signal Builder dialog box, you can select a predefined brake-to-throttle profile to simulate or create a custom profile. The default profile is Passing Maneuver.

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.

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:

Double-click the User Inputs block. In the Signal Builder dialog box, you can select a predefined brake-to-throttle profile to simulate or create a custom profile. The default profile is Passing Maneuver.

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.

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