Main Content

Configure Code Generation for Variant Parameters in Model Workspace Using Code Mappings Editor

The Code Mappings Editor – C (Embedded Coder) is a graphical interface where you can configure data elements and entry-point functions in a model for code generation. A storage class defines properties such as appearance and location, which the code generator uses when producing code for associated data. You can use the editor to associate each category of model data element with a specific storage class throughout a model. Then, override those settings, as needed, for specific data elements. For models with an attached Embedded Coder® Dictionary that defines a service interface configuration, you can map elements of a model to service interfaces defined in a shared dictionary attached to the model.

Variant parameters enable you to conditionally vary the values of block parameters in a Simulink® model. For more information, see Simulink.VariantVariable. For an example, see Create a Simple Variant Parameter Model.

You can use the Code Mappings editor to configure the code generation attributes for ERT-based and GRT-based targets for Simulink.VariantVariable and Simulink.VariantControl objects defined in the model workspace of a model. The objects are listed in the Code Mappings editor if:

  • The Simulink.VariantControl object has its Value property set to a Simulink.Parameter object.

  • The Simulink.VariantVariable has its Specification property set to a Simulink.Parameter object.

If the model is configured to use a data code interface, you can specify a custom storage class defined in the Embedded Coder Dictionary that is associated with the model. If the model uses service code interfaces defined in a shared Embedded Coder Dictionary, you can map the Simulink.VariantVariable and Simulink.VariantControl objects to parameter tuning and parameter argument tuning service interfaces, which enables you to configure the parameters to be tunable in the generated code. For more information, see Configure Parameter and Parameter Argument Tuning Service Interfaces for Model Parameters and Model Parameter Arguments (Embedded Coder).

Note

Simulink.VariantControl objects defined in model workspace do not support default code mappings settings for model data.

Configure Code Generation for Simulink.VariantVariable and Simulink.VariantControl Objects in Model Workspace

1. Open the model slexVariantParameterCodeMappings.

open_system("slexVariantParameterCodeMappings");

The slexVariantParameterCodeMappings model implements a system that takes the speed of a car wiper motor as the input and computes the sweep time and sweep area parameters of the wiper. The model uses three variant parameters GearRatio, SweepAngle, and WiperLength to calculate the value of the wiper parameters. These variant parameters have three choices and the value of each choice varies depending on the position of the wiper in the car: front driver, front passenger, or rear positions.

The variant parameters use a Simulink.VariantControl object named Wiper defined in the model workspace to specify the variant conditions for the choices. The Value property of the variant control is set using a Simulink.Parameter object and the ActivationTime property is set to startup. The Wiper value can be set to the values WiperPosition.FRONT_DRIVER, WiperPosition.FRONT_PASSENGER, or WiperPosition.REAR defined in the WiperPosition.m file.

The variant parameters GearRatio, SweepAngle, and WiperLength are defined as Simulink.VariantVariable objects in the model workspace. The Specification property of these variant parameters are set to Simulink.Parameter objects named GearRatioSpec, SweepAngleSpec, and WiperLengthSpec, respectively, which are also defined in the model workspace.

2. Open Code Mappings Editor for the model. In the model canvas of the Simulink Editor window, click the perspective control in the lower-right corner and select Code. Then, click the Code Mappings tab.

3. In the Code Mappings editor, click the Parameters tab.

The Parameters tab shows the variant parameters and the associated objects, grouped by category.

  • External Parameter Objects — Variant parameter and variant control objects that are defined in the base workspace.

  • Model Variant Controls — Variant Controls defined in the model workspace. In the example, the storage class of the Wiper variant control is set to Auto.

  • Model Variant Variables — Variant parameters defined in the model workspace. For variant parameters, Storage Class is set to read-only because it is derived from the Specification object associated with the parameter. In this example, the GearRatio, SweepAngle, and WiperLength parameters derive their storage classes from the specification objects GearRatioSpec, SweepAngleSpec, and WiperLengthSpec, respectively.

  • Model Parameters — The Specification objects GearRatioSpec, SweepAngleSpec, and WiperLengthSpec appear in this category with storage class set to Struct. The name of the structure is set to WiperProperty for all the objects.

4. Generate code from the model using Embedded Coder®.

 slbuild("slexVariantParameterCodeMappings")
