Main Content

Schedule Execution of Multiple Controllers

In this tutorial, you use Simulink® functions in a Stateflow® chart to improve the design of a model that contains three function-call subsystems. In the model sf_temporal_logic_scheduler:

  • The chart broadcasts the output events A1, A2, and A3 to trigger the function-call subsystems.

  • The subsystems A1, A2, and A3 execute at different rates defined by the chart.

  • The subsystem outputs feed directly into the chart.

    No other blocks in the model access the subsystem outputs.

You can replace function-call subsystems with Simulink functions inside a chart when:

  • The subsystems perform calculations required by the chart.

  • Other blocks in the model do not need access to the subsystem outputs.

Simulink functions are not supported in standalone Stateflow charts in MATLAB®. For more information, see Reuse Simulink Functions in Stateflow Charts.

Note

To skip the conversion steps, you can open the modified model by entering:

openExample("sf_temporal_logic_scheduler_with_sl_fcns")

Open the Model

Open the sf_temporal_logic_scheduler model.

openExample("sf_temporal_logic_scheduler")

Simulink model with three function-call subsystems.

If you simulate the model, you see this result in the scope.

Scope showing three slow executions and two fast executions.

Add Simulink Functions to the Chart

Follow these steps to add Simulink functions to the Temporal Logic Scheduler chart.

  1. In the Simulink model, right-click the A1 block in the lower right corner and select Cut from the context menu.

    Simulink model after subsystem A1 is removed.

  2. Open the Temporal Logic Scheduler chart.

  3. In the chart, right-click outside any states and select Paste from the context menu.

  4. Expand the new Simulink function so that the signature fits inside the function box.

    Stateflow chart after Simulink function A1 is added.

    Tip

    To change the font size of a function, right-click the function box and select a new size from the Font Size menu.

  5. Rename the Simulink function from A1 to f1 by entering y = f1(u) in the function box.

  6. Repeat steps 1 through 5 for these cases:

    • Copying the contents of A2 into a Simulink function named f2.

    • Copying the contents of A3 into a Simulink function named f3.

    Stateflow chart with three Simulink functions.

    Note

    The new functions reside at the chart level because both states FastScheduler and SlowScheduler require access to the function outputs.

Change the Scope of Chart Data

In the Model Explorer, change the scope of chart-level data y to Local because the calculation for that data now occurs inside the chart.

Update State Actions in the Chart

In the Stateflow Editor, you can replace event broadcasts in state actions with function calls.

  1. Edit the state actions in FastScheduler and SlowScheduler to call the Simulink functions f1, f2, and f3.

    Stateflow chart with calls to Simulink functions.

  2. In both states, update each during action as follows.

    du: y = u1-y2;

Add Data to the Chart

For the on every state actions of FastScheduler and SlowScheduler, define three data. (For details, see Add Stateflow Data.)

  1. Add local data y1 and y2 to the chart.

  2. Add output data y3 to the chart.

  3. In the model, connect the output for y3 to the scope.

    Tip

    To flip the Scope block, select the block. Then, in the toolstrip, on the Format tab, click Flip left-right Flip left-right button.

Remove Unused Items in the Model

  1. In the Model Explorer, delete output events A1, A2, and A3 and input data u2 because the function-call subsystems no longer exist.

  2. Delete any dashed signal lines from your model.

Run the New Model

Your new model looks something like this:

Modified Simulink model without function-call subsystems.

If you simulate the new model, the results match those of the original design.

Related Topics