Configure Parameters for C Code Generation - MATLAB & Simulink (original) (raw)

When configuring a model for code generation, you can identify and configure parameters for tunability, for example for calibration. Types of parameters that you can configure are listed in this table.

Type of Parameter Description
Model parameter argument Parameter configured as a model argument. Each model instance that includes the model parameter argument can have the argument set to a unique value.
Model parameter Parameter defined within a model, such as a parameter in the model workspace.
External parameter Parameter defined as an object in the base workspace or in a data dictionary.
Constant Constant-value parameters that could not be inlined.

To configure a parameter for code generation, you must associate the parameter with a data object. For example, before configuring a MATLAB variable for code generation, in the Model Explorer, convert the variable to a parameter object.

When you open a model in the Embedded Coder app, model parameters, model parameter arguments, and constants that are associated with data objects appear in the Code Mappings editor. In the editor, you can configure the parameters for code generation. If a model uses external parameters, you can add them to the Code Mappings editor by clicking theRefresh link to the right of the parameter name. That link initiates an update diagram and, in the editor view, adds external parameters that are used by the model.

Configure parameter data to:

To control whether the parameters throughout a model are tunable, you can gain access to model configuration parameter Default parameter behavior by using the'Auto' will be tunable/inline link on the Code Mappings editorData Defaults tab. For example, you can use that link to make parameters tunable in these cases:

For code generation, examples show how to configure model parameters and model parameter arguments for the model ConfigurationInterface. You can configure code mappings by using the Code Mappings Editor – C or code mappings programming interface (coder.mapping.api.CodeMapping).

Choose Customization Options for Parameters

By default, parameters in a model appear in generated code as fields of global data structures named:

Based on your code interface requirements, decide whether to customize generation of parameter data. If you do not configure customizations, the code generator determines whether to eliminate or change the representation of parameters in generated code for optimization purposes. If you configure customizations, decide:

Other considerations for model parameters include whether to:

For a list of interface requirements that are relevant to parameters that have corresponding storage classes and storage class properties, see Choose Storage Class and Storage Class Properties for Data Stores.

Parameter requirements for example model ConfigurationInterface are:

For this example, configure model parameters and model parameter arguments in ConfigurationInterface to meet these code generation requirements.

Configure Default Code Generation Settings for Parameters

Default code generation settings for parameters can reduce the effort of preparing a model for code generation, especially if a model has a significant number of parameters with which you need to interact while the generated code executes. Choose configuration settings once, and the code generator applies those settings to parameters across the model. Simulink® stores the default configuration as part of the model.

Consider configuring default code generation settings for model parameters if your model uses multiple parameters for the same category that do not have unique requirements or use a shared Embedded Coder Dictionary.

This example shows how to use the Code Mappings Editor – C to configure default settings for model parameters and model parameter argumentsfor the model ConfigurationInterface. Configure model parameters so that they are tunable and are defined and declared in the generated code as separate global variables that have type qualifiersconst and volatile. Configure model parameter arguments to use default settings, where the code generator allocates an area of memory represented as a uniquely named field in an exported structure for each argument instance.

  1. Open model ConfigurationInterface.
    openExample("ConfigurationInterface")
    Simulink model to use for learning how to configure model parameters for code generation.
  2. Open the Embedded Coder app.
  3. In the C Code tab, select > .
  4. In the Code Mappings editor, select Model parameter arguments. Leave the storage class set toDefault. The code generator allocates a separate area of memory for each argument instance.
  5. In the Code Mappings editor, under Parameters, select category Model parameters. Link text 'Auto' will be inlined indicates that the code generator is configured to inline model parameters by default. A requirement for this example is model parameters be tunable. Click 'Auto' will be inlined.
  6. In the Model Configuration Parameters dialog box, set model configuration parameter Default parameter behavior toTunable. Save the change and close the dialog box. In the Code Mappings editor, the link text changes to 'Auto' will be tunable.
  7. In the Code Mappings, with the Model parameters category still selected, set the storage class to ConstVolatile.
    Code Mappings editor with Data Defaults tab selected, Parameters tree node expanded, and storage class for Model parameters set to ConstVolatile.
  8. Save the model.

Configure Code Generation for Individual Parameters

