Create Data Dependent State Behavior
Data dependent state behavior allows you to design Stateflow charts that respond to changes in data values. By connecting state transitions and actions to data conditions, you can create systems that adapt to input signals and internal variables.
Design States That Respond to Data Changes
States can respond to data in several ways:
Transition between states based on data conditions
Execute different actions based on data values
Use data to control timing and sequencing
To implement data dependent behavior:
Define data objects in your chart
Create conditions that use these data objects
Associate conditions with transitions or state actions
Implement detection mechanisms for data changes
Implement Conditional State Actions
State actions can execute conditionally based on data values.
For charts that use C as the action language, use transitions and junctions to implement conditional logic instead of control statements.
Use with Local State Data
Each state can maintain local data to track its internal conditions:
Define local data in the Stateflow chart
Use entry actions to initialize data
Use during actions to update data
Use exit actions to save final data values
Detect Data Changes
Stateflow provides operators to detect when data changes:
hasChanged(data)- Detects any change in valuehasChangedTo(data, value)- Detects change to a specific valuehasChangedFrom(data, value)- Detects change from a specific value
Monitor Signal Edges
For analog signals, edge detection operators identify when values cross thresholds:
rising(expression)- Detects when expression changes from negative to positivefalling(expression)- Detects when expression changes from positive to negativecrossing(expression)- Detects any zero crossing
Create Robust Data Processing
To handle data reliably:
Initialize all data before use
Consider the effects of data type conversion
Handle boundary conditions and invalid values
Use temporal logic to reduce sensitivity to noise
Document data dependencies for maintenance
By implementing data dependent state behavior, you can create responsive systems that adapt to changing conditions while maintaining the structure and clarity of state machine design.