Monitor Chart Activity by Using Active State Data
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.
Active State Data
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.
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.
Example of Active State Data
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:
Green. The output data
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.
Behavior of Traffic Controller Subcharts
Each traffic controller cycles through its child states, from
Yellow and back to
Red. Each state corresponds to a phase in the traffic light
cycle. The output signals
indicate which state is active at any given time.
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
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 state becomes active, the controller cancels its
green light request by setting
false. The controller sets
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
Before transitioning to the
Red state, the controller remains
Yellow state for a fixed amount of time. When the
Yellow state becomes inactive, the controller sets
false, indicating that
the other traffic light can safely turn green. The traffic light cycle then begins
Timing of Traffic Lights
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.
|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.|
|360 seconds||Maximum length of time that the controller checks for cars before requesting a green light.|
|180 seconds||Maximum length of time that the traffic light remains green.|
|120 seconds||Minimum length of time that the traffic light remains green.|
|15 seconds||Length of time that the traffic light remains yellow.|
Explore the Example
Enable logging for each of these symbols. In the Symbols pane, select each symbol. In the Property Inspector, under Logging, select Log signal data.
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
greenLightLockedappear as numeric values of zero or one. The state activity signals
Light2are shown as enumerated data with values of
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.
|t = 0||At the start of the simulation, both traffic lights are red.|
|t = 6||After 6 seconds (|
|t = 12|
After another 6 seconds (
|t = 132|
After 120 seconds (
|t = 147|
After 15 seconds (
|t = 153|
After 6 seconds (
|t = 267|
Light 2 turns yellow 120 seconds
|t = 282|
After 15 seconds (
|t = 288|
After 6 seconds (
The cycle repeats until the simulation ends at t = 1000 seconds.