### Searching for referenced models in model 'slexVariantParameterCodeMappings'.
### Total of 1 models to build.
### Starting build procedure for: slexVariantParameterCodeMappings
### Successful completion of build procedure for: slexVariantParameterCodeMappings

Build Summary

Top model targets:

Model                             Build Reason                                         Status                        Build Duration
===================================================================================================================================
slexVariantParameterCodeMappings  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 8.8059s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 9.2899s

5. Review the generated code.

  • In the C Code tab of the Embedded Coder product, select Open Report. Select the header file slexVariantParameterCodeMappings.h. The code contains the type definition and declaration of the custom storage class of type struct. All the variant parameters are part of this structure.

file = fullfile("slexVariantParameterCodeMappings_ert_rtw",...
    "slexVariantParameterCodeMappings.h");
coder.example.extractLines(file,"/* Type definition for custom storage class: Struct */","/* Real-time Model Data Structure */",1,0)
/* Type definition for custom storage class: Struct */
typedef struct WiperProperty_tag {
  real_T GearRatio;                    /* Referenced by: '<S1>/Gain2' */
  real_T SweepAngle;                   /* Referenced by: '<S1>/Constant' */
  real_T WiperLength;                  /* Referenced by: '<S1>/Constant1' */
} WiperProperty_type;
  • The code declares the variant control with the extern keyword because its storage class is set to Auto and the variant activation time is set to startup.

file = fullfile("slexVariantParameterCodeMappings_ert_rtw",...
    "slexVariantParameterCodeMappings.h");
coder.example.extractLines(file,"* Exported Global Parameters","/* Real-time Model object */",1,1)
 * Exported Global Parameters
 *
 * Note: Exported global parameters are tunable parameters with an exported
 * global storage class designation.  Code generation will declare the memory for
 * these parameters and exports their symbols.
 *
 */
extern WiperPosition Wiper;            /* Variable: Wiper
                                        * Referenced by:
                                        *   '<Root>/Wiper'
                                        *   '<S1>/Constant'
                                        *   '<S1>/Constant1'
                                        *   '<S1>/Gain2'
                                        */

/* Model entry point functions */
extern void slexVariantParameterCodeMappings_initialize(void);
extern void slexVariantParameterCodeMappings_output(void);
extern void slexVariantParameterCodeMappings_update(void);
extern void slexVariantParameterCodeMappings_terminate(void);

/* Exported data declaration */

/* Declaration for custom storage class: Struct */
extern WiperProperty_type WiperProperty;

  • Select the slexVariantParameterCodeMappings.c file. The code defines the variant control and the variant parameters in this file.

file = fullfile("slexVariantParameterCodeMappings_ert_rtw",...
    "slexVariantParameterCodeMappings.c");
coder.example.extractLines(file,"/* Exported block parameters */","/* External inputs (root inport signals with default storage) */",1,0)
/* Exported block parameters */
WiperPosition Wiper = WiperPosition_FRONT_DRIVER;/* Variable: Wiper
                                                  * Referenced by:
                                                  *   '<Root>/Wiper'
                                                  *   '<S1>/Constant'
                                                  *   '<S1>/Constant1'
                                                  *   '<S1>/Gain2'
                                                  */

/* Exported data definition */

/* Definition for custom storage class: Struct */
WiperProperty_type WiperProperty = {
  /* GearRatio */
  0.5,

  /* SweepAngle */
  120.0,

  /* WiperLength */
  56.0
};

Programmatically Use Code Mappings for Simulink.VariantVariable and Simulink.VariantControl Objects

These functions provide programmatic support for Simulink.VariantVariable and Simulink.VariantControl objects in the code mappings programming interface.

FunctionPurpose
setModelVariantControl

Set code mapping properties of Simulink.VariantControl objects

getModelVariantControl

Get code mapping properties of Simulink.VariantControl objects

getModelVariantVariable

Get code mapping properties of Simulink.VariantVariable objects

Use these values for the elementCategory argument of the find (Embedded Coder) function to find Simulink.VariantVariable and Simulink.VariantControl objects in the code mapping categories.

Argument valuePurpose
"ModelVariantControls"Find Simulink.VariantControl objects present in the model code mappings
"ModelVariantControlArguments"Find Simulink.VariantControl objects set as model arguments and present in the model code mappings
"ModelVariantVariables"Find Simulink.VariantVariable objects present in the model code mappings

See Also

|

Topics