You can control the execution of model components (Subsystem and Model blocks) by using export-function models or rate-based models. Benefits of scheduling components are:
Full control over scheduling of model components rather than letting Simulink® implicitly schedule the components.
No need to deal with data dependency issues between components. That is, there are only data transfers.
This topic describes how to create a rate-based model with explicitly scheduled subsystems, reference the model in a test model, and then add periodic event ports to the test model for simulation. For information about using export-function models, see Export-Function Models.
Consider a model with two subsystems. Subsystem1 multiplies its input by 2 while Subsystem2 multiplies its input by 4.
Using Continuous time blocks such as Integrator blocks are not allowed. Instead use discrete time equivalent blocks
Open the Inport 1 dialog box. On the Signal Attributes tab, set the
Sample time to
Open the Inport 2 dialog box. On the Signal Attributes tab, set the
Sample time to
If a rate-based model has multiple rates, single tasking is not allowed. Select the Configuration Parameter check box for Treat each discrete rate as a separate task.
Testing a rate-based model includes referencing the model from a Model block in a test model, adding periodic event ports to the Model block, and then connecting function-calls to the ports.
Create a new Simulink model.
Add a Model block and open the block parameters dialog box.
In the Model name box, enter the file name for the rate-based model.
Select the Show model periodic event ports check box.
Periodic event ports are added to the Model block with the Sample times you specified for the Inport blocks connected to the Subsystem blocks.
Specify the execution rate using function-call initiators (Function-Call Generator blocks or Stateflow® charts). The function-call events and scheduling of the events are located outside of the Model block referencing the rate-based model.
In this example, add Function-Call Generator blocks. Open the block dialog box for the blocks and specify Sample time.
Subsystems or referenced models in a rate-based model with the same sample time must have a common rate initiator. This requirement includes periodic scheduled subsystems and event driven Function-Call Subsystem blocks with the same rate.
Use a fixed-step solver for simulation. Set the Configuration Parameters
Fixed-step, Solver to
auto, and Fixed-step size to
Simulate the behavior of a rate-based model from the test model.
Run a simulation. Some common compile and run-time errors are cause by:
A periodic event port that is not connected to a function-call initiator with the same specified sample time.
A scheduled Inport block (Sample time parameter set to a value) in the referenced component model that does not specify one of the periodic event port rates (sample times specified in the Port discrete rates table).
Observe the behavior of the component model. Open the Scope block.
Generate code from the rate-based model, not from the model test harness. For scheduled subsystems with different discrete rates, multi-tasking is required and the resulting code has separate entry points.
Generate code for the component model. From the menu, select Code > C/C++ Code > Build Model.
Open the code generation report. From the menu, select Code > C/C++ Code > Code Generation Report > Open Model Report.