Generate Subsystem Code as Separate Function and Files
You can configure an atomic subsystem to generate code to a separate function and file. This results in code that is more modular and enables you to unit test code for individual subsystems.
TheFunction packagingSubsystemblock parameter has two settings that cause a subsystem to generate code as a separate function. TheReusable function
option generates a function that passes I/O, states, and parameters as function arguments. TheNonreusable function
option generates a function that passes I/O, states, and parameters as a combination of function arguments and global data structures.
Subsystem Function Dependence
When generating code for a subsystem, the code can reference global data structures of the model, even if the subsystem function code is in a separate file. Each subsystem code file containsinclude
directives and comments describing the dependencies. The code generator checks for cyclic file dependencies and produces warnings about them at build time. For descriptions of how the code generator packages code, seeManage Build Process File Dependencies.
To generate subsystem function code that is independent of the code generated for the parent model, place the subsystem in a library and configure it as a reusable subsystem, as described inGenerate Reusable Code from Library Subsystems Shared Across Models.
If you have Embedded Coder®, you can generate code for library consisting of reusable subsystems that have different function interfaces. For more information, seeLibrary-Based Code Generation for Reusable Library Subsystems(Embedded Coder).
Generate Subsystem as a Reusable Function
Open a model with a subsystem, such as
rtwdemo_atomic
.If you are using Embedded Coder, from theC Codetab, clickView Codeto open theCode Vieweditor.
Right-click theSubsystemblock. From the context menu, selectBlock Parameters (Subsystem).
verif子系统参数对话框y thatTreat as atomic unitis selected. With that parameter selected, on theCode Generationtab, theFunction packagingparameter is available.
Click theCode Generationtab and select
Reusable function
from theFunction packagingparameter. This enables two parameters:TheFunction name optionsparameter controls the naming of the generated function.
TheFile name optionsparameter controls the naming of the generated file.
Set theFile name optionsparameter to
Use subsystem name
.ClickApplyand close the dialog box.
If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameterFile packaging format(Embedded Coder)is set to
Modular
.Generate the code.
# include " SS1.h " / *包括模型头文件global data */ #include "rtwdemo_atomic.h" #include "rtwdemo_atomic_private.h" /* Outputs for atomic system: '
/SS1' */ real_T myfun(DW_myfun_T *localDW) { /* DiscreteIntegrator: ' /Integrator' */ return localDW->Integrator_DSTATE; } /* Update for atomic system: ' /SS1' */ void myfun_Update(real_T rtu_In1, DW_myfun_T *localDW) { /* Update for DiscreteIntegrator: ' /Integrator' */ localDW->Integrator_DSTATE += rtu_In1; } The reusable function
myfun
passes in inputs and states as arguments to the subsystem function.
For more information, seeGenerate Reentrant Code from SubsystemsandGenerate Reusable Code from Library Subsystems Shared Across Models.
Generate Subsystem as a Nonreusable Function
Open a model with a subsystem, such as
rtwdemo_atomic
.If you are using Embedded Coder, from theC Codetab, clickView Codeto open theCode Vieweditor.
Right-click theSubsystemblock. From the context menu, selectBlock Parameters (Subsystem).
verif子系统参数对话框y thatTreat as atomic unitis selected. With that parameter selected, on theCode Generationtab, theFunction packagingparameter is available.
Click theCode Generationtab and select
Nonreusable function
from theFunction packagingparameter. This enables two parameters:TheFunction name optionsparameter controls the naming of the generated function.
TheFile name optionsparameter controls the naming of the generated file.
Set theFile name optionsparameter to
Use subsystem name
.If you are using Embedded Coder, verify that theFunction interfaceparameter is set to
void_void
. For more information, seeGenerate Predictable Function Interface to Match Graphical Block Interface(Embedded Coder).ClickApplyand close the dialog box.
If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameterFile packaging format(Embedded Coder)is set to
Modular
.Generate the code.
# include " SS1.h " / *包括模型头文件global data */ #include "rtwdemo_atomic.h" #include "rtwdemo_atomic_private.h" /* Outputs for atomic system: '
/SS1' */ void myfun(void) { /* Outport: ' /Out1' incorporates: * DiscreteIntegrator: ' /Integrator' */ rtwdemo_atomic_Y.Out1 = rtwdemo_atomic_DW.Integrator_DSTATE; } /* Update for atomic system: ' /SS1' */ void myfun_Update(void) { /* Update for DiscreteIntegrator: ' /Integrator' */ rtwdemo_atomic_DW.Integrator_DSTATE += rtwdemo_atomic_B.Sum; } The nonreusable function
myfun
passes in inputs and states through the global data structuresrtwdemo_atomic_Y
andrtwdemo_atomic_DW
.