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:

The key behaviors of the ladder logic scheduler are:

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:

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

The Edge to Function chart broadcasts the function-call output event

`call`

to activate the Stateflow chart Ladder Logic Scheduler.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 Chart 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
chart 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 Directed Local Event Broadcast Using send).

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

Assign output

`y`

to input`u1`

.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.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.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.The Ladder Logic Scheduler chart goes to sleep.

To run the `sf_ladder_logic_scheduler`

model,
follow these steps:

Open the model by clicking sf_ladder_logic_scheduler or typing

`sf_ladder_logic_scheduler`

at the MATLAB^{®}command prompt.Open the Scope block.

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 Speed Up Simulation.

Was this topic helpful?