Scheduling Subsystems in Stateflow
This example shows three different methods for scheduling subsystem execution in Stateflow®. This model covers three different scenarios:
Scheduling multiple subsystems in a single step
Scheduling a subsystem multiple times in a single step
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.
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:
The Simulink model activates the
Edge to Function
chart at the rising edge of the 1-millisecond pulse generator.The Edge to Function chart broadcasts the function-call output event
call
to activateLadder Logic Scheduler
chart.The
Ladder Logic Scheduler
chart uses sequencing ladder logic to broadcast function-call output events based on the values of the input signalsu1
andu2
.
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
:
If
u1
is positive, the chart sends a function-call output event to execute subsystemA1
. This subsystem multiplies the value ofu1
by a gain of3
and passes this value back to the Ladder Logic Scheduler chart as inputu2
. Control returns to the next condition in theLadder Logic Scheduler
chart.If
u2
is greater than1
, the chart sends a function-call output event to execute subsystemA2
. This subsystem decreases the value ofu2
by1
. Control returns to the final condition in theLadder Logic Scheduler
chart.If
u2
is less than2
, the chart sends a function-call output event to execute subsystemA3
. This subsystem multiplies its input by a gain of2
.
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.
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:
The Simulink model activates the
Edge to Function
chart at the rising edge of the 1-millisecond pulse generator.The
Edge to Function
chart broadcasts the function-call output eventcall
to activate theLoop Scheduler
chart.The
Loop Scheduler
chart calls thesend
operator to broadcast the function-call output eventA1
multiple times.
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 afor
loop that iterates 10 times.In each iteration of the
for
loop, the chart incrementsy
by 1 (the constant value of inputu1
).Each time that the chart broadcasts the output event to subsystem
A1
, the subsystem incrementsy
by 1.5.
Schedule a Subsystems to Execute at Specific Times
This portion of the example shows how to design a temporal logic scheduler in Stateflow.
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
tot=4.6
, fromt=4.7
tot=4.8
, and fromt=4.9
tot=5
), the values change slowly.When the chart executes the subsystems at a fast rate (for example, from
t=4.6
tot=4.7
and fromt=4.8
tot=4.9
), the values change rapidly.