主要内容

为代码生成准备含有变体的模型

Using Embedded Coder®,您可以从Simulink生成代码金宝app®models containing one or more variant choices. The generated code contains preprocessor conditionals that control the activation of each variant choice.

Note

金宝appSimu金宝applink使用具有变体Simulink函数的多种现代引用的模型支持代码生成。

For information on usingSTF_make_rtw_hook文件以自定义构建过程,请参阅Customize Build Process with STF_make_rtw_hook File(Simulink Coder)

将变体控制变量转换为Simulink.ParameterObjects

MATLAB®多变的s allow you to rapidly prototype variant control expressions when you are building your model and generate preprocessor conditionals for code generation. However, if you want to specify other code generation attributes (such as data type), you can convert MATLAB variables intoSimulink.Parameterobjects.

  1. Specify the model in which you want to replace MATLAB variant control variables withSimulink.Parameterobjects.

    model ='my_model_containing_variant_choices'; open_system(model);
  2. 获取在变体控制表达式中引用的变量。

    vars = Simulink.VariantManager.findVariantControlVars(model)
    vars = 4x1结构数组带有字段:名称值存在源源型
  3. Create an external header file for specifying variant control values so that the variable definitions are imported when the code runs.

    headerFileName = [model'_importedDefines.h']; headerPreamble = strrep(upper(headerFileName),'.','_'); fid = fopen(headerFileName,'w+'); fidErr = (fid == -1);if(fidErr) fprintf('There was an error creating header file %s:...\n', headerFileName);elsefprintf('+++ Creating header file ''%s'' with variant control...多变的定义。\ n \ n',headerFileName);fprintf(fid,'#ifndef %s\n',headerpreamble);fprintf(fid,'#define %s\n',headerpreamble);end

    Variant control variables defined asSimulink.Parameterobjects can have one of these storage classes.

    • Define或者进口定义指定标头文件

    • 编译

    • SystemConstant (AUTOSAR)

    • 您自己的存储类将数据定义为宏

    Note

    If you generate code withstartupactivation time, specify the supported custom storage class for the objects. For more information on built-in and custom storage classes supported withstartupactivation time see不同变体激活时间的存储类.

  4. Loop through all the MATLAB variables to convert them intoSimulink.Parameterobjects.

    计数= 0;forcountVars = 1:length(vars) var = vars(countVars).Name; val = vars(countVars).Value;ifisa(val,'Simulink.Parameter')% 没做什么continue;endcount = count+1;% Create and configure Simulink.Parameter objects% corresponding to the control variable names.% Specify the storage class as Define (Custom).newVal = Simulink.Parameter(val); newVal.DataType ='int16'; newVal.CoderInfo.StorageClass ='风俗'; newVal.CoderInfo.CustomStorageClass ='Define (Custom)'; newVal.CoderInfo.CustomAttributes.HeaderFile = headerFileName; Simulink.data.assigninGlobal(model, var, newVal);if~fidErr fprintf(fid,'#endif\n'); fclose(fid);endend

Note

标题文件可以为空Definestorage class.

Configure Model for Generating Preprocessor Conditionals

如果您表示一个内部的变体选择变体子系统block or a变体模型block, code generated for each variant choice is enclosed within C preprocessor conditionals#if,#别的,#elif, 和#万一.

If you represent variant choices using aVariant Sourceblock or a变体水槽block, code generated for each variant choice is enclosed within C preprocessor conditionals#if#万一.

因此,在编译时间选择活动变体,并且预处理程序条件确定要执行的代码的哪些部分。

Note

您必须具有嵌入式编码器®许可才能生成代码。

  1. 在里面ModelingSimulink工具条金宝app的选项卡,单击Model Settings.

  2. 选择Code Generation窗格,并设置System target filetoert.tlc.

  3. 在里面Reportpane, selectCreate code generation report.

    Note

    在里面Code Placement窗格,如果Compactoption is selected fromFile packaging formatdrop-down list,model_types.hfile is not generated and contents ofmodel_types.hfile are moved to模型文件。

  4. 选择Code Generationpane, and clearIgnore custom storage classesApply.

  5. 在您的模型中,右键单击包含变体选择的块(变体子系统,Variant Source,变体水槽, or变体模型block) and select块参数.

  6. Ensure that表达(default option) is selected for变体控制模式parameter .

  7. From theVariant activation timelist, selectcode compile.

    金宝appSimulink在更新图或仿真过程中分析所有变体选择。该分析提供了所有变体选择的代码生成准备就绪的早期验证。

  8. 构建模型。

Related Examples

More About