Monitor Chart Activity by Using Active State Data
If your Stateflow® chart includes data that is 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 example, in this model of a traffic signal, the state that is active determines the value of the output signal color
. You can simplify the design of the chart by enabling active state data. In this case, the Stateflow chart can provide the color of the traffic signal by tracking state activity, so you do not have to explicitly update the value of color
.
To enable active state data, select a state to monitor. Then, in the Property Inspector:
1. Select Create output for monitoring.
2. Select one of these activity types:
Self activity
— Boolean value that indicates whether the state is activeChild activity
— Enumerated value that indicates which child state is activeLeaf state activity
— Enumerated value that indicates which leaf state is active
3. Enter the Data name for the active state data symbol.
4. Optionally, for Child activity
or Leaf state activity
, enter the Enum name for the active state data type.
By default, Stateflow charts report state activity as output data to the Simulink model. To change the scope of an active state data symbol to local data, use the Symbols pane.
Model a Traffic Signal Controller
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 that controls the traffic lights. The subcharts have an identical hierarchy that consists 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 that verifies that the two traffic lights are never green simultaneously.
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.
Red Light
The traffic light cycle begins when the Red
state becomes active. 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
.
Green Light
When the Green
state becomes active, the controller cancels its green light request by setting greenLightRequest
to false
. The controller sets greenLightLocked
to true
to prevent the other traffic signal from turning green. After a short delay, 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.
Yellow Light
When the Yellow
state becomes inactive, the controller sets greenLightLocked
to false, indicating that the other traffic light can safely turn green. The controller remains in the Yellow
state for a fixed amount of time before transitioning to the Red
state. The traffic light cycle then begins again.
Timing of Traffic Lights
Several parameters define the timing of the traffic light cycle. To change the timing of the traffic lights, double-click the Traffic Controller
chart and, in the dialog box, enter new values for these parameters:
REDDELAY
— Length of time before the controller checks for cars at the intersection. This value is also the minimum length of time before the traffic light can turn green after the controller requests a green light. The default value is 6 seconds.MAXREDDELAY
— Maximum length of time that the controller checks for cars before requesting a green light. The default value is 360 seconds.GREENDELAY
— Maximum length of time that the traffic light remains green. The default value is 180 seconds.MINGREENDELAY
— Minimum length of time that the traffic light remains green. The default value is 120 seconds.YELLOWDELAY
— Length of time that the traffic light remains yellow. The default value is 15 seconds.
Explore the Example
Open the chart by clicking the arrow in the bottom-left corner.
In the Symbols pane, select
greenLightRequested
. Then, in the Property Inspector, under Logging, select Log signal data.Repeat the previous step for
greenLightLocked
,Light1
, andLight2
.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
andgreenLightLocked
appear as numeric values of zero or one. The state activity signalsLight1
andLight2
are enumerated data with values ofGreen
,Yellow
,Red
, andNone
.
To trace the chart activity during the simulation, you can use the zoom and cursor buttons in the Simulation Data Inspector. For example, these are the key moments during the first 300 seconds of the simulation:
— At the start of the simulation, both traffic lights are red.
Light1
andLight2
areRed
,greenLightRequested
isfalse
, andgreenLightLocked
isfalse
.— After 6 seconds, the default value of
REDDELAY
, there are cars waiting in both streets, so both traffic lights request a green light.Light1
andLight2
are stillRed
,greenLightRequested
istrue
, andgreenLightLocked
isfalse
.— After another 6 seconds, the default value of
REDDELAY
, Light 1 becomes green, cancels the green light request, and setsgreenLightLocked
totrue
. Then, Light 2 requests a green light.Light1
isGreen
,Light2
isRed
,greenLightRequested
becomesfalse
and thentrue
, andgreenLightLocked
istrue
.— After 120 seconds, the default value of
MINGREENDELAY
, Light 1 turns yellow.Light1
isYellow
,Light2
isRed
,greenLightRequested
istrue
, andgreenLightLocked
istrue
.— After 15 seconds, the default value of
YELLOWDELAY
, Light 1 turns red and setsgreenLightLocked
tofalse
. Then, Light 2 turns green, cancels the green light request, and setsgreenLightLocked
totrue
.Light1
isRed
,Light2
isGreen
,greenLightRequested
isfalse
, andgreenLightLocked
becomesfalse
and thentrue
.— After 6 seconds, the default value of
REDDELAY
, Light 1 requests a green light.Light1
isRed
,Light2
isGreen
,greenLightRequested
istrue
, andgreenLightLocked
istrue
.— After Light 2 is green for 120 seconds, the default value of
MINGREENDELAY
, Light 2 turns yellow.Light1
isRed
,Light2
isYellow
,greenLightRequested
istrue
, andgreenLightLocked
istrue
.— After 15 seconds, the default value of
YELLOWDELAY
, Light 2 turns red and setsgreenLightLocked
tofalse
. Then, Light 1 turns green, cancels the green light request, and setsgreenLightLocked
totrue
.Light1
isGreen
,Light2
isRed
,greenLightRequested
isfalse
, andgreenLightLocked
becomesfalse
and thentrue
.— After 6 seconds, the default value of
REDDELAY
, Light 2 requests a green light.Light1
isGreen
,Light2
isRed
,greenLightRequested
istrue
, andgreenLightLocked
istrue
.
The cycle repeats until the simulation ends at seconds.