You can configure individual parameters for code generation. For example, if a model has two parameters of the same category that have unique code generation requirements, configure the parameters individually. Or, if you configure default settings for a category of parameters, you can override those settings for specific parameters.

If your model meets at least one of these criteria, consider configuring code generation settings for parameters individually:

This example shows how to use the Code Mappings editor to apply your default storage class setting for model parameters to parameters K1,Table1, and Table2 in model ConfigurationInterface. You configure code identifiers for those parameters and for model parameter argumentsLOWER and UPPER. You can specify code generation identifiers, for example for integration, without modifying the model design.

  1. If you have not already done so, complete the steps in Configure Default Code Generation Settings for Parameters.
  2. In the Code Mappings editor, click the Parameters tab. Expand Model Parameter Arguments. By default, the storage class for each model parameter is set to Auto, which means that the code generator might eliminate or change the representation of relevant code for optimization purposes. If optimizations are not possible, the code generator applies the model default configuration. For this example, leave the storage class set to Auto.
  3. Expand Model Parameters. By default, the storage class for each parameter is set to Auto. Use the model default configuration, which specifies storage class ConstVolatile.
    • To avoid optimizations and force the code generator to use the default configuration, set the storage class to Model default.
    • To override the default configuration, specify the storage class that meets the code generation requirements for that parameter.
  4. In the Code Mappings editor, under Model Parameters, select parameters K1, Table1, andTable2. Set the storage class to Model default: ConstVolatile.
    Code Mappings editor with Parameters tab selected, parameters K1, Table1, and Table2 selected, and storage class being set to Model default: ConstVolatile.
  5. Configure the code identifiers for model parameters with names that include the prefix mp_. In the Code Mappings editor, select model parameter K1. Click the Icon to configure additional code mapping properties icon and set the storage class propertyIdentifier to mp_K1. For parametersTable1 and Table2, setIdentifier to mp_Table1 andmp_Table2.
    Code Mappings editor with Parameters tab selected, Model parameters tree node expanded, and storage class for parameters K1, Table1, and Table2 set to Model default: ConstVolatile. Mapping Inspector shows Identifier property for parameter Table2 set to mp_Table2.
  6. Save the model.
  7. Generate and view the code. For example, find the data definitions for model parameter mp_K1 in ConfigurationInterface.c.
    const volatile int8_T mp_K1 = 2;
    Find where the parameter is used in the step entry-point function.
    if (mode) {
    output = (real_T)mp_K1 * dout_Table1;
    } else {
    output = dstate_X;
    }

Organize Parameter Data into a Structure by Using Struct Storage Class

This example shows how to use the Struct storage class to organize block parameter values into a structure in the generated code. You apply the storage class directly to individual data items in a model.

To create a custom structure of parameter data in the generated code, consider creating a corresponding structure in Simulink. See Organize Data into Structures in Generated Code.

Create the Example Model

Create this example model with three Constant blocks and threeOutport blocks. Name the model ex_struct_param.

Set Up Constant Parameters

  1. Open the Model Data Editor. On the Modeling tab, click Model Data Editor.
  2. In the Model Data Editor, select the Parameters tab.
  3. In the model, select the upper Constant block.
  4. In the Model Data Editor, use the Value column to set the constant value to p1. Next to p1, click the action button three vertical dots and select .
  5. In the Create New Data dialog box, set Value toSimulink.Parameter(1.0) and clickCreate.
    Simulink adds a Simulink.Parameter object namedp1 to the model workspace.
  6. In the property dialog box for p1, clickOK.
    If you did not have to set up other parameters, you could click theCode Generation tab. Then, click theConfigure in Coder App button, which opens theEmbedded Coder app and Code Mappings editor.
  7. Use the Model Data Editor to set the other constant values by using parameter objects named p2 (value 2.0) and p3 (value 3.0).

Configure Constant Parameters for Code Generation

  1. Open the Embedded Coder app.
  2. On the C Code tab, select > .
  3. In the Code Mappings editor, click the Parameters tab.
  4. Expand Model Parameters.
  5. Set the storage class for parameters p1,p2, and p3 toStruct.
  6. Select parameter p1. Click the Icon to configure additional code mapping properties icon and set propertyStructName to my_struct. For parameters p2 and p3, setStructName to my_struct.
  7. Save the model.

Generate and Inspect Code

Generate and inspect the code.

The file ex_struct_param.h defines structure type,my_struct_type.

/* Type definition for custom storage class: Struct */ typedef struct my_struct_tag { real_T p1; real_T p2; real_T p3; } my_struct_type;

The file ex_struct_param.c defines global variablemy_struct.

/* Definition for custom storage class: Struct / my_struct_type my_struct = { / p1 */ 1.0,

 /* p2 */
 2.0,

 /* p3 */
 3.0

};

Configure Code Generation Settings for Parameters Programmatically

To automate configuration of parameters for code generation, use the programming interface for code mappings. For example, when creating custom block libraries or part of an application test environment, use the programming interface to automate data configuration.

This example shows how to configure default settings for model parameters and model parameter arguments for model ConfigurationInterface. Configure model parameters so that they are tunable and are defined and declared in the generated code as separate global variables that have type qualifiers const andvolatile. Configure model parameter arguments to use default settings, where the code generator allocates an area of memory represented as a uniquely named field in an exported structure for each argument instance. Apply your default storage class setting for model parameters to parameters K1,Table1, and Table2. Configure code identifiers for those parameters.

  1. Open the example model.
    openExample("ConfigurationInterface")
  2. Set model configuration parameter Default parameter behavior to Tunable.
    model = "ConfigurationInterface";
    set_param(model,"DefaultParameterBehavior","Tunable");
  3. Create object cm by calling functioncoder.mapping.api.get. The object stores the code generation configuration for data elements and functions in model ConfigurationInterface.
    cm = coder.mapping.api.get("ConfigurationInterface");
  4. Configure default settings for model parameters by calling functionsetDataDefault. For the arguments, specify these values:
    • The object returned bycoder.mapping.api.get
    • ModelParameters for the default category
    • Property name StorageClass with property valueConstVolatile
      setDataDefault(cm,"ModelParameters","StorageClass","ConstVolatile");
  5. Configure default settings for model parameter arguments. In the call tosetDataDefault, specify these values for arguments:
    • The object returned bycoder.mapping.api.get.
    • ModelParameterArguments for the default category.
    • Property name StorageClass with property valueDefault.
      setDataDefault(cm,"ModelParameterArguments","StorageClass","Default");
  6. Verify your default configuration for model parameters and model parameter arguments. Issue calls to getDataDefault that specify the object returned by coder.mapping.api.get, categoryModelParameters orModelParameterArguments, andStorageClass.
    getDataDefault(cm,"ModelParameters","StorageClass")
    getDataDefault(cm,"ModelParameterArguments","StorageClass")
  7. Apply the default configuration for model parameters to parameters K1, Table1, and Table2.
    By default, Simulink sets the storage class for individual parameters toAuto. When the storage class is Auto, the code generator:
    • Determines whether to eliminate the data from the generated code for optimization purposes.
    • If retaining the data, determines how to efficiently represent the data in the generated code, taking into account default configuration settings.
      Configure the code generator to apply your default model parameter settings to parameters K1, Table1, andTable2. For each parameter, call functionsetModelParameter. Specify the object returned bycoder.mapping.api.get, the parameter name, property name StorageClass, and property value Model default.
      setModelParameter(cm,"K1","StorageClass","Model default");
      setModelParameter(cm,"Table1","StorageClass","Model default");
      setModelParameter(cm,"Table2","StorageClass","Model default");
  8. Verify your configuration changes for parameters K1,Table1, and Table2 by using calls to function getModelParameter.
    getModelParameter(cm,"K1","StorageClass")
    getModelParameter(cm,"Table1","StorageClass")
    getModelParameter(cm,"Table2","StorageClass")
  9. Configure code identifiers for the model parameters. For each parameter, call function setModelParameter. Specify the object returned bycoder.mapping.api.get, the parameter name, property name Identifier, and one of these property values.
    Model Parameter Code Identifier
    K1 mp_K1
    Table1 mp_Table1
    Table2 mp_Table2
    setModelParameter(cm,"K1","Identifier","mp_K1");
    setModelParameter(cm,"Table1","Identifier","mp_Table1");
    setModelParameter(cm,"Table2","Identifier","mp_Table2");
  10. Verify your configuration changes for the model parameters by using calls to function getModelParameter.
    getModelParameter(cm,"K1","Identifier")
    getModelParameter(cm,"Table1","Identifier")
    getModelParameter(cm,"Table2","Identifier")
  11. Save the model.
  12. Generate and view the code. For example, find the data definitions for model parameter mp_K1 in ConfigurationInterface.c.
    const volatile int8_T mp_K1 = 2;
    Find where the parameter is used in the step entry-point function.
    if (mode) {
    output = (real_T)mp_K1 * dout_Table1;
    } else {
    output = dstate_X;
    }

