Main Content

生成子系统代码作为单独的函数和文件

您可以配置原子子系统以生成代码到单独的函数和文件。这将导致更模块化的代码,并使您能够对单个子系统进行测试代码。

功能包装子系统block parameter has two settings that cause a subsystem to generate code as a separate function. The可重用的function选项生成一个将I/O,状态和参数作为函数参数传递的函数。这不可修复的功能选项生成一个通过I/O,状态和参数作为函数参数和全局数据结构的组合的函数。

子系统功能依赖性

在为子系统生成代码时,即使子系统功能代码在单独的文件中,代码也可以引用模型的全局数据结构。每个子系统代码文件包含包括指令和评论描述依赖关系。代码生成器检查是否有循环文件依赖关系,并在构建时间发出有关它们的警告。有关代码生成器包代码的描述,请参见管理构建过程文件依赖性

要生成独立于为父模型生成的代码的子系统函数代码,请将子系统放在库中,并将其配置为可重复使用的子系统,如从模型共享的库子系统中生成可重复使用的代码

如果您有嵌入式编码器®,您可以生成由具有不同功能接口的可重复使用子系统组成的库代码。有关更多信息,请参阅可重复使用的库子系统的基于库的代码生成(嵌入式编码器)

生成子系统作为可重复使用的功能

  1. 打开一个具有子系统的模型,例如rtwdemo_atomic

    如果您使用的是嵌入式编码器,请从C Code选项卡,单击查看代码打开代码视图编辑。

  2. 右键单击子系统堵塞。从上下文菜单中选择块参数(子系统)

  3. 在“子系统参数”对话框中,验证作为原子单位选择。选择该参数,在代码生成标签,功能包装参数可用。

  4. 点击代码生成选项卡和选择可重用的function来自功能包装范围。这启用了两个参数:

  5. 设置File name options参数为使用子系统名称

  6. 点击申请and close the dialog box.

  7. If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameter文件包装格式(嵌入式编码器)is set to模块化的

  8. 生成代码。

    #include "SS1.h" /* Include model header file for 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; }

    可重复使用的功能myfun将输入和状态作为参数传递给子系统函数。

有关更多信息,请参阅从子系统生成返回代码and从模型共享的库子系统中生成可重复使用的代码

Generate Subsystem as a Nonreusable Function

  1. 打开一个具有子系统的模型,例如rtwdemo_atomic

    如果您使用的是嵌入式编码器,请从C Code选项卡,单击查看代码打开代码视图编辑。

  2. 右键单击子系统堵塞。从上下文菜单中选择块参数(子系统)

  3. 在“子系统参数”对话框中,验证作为原子单位选择。选择该参数,在代码生成标签,功能包装参数可用。

  4. 点击代码生成选项卡和选择不可修复的功能来自功能包装范围。这启用了两个参数:

  5. 设置File name options参数为使用子系统名称

    如果您使用的是嵌入式编码器,请验证功能接口parameter is set tovoid_void。有关更多信息,请参阅生成可预测的函数接口以匹配图形块接口(嵌入式编码器)

  6. 点击申请and close the dialog box.

  7. If you are using Embedded Coder, open the Configuration Parameters dialog box. Verify that the model configuration parameter文件包装格式(嵌入式编码器)is set to模块化的

  8. 生成代码。

    #include "SS1.h" /* Include model header file for 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; }

    不可重复的函数myfun通过全球数据结构传递输入和状态rtwdemo_atomic_yandrtwdemo_atomic_dw

Related Topics