Skip to Main Content Skip to Search
Product Documentation

Generate Code for Variant Subsystems

Open the Example Model

Open model, AutoSSVar.mdl, which contains a variant subsystem.

Define the Variant Control Variables

To recreate the variant control variables specifically for code generation:

  1. Open the Model Explorer and click the Base Workspace.

  2. A variant control variable can be a Simulink.Parameter object or a mpt.Parameter object. In the Model Explorer, remove the current variant control variables, EMIS and FUEL, and re-create them as Simulink.Parameter objects. Select Add and Simulink Parameter to create two variant control variables, EMIS and FUEL.

  3. In the Simulink.Parameter property dialog box, specify the Value as 1 and the Data type as int8 for both EMIS and FUEL.

  4. Specify the Storage class parameter for both FUEL and EMIS as ImportedDefine(Custom):

  5. Specify the value of the variant control variable. Because the storage class is ImportedDefine(Custom), specify the Header File parameter as an external header file, AutoSSVar_variables.h, in the Custom Attributes section of the Simulink.Parameter property dialog box.

  6. Supply the values of the variant control variables in the external header file.

    #define FUEL 1
    #define EMIS 1
    

      Note   The generated code refers to a variant control variable as a user-defined macro. The generated code does not contain the value of the macro. The value of the variant control variable determines the active variant in the compiled code.

  7. The variant objects for this model already reside in the base workspace. For information on how to create the variant objects, follow the instructions for Creating Variant Objects. Check that only one variant object is active in the generated code by implementing the condition expressions of the variant objects such that only one evaluates to true. The generated code includes a test of the variant objects to determine that there is only one active variant. If this test fails, your code does not compile.

Make Each Child Subsystem an Atomic Subsystem

  1. Double-click the Variant Subsystem block, Engine, to display the child subsystems.

  2. For each child subsystem, right-click the subsystem and select Subsystem Parameters from the list. The Block parameters dialog box opens.

  3. To specify each child subsystem as an atomic subsystem, in the Block parameters dialog box, select the Treat as atomic unit parameter.

Configure Your Model for Generating Preprocessor Conditional Directives

In order to generate preprocessor conditional directives configure your model as follows:

  1. On the Code Generation pane of the Configuration Parameter dialog box, specify the System target file parameter as ert.tlc and clear Ignore custom storage classes. In order to generate preprocessor conditionals, you must use custom storage classes.

  2. On the Optimization > Signals and ParametersMATLAB pane of the Configuration Parameters dialog box, select Inline parameters.

  3. On the Code Generation > Interface pane of the Configuration Parameter dialog box, select the Enable All option of the Generate preprocessor conditionals parameter. This parameter is a global setting for the parent model and enables generating preprocessor conditionals for all variants of all variant blocks in the model. For more information, see Generate preprocessor conditionals.

  4. On the Code Generation > Report pane of the Configuration Parameter dialog box, select Create code generation report.

View the Generated Code

The generated code contains all child subsystems of the Variant Subsystem block protected by C preprocessor conditionals. In this case, the selection of the active variant (subsystem) is deferred until the generated code is compiled. Only one variant object, which is encoded in C macros, must evaluate to true.

After building the model, look at the variants in the generated code. AutoSSVar_types.h includes the following:

Calls to the step and initialization functions are conditionally compiled as shown in a portion of the step function, AutoSSVar_step, in AutoSSVar.c:

#if DE
  
  rtb_MergeForOutportOut1 = 2.2 * AutoSSVar_U.In1;

#elif DU

  rtb_MergeForOutportOut1 = 2.1 * AutoSSVar_U.In1;

#elif GE

  rtb_MergeForOutportOut1 = 1.2 * AutoSSVar_U.In1;

#elif GU

  rtb_MergeForOutportOut1 = 1.1 * AutoSSVar_U.In1;

#endif                                 /* DE */
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS