文档

为引用模型生成代码

关于Generating Code for Referenced Models

To generate code for referenced models, you

  1. Create a subsystem in an existing model.

  2. Convert the subsystem to a referenced model (Model堵塞)。

  3. Call the referenced model from the top model.

  4. Generate code for the top model and referenced model.

  5. Explore the generated code and the code generation folder.

You can accomplish some of these tasks automatically with a function called金宝appsimulink.subsystem.convertTomodelReference

Create and Configure the Subsystem

在the first part of this example, you define a subsystem for thevdpexample model, set configuration parameters for the model, and use the金宝appsimulink.subsystem.convertTomodelReference将其转换为两个新模型的功能 - 顶部模型(vdptop) and a referenced modelvdpmultRM包含您创建的子系统(vdpmult)。

  1. 在the MATLAB®Command Window, create a new working folder wherever you want to work and光盘进去:

    mkdir mrexample cd mrexample
  2. 打开vdpexample model by typing:

    vdp
  3. Drag a box around the three blocks outlined in blue below:

  4. ChooseCreate Subsystemfrom thetab on the toolstrip.

    A subsystem block replaces the selected blocks.

  5. If the new subsystem block is not where you want it, move it to a preferred location.

  6. 重命名块vdpmult

  7. 右键单击vdpmult堵塞and selectBlock Parameters (Subsystem)

    Block Parametersdialog box appears.

  8. 在theBlock Parametersdialog box, select作为原子单位,然后单击OK

    这border of thevdpmult子系统变厚以表明它现在是原子。一个原子子系统是相对于父模型的单位执行的:子系统块执行不会与父块的执行相交。此属性使得可以提取子系统作为独立模型和生成代码中的功能。

    这堵塞diagram appears.

You must set several properties before you can extract a subsystem for use as a referenced model. To set the properties:

  1. Open Model Explorer. On theModeling选项卡,单击模型资源管理器

  2. 在the模型层次结构窗格中,展开模型,揭示其组件。

  3. 选择配置节点。

  4. 在the内容pane, right-clickConfiguration (Active)并点击Open在上下文菜单中,打开“配置参数”对话框。

  5. 在the left pane of the Configuration Parameters dialog box, selectSolver

  6. Change theTypeto固定步骤,然后单击申请。生成代码时必须使用固定步骤求解器,尽管引用的模型可以使用与顶级模型不同的求解器。

  7. 在左窗格中,单击前面的符号Diagnostics。在the left pane belowDiagnostics, selectData Validity。在the信号区域,设置信号分辨率toExplicit only。Alternatively, if you do not want to useSimulink.Signalobjects, set信号分辨率toNone

  8. 点击申请

    这model now has the properties that model referencing requires.

  9. 在左窗格中,单击Model Referencing。在theOptions for all referenced modelssection, set重建toIf any changes in known dependencies detected。点击OK。当不需要时,此设置可防止代码再生。

  10. 在thevdpmodel window, chooseFile>Save as。Save the model asvdptopin your working folder. Leave the model open.

将模型转换为使用模型参考

在示例的这一部分中,您使用转换功能Simulink.SubSystem.convertToModelReferenceto extract the subsystemvdpmultfromvdptopand convertvdpmultinto a referenced model namedvdpmultRM。要查看转换函数的完整语法,请在MATLAB提示下输入:

帮助sim金宝appulink.subsystem.convertTomodelReference

For additional information, type:

doc Simulink.SubSystem.convertToModelReference

If you want to see an example ofSimulink.SubSystem.convertToModelReference在自己使用之前,请输入:

sldemo_mdlref_conversion

金宝app®还提供菜单命令,子系统和模型参考>Convert Subsystem to>引用模型, that you can use to convert a subsystem to a referenced model. The command callsSimulink.SubSystem.convertToModelReference带有默认参数。有关更多信息,请参阅Convert Subsystems to Referenced Models(金宝appSimulink)。

将子系统提取到引用模型

To useSimulink.SubSystem.convertToModelReferenceto extractvdpmult并将其转换为引用的模型,类型:

Simulink.SubSystem.convertToModelReference... ('vdptop/vdpmult', 'vdpmultRM',... 'ReplaceSubsystem', true, 'BuildTarget', 'Sim')

This command:

  1. 提取子系统vdpmultfromvdptop

  2. 将提取的子系统转换为一个名称的单独模型vdpmultRMand saves the model to the working folder.

  3. vdptop,用引用的模型块替换提取的子系统vdpmultRM

  4. 创建一个模拟目标vdptopandvdpmultRM

这converter prints progress messages and terminates with

ans = 1

这parent modelvdptop现在看起来像这样:

Note the changes in the appearance of the blockvdpmult。这se changes indicate that it is now a Model block rather than a subsystem. As a Model block, it does not have contents of its own: the previous contents now exist in the referenced modelvdpmultRM, whose name appears at the top of the Model block. Widen the Model block to expose the complete name of the referenced model.

