Main Content

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 functionoption generates a function that passes I/O, states, and parameters as function arguments. TheNonreusable functionoption 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 containsincludedirectives 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

  1. Open a model with a subsystem, such asrtwdemo_atomic.

    If you are using Embedded Coder, from theC Codetab, clickView Codeto open theCode Vieweditor.

  2. Right-click theSubsystemblock. From the context menu, selectBlock Parameters (Subsystem).

  3. verif子系统参数对话框y thatTreat as atomic unitis selected. With that parameter selected, on theCode Generationtab, theFunction packagingparameter is available.

  4. Click theCode Generationtab and selectReusable functionfrom theFunction packagingparameter. This enables two parameters:

  5. Set theFile name optionsparameter toUse subsystem name.

  6. ClickApplyand close the dialog box.

  7. If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameterFile packaging format(Embedded Coder)is set toModular.

  8. 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 functionmyfunpasses 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

  1. Open a model with a subsystem, such asrtwdemo_atomic.

    If you are using Embedded Coder, from theC Codetab, clickView Codeto open theCode Vieweditor.

  2. Right-click theSubsystemblock. From the context menu, selectBlock Parameters (Subsystem).

  3. verif子系统参数对话框y thatTreat as atomic unitis selected. With that parameter selected, on theCode Generationtab, theFunction packagingparameter is available.

  4. Click theCode Generationtab and selectNonreusable functionfrom theFunction packagingparameter. This enables two parameters:

  5. Set theFile name optionsparameter toUse subsystem name.

    If you are using Embedded Coder, verify that theFunction interfaceparameter is set tovoid_void. For more information, seeGenerate Predictable Function Interface to Match Graphical Block Interface(Embedded Coder).

  6. ClickApplyand close the dialog box.

  7. If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameterFile packaging format(Embedded Coder)is set toModular.

  8. 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 functionmyfunpasses in inputs and states through the global data structuresrtwdemo_atomic_Yandrtwdemo_atomic_DW.

Related Topics