Main Content

Generate C Parameter Tuning Service Interface Code for Component Deployment

Since R2022b

This example shows how to generate component code that includes interface support for tunable parameters. The generated interface code must align with target environment interface requirements.

In the example you:

  1. Represent a tunable parameter in the model.

  2. Configure the code generator to apply a code interface.

  3. Generate and inspect the interface code.

Represent Tunable Parameter in Top Model

Open example component model ComponentDeploymentFcn.

open_system('ComponentDeploymentFcn');

component-deployment-fcn-model.png

The accumulator function receives the value of a state variable from the integrator function, increments the value by one, and applies a gain value, which is tunable during execution.

Open the function-call subsystem Accumulator.

component-deployment-fcn-accumulator-fcn.png

In the subsystem, the tunable parameter is represented as the gain parameter k. The target environment expects the name of the corresponding variable in the generated code to be CD_tunable.gain.

Configure Tunable Parameter Code Interface

The example model is linked to the shared Embedded Coder Dictionary file ComponentDeploymentCoderDictionary.sldd, which defines a service code interface configuration. That configuration defines a parameter tuning service interface named ParameterTuningService. That service is configured to apply storage class TuningStruct. That storage class is defined with these property settings:

  • Access is set to Direct.

  • Data Scope is set to Exported.

  • Header File is set to $N.h, where $N is the model name.

  • Definition File is set to $N.c, where $N is the model name.

  • Use different property settings for single-instance and multi-instance data is cleared.

  • Storage Type is set to Structured.

  • Type Name is set to naming rule CD_tunable_T$M.

  • Instance Name is set to naming rule CD_tunable.

  • Data Initialization is set to Static.

  • Memory Section is set to None.

  • Preserve array dimensions is cleared.

  • No qualifiers are specified.

Configure the model such that the code generator applies the coder dictionary default interface for tunable parameters.

  1. Open the Embedded Coder app.

  2. Select Code Interface > Component Interface.

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

  4. Expand the Model Parameters node and select the row for k.

  5. From the menu in the Parameter Tuning Service column of the selected row, select Dictionary default: ParameterTuningService.

  6. In the selected row, click the pencil icon.

  7. In the dialog box that appears, set the Identifier property to gain. Alternatively, you can set the property in the Property Inspector.

  8. Save configuration changes by saving the model.

Generate and Inspect Interface Code

Generate code from the example model. The code generator creates the folder ComponentDeploymentFcn_ert_rtw in the current working folder and places source code files in that folder. The generated code is in two primary files: header file ComponentDeploymentFcn.h and source code file ComponentDeploymentFcn.c. Model data and entry-point functions are accessible to a caller by including the header file in target environment software.

To inspect the generated code, use the generated Code Interface Report or, in the Embedded Coder app, use the Code view. The report is helpful for verifying that the generated interface code aligns with code interface requirements.

For a parameter tuning service interface, the report provides this information for each tunable parameter:

  • Parameter source

  • Code identifier

  • Data type

  • Dimension

Header File

The header file ComponentDeploymentFcn.h includes interface header file services.h, defines an instance of the real-time model data structure, and declares structures and callable entry-point functions for data and function interface elements represented in the model.

This code fragment shows code in the header file that is relevant to tunable parameters.

typedef struct {
  real_T k;
} CD_tunable_T;
.
.
.
extern void CD_accumulator(void);
.
.
.
extern CD_tunable_T CD_tunable;

Source Code File

This code fragment in the generated source code file ComponentDeploymentFcn.c shows code relevant to the parameter tuning service interface.

CD_tunable_T CD_tunable = {

  3.0
};
.
.
.
void CD_accumulator(void)
{
  const real_T *tmpIrvIRead;
  int32_T i;
  tmpIrvIRead = get_CD_accumulator_DataTransfer();
  for (i = 0; i < 10; i++) {
    CD_measured.delay[i] += tmpIrvIRead[i];
    (getref_CD_accumulator_OutBus_y())[i] = CD_tunable.k * CD_measured.delay[i];
  }
}

The code initializes CD_tunable to a value of three. In the for loop, the accumulator function applies the gain value when computing the accumulation results.

Related Examples

More About