Implementing the States to Represent Operating Modes

Build It Yourself or Use the Supplied Model

To implement the states yourself, work through the exercises in this section. Otherwise, open the supplied model by entering this command at the MATLAB® prompt:

addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples'))
Stage2States

Design Considerations for Defining the States

The following sections describe the rationale for the hierarchy and decomposition of states in the chart.

When to Use States

Whether or not to use states depends on the control logic you want to implement. You can model two types of control logic: finite state machines and stateless flow charts. Each type is optimized for different applications, as follows:

Control Logic Optimized for Modeling
Finite state machinesPhysical systems that transition between a finite number of operating modes. In Stateflow® charts, you represent each mode as a state.
Stateless flow chartsAbstract logic patterns — such as if, if-else, and case statements — and iterative loops — such as for, while, and do loops. You represent these logic constructs with connective junctions and transitions in Stateflow charts. No states are required. See Decision Logic.

The Air Controller chart is a system that cools a physical plant by transitioning between several modes of operation and, therefore, can be modeled as a finite state machine. In the following sections, you will design the states that model each mode of operation.

Determining the States to Define

States model modes of operation in a physical system. To determine the number and type of states required for your Air Controller chart, you must identify each mode in which the system can operate. Often, a table or grid is helpful for analyzing each mode and determining dependencies between modes.

Analysis of Operating Modes.  For Air Controller, the modes of operation are

Operating ModeDescriptionDependencies
Power OffTurns off all power in the control systemNo fan can operate when power is off.
Power OnTurns on all power in the control systemZero, one, or two fans can operate when power is on.
Fan 1Activates Fan 1Fan 1 can be active at the same time as Fan 2. When activated, Fan 1 can turn on or off.
Fan 1 OnCycles on Fan 1Fan 1 On can be active if Fan 1 is active and power is on.
Fan 1 OffCycles off Fan 1Fan 1 Off can be active if Fan 1 is active, and power is on.
Fan 2Activates Fan 2Fan 2 can be active at the same time as Fan 1. When activated, Fan 2 can turn on or off.
Fan 2 OnCycles on Fan 2Fan 2 On can be active if Fan 2 is active and power is on.
Fan 2 OffCycles off Fan 2Fan 2 Off can be active if Fan 2 is active and power is on.
Calculate airflowCalculates a constant value of 0, 1, or 2 to indicate how fast air is flowing. Outputs this value to the Simulink® subsystem for selecting a cooling factor.Calculates the constant value, based on how many fans have cycled on at each time step.

Number of States to Define.  The number of states depends on the number of operating modes to be represented. In Analysis of Operating Modes, you learned that the Air Controller chart has nine operating modes. Therefore, you need to define nine states to model each mode. Here are the names you will assign to the states that represent each operating mode in Implementing the States to Represent Operating Modes:

State NameOperating Mode
PowerOffPower Off
PowerOnPower On
FAN1Fan 1
FAN2Fan 2
SpeedValueCalculate airflow
FAN1.OnFan 1 On
FAN1.OffFan 1 Off
FAN2.OnFan 2 On
FAN2.OffFan 2 Off

    Note:   Notice the use of dot notation to refer to the On and Off states for FAN1 and FAN2. You use namespace dot notation to give objects unique identifiers when they have the same name in different parts of the chart hierarchy.

Determining the Hierarchy of States

Stateflow objects can exist in a hierarchy. For example, states can contain other states — referred to as substates — and, in turn, can be contained by other states — referred to as superstates. You need to determine the hierarchical structure of states you will define for the Air Controller chart. Often, dependencies among states imply a hierarchical relationship — such as parent to child — between the states.

Based on the dependencies described in Analysis of Operating Modes, here is an analysis of state hierarchy for the Air Controller chart:

Dependent StatesImplied Hierarchy
FAN1 and FAN2 depend on PowerOn. No fan can operate unless PowerOn is active.FAN1 and FAN2 should be substates of a PowerOn state.
FAN1.On and FAN1.Off depend on Fan1 and PowerOn. FAN1 must be active before it can be cycled on or off.FAN1 should have two substates, On and Off. In this hierarchical relationship, On and Off will inherit from FAN1 the dependency on PowerOn.
FAN2.On and FAN2.Off depend on FAN2 and PowerOn. FAN2 must be active before it can be cycled on or off.FAN2 should have two substates, On and Off. In this hierarchical relationship, On and Off will inherit from FAN2 the dependency on PowerOn.
The state that calculates airflow needs to know how many fans are running at each time step.The state that calculates airflow should be a substate of PowerOn so it can check the status of FAN1 and FAN2 at the same level of hierarchy.

Determining the Decomposition of States

The decomposition of a state dictates whether its substates execute exclusively of each other — as exclusive (OR) states — or can be activated at the same time — as parallel (AND) states. No two exclusive (OR) states can ever be active at the same time, while any number of parallel (AND) states can be activated concurrently.

The Air Controller chart requires both types of states. Here is a breakdown of the exclusive (OR) and parallel (AND) states required for the Stateflow chart:

StateDecompositionRationale
PowerOff, PowerOnExclusive (OR) statesThe power can never be on and off at the same time.
FAN1, FAN2Parallel (AND) statesZero, one, or two fans can operate at the same time, depending on how much cooling is required.
FAN1.On, FAN1.OffExclusive (OR) statesFan 1 can never be on and off at the same time.
FAN2.On, FAN2.OffExclusive (OR) statesFan 2 can never be on and off at the same time.
SpeedValueParallel (AND) stateSpeedValue is an observer state that monitors the status of Fan 1 and Fan 2, updating its output based on how many fans are operating at each time step. SpeedValue must be activated at the same time as Fan 1 and Fan 2, but execute last so it can capture the most current status of the fans.

Adding the Power On and Power Off States

When you add states to the Air Controller chart, you will work from the top down in the Stateflow hierarchy. As you learned in Determining the Decomposition of States, the PowerOff and PowerOn states are exclusive (OR) states that turn power off and on in the control system. These states are never active at the same time. By default, states are exclusive (OR) states, represented graphically as rectangles with solid borders.

To add PowerOn and PowerOff to your chart, follow these steps:

  1. Open the model Stage1Interface — either the one you created in the previous exercise or the supplied model for stage 1.

    To open the supplied model, enter the following command at the MATLAB prompt:

    addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples'))
    Stage1Interface
  2. Save the model as Stage2States in your local work folder.

  3. In Stage2States, double-click the Air Controller block to open the Stateflow chart.

    The Stateflow Editor for Air Controller opens on your desktop. Notice the object palette on the left side of the editor window. This palette displays a set of tools for drawing graphical chart objects, including states:

  4. Left-click the state tool icon:

  5. Move your pointer into the drawing area.

    The pointer changes to a rectangle, the graphical representation of a state.

  6. Click in the upper-left corner of the drawing area to place the state.

    The new state appears with a blinking text cursor in its upper-left corner.

  7. At the text cursor, type PowerOn to name the state.

      Tip   If you click away from the text cursor before typing the new name, the cursor changes to a question mark. Click the question mark to restore the text cursor.

  8. Move your pointer to the lower-right corner of the rectangle so it changes to this symbol:

  9. Drag the lower-right corner to enlarge the state as shown:

  10. Click the state tool icon again and draw a smaller state named PowerOff at the bottom of the drawing area, like this:

  11. Save the chart by selecting File > Save in the Stateflow Editor, but leave the chart open for the next exercise.

Adding and Configuring Parallel States

In Determining the States to Define, you learned that FAN1, FAN2, and SpeedValue will be represented by parallel (AND) substates of the PowerOn state. Parallel states appear graphically as rectangles with dashed borders.

In this set of exercises, you will learn how to:

  • Assign parallel decomposition to PowerOn so its substates can be activated concurrently.

    Recall that the decomposition of a state determines whether its substates will be exclusive or parallel.

  • Add parallel substates to a state in the chart.

  • Set the order of execution for the parallel substates.

    Even though parallel states can be activated concurrently, they execute in a sequential order.

Setting Parallel Decomposition

Follow these steps:

  1. In the Air Controller chart, right-click inside PowerOn.

    A submenu opens, presenting tasks you can perform and properties you can set for the selected state.

  2. In the submenu, select Decomposition > AND (Parallel).

  3. Save the model Stage2States, but leave the chart open for the next exercise.

