Skip to Main Content Skip to Search
Product Documentation

Scheduling Execution of Simulink Subsystems

When to Implement Schedulers Using Stateflow Charts

Use Stateflow charts to schedule the order of execution of Simulink subsystems explicitly in a model. Stateflow schedulers extend control of subsystem execution in a Simulink model, which determines order of execution implicitly based on block connectivity via sample time propagation.

Types of Schedulers

You can implement the following types of schedulers using Stateflow charts.

Scheduler Design PatternDescription
Ladder logic schedulerSchedules multiple Simulink subsystems to execute in a single time step
Loop schedulerSchedules one Simulink subsystem to execute multiple times in a single time step
Temporal logic schedulerSchedules Simulink subsystems to execute at specific times

Scheduling Multiple Subsystems in a Single Time Step

The ladder logic scheduler design pattern allows you to specify the order in which multiple Simulink subsystems execute in a single time step. The model sf_ladder_logic_scheduler illustrates this design pattern.

The Ladder Logic Scheduler chart contains the following logic:

Key Behaviors of Ladder Logic Scheduler

The key behaviors of the ladder logic scheduler are:

Function-Call Output Events Trigger Multiple Subsystems.  In a given time step, the Stateflow chart broadcasts a series of function-call output events to trigger the execution of three function-call subsystems — A1, A2, and A3 — in the Simulink model in an order determined by the ladder logic scheduler. Here is the sequence of activities during each time step:

  1. The Simulink model activates the Stateflow chart Edge to Function at a rising edge of the 1-millisecond pulse generator.

  2. The Edge to Function chart broadcasts the function-call output event call to activate the Stateflow chart Ladder Logic Scheduler.

  3. The Ladder Logic Scheduler chart broadcasts function-call output events to trigger the function-call subsystems A1, A2, and A3, based on the values of inputs u1 and u2 (see Flow Graph Determines Order of Execution).

Flow Graph Determines Order of Execution.  The Ladder Logic Scheduler chart uses Stateflow flow charting capabilities to implement the logic that schedules the execution of the Simulink function-call subsystems. The chart contains a Stateflow flow graph that resembles a ladder diagram. Each rung in the ladder represents a rule or condition that determines whether to execute one of the Simulink function-call subsystems. The flow logic evaluates each condition sequentially, which has the effect of scheduling the execution of multiple subsystems within the same time step. The chart executes each subsystem by using the send action to broadcast a function-call output event (see Example of Directed Event Broadcasting Using send).

Here is the sequence of activities that occurs in the Ladder Logic Scheduler chart in each time step:

  1. Assign output y to input u1.

  2. If u1 is positive, send function-call output event A1 to the Simulink model.

    The subsystem connected to A1 executes. This subsystem multiplies its input by a gain of 2 and passes this value back to the Stateflow Ladder Logic Scheduler chart as input u2. Control returns to the next condition in the Ladder Logic Scheduler.

  3. If u2 is positive or zero, send function-call output event A2 to the Simulink model.

    The subsystem connected to A2 executes. This subsystem outputs its input value unchanged. Control returns to the next condition in the Ladder Logic Scheduler.

  4. If u1 and u2 are positive, send function-call output event A3 to the Simulink model.

    The subsystem connected to A3 executes. This subsystem multiplies its input by a gain of 1.

  5. The Ladder Logic Scheduler chart goes to sleep.

Running the Ladder Logic Scheduler

To run the sf_ladder_logic_scheduler model, follow these steps:

  1. Open the model by clicking sf_ladder_logic_scheduler or typing sf_ladder_logic_scheduler at the MATLAB command prompt.

  2. Open the Scope block.

  3. Start simulation.

    The scope shows how output y changes, depending on which subsystems the Ladder Logic Scheduler chart calls during each time step.

      Tip   If you keep the chart closed, the simulation runs much faster. For other tips, see Speeding Up Simulation.

Scheduling One Subsystem in a Single Time Step

The loop scheduler design pattern allows you to schedule one Simulink subsystem to execute multiple times in a single time step. The model sf_loop_scheduler illustrates this design pattern.

The Looping Scheduler chart contains the following logic:

Key Behaviors of Loop Scheduler

The key behaviors of the loop scheduler are:

Function-Call Output Event Triggers Subsystem Multiple Times.  In a given time step, the Stateflow chart broadcasts a function-call output event to trigger the execution of the function-call subsystem A1 multiple times in the Simulink model. Here is the sequence of activities during each time step:

  1. The Simulink model activates the Stateflow chart Edge to Function at a rising edge of the 1-millisecond pulse generator.

  2. The Edge to Function chart broadcasts the function-call output event call to activate the Stateflow chart Looping Scheduler.

  3. The Looping Scheduler chart broadcasts a function-call output event from a for loop to trigger the function-call subsystem A1 multiple times (see Flow Graph Implements For Loop).

Flow Graph Implements For Loop.  The Looping Scheduler chart uses Stateflow flow charting capabilities to implement a for loop for broadcasting an event multiple times in a single time step. The chart contains a Stateflow flow graph that uses a local data variable i to control the loop. At each iteration, the chart updates output y and issues the send action to broadcast a function-call output event that executes subsystem A1. Subsystem A1 uses the value of y to recompute its output and send the value back to the Looping Scheduler chart.

Running the Loop Scheduler

To run the sf_loop_scheduler model, follow these steps:

  1. Open the model by clicking sf_loop_scheduler or typing sf_loop_scheduler at the MATLAB command prompt.

  2. Open the Scope block.

  3. Start simulation.

    The scope displays the value of y at each time step.

In this example, the Looping Scheduler chart executes the for loop 10 times in each time step. During each iteration:

  1. The chart increments y by 1 (the constant value of input u1).

  2. The chart broadcasts a function-call output event that executes subsystem A1.

  3. Subsystem A1 multiplies y by a gain of 1.

  4. Control returns to the chart.

Scheduling Subsystems to Execute at Specific Times

The temporal logic scheduler design pattern allows you to schedule Simulink subsystems to execute at specified times. The model sf_temporal_logic_scheduler illustrates this design pattern.

The Temporal Logic Scheduler chart contains the following logic:

Key Behaviors of Temporal Logic Scheduler

The Temporal Logic Scheduler chart contains two states that schedule the execution of the function-call subsystems A1, A2, and A3 at different rates, as determined by the temporal logic operator every (see Operators for Event-Based Temporal Logic).

In the FastScheduler state, the every operator schedules function calls as follows:

The SlowScheduler state schedules function calls less frequently — at 8, 16, and 32 times slower than the base rate. The chart switches between fast and slow executions after every 100 invocations of the call event.

Running the Temporal Logic Scheduler

To run the sf_temporal_logic_scheduler model, follow these steps:

  1. Open the model by clicking sf_temporal_logic_scheduler or typing sf_temporal_logic_scheduler at the MATLAB command prompt.

  2. Open the Scope block.

  3. Start simulation.

  4. After the simulation ends, click the Autoscale button in the Scope block.

    The scope illustrates the different rates of execution.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS