Implementing State Actions

Build It Yourself or Use the Supplied Model

To implement the state actions yourself, work through the exercises in this section. Otherwise, open the supplied model to see how the actions should appear in the states. Enter this command at the MATLAB® prompt:

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

Design Considerations for Defining State Actions and Variables

The following sections describe the decisions you make for defining state actions and variables.

Deciding Whether to Use State Variables

At this stage of the workflow for developing Stateflow® charts, you must determine if any of your states require local or persistent variables. If so, you define these data elements using the Stateflow Editor or the Model Explorer, as described in Add Data in the Stateflow User's Guide.

The states in the Air Controller chart do not require local or persistent data, only the input and output data that you defined in Defining the Inputs and Outputs.

Deciding Whether to Use State Actions

During simulation of a Stateflow chart, states can perform actions while they are active. Often, actions are used to manipulate data, using a variety of constructs such as binary, bitwise, unary, assignment, and type cast operators.

When building the Air Controller chart, you need to determine whether any states should perform actions. Some charts do not use state actions at all, but instead perform actions only during the transitions from state to state. Other charts require both types of state actions.

For the Air Controller chart, think about whether data values need to be initialized or modified during any of its modes of operation. Recall that the chart receives the air temperature of the plant as the input temp from the Physical Plant subsystem. The chart then uses this value to activate fans if necessary to cool the air. Based on how many fans are running, the chart sets a value that indicates speed of airflow, which it sends at each time step to the Simulink® subsystem as the output airflow. The Air Controller does not modify the value of temp, but does need to update the value of airflow.

The next consideration is when to update, and for that matter, initialize the value of airflow. If the when translates to a mode of operation, the action should likely be performed by the state that represents that mode of operation. Here is the analysis for the Air Controller chart:

ActionWhenHow
Initialize airflow to 0.Before simulationSet an initial value when you first define airflow (as you did in Defining the Inputs and Outputs).
Set airflow to 0.Whenever power is offAdd an action in the state PowerOff.
Update airflow to 0, 1, or 2, based on how many fans are running.Whenever power is onAdd an action in the state SpeedValue, which becomes active concurrently with FAN1 and FAN2 when the state PowerOn is active.

Deciding the Type of State Action to Use

States perform actions at different phases of their execution cycle from the time they become active to the time they become inactive. Three basic state actions are:

Type of ActionWhen ExecutedHow Often Executed While State Is Active
EntryWhen the state is entered (becomes active)Once
DuringWhile the state is active and no valid transition to another state is availableAt every time step
ExitBefore a transition is taken to another stateOnce

For example, you can use entry actions to initialize data, during actions to update data, and exit actions to configure data for the next transition. (There are other types of state actions, but they involve concepts that go beyond the scope of this guide. For more information, see Syntax for States and Transitions.)

Based on the requirements in Deciding Whether to Use State Actions, you will write the following state actions for the Air Controller chart:

  • Entry action in state PowerOff to set airflow to 0

  • During action in state SpeedValue to calculate the value of airflow at every time step

Writing an Entry Action

The syntax for entry actions is

entry:one or more actions;
en:one or more actions;

To write the entry action for PowerOff, follow these steps:

  1. Open the model Stage2States — either the one you created in the previous exercises or the supplied model for stage 2.

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

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

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

  4. Click inside the PowerOff state after the last letter of its name label to get a blinking text cursor.

  5. Press the Enter key and type

    entry: airflow = 0;

    Your chart should look like this:

  6. Save Stage3Actions, but leave the chart open for the next exercise.

Writing a During Action

The syntax for during actions is

during:one or more actions;
du:one or more actions;

The during action for SpeedValue uses a Boolean expression to determine whether zero, one, or two fans are running at each time step.

To write the during action for SpeedValue, follow these steps:

  1. Click inside the SpeedValue state after the last letter of its name label to get a blinking text cursor.

  2. Press the Enter key and type

    during: airflow = in(FAN1.On) + in(FAN2.On);

    Your chart should look like this:

    The Boolean expression in(FAN1.On) is true — and its value equals 1 — if the On state of FAN1 is active. If FAN1 is not on — that is, its Off state is active or power is off — then in(FAN1.On) equals 0. Similarly, the value of in(FAN2.On) represents whether FAN2 is on or off. Therefore, the sum of these Boolean expressions indicates whether 0, 1, or 2 fans are operating during each time step.

  3. Save the model Stage3Actions.

Where to go next.  Now you are ready to specify conditions for when state-to-state transitions occur. See Adding the Transitions.

Was this topic helpful?