Main Content

Scheduling Subsystems in Stateflow

This example shows three different methods for scheduling subsysem execution in Stateflow®. This model covers three different scenarios:

  1. Scheduling multiple subsytems in a single step

  2. Scheduling a subsystem multiple times in a single step

  3. Scheduling multiple subsystems to execute at specific times

For each scenario, you can use a different type of Stateflow logic to manage the execution of your subsystems.To schedule multiple subsystems in a single step, use ladder logic. The ladder logic scheduler design pattern allows you to specify the order in which multiple Simulink® subsystems execute in a single time step. To schedule a subsystem multiple times in a single step, use loop logic. The loop scheduler design pattern allows you to execute a Simulink subsystem multiple times in a single time step. To schedule multiple subsystems to execute at specific times, use temporal logic. The temporal logic scheduler design pattern allows you to schedule Simulink subsystems to execute at specified times.

In all three models, Stateflow schedulers extend control of subsystem execution in a Simulink model, which determines order of execution implicitly based on block connectivity and sample time propagation.

Scheduling Multiple Subsystems in a Single Step

This portion of the example shows how to design a ladder logic scheduler in Stateflow.

ladder_logic_scheduler.png

The Ladder Logic Scheduler chart broadcasts a series of function-call output events to execute three function-call subsystems (A1, A2, and A3). During each time step:

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

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

  3. The Ladder Logic Scheduler chart uses sequencing ladder logic to broadcast function-call output events based on the values of the input signals u1 and u2.

ladder_logic.png

The chart evaluates each condition sequentially. When a condition is valid, the chart calls the send operator to broadcast an output event. The corresponding subsystem computes its output and returns control back to the Ladder Logic Scheduler chart.

When you simulate the model, the scope shows the input and output of each function-call subsystem. During each time step, the Ladder Logic Scheduler chart executes the subsystems depending on the values of the input signals u1 and u2:

  1. If u1 is positive, the chart sends a function-call output event to execute subsystem A1. This subsystem multiplies the value of u1 by a gain of 3 and passes this value back to the Ladder Logic Scheduler chart as input u2. Control returns to the next condition in the Ladder Logic Scheduler chart.

  2. If u2 is greater than 1, the chart sends a function-call output event to execute subsystem A2. This subsystem decreases the value of u2 by 1. Control returns to the final condition in the Ladder Logic Scheduler chart.

  3. If u2 is less than 2, the chart sends a function-call output event to execute subsystem A3. This subsystem multiplies its input by a gain of 2.

In the scope, horizontal segments indicate time steps when a subsystem does not execute.

ladder_logic_scheduler_scope.png

Schedule a Subsystem Multiple Times in a Single Step

This portion of the example shows how to design a loop scheduler in Stateflow.

loop_scheduler.png

The Loop Scheduler chart broadcasts a function-call output event to execute the function-call subsystem A1 multiple times every time step. During each time step:

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

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

  3. The Loop Scheduler chart calls the send operator to broadcast the function-call output event A1 multiple times.

loop_logic.png

Each broadcast of the event A1 executes the subsystem A1. The subsystem computes its output and returns control back to the Loop Scheduler chart.

When you simulate the model, the scope displays the value of y at each time step. During each time step, the value of y increases by 25 because:

  • The flow chart in the Loop Scheduler chart implements a for loop that iterates 10 times.

  • In each iteration of the for loop, the chart increments y by 1 (the constant value of input u1).

  • Each time that the chart broadcasts the output event to subsystem A1, the subsystem increments y by 1.5.

loop_scheduler_scope.png

Schedule a Subsystems to Execute at Specific Times

This portion of the example shows how to design a temporal logic scheduler in Stateflow.

temporal_logic_scheduler.png

In this example, the Temporal Logic Scheduler chart contains two states that schedule the execution of three function-call subsystems (A1, A2, and A3) at different rates, as determined by the temporal logic operator every.

temporal_logic.png

When the FastScheduler state is active, the chart schedules function calls to different Simulink subsystems at a fraction of the base rate at which the input event call wakes up the chart.

  • The chart sends an event to execute subsystem A1 at the base rate.

  • The chart sends an event to execute subsystem A2 at half the base rate.

  • The chart sends an event to execute subsystem A3 at one quarter the base rate.

When the SlowScheduler state is active, the chart schedules function calls for A1, A2, and A3 at 1/8, 1/16, and 1/32 times the base rate.

The chart switches between the fast and slow execution modes after every 100 invocations of the call event.

When you simulate the model, the scope displays the value of y at each time step. The changes in value illustrate the different rates of execution.

  • When the chart executes the subsystems at a slow rate (for example, from t=4.5 to t=4.6, from t=4.7 to t=4.8, and from t=4.9 to t=5), the values change slowly.

  • When the chart executes the subsystems at a fast rate (for example, from t=4.6 to t=4.7 and from t=4.8 to t=4.9 ), the values change rapidly.

temporal_logic_scheduler_scope.png

See Also

Related Topics