Main Content

代表模型中生成子系统和变体d Code

Required products:万博1manbetx®, Embedded Coder®,Simulink Coder™

Using Simulink, you can create models that are based on a modular design platform that comprises a fixed common structure with a finite set of variable components. The variability helps you develop a single, fixed master design with variable components. For more information, seeWhat Are Variants and When to Use Them. When you implement variants in the generated code, you can:

  • Reuse generated code from a set of application models that share functionality with minor variations.

  • Share generated code with a third party that activates one of the variants in the code.

  • Validate the supported variants for a model and then choose to activate one variant for a particular application, without regenerating and re-validating the code.

  • Generate code for the default variant that is selected when an active variant does not exist.

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.

This example shows how to represent variant choices in a Simulink model and then prepare the model so that those variant choices are represented in generated code.

Step 1: Represent Variant Choices in万博1manbetx

Variant choices are two or more configurations of a component in your model. This example uses the modelrtwdemo_preprocessor_subsysto illustrate how to represent variant choices insideVariant Subsystemblocks. For other ways to represent variant choices, seeCompare Variant Blocks.

  1. Open the modelrtwdemo_preprocessor_subsys.

    open_system('rtwdemo_preprocessor_subsys')

    The model contains twoVariant Subsystemblocks:LeftControllerandRightController.

    Note

    You can only addInport,Outport,子系统, andModelblocks inside aVariant Subsystemblock.

  2. Open theLeftControllerblock.

    TheLeftControllerblock serves as the container for the variant choices. It contains two variant choices represented using子系统blocksNonlinearandLinear. The nonlinear controller subsystems implement hysteresis, whereas the linear controller subsystems act as simple low-pass filters.

    The子系统blocks have the same number of inports and outports as the containingVariant Subsystemblock.

    Variant choices can have different numbers of inports and outports. SeeMap Inports and Outports of Variant Choices in Variant Subsystem.

  3. Open theNonlinearblock.

    TheNonlinearblock represents one variant choice that Simulink activates when a condition is satisfied. TheLinearblock represents another variant choice.

    Tip

    When you are prototyping variant choices, you can create empty子系统blocks with no inputs or outputs inside aVariant Subsystemblock. The empty subsystem recreates the situation in which that subsystem is inactive without the need for completely modeling the variant choice.

Step 2: Specify Conditions That Control Variant Choice Selection

You can switch between variant choices by constructing conditional expressions called variant controls for each variant choice represented in aVariant Subsystemblock. Variant controls determine which variant choice is active, and changing the value of a variant control causes the active variant choice to switch.

A variant control is a Boolean expression that activates a specific variant choice when it evaluates totrue.

For more information, seeIntroduction to Variant Controls.

  1. Right-click theLeftControllerblock and selectBlock Parameters (Subsystem).

    TheConditioncolumn displays the Boolean expression that whentrueactivates each variant choice. In this example, these conditions are specified usingSimulink.VariantobjectsLINEARandNONLINEAR.

  2. Use these commands to specify a variant control using aSimulink.Variantobject.

    LINEAR = Simulink.Variant; LINEAR.Condition = 'VSSMODE==0'; NONLINEAR = Simulink.Variant; NONLINEAR.Condition = 'VSSMODE==1';

    Here,VSSMODEis called a variant control variable that can be specified in one of the ways listed in类型of Variant Control Variables (Operands) in Variant Blocks.

  3. Define the variant control variableVSSMODE.

    You can defineVSSMODEas a scalar variable or as aSimulink.Parameterobject. In addition to enabling the specification of parameter value,Simulink.Parameterobjects allow you to specify other attributes such as data type that are required for generating code.

    VSSMODE = Simulink.Parameter; VSSMODE.Value = 1; VSSMODE.DataType = 'int32'; VSSMODE.CoderInfo.StorageClass = 'Custom'; VSSMODE.CoderInfo.CustomStorageClass = 'ImportedDefine'; VSSMODE.CoderInfo.CustomAttributes.HeaderFile = 'rtwdemo_importedmacros.h';

    Variant control variables defined asSimulink.Parameterobjects can have one of these storage classes.

    • DefineorImportedDefinewith header file specified

    • CompilerFlag

    • SystemConstant (AUTOSAR)

    • Your own custom storage class that defines data as a macro

    You can also convert a scalar variant control variable into aSimulink.Parameterobject. SeeConvert Variant Control Variables into Simulink.Parameter Objects.

Step 3: Configure Model for Generating Preprocessor Conditionals

Code generated for each variant choice is enclosed within C preprocessor conditionals#if,#else,#elif, and#endif. Therefore, the active variant is selected at compile time and the preprocessor conditionals determine which sections of the code to execute.

  1. In theModelingtab of the Simulink toolstrip, clickModel Settings.

  2. Select theCode Generationpane, and setSystem target filetoert.tlc.

  3. In theReportpane, selectCreate code generation report.

  4. In the Configuration Parameters dialog box, clearIgnore custom storage classesand clickApply.

  5. In your model, right-click theLeftControllerblock and selectBlock Parameters (Subsystem).

  6. Set theVariant activation timeparameter tocode compile.

    When you select this option, Simulink analyzes all variant choices during an update diagram or simulation. This analysis provides early validation of the code generation readiness of all variant choices.

  7. Build the model.

Step 4: Review Generated Code

The code generation report contains a section dedicated to the subsystems that have variants controlled by preprocessor conditionals.

  1. In theC Codetab of the toolstrip, selectOpen Report.

  2. Select theCode Variant Reportfrom the left.

    In this example, the generated code includes references to theSimulink.VariantobjectsLINEARandNONLINEAR. The code also includes the definitions of macros corresponding to those variants. The definitions depend on the value ofVSSMODE, which is supplied in an external header filertwdemo_importedmacros.h. The active variant is determined by using preprocessor conditionals (#if) on the macros (#define)LINEARandNONLINEAR.

  3. Select thertwdemo_preprocessor_subsys_types.hfile from the left.

    This file contains the definitions of macrosLINEARandNONLINEAR.

    #ifndef LINEAR #define LINEAR (VSSMODE == 0) #endif #ifndef NONLINEAR #define NONLINEAR (VSSMODE == 1) #endif

  4. Select thertwdemo_preprocessor_subsys.cfile from the left.

    In this file, calls to the step and initialization functions of each variant are conditionally compiled.

    /* Outputs for Atomic SubSystem: '/LeftController' */ #if LINEAR /* Output and update for atomic system: '/Linear' */ ... #elif NONLINEAR /* Output and update for atomic system: '/Nonlinear' */ ... #endif

Limitations

  • When you are generating code forVariant Subsystemblocks, the blocks cannot have:

    • Mass matrices

    • Function call ports

    • Outports with constant sample time

    • Simscape™ blocks

  • The port numbers and names for each active child subsystem must belong to a subset of the port numbers and names of the parent Variant Subsystem block.

Related Examples

More About