Choose Storage Class and Storage Class Properties for Model Parameters

Depending on your code generation requirements, choose from the list of available storage classes to configure code generation for model parameters. The list of storage classes is defined in the coder dictionary.

Note

Requirements Storage Class for Default Mappings Storage Class for Individual Mappings
Enable optimizations, potentially generating more efficient code. Auto
For data elements that cannot be optimized, represent data as a field of a standard data structure. Default
Prevent optimizations from eliminating storage for a data element and use the default mapping for the data element category. Model Default
When using a shared coder dictionary, select the dictionary default for data elements that you do not want the code generator to optimize. Dictionary Default
Generate a structure that stores Boolean, fixed-point, or integer data in named bitfields. Bitfield
Support preprocessor conditionals defined by using a compiler flag or option. CompilerFlag CompilerFlag
Generate a global variable definition and declaration that has theconst, volatile, orconst and volatile type qualifiers. Const, Volatile, and ConstVolatile Const, Volatile, and ConstVolatile
Generate a macro (#define directive) or code that uses a macro defined in a header file in your external code. Define, ImportedDefine Define, ImportedDefine
Generate a global variable definition and declaration. ExportedGlobal ExportedGlobal
Generate a global variable definition and declaration to a specified file. ExportToFile ExportToFile
Generate a global variable definition and declaration that has thestatic type qualifier. FileScope (Local and shared local data store mappings only) FileScope (Local and shared local data store mappings only)
Generate code that interacts with data by calling your custom accessor functions. GetSet GetSet
Generate code that reads from and writes to a global variable or global variable pointer defined by your external code. ImportedExtern, ImportedExternPointer ImportedExtern, ImportedExternPointer
Generate code that reads from and writes to a global variable defined by your external header file. ImportFromFile ImportFromFile
Generate a global structure that has a name, which you can specify. Struct
Generate variables for single-instance data and generate structures for multi-instance data. MultiInstance (Not supported for external parameters) MultiInstance (Not supported for external parameters)

The list of available storage classes might include other project-specific storage classes defined in an Embedded Coder Dictionary. If you have special requirements that are not met by the listed storage classes and you have Embedded Coder software, you can define a storage class. See Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture.

For an individual model parameter, use the Identifier storage class property to configure a name for the variable representing the parameter in the generated code. With Embedded Coder, depending on the storage class that you choose, you can also configure these properties.

Property Description Storage Classes
DefinitionFile Source definition file that contains definitions for global data, which is read by the parameter and external code ExportToFile andVolatile
GetFunction Parameter appears in the generated code as a call to a specifiedget function GetSet
HeaderFile Source header file that contains declarations for global data, which is read by the parameter and external code ExportToFile, GetSet,ImportFromFile, andVolatile
Memory Section (default parameter configuration only) Memory section that contains data read by the parameter Default
Owner Code generator places the definition for parameters in the code generated for one of multiple models in a model hierarchy that share definitions. You must select the model configuration parameterUse owner from data object for data definition placement. See Control Placement of Global Data Definitions and Declarations in Generated Files. ExportToFile andVolatile
PreserveDimensions Code generator preserves dimensions of parameter data that is represented in generated code as a multidimensional array. You must set model configuration parameter Array layout toRow-major. See Preserve Dimensions of Multidimensional Arrays in Generated Code. ExportToFile, FileScope,GetSet, ImportFromFile,Localizable andVolatile
SetFunction Parameter appears in the generated code as a call to a specifiedset function. GetSet
StructName Name for a structure in the generated code for parameter. BitField and Struct

See Also

Code Mappings Editor – C | coder.mapping.api.CodeMapping