The first step for creating independent functions in the generated code from a Simulink® model is to define the functions in the context of an export-function model. See Export-Function Models Overview.
To open a completed export-function model, see ex_export_function_model
.
At the top-level of an export-function model, functions are modeled within Function-Call Subsystem, function-call Model, Simulink Function, and S-Function blocks. This example uses Function-Call Subsystem blocks to model two functions.
Add two Function-Call Subsystem blocks.
In the first subsystem, model a unit delay that increments by 0.05 at each time step.
In the second subsystem, model the square of an input signal.
Add Inport and Outport blocks.
Inport blocks configured to output function-call signals control the execution of Function-Call Subsystem blocks during a simulation. The function-call Inport blocks also create an entry point function in the generated code.
Attach Inport blocks to the function()
input ports
on the Function-Call Subsystem blocks.
Specify sample times. Open the Inport block dialogs and select the
Signal Attributes tab. Select the Output function
call check box. Set Sample time for the delay function
to 0.1
(100 ms), and the square function to 0.01
(10 ms).
Setting the sample time is only for simulation testing. Sample time values do not affect the generated code. However, comments are added in the code identifying the rate you should call the functions.
You could set the sample times to -1
and allow any function-call
rate (periodic or aperiodic) during simulation. By setting sample times, Simulink checks the calling rate with the specified rate and displays an error if
there is a mismatch.
Rename blocks. The names help to identify signals in the generated code.
Update the model (Ctrl-D). Dotted-and-dashed lines identify function-call signals.
Open the Configuration Parameters dialog box. On the Modeling
tab and from the Setup section, select Model
Settings
.
In the Solver pane, set Solver selection Type to
Fixed-step
, Solver to
auto
, and Fixed-step size to
auto
.
In the Code generation pane, set System target file to
ert.tlc
. This step requires an Embedded Coder®
license.
After you create an export-function model, you can test it with simulations. Choose one of the following simulation testing methods: Test Export-Function Model Simulation Using Input Matrix, Test Export-Function Model Simulation Using Function-Call Generators, and Test Export-Function Model Simulation Using Stateflow Chart.