Documentation Center

  • Trial Software
  • Product Updates

Model Bang-Bang Controller with State Transition Table

Why Use State Transition Tables?

A state transition table is an alternative way of expressing modal logic. Instead of drawing states and transitions graphically in a Stateflow® chart, you express the modal logic in tabular format. State transition tables use MATLAB® as the action language.

Benefits of using state transition tables include:

  • Ease of modeling train-like state machines, where the modal logic involves transitions from one state to its neighbor

  • Concise, compact format for a state machine

  • Reduced maintenance of graphical objects

    When you add or remove states from a chart, you have to rearrange states, transitions, and junctions. When you add or remove states from a state transition table, you do not have to rearrange any graphical objects.

Design Requirements

This example shows how to model a bang-bang controller for temperature regulation of a boiler, using a state transition table. The controller must turn the boiler on and off to meet the following design requirements:

  • High temperature cannot exceed 25 degrees Celsius.

  • Low temperature cannot fall below 23 degrees Celsius.

  • Steady-state operation requires a warm-up period of 10 seconds.

  • When the alarm signal sounds, the boiler must shut down immediately.

  • When the all-clear signal sounds, the boiler can turn on again.

Identify System Attributes

You can identify the operating modes and data requirements for the bang-bang controller based on its design requirements.

Operating Modes

The high-level operating modes for the boiler are:

  • Normal operation, when no alarm signal sounds.

  • Alarm state, during an alarm signal.

During normal operation, the boiler can be in one of three states:

  • Off, when the temperature is above 25 degrees Celsius.

  • Warm-up, during the first 10 seconds of being on.

  • On, steady-state after 10 seconds of warm-up, when the temperature is below 23 degrees Celsius.

Data Requirements

The bang-bang controller requires the following data.

ScopeDescriptionVariable Name
InputHigh temperature set pointreference_high
InputLow temperature set pointreference_low
InputAlarm indicatorALARM
InputAll-clear indicatorCLEAR
InputCurrent temperature of the boilertemp
LocalIndicator that the boiler completed warm-updoneWarmup
OutputCommand to set the boiler mode: off, warm-up, or onboiler_cmd

Build the Controller or Use the Supplied Model

To build the bang-bang controller model yourself using a state transition table, work through the exercises, starting with Create a New State Transition Table. Otherwise, open the supplied model. At the MATLAB prompt, enter these commands:

addpath (fullfile(docroot,'toolbox','stateflow','examples'))
ex_stt_boiler

Create a New State Transition Table

You use a state transition table to represent the bang-bang controller. Compared to a graphical state transition diagram, the state transition table is a compact way to represent modal logic that involves transitions between neighboring states. State transition tables use MATLAB syntax.

  1. Open the partially built boiler plant model by entering these commands:

    addpath (fullfile(docroot,'toolbox','stateflow','examples'))
    ex_stt_boiler_exercise 

    This model contains all required Simulink® blocks, except for the bang-bang controller.

  2. Delete the five output ports and the single input port.

  3. Open the Library Browser by selecting View > Library Browser.

  4. In the left pane of the Library Browser, select the Stateflow library, then drag a State Transition Table block from the right pane into your boiler model.

    Your model should look something like this model.

  5. Close the Library Browser.

Now you are ready to add states and hierarchy to the state transition table.

Add States and Hierarchy

To represent the operating modes of the boiler, add states and hierarchy to the state transition table.

  1. Open the state transition table.

    In the first cell, the gray text provides hints on how to specify conditions, actions, and destinations for transitions, using MATLAB syntax.

    If you want to remove these hints, select Table > Disable Cell Hints.

  2. Represent the high-level operating modes, normal and alarm:

    1. Double-click state1 and rename it Normal.

    2. Double-click state2 and rename it Alarm.

  3. Represent the three states of normal operation as substates of Normal:

    1. Right-click the Normal state, select Insert Row > Child State Row, and name the new state Off.

    2. Repeat step a two more times to create the child states Warmup and On, in that order.

    By default, when there is ambiguity, the top exclusive (OR) state at every level of hierarchy becomes active first. For this reason, the Normal and Off states appear with default transitions. This configuration meets the design requirements for this model.

     How do I set a default state?

Your state transition table should look like this table.

Now you are ready to specify actions for each state.

Specify State Actions