如果父型模型vdptophad been closed at the time of conversion, the converter would have opened it. Extracting a subsystem to a referenced model doesnotautomatically create or change a saved copy of the parent model. To preserve the changes to the parent model, savevdptop

右键单击Model堵塞vdpmultRM并选择Opento open the referenced model. The model looks like this:

由转换器创建和更改的文件

这files in your working folder now consist of the following (not in this order).

File Description

vdptopmodel file

Top model that contains aModel堵塞where thevdpmult子系统是

vdpmultRMmodel file

Referenced model created for thevdpmultsubsystem

vdpmultRM_msf.mexw64

Static library file (Microsoft®Windows®platforms only). The file extension is system-dependent and may differ. This file executes when thevdptopmodel calls the Model blockvdpmult。When called,vdpmultin turn calls the referenced modelvdpmultRM

/slprj

生成的模型参考代码的文件夹

模型参考模拟目标的代码放在slprj/simsubfolder. Generated code for GRT, ERT, and otherSimulink Coder™目标放在slprj子文件夹以这些目标命名。您将在此示例稍后检查一些模型参考代码。有关代码生成文件夹的更多信息,请参阅使用代码生成文件夹

Run the Converted Model

打开Scope堵塞invdptopif it is not visible. In thevdptop窗口,单击Runtool or clickRunfrom theSimulation标签。该模型调用vdpmultRM_msf模拟目标要模拟。输出看起来像这样:

生成GRT目标的模型参考代码

这functionSimulink.SubSystem.convertToModelReferencecreated the model and the simulation target files for the referenced modelvdpmultRM。在this part of the example, you generate code for that model and thevdptop模型,并运行您创建的可执行文件:

  1. 验证您仍在工作mrexamplefolder.

  2. If the modelvdptop不打开,打开它。确保它是活动窗口。

  3. Open Model Explorer. On theModeling选项卡,单击模型资源管理器

  4. 在the模型层次结构pane, click the symbol preceding thevdptop揭示其组件的模型。

  5. Select the Configurations node below the model node.

  6. 在the内容pane, right-clickConfiguration (Active)并点击Open在上下文菜单中,打开“配置参数”对话框。

  7. 在左侧窗格中,选择Data Import/Export

  8. 在theSave to workspace or filesection, selectTimeand输出andclearData stores。点击申请

    这se settings instruct the modelvdptop(and later its executable) to log time and output data to MAT-files for each time step.

  9. 生成GRT代码(默认代码)和顶部模型和引用模型的可执行文件。例如,在模型中,按Ctrl+B

Simulink Coderbuild process generates and compiles code. The current folder now contains a new file and a new folder.

File Description

vdptop.exe

这executable created by the build process

vdptop_grt_rtw/

这build folder, containing generated code for the top model

这build process also generated GRT code for the referenced model and placed it in theslprjfolder.

To view a model’s generated code in the模型资源管理器,该模型必须打开。使用模型资源管理器to inspect the newly created build folder,vdptop_grt_rtw

  1. Open Model Explorer. On theModeling选项卡,单击模型资源管理器

  2. 在the模型层次结构pane, expand the model name to reveal its components.

  3. ExpandVDPTOP的代码reveal its components.

  4. 点击This Model

    生成的代码文件列表vdptopappears in the内容pane:

    rtmodel.h vdptop.c vdptop.h vdptop.mk vdptop_private.h vdptop_types.h

    You can browse code by selecting a file of interest in the内容窗格。

    要在文本编辑器中打开文件,请单击文件名,然后单击出现在灰色区域顶部的灰色区域的超链接Document窗格。该图显示了查看代码vdptop.c在文本编辑器中。您的代码可能有所不同。

    要查看代码生成报告中生成的代码,请参见Generate a Code Generation Report

使用代码生成文件夹

Model reference code is generated in your代码生成文件夹(Simulink) and simulation target code is generated in yoursimulation cache folder(金宝appSimulink)。由于这个过程,有限制:

  • When and where model reference targets are built.

  • 如何访问模型参考目标。

Model堵塞s can be stored anywhere on the MATLAB path. A given top model can include models stored on different file systems or in different folders. The same is not true for the simulation targets and generated code derived from these models. Under most circumstances, to allow code reuse, models referenced by a given top model must be set up to simulate and generate model reference target code in a single code generation folder.

This means that, if you reference the same model from several top models, each stored in a different folder, you must choose one of these approaches:

  • 始终使用相同的代码生成文件夹,并确保模型在您的路径上。

  • 允许单独的代码生成文件夹,仿真目标和Simulink Codertargets to be generated in each folder in which you work.

第二种方法需要维护模型参考代码的几个实例,并且生成的代码可能会变得多余。例如,当您更改引用模型时。因此,为了最大程度地减少参考模型的代码再生,请为所有会话选择一个特定的代码生成文件夹。

Related Topics