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. 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 ModeData TypeDescription
Self activityBooleanIs the state active?
Child activityEnumerationWhich child state is active?
Leaf state activityEnumerationWhich leaf state is active?

To enable active state data, use the Property Inspector.

  1. Select the Create output for monitoring check box.

  2. Select an activity mode from the drop-down list.

  3. Enter the Data name for the active state data symbol.

  4. (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 window.

Example of Active State Data

The Stateflow example sf_traffic_light uses active state data to implement the controller system for a pair of traffic lights.

Inside the Traffic Controller chart, a pair of parallel subcharts manages 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.

Behavior of Traffic Controller Subcharts

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

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.

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, 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.

Yellow Light

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.

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.

ParameterPreset ValueDescription
REDDELAY6 secondsLength 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.
MAXREDDELAY360 secondsMaximum length of time that the controller checks for cars before requesting a green light.
GREENDELAY180 secondsMaximum length of time that the traffic light remains green.
MINGREENDELAY120 secondsMinimum length of time that the traffic light remains green.
YELLOWDELAY15 secondsLength of time that the traffic light remains yellow.

Explore the Example

  1. In the Property Inspector, enable logging for these symbols:

    • greenLightRequested

    • greenLightLocked

    • Light1

    • Light2

  2. Run the simulation.

  3. 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.

TimeDescriptionLight 1Light2greenLightRequestedgreenLightLocked
t = 0At the start of the simulation, both traffic lights are red.RedRedfalsefalse
t = 6After 6 seconds (REDDELAY), there are cars waiting in both streets. Both traffic lights request a green light by setting greenLightRequested = true.RedRedtruefalse
t = 12

After another 6 seconds (REDDELAY):

  • Light 1 turns green, setting greenLightLocked = true and greenLightRequested = false.

  • Light 2 requests a green light by setting greenLightRequested = true.

GreenRedfalse, then truetrue
t = 132

After 120 seconds (MINGREENDELAY), Light 1 turns yellow.

YellowRedtruetrue
t = 147

After 15 seconds (YELLOWDELAY):

  • Light 1 turns red, setting greenLightLocked = false.

  • Light 2 turns green, setting greenLightLocked = true and greenLightRequested = false.

RedGreenfalsefalse, then true
t = 153

After 6 seconds (REDDELAY), Light 1 requests a green light by setting greenLightRequested = true.

RedGreentruetrue
t = 267

Light 2 turns yellow 120 seconds (MINGREENDELAY) after turning green.

RedYellowtruetrue
t = 282

After 15 seconds (YELLOWDELAY):

  • Light 2 turns red, setting greenLightLocked = false.

  • Light 1 turns green, setting greenLightLocked = true and greenLightRequested = false.

GreenRedfalsefalse, then true
t = 288

After 6 seconds (REDDELAY), Light 2 requests a green light by setting greenLightRequested = true.

GreenRedtruetrue

The cycle repeats until the simulation ends at t = 1000 seconds.

Related Examples

More About