Main Content

Schedule Simulink Functions by Using Stateflow

This example shows how to schedule a Simulink® function in a Stateflow® chart by using temporal logic. A Simulink function is a Simulink subsystem that you define inside the Stateflow chart and call in the actions of states and transitions. For more information, see Reuse Simulink Functions in Stateflow Charts.

Open and Examine the Model

In this example, the Simulink function algo increments the input by one at each time step. The Stateflow chart includes two states that call the function algo at different rates.

Temporal logic operators determine the schedules for function calls and transitions between the states. The event-based temporal logic operator every sets FastScheduler to call the Simulink function every two time steps and SlowScheduler to call the same function every five time steps. Consequently, FastScheduler executes the function more frequently than SlowScheduler.

The transition from FastScheduler to SlowScheduler occurs after FastScheduler is active for 1.5 seconds. The transition back to FastScheduler occurs after SlowScheduler is active for 3.5 seconds. The absolute-time temporal logic operator after controls the timing of transitions between states.

Differences Between Event-Based and Absolute-Time Temporal Logic Operators

The type of temporal logic operator that you use depends on whether you are scheduling a function call or a transition.

Event-based temporal logic operators, such as every, depend on the step size used by the Simulink solver. The number of function calls since a state became active also depends on the solver's step size. Therefore, in charts in a Simulink model, using every as an absolute-time temporal logic operator is not supported.

Absolute-time temporal logic operators such as after depend on the elapsed time since a state became active. In charts in a Simulink model, using at as an absolute-time temporal logic operator is not supported

For more information, see Control Chart Execution by Using Temporal Logic.

View Simulation Result

Run the model. The Scope block illustrates the rates of each function call by the differing slope steepness. Steeper slopes indicate the more frequent Simulink function calls.

See Also

| |

Related Topics