To describe the behavior that occurs in each state, specify state actions in the table. In this exercise, you initialize modes of operation as the boiler enters normal and alarm states, using the variables boiler_cmd and doneWarmup (described in Data Requirements).

  1. In the following states, click after the state name, press Enter, and type the specified entry actions.

    In State:Type:Resulting Behavior
    Off
    entry:
    boiler_cmd = 0;
    doneWarmup = false;
    Turns off the boiler and indicates that the boiler has not warmed up.
    Warmup
    entry:
    boiler_cmd = 2;
    Starts warming up the boiler.
    On
    entry:
    boiler_cmd = 1;
    Turns on the boiler.
    Alarm
    entry:
    boiler_cmd = 0;
    Turns off the boiler.

  2. Save the state transition table.

Your state transition table should look like this table.

Now you are ready to specify the conditions and actions for transitioning from one state to another state.

Specify Transition Conditions and Actions

To indicate when to change from one operating mode to another, specify transition conditions and actions in the table. In this exercise, you construct statements using variables described in Data Requirements.

  1. In the Normal state row, enter:

    if
    [ALARM]
     
    Alarm

    During simulation:

    1. When first entered, the chart activates the Normal state.

    2. At each time step, normal operation cycles through the Off, Warmup, and On states until the ALARM condition is true.

    3. When the ALARM condition is true, the boiler transitions to the Alarm state and shuts down immediately.

  2. In the Off state row, enter:

    if
    [temp <= reference_low]
     
    Warmup

    During simulation, when the current temperature of the boiler drops below 23 degrees Celsius, the boiler starts to warm up.

  3. In the Warmup state row, enter:

    ifelse-if
    [doneWarmup][after(10, sec)]
     {doneWarmup = true;}
    OnOn

    During simulation, the boiler warms up for 10 seconds and then transitions to the On state.

  4. In the On state row, enter:

    if
    [temp >= reference_high]
     
    Off

    During simulation, when the current temperature of the boiler rises above 25 degrees Celsius, the boiler shuts off.

  5. In the Alarm state row, enter:

    if
    [CLEAR]
     
    Normal

    During simulation, when the all-clear condition is true, the boiler returns to normal mode.

  6. Save the state transition table.

Your state transition table should look like this table.

Now you are ready to add data definitions using the Symbol Wizard.

Define Data

State transition tables use MATLAB syntax and honor the language requirements for C/C++ code generation. One of these requirements is that you define the size, type, and complexity of all MATLAB variables so that their properties can be determined at compile time. Even though you have not yet explicitly defined the data in your state transition table, you can use the Symbol Wizard. During simulation, the Symbol Wizard alerts you to unresolved symbols, infers their properties, and adds the missing data to your table.

  1. In the Simulink model editor menu, select Simulation > Run.

    Two dialog boxes appear:

    • The Diagnostic Viewer indicates that you have unresolved symbols in the state transition table.

    • The Symbol Wizard attempts to resolve the missing data. The wizard correctly infers the scope of all data except for the inputs ALARM and CLEAR.

  2. In the Symbol Wizard, correct the scopes of ALARM and CLEAR by selecting Input from their Scope drop-down lists.

  3. When the Model Explorer opens, verify that the Symbol Wizard added all required data definitions correctly.

    Some of the inputs are assigned to the wrong ports.

  4. In the Contents pane of the Model Explorer, reassign input ports as follows:

    Assign:To Port:
    reference_low2
    reference_high1
    temp5
    ALARM3
    CLEAR4

  5. Save the state transition table.

  6. Close the Diagnostic Viewer and the Model Explorer.

In the Simulink model, the inputs and outputs that you just defined appear in the State Transition Table block. Now you are ready to connect these inputs and outputs to the Simulink signals and run the model.

Connect the Transition Table and Run the Model

  1. In the Simulink model, connect the state transition table to the Simulink inputs and outputs:

  2. Save the model.

  3. Start the simulation by selecting Simulation > Run.

The following output appears in the Scope block.

View the Graphical Representation

Stateflow automatically generates a read-only graphical representation of the state transition table you created. You might want to view the graphical state transition diagram to use animation and interactive debugging. Animation lets you visually verify that your chart behaves as expected. Interactive debugging lets you set breakpoints, control execution, and view data values directly from the state diagram.

  1. In the state transition table, select Table > View State Diagram.

    The top-level state transition diagram looks like this:

    The Normal state appears as a subchart.

  2. Double-click the Normal state to view the states and transitions it contains.

Was this topic helpful?