If your Stateflow® chart includes data that is highly correlated to the chart hierarchy, you can simplify your design by using active state data. By enabling active state data, you can:
Avoid manual data updates reflecting chart activity.
Log and monitor chart activity in the Simulation Data Inspector.
Use chart activity data to control other subsystems.
Export chart activity data to other Simulink® blocks.
For more information, see Create a Hierarchy to Manage System Complexity.
Using active state data output can simplify the design of some Stateflow charts. For example, in this model of a traffic signal, the state that is
active determines the value of the symbol color
. When you enable
active state data, Stateflow can provide the color of the traffic signal by tracking state activity.
Explicitly updating color
is no longer necessary, so you can delete
this symbol and simplify the design of the chart.
Stateflow provides active state data through an output port to Simulink or as local data to your chart. This table lists the different modes of active state data available.
Activity Mode | Data Type | Description |
---|---|---|
Self activity | Boolean | Is the state active? |
Child activity | Enumeration | Which child state is active? |
Leaf state activity | Enumeration | Which leaf state is active? |
To enable active state data, use the Property Inspector.
Select the Create output for monitoring check box.
Select an activity mode from the drop-down list.
Enter the Data name for the active state data symbol.
(Optional) For Child or Leaf state activity, enter the Enum name for the active state data type.
By default, Stateflow reports state activity as output data. To change the scope of an active state data symbol to local data, use the Symbols pane.
This example uses active state data to model the controller system for a pair of traffic lights.
Inside the Traffic Controller chart, two parallel subcharts manage the logic controlling the traffic lights. The subcharts have an identical hierarchy consisting of three child states: Red
, Yellow
, and Green
. The output data Light1
and Light2
correspond to the active child states in the subcharts. These signals:
Determine the phase of the animated traffic lights.
Contribute to the number of cars waiting at each light.
Drive a Safety Assertion subsystem verifying that the two traffic lights are never simultaneously green.
To see the subcharts inside the Traffic Controller chart, click the arrow at the bottom left corner of the chart.
Each traffic controller cycles through its child states, from Red
to Green
to Yellow
and back to
Red
. Each state corresponds to a phase in the traffic light
cycle. The output signals Light1
and Light2
indicate which state is active at any given time.
When the Red
state becomes active, the traffic light cycle
begins. After a short delay, the controller checks for cars waiting at the
intersection. If it detects at least one car, or if a fixed length of time elapses,
then the controller requests a green light by setting
greenLightRequest
to true
. After making
the request, the controller remains in the Red
state for a short
length of time until it detects that the other traffic signal is red. The controller
then makes the transition to Green
.
When the Green
state becomes active, the controller cancels its
green light request by setting greenLightRequest
to
false
. The controller sets
greenLightLocked
to true
, preventing the
other traffic signal from turning green. After some time, the controller checks for
a green light request from the other controller. If it receives a request, or if a
fixed length of time elapses, then the controller transitions to the
Yellow
state.
Before transitioning to the Red
state, the controller remains
in the Yellow
state for a fixed amount of time. When the
Yellow
state becomes inactive, the controller sets
greenLightLocked
to false
, indicating that
the other traffic light can safely turn green. The traffic light cycle then begins
again.
Several parameters define the timing of the traffic light cycle. To change the values of these parameters, double-click the Traffic Controller chart and enter the new values in the Block Parameters dialog box.
Parameter | Preset Value | Description |
---|---|---|
REDDELAY | 6 seconds | Length of time before the controller begins to check for cars at the intersection. Also, minimum length of time before the traffic light can turn green after the controller requests a green light. |
MAXREDDELAY | 360 seconds | Maximum length of time that the controller checks for cars before requesting a green light. |
GREENDELAY | 180 seconds | Maximum length of time that the traffic light remains green. |
MINGREENDELAY | 120 seconds | Minimum length of time that the traffic light remains green. |
YELLOWDELAY | 15 seconds | Length of time that the traffic light remains yellow. |
Enable logging for each of these symbols. In the Symbols pane, select each symbol. In the Property Inspector, under Logging, select Log signal data.
greenLightRequested
greenLightLocked
Light1
Light2
In the Simulation tab, click
Run
.
In the Simulation tab, under Review
Results, click Data Inspector
.
In the Simulation Data Inspector, display the logged signals in separate
axes. The Boolean signals greenLightRequested
and
greenLightLocked
appear as numeric values of zero or
one. The state activity signals Light1
and
Light2
are shown as enumerated data with values of
Green
, Yellow
,
Red
, and None
.
To trace the chart activity during the simulation, you can use the zoom and cursor buttons in the Simulation Data Inspector. For example, this table details the activity during the first 300 seconds of the simulation.
Time | Description | Light 1 | Light2 | greenLightRequested | greenLightLocked |
---|---|---|---|---|---|
t = 0 | At the start of the simulation, both traffic lights are red. | Red | Red | false | false |
t = 6 | After 6 seconds (REDDELAY ), there are cars
waiting in both streets. Both traffic lights request a green light
by setting greenLightRequested =
true . | Red | Red | true | false |
t = 12 | After another 6 seconds (
| Green | Red | false , then true | true |
t = 132 | After 120 seconds ( | Yellow | Red | true | true |
t = 147 | After 15 seconds (
| Red | Green | false | false , then true |
t = 153 | After 6 seconds ( | Red | Green | true | true |
t = 267 | Light 2 turns yellow 120 seconds
( | Red | Yellow | true | true |
t = 282 | After 15 seconds (
| Green | Red | false | false , then true |
t = 288 | After 6 seconds ( | Green | Red | true | true |
The cycle repeats until the simulation ends at t = 1000 seconds.