To implement operating modes that run concurrently, use parallelism in your Stateflow® chart. For example, as part of a complex system design, you can employ parallel states to model independent components or subsystems that are active simultaneously. For more information, see Model Finite State Machines.
Stateflow charts can combine exclusive (OR) states and parallel (AND) states:
Exclusive (OR) states represent mutually exclusive modes of operation. No two exclusive states at the same hierarchical level can be active or execute at the same time. Stateflow represents each exclusive state by a solid rectangle.
Parallel (AND) states represent independent modes of operation. Two or more parallel states can be active at the same time, although they execute in a serial fashion. Stateflow represents each parallel state by a dashed rectangle with a number indicating its execution order.
All states at a given hierarchical level must be of the same type. The parent state, or in the case of top-level states, the chart itself, has OR (exclusive) or AND (parallel) decomposition. The default state decomposition type is OR (exclusive). To change the decomposition type, right-click the parent state and select Decomposition > AND (Parallel).
This example employs parallelism to implement an air controller that maintains air temperature at 120 degrees in a physical plant.
The controller operates two fans. The first fan turns on when the air temperature rises above 120 degrees. The second fan provides additional cooling when the air temperature rises above 150 degrees. The chart models these fans as parallel states
FAN2, both of which are active when the controller is turned on. Except for their operating thresholds, the fans have an identical configuration of states and transitions that reflects the two modes of fan operation (
A third parallel state
SpeedValue calculates the value of the output data
airflow based on how many fans have cycled on at each time step. The Boolean expression
in(FAN1.On) has a value of 1 when the
On state of
FAN1 is active. Otherwise,
in(FAN1.On) equals 0. Similarly, the value of
in(FAN2.On) represents whether
FAN2 has cycled on or off. The sum of these expressions indicates the number of fans that are turned on during each time step.
This table lists the rationale for using exclusive (OR) and parallel (AND) states in the air controller chart.
|Exclusive (OR) states||The controller cannot be on and off at the same time.|
|Parallel (AND) states||The fans operate as independent components that turn on or off depending on how much cooling is required.|
|Exclusive (OR) states||Fan 1 cannot be on and off at the same time.|
|Exclusive (OR) states||Fan 2 cannot be on and off at the same time.|
|Parallel (AND) state|
To give objects unique identifiers when they have the same name in different parts
of the chart hierarchy, use dot notation such as
Fan2.On. For more information, see Identify Data by Using Dot Notation.
SpeedValue are active concurrently, these states execute in
serial fashion during simulation. The numbers in the upper-right corners of the states
specify the order of execution. The rationale for this order of execution is:
FAN1 executes first because it cycles on at a lower
FAN2. It can turn on regardless of
FAN2 is on or off.
FAN2 executes second because it cycles on at a higher
FAN1. It can turn on only if
FAN1 is already on.
SpeedValue executes last so it can observe the most
up-to-date status of
By default, Stateflow assigns the execution order of parallel states based on their order of creation in the chart. To change the execution order of a parallel state, right-click the state and select a value from the Execution Order drop-down list.
The Stateflow example contains a Stateflow chart and a Simulink® subsystem.
Based on the air temperature
temp, the Air
Controller chart turns on the fans and passes the value of
airflow to the Physical Plant subsystem. This
output value determines the amount of cooling activity, as indicated by this
|Value of ||Description||Cooling Activity Factor kCool|
|0||No fans are running. The value of ||0|
|1||One fan is running. The value of ||0.05|
|2||Two fans are running. The value of ||0.1|
The Physical Plant block updates the air temperature inside the plant based on the equations
TInitial is the initial temperature (default = 70o)
TAmbient is the ambient temperature (default = 160o)
kHeat is the heat transfer factor for the plant (default = 0.01)
kCool is the cooling activity
factor corresponding to
The new value of
temp determines the amount of cooling
at the next time step of the simulation.