Main Content

Generate Code for Model References Connected to Variant Blocks

This example shows how to use Model blocks with variant blocks to reuse and incrementally load, build, and generate code for multiple variations of a component.

When you generate code for a referenced model hierarchy, the code generator places the code for the top model in the code generation folder and the code for referenced models in an slprj folder. Within the slprj folder, the subfolders further categorize the generated code by each referenced model and by file type, such as source code, header files, and other necessary files for compilation.

If you have an Embedded Coder® license, custom targets must declare themselves as compliant with the referenced model if they support Model blocks. For more information, see Support Model Referencing.

To get started with an example on how to generate code for a referenced model, see Generate Code for Model Reference Hierarchy.

Explore the Model

Open the slexVariantMdlRefCondProp model from the Propagate Variant Conditions to Enable or Disable Model or Subsystem References in Hierarchy example.

This model contains two Model blocks sub1 and sub2. The sub1 block references the model slexVariantMdlRefCondProp_sub1 and serves as an input to the Variant Source1 block. The sub2 block references the model slexVariantMdlRefCondProp_sub2. The output of sub2 is determined by the variant conditions for the Variant Sink block in the slexVariantMdlRefCondProp_sub2 model.

The variant conditions at the input and the output ports of the Variant Source and Variant Sink blocks determine the activation and deactivation of connected blocks. In this model, the Variant Source1 block has a variant condition V == 1 that propagates to the Model block sub1 during simulation. Here, V is a scalar MATLAB™ variant control variable. You can specify V as a different type of variant control variable based on your requirements as described in Types of Variant Control Variables (Operands) in Variant Blocks.

If sub1 is active, the blocks within sub1 are also active. If sub1 is inactive, the blocks within sub1 are also inactive, making sub1 conditional.

You can also convert a subsystem to a referenced model. For more details, see Convert Subsystems to Referenced Models.

model = "slexVariantMdlRefCondProp";
open_system(model);

Specify the value of V as 1. Simulate the model to observe the active and inactive variant choices.

V = 1;
sim(model);

Configure Model to Generate Code with if Conditions

By default, Simulink® supports generating code only for a specific choice of a variant block. You can customize your model to generate code for multiple choices of the block by using the Variant activation time parameter. The different choices are guarded by if and else if conditional statements in the generated code. These conditions enable code execution to selectively include or exclude startup routines based on variant conditions that evaluate to true during execution. You are not required to recompile the code each time the value of the variant control variable changes. Additionally, this approach allows you to analyze variant choices for potential incompatibilities, such as data type and dimension mismatches, prior to simulation and code generation. For more information, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.

To generate code with if conditional statements to choose between variants make sure that the Variant activation time parameter in both the Variant Source1 and Variant Sink blocks is set to startup.

set_param(model+"/Variant Source1","VariantActivationTime","startup");
set_param(model+"_sub2/Variant Sink","VariantActivationTime","startup");

1. In the Apps tab of the toolstrip, navigate to Simulink Coder. For detailed information on the settings to generate code, see Generate Code Using Simulink Coder.

2. In the C code tab, select Build > Generate code.

The code generator produces a standalone executable for the top model and a library module called a model reference target for each referenced model. The code for the top model is placed in the code generation folder and the code for referenced models is placed in an slprj folder. The subfolders in slprj are separate places for different types of files. For folder information, see Manage Build Process Folders.

  • slexVariantMdlRefCondProp_grt_rtw - The build folder containing the generated code for the top model.

  • slprj > grt > slexVariantMdlRefCondProp_sub1 and slprj > grt > slexVariantMdlRefCondProp_sub2 - The folders containing the generated code for the referenced models.

  • slexVariantMdlRefCondProp.exe - The executable created by the build process.

  • slexVariantMdlRefCondProp.slxc - The Simulink cache file for the top model.

  • slexVariantMdlRefCondProp_sub1.slxc and slexVariantMdlRefCondProp_sub2.slxc - The Simulink cache files for the referenced models.

slbuild(model);

3.The header files contain the necessary declarations and definitions for all variant choices. Since the code generator cannot determine which variant will be used at compile time, these header files are not guarded. They are included in their entirety to support any variant choice that might be chosen at startup.

However, The instances referring to the Model blocks are guarded in if conditional statements. These if statements enable you to conditionally execute startup routines based on the variant condition that evaluates to true. For more information, see Run Executables for Variant Blocks Without Recompiling Code for Changing Active Choices.

Configure Model to Generate Code with Preprocessor #if Conditionals

You can customize your model to generate code for multiple variant choices guarded by preprocessor conditional statements #if by using the Variant activation time parameter. These conditional statements enable you to conditionally compile the code to selectively include or exclude code based on the variant condition that evaluates to true. You are not required to regenerate code each time the value of the variant control variable changes. Additionally, this approach allows to analyze variant choices for potential incompatibilities, such as data type and dimension mismatches, prior to simulation and code generation. For more information, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.

To generate code with preprocessor conditionals #if and #elif, you must have an Embedded Coder license and the model must specify an ERT-based system target file, for example, ert.tlc. You must also set the Variant activation time parameter of the Variant Source1 and Variant Sink blocks to code compile.

