Main Content

Scheduling Subsystems in Stateflow

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

  1. Scheduling multiple subsystems 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.

open_system("sf_ladder_logic_scheduler")

A model with two charts named Edge to Function and Ladder Logic Scheduler. The Ladder Logic Scheduler chart has two inputs named u1 and u2 and an output named y. Additionally, the Ladder Logic Scheduler chart triggers three subsystems named A1, A2, and A3 with output events.

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.

A chart that only contains junctions. If an input named u1 is greater than 0, the chart triggers the A1 subsystem with an event. If an input named u2 is greater than 1, the chart also triggers the A2 subsystem with an event. Finally, if u2 is greater than 2, the chart also triggers the A3 subsystem with an event.

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.

Schedule a Subsystem Multiple Times in a Single Step

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

open_system("sf_loop_scheduler")

A model with two charts named Edge to Function 1 and Loop Scheduler. The Loop Scheduler chart uses output events to trigger a subsystem named A4.

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.

The Loop Scheduler chart, which only contains junctions. The junctions form a loop that, on each iteration, sends an event to the A1 subsystem and increments an output named y.

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.

A Scope block graph that shows the value of the output named y increasing in a series of steps and plateaus.

Schedule Subsystems to Execute at Specific Times

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

open_system("sf_temporal_logic_scheduler")

A model with two charts named Edge to Function 2 and Temporal Logic Scheduler. A 1-millisecond pulse block triggers the Edge to Function 2 block, which in turn uses a function-call input event to trigger the Temporal Logic Scheduler chart

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.

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.

A Scope block graph that shows how the output named y changes over time.

See Also

Topics