为代码生成准备含有变体的模型
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.Parameter
Objects
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.Parameter
objects.
Specify the model in which you want to replace MATLAB variant control variables with
Simulink.Parameter
objects.model ='my_model_containing_variant_choices'; open_system(model);
获取在变体控制表达式中引用的变量。
vars = Simulink.VariantManager.findVariantControlVars(model)
vars = 4x1结构数组带有字段:名称值存在源源型
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 as
Simulink.Parameter
objects can have one of these storage classes.Define
或者进口定义
指定标头文件编译
SystemConstant (AUTOSAR)
您自己的存储类将数据定义为宏
Note
If you generate code with
startup
activation time, specify the supported custom storage class for the objects. For more information on built-in and custom storage classes supported withstartup
activation time see不同变体激活时间的存储类.Loop through all the MATLAB variables to convert them into
Simulink.Parameter
objects.计数= 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
标题文件可以为空Define
storage 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
您必须具有嵌入式编码器®许可才能生成代码。
在里面ModelingSimulink工具条金宝app的选项卡,单击Model Settings.
选择Code Generation窗格,并设置System target fileto
ert.tlc
.在里面Reportpane, selectCreate code generation report.
Note
在里面Code Placement窗格,如果
Compact
option is selected fromFile packaging formatdrop-down list,model_types.h
file is not generated and contents ofmodel_types.h
file are moved to模型
文件。选择Code Generationpane, and clearIgnore custom storage classes和Apply.
在您的模型中,右键单击包含变体选择的块(变体子系统,Variant Source,变体水槽, or变体模型block) and select块参数.
Ensure that
表达
(default option) is selected for变体控制模式parameter .From theVariant activation timelist, select
code compile
.金宝appSimulink在更新图或仿真过程中分析所有变体选择。该分析提供了所有变体选择的代码生成准备就绪的早期验证。
构建模型。
Related Examples
- Define, Configure, and Activate Variant Choices in a Variant Subsystem Block
- 创建和验证变体配置
- Create Variant Controls Programmatically
- 使用变体选择
- Generate Code from Variant Blocks with Startup Activation Time
More About
- Code Generation for Variant Blocks(Embedded Coder)
- 在创代表子系统和变体模型erated Code(Embedded Coder)
- Represent Variant Source and Sink Blocks in Generated Code(Embedded Coder)
- Model AUTOSAR Variants(Autosar区块)
- 变体系统设计