Model Bang-Bang Controller by Using a State Transition Table
A state transition table represents a finite state machine for sequential modal logic in tabular format. Instead of drawing states and transitions in a Stateflow® chart, you can use a state transition table to model a state machine in a concise, compact format that requires minimal maintenance of graphical objects. For more information, see Model Finite State Machines Using State Transition Tables.
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.
Scope | Description | Variable Name |
---|---|---|
Input | High temperature set point | reference_high |
Input | Low temperature set point | reference_low |
Input | Alarm indicator | ALARM |
Input | All-clear indicator | CLEAR |
Input | Current temperature of the boiler | temp |
Local | Indicator that the boiler completed warm-up | doneWarmup |
Output | Command to set the boiler mode: off, warm-up, or on | boiler_cmd |
Add a New State Transition Table
In this exercise, you add a state transition table to a Simulink® model that contains the required Simulink blocks, except for the bang-bang controller.
To implement the model yourself, follow these steps. Otherwise, you can open the completed model.
1. Open the example.
2. Delete the five output ports and the single input port.
3. Add a State Transition Table block to the model.
Add States and Hierarchy
To represent the operating modes of the boiler, add states and hierarchy to the state transition table.
Open the state transition table.
Represent the high-level operating modes: normal and alarm.
Double-click
state1
and rename itNormal
.Double-click
state2
and rename itAlarm
.
Represent the three states of normal operation as substates of
Normal
:Right-click the
Normal
state, select Insert Row > Child State Row, and name the new stateOff
.Repeat step a two more times to create the child states
Warmup
andOn
, 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
andOff
states appear with default transitions. This configuration meets the design requirements for this model. To set a default state, right-click the state and select Set to default.
Your state transition table looks 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).
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. Save the state transition table.
Your state transition table looks 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.
In the
Normal
state row, enter:if [ALARM]
Alarm During simulation:
When first entered, the chart activates the
Normal
state.At each time step, normal operation cycles through the
Off
,Warmup
, andOn
states until the ALARM condition is true.When the ALARM condition is true, the boiler transitions to the
Alarm
state and shuts down immediately.
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.
In the
Warmup
state row, enter:if else-if [doneWarmup]
after(10, sec)
{doneWarmup = true;}
On On During simulation, the boiler warms up for 10 seconds and then transitions to the
On
state.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.
In the
Alarm
state row, enter:if [CLEAR]
Normal During simulation, when the all-clear condition is true, the boiler returns to normal mode.
Save the state transition table.
Your state transition table looks like this table.
Now you are ready to add data definitions by using the Symbols Pane.
Define Data
Next, define the data for the state transition table. You can use data to explicitly define the size, type, and complexity of the MATLAB® variables in the table.
You can define data by using the Symbols pane. In the Modeling tab, click Symbols Pane. The Symbols pane displays data associated with the variables you assign in the table. Stateflow infers which data are input, output, and parameter data, but the data is undefined until you resolve it. In the Name column, the warning symbol indicates undefined data.
To resolve the undefined data, in the Symbols pane, click the Resolve undefined symbols icon
. The Stateflow Editor assigns a scope to each symbol in the chart.
Symbol Scope reference_high
Input Data reference_low
Input Data temp
Input Data boiler_cmd
Output Data doneWarmup
Local Data ALARM
Parameter CLEAR
Parameter Because the Stateflow Editor infers the scope based on context, it may not always assign the appropriate scope.
Correct the scopes of
ALARM
andCLEAR
. In the Type column, select the Parameter Data icon. In the drop-down menu, select Input Data.
For each input, assign the correct value in the Port column:
Assign: To Port: reference_low
2 reference_high
1 temp
5 ALARM
3 CLEAR
4 Save the state transition table.
In the Simulink model, the inputs and outputs that you defined appear as input and output ports on the State Transition Table block.
Connect the Transition Table and Run the Model
In the Simulink model, connect the state transition table to the Simulink inputs and outputs:
Save the model.
Reopen your state transition table.
Start the simulation by selecting Run.
As the simulation runs, you can watch the animation in the state transition table activate different states.
When performing interactive debugging, you can set breakpoints on different states and view the data values at different points in the simulation. For more information about debugging, see Set Breakpoints to Debug Charts.