Main Content

Configure Parameters for C Code Generation

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 ParameterDescription
Model parameterParameter defined within a model, such as a parameter in the model workspace.
External parameterParameter defined as an object in the base workspace or in a data dictionary.

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 Simulink Coder app, model parameters 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 the Refresh 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:

  • Make the data accessible for interaction while the generated code executes.

  • Minimize the amount of data that is stored in memory.

  • Promote parameter data to the model interface so that other components and systems can access that data.

  • Improve readability and traceability of the generated code.

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 editor Data Defaults tab. For example, you can use that link to make parameters tunable in these cases:

  • Refine parameter settings during rapid prototyping

  • Calibrate parameters

  • Optimize parameters for production code

For code generation, examples show how to configure model parameters and model parameter arguments for the model ConfigurationRapidPrototypingInterface. 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 a global data structure named model_P.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:

  • Whether to set up a default configuration

    If a model includes a significant number of parameters of a category that must be tunable (for example, more than 10), it is more efficient to configure the parameters of that category by using a default setting, and then override that setting for special cases. If the model includes a few parameters of a given category that have unique source, naming, or placement requirements, consider configuring the parameters individually.

  • How to declare and handle model parameter data in the generated code

    • As separate global variables

    • To read model parameter data from global variables defined in external code

    • As calls to access functions. Requires Embedded Coder®

    For more information about these options, see Control Data and Function Interface in Generated Code.

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 ConfigurationRapidPrototypingInterface are:

  • By default, retain model parameters in the generated code for tuning. Do not optimize the code by inlining the parameters.

  • Apply prefix mp_ to names of variables that represent model parameters.

For this example, configure model parameters in ConfigurationRapidPrototypingInterface 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.

This example shows how to use the Code Mappings Editor – C to configure default settings for model parameters for the model ConfigurationRapidPrototypingInterface. Configure model parameters so that they are tunable and are defined and declared in the generated code as separate global variables.

  1. Open model ConfigurationRapidPrototypingInterface.

    openExample("ConfigurationRapidPrototypingInterface")
    

    Simulink model to use for learning how to configure model parameters for code generation.

  2. Open the Simulink Coder app.

  3. In the C Code tab, select Code Interface > Default Code Mappings.

  4. 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.

  5. In the Model Configuration Parameters dialog box, set model configuration parameter Default parameter behavior to Tunable. Save the change and close the dialog box. In the Code Mappings editor, the link text changes to 'Auto' will be tunable.

  6. In the Code Mappings, with the Model parameters category still selected, set the storage class to ExportedGlobal.

    Code Mappings editor with Data Defaults tab selected, Parameters tree node expanded, and storage class for Model parameters set to ExportedGlobal.

  7. 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:

  • Uses multiple parameters of the same category that have unique source, naming, or placement requirements.

  • Uses a few parameters of the same category.

  • Has a default configuration for a category of parameters and you need to override the configuration for some specific parameters.

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. 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. Expand Model Parameters. By default, the storage class for each parameter is set to Auto. Use the model default configuration, which specifies storage class ExportedGlobal.

    • 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.

  3. In the Code Mappings editor, under Model Parameters, select parameters K1, Table1, and Table2. Set the storage class to Model default: ExportedGlobal.

    Code Mappings editor with Parameters tab selected, parameters K1, Table1, and Table2 selected, and storage class being set to Model default: ExportedGlobal.

  4. 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 property Identifier to mp_K1. For parameters Table1 and Table2, set Identifier to mp_Table1 and mp_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_Table2D.

  5. Save the model.

  6. Generate and view the code. For example, find the data definitions for model parameter mp_K1 in ConfigurationRapidPrototypingInterface.c.

    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;
      }
    

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 for model ConfigurationRapidPrototypingInterface. Configure model parameters so that they are tunable and are defined and declared in the generated code as separate global variables. 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("ConfigurationRapidPrototypingInterface")
    
  2. Set model configuration parameter Default parameter behavior to Tunable.

    model = "ConfigurationRapidPrototypingInterface";
    set_param(model,"DefaultParameterBehavior","Tunable");
    
  3. Create object cm by calling function coder.mapping.api.get. The object stores the code generation configuration for data elements in model ConfigurationRapidPrototypingInterface.

    cm = coder.mapping.api.get("ConfigurationRapidPrototypingInterface");
    
  4. Configure default settings for model parameters by calling function setDataDefault. For the arguments, specify these values:

    • The object returned by coder.mapping.api.get

    • ModelParameters for the default category

    • Property name StorageClass with property value ExportedGlobal

    setDataDefault(cm,"ModelParameters","StorageClass","ExportedGlobal");
    
  5. Verify your default configuration for model parameters. Issue calls to getDataDefault that specify the object returned by coder.mapping.api.get, category ModelParameters, and StorageClass.

    getDataDefault(cm,"ModelParameters","StorageClass")
    
    ans =
    
        'ExportedGlobal'
    
  6. Apply the default configuration for model parameters to parameters K1, Table1, and Table2.

    By default, Simulink sets the storage class for individual parameters to Auto. 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, and Table2. For each parameter, call function setModelParameter. Specify the object returned by coder.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");
    
  7. Verify your configuration changes for parameters K1, Table1, and Table2 by using calls to function getModelParameter.

    getModelParameter(cm,"K1","StorageClass")
    
    ans =
    
        'Model default'
    
    getModelParameter(cm,"Table1","StorageClass")
    
    ans =
    
        'Model default'
    
    getModelParameter(cm,"Table2","StorageClass")
    
    ans =
    
        'Model default'
    
  8. Configure code identifiers for the model parameters. For each parameter, call function setModelParameter. Specify the object returned by coder.mapping.api.get, the parameter name, property name Identifier, and one of these property values.

    Model ParameterCode Identifier
    K1mp_K1
    Table1mp_Table1
    Table2mp_Table2
    setModelParameter(cm,"K1","Identifier","mp_K1");
    setModelParameter(cm,"Table1","Identifier","mp_Table1");
    setModelParameter(cm,"Table2","Identifier","mp_Table2");
    
  9. Verify your configuration changes for the model parameters by using calls to function getModelParameter.

    getModelParameter(cm,"K1","Identifier")
    
    ans =
    
        'mp_K1'
    
    getModelParameter(cm,"Table1","Identifier")
    
    ans =
    
        'mp_Table1'
    
    getModelParameter(cm,"Table2","Identifier")
    
    ans =
    
        'mp_Table2'
    
  10. Save the model.

  11. Generate and view the code. For example, find the data definitions for model parameter mp_K1 in ConfigurationRapidPrototypingInterface.c.

    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.

Note

  • For constants, only the Default storage class applies.

  • For model parameter arguments, only the Auto, Default, and Model default storage classes apply.

  • Top-level models do not support individual mappings for model parameter arguments. If you select the Auto storage class for a model parameter argument in a top-level model, the code generator inlines the argument.

RequirementsStorage Class for Default MappingsStorage 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
Generate a global variable definition and declaration.ExportedGlobalExportedGlobal
Generate code that reads from and writes to a global variable or global variable pointer defined by your external code.ImportedExtern, ImportedExternPointerImportedExtern, ImportedExternPointer

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 (Embedded Coder).

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.

See Also

|

Related Topics