1. In the Apps tab of the toolstrip, navigate to Embedded Coder. For detailed information on the settings to generate code, see Generate Code Using Embedded Coder.

2. In the C code tab, select Build > Generate code. The code generator generates code for the top model and a library module called a model reference target for each referenced model. The code for the top model is placed in the code generation folder and the code for referenced models is placed in an slprj folder.

  • slexVariantMdlRefCondProp_ert_rtw - The build folder containing the generated code for the top model.

  • slprj > ert > slexVariantMdlRefCondProp_sub1 and slprj > ert > slexVariantMdlRefCondProp_sub2 - The folders containing the generated code for the referenced models.

  • slexVariantMdlRefCondProp.slxc - The Simulink cache file for the top model.

  • slexVariantMdlRefCondProp_sub1.slxc and slexVariantMdlRefCondProp_sub2.slxc - The Simulink cache files for the referenced models.

3.Open the slexVariantMdlRefCondProp.c file. The header files and instances referring to the Model blocks are guarded in preprocessor conditionals #if and #elif. The preprocessor conditionals enable you to conditionally compile the code based on the variant conditions that evaluate to true. For more information, see Compile Code Conditionally for Variations of Component Represented Using Variant Block.

#include "slexVariantMdlRefCondProp_sub2.h"
#if V == 1
#include "slexVariantMdlRefCondProp_sub1.h"
#endif
void step(void)
{
 ...
#if V == 1
rtb_sub2_o2 = sin(((real_T)slexVariantMdlRefCondProp_DW.counter +
                   slexVariantMdlRefCondProp_P.Sine1_Offset) * 2.0 *
                  3.1415926535897931 /
                  slexVariantMdlRefCondProp_P.Sine1_NumSamp) *
  slexVariantMdlRefCondProp_P.Sine1_Amp +
  slexVariantMdlRefCondProp_P.Sine1_Bias;
slexVariantMdlRefCondProp_sub1(&rtb_sub2_o2,
  &rtb_VariantMerge_For_Variant_Source_Variant_Source1);
# elif V == 2
rtb_sub2_o2 = sin(((real_T)slexVariantMdlRefCondProp_DW.counter_i +
                   slexVariantMdlRefCondProp_P.Sine2_Offset) * 2.0 *
                  3.1415926535897931 /
                  slexVariantMdlRefCondProp_P.Sine2_NumSamp) *
  slexVariantMdlRefCondProp_P.Sine2_Amp +
  slexVariantMdlRefCondProp_P.Sine2_Bias;
rtb_sub2_o1 = sin(((real_T)slexVariantMdlRefCondProp_DW.counter_f +
                   slexVariantMdlRefCondProp_P.Sine3_Offset) * 2.0 *
                  3.1415926535897931 /
                  slexVariantMdlRefCondProp_P.Sine3_NumSamp) *
  slexVariantMdlRefCondProp_P.Sine3_Amp +
  slexVariantMdlRefCondProp_P.Sine3_Bias;
rtb_VariantMerge_For_Variant_Source_Variant_Source1 = rtb_sub2_o2 +
  rtb_sub2_o1;
#endif
slexVariantMdlRefCondProp_Y.Out1 =
  rtb_VariantMerge_For_Variant_Source_Variant_Source1;
slexVariantMdlRefCondProp_sub2(&rtb_sub2_o1, &rtb_sub2_o2, &rtb_sub2_o3,
  &(slexVariantMdlRefCondProp_DW.sub2_InstanceData.rtdw));
slexVariantMdlRefCondProp_Y.Out2 = slexVariantMdlRefCondProp_P.Gain4_Gain *
  rtb_sub2_o1;
#if V == 3
slexVariantMdlRefCondProp_Y.Out3 = slexVariantMdlRefCondProp_P.Gain1_Gain *
  rtb_sub2_o2;
#elif V == 4
slexVariantMdlRefCondProp_Y.Out4 = slexVariantMdlRefCondProp_P.Gain6_Gain *
  rtb_sub2_o3;
#elif V == 1
slexVariantMdlRefCondProp_DW.counter++;
if (slexVariantMdlRefCondProp_DW.counter ==
    slexVariantMdlRefCondProp_P.Sine1_NumSamp) {
  slexVariantMdlRefCondProp_DW.counter = 0;
}
#elif V == 2
slexVariantMdlRefCondProp_DW.counter_i++;
if (slexVariantMdlRefCondProp_DW.counter_i ==
    slexVariantMdlRefCondProp_P.Sine2_NumSamp) {
  slexVariantMdlRefCondProp_DW.counter_i = 0;
}
slexVariantMdlRefCondProp_DW.counter_f++;
if (slexVariantMdlRefCondProp_DW.counter_f ==
    slexVariantMdlRefCondProp_P.Sine3_NumSamp) {
  slexVariantMdlRefCondProp_DW.counter_f = 0;
}
#endif
}

Note

When the Variant activation time for a variant block is set to update diagram or update diagram analyze all choices, references to Model blocks connected to inactive variant choices are not included in the generated code.

Related Topics