Monitor State Activity Through Active State Data

Active state data can simplify the design of some Stateflow® charts because you do not have to maintain data that is highly correlated to the chart hierarchy. When you enable active state data, Stateflow reports state activity through an output port to Simulink® or as local data in your chart. Using active state data, you can:

  • Avoid manual data updates reflecting chart activity.

  • View chart activity by using a scope or the Simulation Data Inspector.

  • Log chart activity for diagnostics.

  • Drive other Simulink subsystems.

Types of Active State Data

When you enable active state data, Stateflow creates a Boolean or enumerated data object to match the activity type.

Activity TypeActive State Data TypeDescription
Self activityBooleanIs the state active?
Child activityEnumerationWhich child is active?
Leaf state activityEnumerationWhich leaf state is active?

For self-activity of a chart or state, the data value is true when active and false when inactive. For child and leaf state activity, the data is an enumerated type. Stateflow can define the enumeration class or you can create the definition manually. For more information, see Define State Activity Enumeration Type.

You can enable active state data for a Stateflow chart, state, state transition table, or atomic subchart. This table lists the activity types supported by each kind of Stateflow object.

Stateflow ObjectSelf-Activity Child ActivityLeaf State Activity
Charts Not supportedSupportedSupported
StatesSupportedSupportedSupported
Atomic subchartsSupported at the container levelSupported inside the subchartSupported inside the subchart
State transition tablesNot supportedSupportedSupported

Enable Active State Data

You can enable active state data in either the Property Inspector or the Model Explorer.

  • Property Inspector

    1. Open the Property Inspector by selecting View > Property Inspector.

    2. In the Stateflow Editor canvas, select the Stateflow object to monitor.

    3. In the Monitoring section of the Property Inspector, select the Create output for monitoring check box and edit the active state data properties.

  • Model Explorer

    1. Open the Model Explorer by selecting View > Model Explorer.

    2. In the Model Hierarchy pane, double-click the Stateflow object to monitor.

    3. In the Stateflow object pane, select the Create output for monitoring check box and edit the active state data properties.

Active State Data Properties

Activity Type

Type of state activity to monitor. Choose from these options:

  • Self activity

  • Child activity

  • Leaf state activity

Data Name

Name of the active state data object. For more information, see Rules for Naming Stateflow Objects.

Enum Name

Name of the enumerated data type for the active state data object. This property applies only to child and leaf state activity.

Define Enumerated Type Manually

Specifies whether you define the enumerated data type manually. This property applies only to child and leaf state activity. For more information, see Define State Activity Enumeration Type.

Set Scope for Active State Data

By default, active state data has a scope of Output. Stateflow creates an output port on the chart block in the Simulink model.

To access active state data inside a Stateflow chart, change the scope to Local in the Symbols window or in the Model Explorer. For more information, see Set Data Properties.

You can specify information for code generation by binding the local active state data to a Simulink.Signal object. Modify the properties of the object through the CoderInfo property. For more information, see Simulink.CoderInfo.

Define State Activity Enumeration Type

By default, Stateflow defines the enumeration data type for child and leaf activity. If you select the Define enumerated type manually check box and no enumeration data type definition exists, then Stateflow provides a link to create a definition. Clicking the Create enum definition from template link generates a customizable definition.

The enumeration data type definition contains one literal for each state name plus an extra literal to indicate that no substate is active. For example, in the model sf_car, the state gear_state contains four child states that correspond to the gears in a car: first, second, third, fourth. The model specifies the child activity data type with this enumeration class definition:

classdef gearType < Simulink.IntEnumType
   enumeration
		None(0),
		first(1),
		second(2),
		third(3),
		fourth(4)
   end
...
end
For more information, see Define Enumerated Data Types.

The base storage type for automatically created enumerations defaults to Native Integer. For a smaller memory footprint, in the Optimization pane of the Configuration Parameters dialog box, change the value of the Base storage type for automatically created enumerations field. For more information, see Base storage type for automatically created enumerations (Simulink Coder).

Leaf State Activity and Parallel States

When you enable leaf state activity, a state with parallel (AND) decomposition is treated as a leaf state. State activity of the parallel substates is not available because these substates are active simultaneously. For example, suppose that you enable leaf state activity for this chart. Because state B has parallel decomposition, its substates B1 and B2 are active simultaneously so B is treated as a leaf state of the chart.

During simulation, a scope connected to the active state output data shows the enumerated values for the leaf states A1, A2, and B.

Limitations for Active State Data

  • Enabling child activity output for states that have no children results in an error at compilation and run time.

  • You cannot enable child or leaf state activity in charts or states with parallel decomposition. To check state activity in substates of parallel states, use the in operator. For more information, see Check State Activity by Using the in Operator.

  • Do not select the chart property Initialize Outputs Every Time Chart Wakes Up on charts that use active state output data. With this setting, the behavior of the output data is unpredictable.

See Also

|

Related Topics