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 FAN1
and 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 (On
and Off
).
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.
State | Decomposition | Rationale |
---|---|---|
PowerOff , PowerOn | Exclusive (OR) states | The controller cannot be on and off at the same time. |
FAN1 , FAN2 | Parallel (AND) states | The fans operate as independent components that turn on or off depending on how much cooling is required. |
FAN1.On , FAN1.Off | Exclusive (OR) states | Fan 1 cannot be on and off at the same time. |
FAN2.On , FAN2.Off | Exclusive (OR) states | Fan 2 cannot be on and off at the same time. |
SpeedValue | Parallel (AND) state | SpeedValue represents an independent subsystem
that monitors the status of the fans at each time step. |
Note
To give objects unique identifiers when they have the same name in different parts
of the chart hierarchy, use dot notation such as Fan1.On
and
Fan2.On
. For more information, see Identify Data by Using Dot Notation.
Although FAN1
, FAN2
, and
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
temperature than FAN2
. It can turn on regardless of
whether FAN2
is on or off.
FAN2
executes second because it cycles on at a higher
temperature than 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 FAN1
and
FAN2
.
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
table.
Value of airflow | Description | Cooling Activity Factor kCool |
---|---|---|
0 | No fans are running. The value of temp does
not decrease. | 0 |
1 | One fan is running. The value of temp
decreases according to the cooling activity factor. | 0.05 |
2 | Two fans are running. The value of temp
decreases according to the cooling activity factor. | 0.1 |
The Physical Plant block updates the air temperature inside the plant based on the equations
temp
(0) =
TInitial
temp
'(t) =
(TAmbient -
temp
(t))·(kHeat
- kCool),
where:
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 airflow
The new value of temp
determines the amount of cooling
at the next time step of the simulation.