Main Content

Use Simulink.Parameter Type of Variant Control Variables for Code Generation in Variant Blocks

If you intend to generate code for a model containing variant blocks, specify variant control variables as Simulink.Parameter objects. The Simulink.Parameter objects allow you to specify other attributes, such as data type and storage class, and control the appearance and placement of variant control variables in generated code.

You can define a variant control variable of type Simulink.Parameter only in the base workspace or in a data dictionary. Defining Simulink.Parameter type of variant control variables in a mask or model workspace is not supported. For more information on storage locations for variant control variables, see Storage Locations for Variant Control Variables (Operands) in Variant Blocks.

Simulink.Parameter objects within structures and that have data types other than Simulink.Bus objects are not supported.

Prerequisites

To learn more about how to use Variant Subsystems in Simulink®, Implement Variations in Separate Hierarchy Using Variant Subsystems example.

Explore the Model

1. Open the slexVariantSubsystems model.

model = "slexVariantSubsystems"
open_system(model);
model = 

    "slexVariantSubsystems"

2.In the MATLAB™ Editor, define a Simulink.Parameter object, V and set its value to 1. The parameter object V uses the custom storage class ExportedGlobal to define V in an external file. You can further control the appearance and placement of V and prevent optimizations from eliminating storage for V using the storage class property. For more information, see Storage Classes for Different Variant Activation Times.

V = Simulink.Parameter;
V.Value = 1;
V.DataType = "int32";
V.CoderInfo.StorageClass = "ExportedGlobal";

Note: Variant control variables defined as Simulink.Parameter objects can have any of the storage classes listed in Storage Classes for Different Variant Activation Times. You can also convert a scalar variant control variable into a Simulink.Parameter object. For more information, see Variant Control Variables into Simulink.Parameter Objects.

3. Specify the Simulink.Parameter object as the variant control variable in the Block Parameters dialog box of the Controller block. Also, change the Variant activation time to code compile.

linearBlkPath = "slexVariantSubsystems/Controller/Linear Controller";
nonLinearBlkPath = "slexVariantSubsystems/Controller/Nonlinear Controller";
set_param(linearBlkPath,"VariantControl","V == 1");
set_param(nonLinearBlkPath,"VariantControl","V == 2");
set_param(model+"/Controller","VariantActivationTime","startup");

Ensure that the Linear Controller and NonLinear Controller blocks are atomic.

set_param(linearBlkPath,"TreatAsAtomicUnit","on");
set_param(nonLinearBlkPath,"TreatAsAtomicUnit","on");

4. During simulation, the Linear Controller block becomes active. Double-click the Controller block to see the active choice.

sim(model);

Similarly, if you change the value of V to 2, the Nonlinear Controller block becomes active during simulation.

V.Value = 2;
sim(model);

Read Value of Variant Control Variable from Custom Header File

1. Specify the system target file as grt.tlc.

set_param(model,"SystemTargetFile","grt.tlc");

2. In the Apps tab of the toolstrip, navigate to Simulink Coder.

3. In the C code tab, select Build > Generate code. Alternatively, enter this command in the Command Window.

slbuild(model);

As the storage class for variable V is configured as ExportedGlobal, the code generator creates global definitions and declarations for V in the generated code. You can then access the value of V by using #include in your external code. This value of V determines which of the specified variants must be activated in the embedded executable. However, the value of V specified in the Simulink.Parameter object is still used during simulation, while the value specified in the external code is used for compiling the generated code and activating the desired variant in the embedded executable.

The generated code contains both Linear and Nonlinear choices in if and else if conditions because of the startup activation time.

/* Model step function */
void slexVariantSubsystems_step(void)
{
 if (V == 1)
   // Logic for Linear Controller
else if (V == 2)
  // Logic for Nonlinear Controller
}

If you are using Embedded Coder® with an ERT-based system target file, for example, ert.tlc, and you want to generate a code that guards the variant choices in preprocessor conditionals #if and #elif, specify the Variant activation time parameter of the variant blocks to code compile. These preprocessor conditionals enable you to conditionally compile variant choices as described in Compile Code Conditionally for Variations of Component Represented Using Variant Block.

Related Topics