Adding the Fan States

Follow these steps:

  1. Left-click the state tool icon in the Stateflow Editor and place two states inside the PowerOn state.

      Tip   Instead of using the state tool icon to add multiple states, you can right-click inside an existing state and drag a copy to a new position in the chart. This shortcut is convenient when you need to create states of the same size and shape, such as the fan states.

  2. Notice the appearance of the states you just added.

    The borders of the two states appear as dashed lines, indicating that they are parallel states. Note also that the substates display numbers in their upper-right corners. These numbers specify the order of execution. Although multiple parallel (AND) states in the same chart are activated concurrently, the chart must determine when to execute each one during simulation.

  3. Name the new substates FAN1 and FAN2.

    You have created hierarchy in the Air Controller chart. PowerOn is now a superstate while FAN1 and FAN2 are substates. Your chart should look something like this:

      Note   Your chart might not show the same execution order for parallel substates FAN1 and FAN2. The reason is that, by default, Stateflow software orders parallel states based on order of creation. If you add FAN2 before FAN1 in your chart, FAN2 moves to the top of the order. You will fine-tune order of activation in a later exercise, Setting Explicit Ordering of Parallel States.

      Tip   If you want to move a state together with its substates — and any other graphical objects it contains — double-click the state. It turns gray, indicating that the state is grouped with the objects inside it and that they can be moved as a unit. To ungroup the objects, double-click the state again.

  4. Save the model Stage2States, but leave the chart open for the next exercise.

Adding the SpeedValue State

Recall that SpeedValue acts as an observer state, which monitors the status of the FAN1 and FAN2 states. To add the SpeedValue state, follow these steps:

  1. Add another substate to PowerOn under FAN1 and FAN2, either by using the state tool icon or copying an existing state in the chart.

    You might need to resize the substate to prevent overlap with other substates, but remain within the borders of PowerOn.

  2. Name the state SpeedValue.

    Like FAN1 and FAN2, SpeedValue appears as a parallel substate because its parent, the superstate PowerOn, has parallel decomposition.

  3. Save the model Stage2States, but leave the chart open for the next exercise, Setting Explicit Ordering of Parallel States.

Setting Explicit Ordering of Parallel States

Recall that, by default, Stateflow software assigns execution order of parallel states based on order of creation in the chart. This behavior is called explicit ordering. In this exercise, you will set the execution order explicitly for each parallel state in your chart.

  1. In the Stateflow Editor, select File > Model Properties > Chart Properties.

  2. In the Chart properties dialog box, verify that the check box User specified state/transition execution order is selected and click OK.

      Note:   This option also lets you explicitly specify the order in which transitions execute when there is a choice of transitions to take from one state to another. This behavior does not apply to the Air Controller chart because it is deterministic: for each exclusive (OR) state, there is one and only one transition to a next exclusive (OR) state. You will learn more about transitions in Drawing the Transitions Between States.

  3. Assign order of execution for each parallel state in the Air Controller chart:

    1. Right-click inside each parallel state to bring up its state properties submenu.

    2. From the submenu, select Execution Order and make these assignments:

      For State:Assign:
      FAN11
      FAN22
      SpeedValue3

      Here is the rationale for this order of execution:

      • FAN1 should execute first because it cycles on at a lower temperature than FAN2.

      • SpeedValue should execute last so it can observe the most current status of FAN1 and FAN2.

  4. Save the model Stage2States, but leave the chart open for the next exercise, Adding the On and Off States for the Fans.

Adding the On and Off States for the Fans

In this exercise, you will enter the on and off substates for each fan. Because fans cannot cycle on and off at the same time, these states must be exclusive, not parallel. Even though FAN1 and FAN2 are parallel states, their decomposition is exclusive (OR) by default. As a result, any substate that you add to FAN1 or FAN2 will be an exclusive (OR) state.

Follow these steps:

  1. Add two substates inside FAN1 and FAN2.

  2. Resize the substates to fit within the borders of FAN1 and FAN2.

  3. In each fan state, name one substate On and name the other Off.

    Your Air Controller chart should now look something like this:

  4. Save the model Stage2States.

Where to go next.  Now you are ready to specify the actions that execute when a state is active. See Implementing State Actions.

Was this topic helpful?