Documentation Center |
On this page… |
---|
Types of Data Value Changes That You Can Detect |
You can detect changes in Stateflow^{®} data from one time step to the next time step. All charts can detect changes on chart input data. C charts can also detect changes in chart output data, local chart variables, machine-parented variables, and data store memory data.
For each of these types of data, you can use operators that detect the following changes.
Type of Change | Operator |
---|---|
Data changes value from the beginning of the last time step to the beginning of the current time step. | See hasChanged Operator. |
Data changes from a specified value at the beginning of the last time step to a different value at the beginning of the current time step. | |
Data changes to a specified value at the beginning of the current time step from a different value at the beginning of the last time step. |
Change detection operators return 1 if the data value changes or 0 if there is no change. See Change Detection Operators.
Stateflow software ships with a model sf_tetris2 that shows how you can detect asynchronous changes in inputs — in this case, user keystrokes — to manipulate a Tetris shape as it moves through the playing field. The chart TetrisLogic implements this logic:
TetrisLogic contains a subchart called Moving that calls the operator hasChanged to determine when users press any of the Tetris control keys, and then moves the shape accordingly. Here is a look inside the subchart:
To run the model, follow these steps:
Open the model by typing sf_tetris2 at the MATLAB^{®} prompt.
Start simulation.
A chart detects changes in chart data by evaluating values at time step boundaries. That is, the chart compares the value at the beginning of the previous execution step with the value at the beginning of the current execution step. To detect changes, the chart automatically double-buffers these values in local variables, as follows:
Local Buffer: | Stores: |
---|---|
var_name_prev | Value of data at the beginning of the last time step |
var_name_start | Value of data at the beginning of the current time step |
Note: Double-buffering occurs once per time step except if multiple input events occur in the same time step. Then, double-buffering occurs once per input event (see Handle Changes When Multiple Input Events Occur). |
When you invoke change detection operations in an action, Stateflow software performs the following operations:
Double-buffers data values after a Simulink^{®} event triggers the chart, but before the chart begins execution.
Compares values in _prev and _start buffers. If the values match, the change detection operator returns 0 (no change); otherwise, it returns 1 (change).
The following diagram places these tasks in the context of the chart life cycle:
The fact that buffering occurs before chart execution has implications for change detection in the following scenarios:
Stateflow software attempts to filter out transient changes in local chart variables by evaluating their values only at time boundaries (see How Change Detection Works). This behavior means that the software evaluates the specified local variable only once at the end of the execution step and, therefore, returns a consistent result. That is, the return value remains constant even if the value of the local variable fluctuates within a given time step.
For example, suppose that in the current time step a local variable temp changes from its value at the previous time step, but then reverts to the original value. In this case, the operator hasChanged(temp) returns 0 for the next time step, indicating that no change occurred. For more information, see Change Detection Operators.
When multiple input events occur in the same time step, Stateflow software updates the _prev and _start buffers once per event. In this way, a chart detects changes between input events, even if the changes occur more than once in a given time step.
Change detection operators check for changes in chart inputs, outputs, and local variables, and in Stateflow data that is bound to Simulink data store memory.
You can invoke change detection operators wherever you call built-in functions in a chart — in state actions, transition actions, condition actions, and conditions. There are three change detection operators:
The hasChanged operator detects any change in Stateflow data since the last time step, using the following heuristic:
where x_{start} represents the value at the beginning of the current time step and x_{prev} represents the value at the beginning of the previous time step.
hasChanged ( u ) hasChanged ( m [ expr ] ) hasChanged ( s [ expr ] )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a chart
Stateflow data that is bound to Simulink data store memory
The arguments cannot be expressions or custom code variables.
Description. hasChanged ( u ) returns 1 if u changes value since the last time step. If u is a matrix, hasChanged returns 1 if any element of u changes value since the last time step.
hasChanged ( m [ expr ] ) returns 1 if the value at location expr of matrix m changes value since the last time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChanged ( s [ expr ] ) returns 1 if the value at location expr of aggregate data s has changed since the last time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
All forms of hasChanged return zero if a chart writes to the data, but does not change its value.
The hasChangedFrom operator detects when Stateflow data changes from a specified value since the last time step, using the following heuristic:
where x_{start} represents the value at the beginning of the current time step and x_{prev} represents the value at the beginning of the previous time step.
hasChangedFrom ( u , v ) hasChangedFrom ( m [ expr ], v ) hasChangedFrom ( s [ expr ], v )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a chart
Stateflow data that is bound to Simulink data store memory
Note: The first arguments u, m, and s cannot be expressions or custom code variables. The second argument v can be an expression. However, if the first argument is a matrix variable, then v must resolve to a scalar value or a matrix value with the same dimensions as the first argument. |
Description. hasChangedFrom ( u, v ) returns 1 if u changes from the value specified by v since the last time step. If u is a matrix variable whose elements all equal the value specified by v, hasChangedFrom returns 1 if one or more elements of the matrix changes to a different value in the current time step.
hasChangedFrom ( m [ expr ], v) returns 1 if the value at location expr of matrix m changes from the value specified by v since the last time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChangedFrom ( s [ expr ], v) returns 1 if the value at location expr of aggregate data s changes from the value specified by v since the last time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
The hasChangedTo operator detects when Stateflow data changes to a specified value since the last time step, using the following heuristic:
where x_{start} represents the value at the beginning of the current time step and x_{prev} represents the value at the beginning of the previous time step.
hasChangedTo ( u , v ) hasChangedTo ( m [ expr ], v ) hasChangedTo ( s [ expr ], v )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a chart
Stateflow data that is bound to Simulink data store memory
Note: The first arguments u, m, and s cannot be expressions or custom code variables. The second argument v can be an expression. However, if the first argument is a matrix variable, then v must resolve to either a scalar value or a matrix value with the same dimensions as the first argument. |
Description. hasChangedTo ( u, v ) returns 1 if u changes to the value specified by v in the current time step. If u is a matrix variable, hasChangedTo returns 1 if any its of its elements changes value so that all elements of the matrix equal the value specified by v in the current time step.
hasChangedTo ( m [ expr ], v) returns 1 if the value at location expr of matrix m changes to the value specified by v in the current time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChangedTo ( s [ expr ], v) returns 1 if the value at location expr of aggregate data s changes to the value specified by v in the current time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
The following modelmodel shows how to use the hasChanged, hasChangedFrom, and hasChangedTo operators to detect specific changes in an input signal. In this example, a Ramp block sends a discrete, increasing time signal to a chart:
The model uses a fixed-step solver with a step size of 1. The signal increments by 1 at each time step. The chart analyzes the input signal for the following changes at each time step:
Any change from the previous time step
Change to the value 3
Change from the value 3
To check the signal, the chart calls three change detection operators in a transition action, and outputs the return values as y1, y2, and y3, as follows:
During simulation, the outputs y1, y2, and y3 represent changes in the input signal, as shown in this scope: