Prepare Variant-Containing Model for Code Generation
Using Embedded Coder®, you can generate code from Simulink® models containing one or more variant choices. The generated code contains preprocessor conditionals that control the activation of each variant choice.
Note
Simulink supports using multi-instance referenced models with variant Simulink Functions for code generation.
For information on using STF_make_rtw_hook
file to customize build
process, see Customize Build Process with STF_make_rtw_hook File (Simulink Coder).
Convert Variant Control Variables into Simulink.Parameter
Objects
MATLAB® variables 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 into Simulink.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);
Get the variables that are referenced in variant control expressions.
vars = Simulink.VariantManager.findVariantControlVars(model)
vars = 4x1 struct array with fields: Name Value Exists Source SourceType
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); else fprintf('+++ Creating header file ''%s'' with variant control variable definitions.\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
orImportedDefine
with header file specifiedCompilerFlag
SystemConstant (AUTOSAR)
Your own storage class that defines data as a macro
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 Storage Classes for Different Variant Activation Times.Loop through all the MATLAB variables to convert them into
Simulink.Parameter
objects.count = 0; for countVars = 1:length(vars) var = vars(countVars).Name; val = vars(countVars).Value; if isa(val, 'Simulink.Parameter') % Do nothing continue; end count = 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 = 'Custom'; newVal.CoderInfo.CustomStorageClass = 'Define (Custom)'; newVal.CoderInfo.CustomAttributes.HeaderFile = headerFileName; Simulink.data.assigninGlobal(model, var, newVal); if ~fidErr fprintf(fid, '#endif\n'); fclose(fid); end end
Note
The header file can be empty for the Define
storage class.
Configure Model for Generating Preprocessor Conditionals
If you represent variant choices inside a Variant Subsystem block or a
Variant Model block, code generated for each variant choice is enclosed
within C preprocessor conditionals #if
, #else
,
#elif
, and #endif
.
If you represent variant choices using a Variant Source block or a
Variant Sink block, code generated for each variant choice is enclosed
within C preprocessor conditionals #if
and
#endif
.
Therefore, the active variant is selected at compile time and the preprocessor conditionals determine which sections of the code to execute.
Note
You must have an Embedded Coder license to generate code.
In the Modeling tab of the Simulink toolstrip, click Model Settings.
Select the Code Generation pane, and set System target file to
ert.tlc
.In the Report pane, select Create code generation report.
Note
In the Code Placement pane, if
Compact
option is selected from File packaging format drop-down list,model_types.h
file is not generated and contents ofmodel_types.h
file are moved tomodel.h
file.Select the Code Generation pane, and clear Ignore custom storage classes and Apply.
In your model, right-click the block containing the variant choices (Variant Subsystem, Variant Source, Variant Sink, or Variant Model block) and select Block Parameters.
Ensure that
Expression
(default option) is selected for Variant control mode parameter.From the Variant activation time list, select
code compile
.Simulink analyzes all variant choices during an update diagram or simulation. This analysis provides early validation of the code generation readiness for all variant choices.
Build the model.
Related Examples
- Compile Code Conditionally for Variations of Component Represented Using Variant Block (Embedded Coder)
- Run Executable for Different Variant Parameter Values Without Recompiling Code
More About
- Generate Code for Variant Subsystem Blocks (Embedded Coder)
- Generate Code from Nested Variant Subsystem with Code Compile and Startup Activation
- Generate Code for Variant Source and Variant Sink Blocks (Embedded Coder)
- Generate Code for Model References Connected to Variant Blocks (Embedded Coder)
- Variant System Design