Real-Time Workshop    

C API for Parameter Tuning

Before reading this section, you should be familiar with the parameter storage and tuning concepts described in Parameters: Storage, Interfacing, and Tuning.

Overview

Real-Time Workshop provides data structures and a C API that enable a running program to access model parameters without use of external mode. Using the C API, you can

To access model parameters via the C API, you generate a model-specific parameter mapping file, model_pt.c. This file contains parameter mapping arrays containing information required for parameter tuning:

Your code should not access the data structures of model_pt.c directly. Pointers to these arrays are loaded into a ModelMappingInfo structure that is cached in the rtModel data structure. Your code must obtain a pointer to the ModelMappingInfo structure, using an accessor macro provided for the purpose. Your code can then use the rtBlockTuning and rtVariableTuning structures to access model parameters.

Real-Time Workshop provides sample code demonstrating how to use the parameter mapping information. You can use this sample code as a starting point in developing your own parameter tuning code.

The following sections discuss:

Generating the model.pt File

To generate the model_pt.c file, you must set the global TLC variable ParameterTuning to 1 (by default, ParameterTuning is disabled.) You can use the following statement in your system target file for this purpose.

Alternatively, you can append the following command to the System target file field on the Target configuration section of the Real-Time Workshop pane.

The the model_pt.c file is written to the build directory.

Parameter Map Data Types and Data Structures

The file matlabroot/rtw/c/src/pt_info.h defines enumerated data types and data structures used in the parameter map. Please refer to pt_info.h while reading this discussion.

Enumerated Types.   Two enumerations, ParamClass and ParamSource, are defined in pt_info.h.

The ParamClass enumeration specifies how a parameter is to be updated. The values rt_SCALAR and rt_VECTOR represent scalars and column vectors, respectively. The C declarations for these types are

The value rt_MATRIX_ROW_MAJOR indicates that the parameter is a matrix that is stored in memory in row major ordering. Conceptually, the C declaration for a parameter of this type is

The value rt_MATRIX_COL_MAJOR specifies that the parameter is a matrix that is stored in memory in column major ordering. Conceptually, the C declaration for a parameter of this type is

The value rt_MATRIX_COL_MAJOR_ND specifies that the parameter is an N-dimensional matrix. Conceptually, the C declaration for a parameter of this type is

Note that Real-Time Workshop actually declares matrices as vectors in column major order in each case. For example, a 2x3 matrix is represented as follows.

The ParamSource enumeration specifies the source of the parameter, which may be one of the following:

Map Vector.   The map vector (rtParametersMap) is an array containing the absolute base addresses of all block parameters that are members of rtP, the global parameter data structure. The code fragment below shows an example map vector. This example was generated from the model shown in Figure 14-1.

ParameterTuning, BlockTuning, and VariableTuning Structures.   The ParameterTuning structure contains the core of information stored in the BlockTuning and VariableTuning structures. ParameterTuning is defined as follows:

The paramClass and source fields take on one of the enumerated values mentioned in Enumerated Types.

The dataType field is the Simulink data type of the parameter, indicated by an enumerated value such as SS_DOUBLE.

The mapOffset field is the offset to the parameter's entry in the map vector. Using mapOffset, your code can obtain the actual address of the parameter.

The numInstances field is described in Mapping Parameter Instances in Simulink and Stateflow.

The fields nDims, nRows and nCols indicate the number of dimensions, rows and columns in the parameter, respectively.

If the number of dimensions of the parameter is greater than 2, the value dimsOffset is used to index into the dimensions map. This array contains the dimensions sizes for parameters having dimensions greater than 2. If there are no parameters having more than 2 dimensions, the dimensions map is empty.

The following table summarizes the relationship of the number of dimensions to the dimensions information in the ParameterTuning structure.

Table 14-4: Parameter Tuning Dimensions Information
Number of
Dimensions

Dimensions Information Fields
<= 2
nRows and nCols valid, nDims = 2, dimsOffset = -1
> 2
nRows=-1, nCols=-1, nDims and dimsOffset valid

The BlockTuning structure, in addition to the ParameterTuning information, contains the names of the originating block and parameter.

The VariableTuning structure, in addition to the ParameterTuning information, contains the name of the workspace variable.

Inlining Parameters

The Inline parameters option affects the information generated in the rtBlockTuning and rtVariableTuning arrays.

If Inline parameters is deselected:

If Inline parameters is selected:

Example Parameter Maps.   In this section, we will examine parameter mapping information generated from a simple model. In the example model, the amplitude and frequency of the Sine Wave block are controlled by the workspace variables amp and freq, as shown below.

Figure 14-10: Example Model Referencing Workspace Variables as Parameters

The following code fragment shows the rtBlockTuning and rtVariableTuning arrays generated from this model (in model_pt.c), as well as the parameter map and the function initializing the map, with Inline parameters off.

The following code fragment shows the rtBlockTuning and rtVariableTuning arrays generated (in model_pt.c), as well as the parameter map and the function initializing the map, with Inline parameters on. The workspace variables amp and freq have been declared tunable with storage class SimulinkGlobal(Auto).

Mapping Parameter Instances in Simulink and Stateflow

Simulink and Stateflow can have a shared or nonshared mapping of a parameter, depending on the parameter's Simulink storage class and Stateflow scope. A shared mapping is one in which the address of the parameter is the same in the code generated for Simulink blocks and Stateflow charts. This table shows how Simulink storage class and Stateflow scope affect the sharing of parameters in Simulink and Stateflow


Simulink
SimulinkGlobal
storage class

Simulink
Exported- Global
storage class

Simulink
ImportedExtern
storage class

Simulink
ImportedExtern- Pointer
storage class

Stateflow
imported
scope

Nonshared
Shared
(see note b)
Shared
(see note b)
Error
Stateflow
exported
scope

Nonshared
Error
Shared
(recommended: see note a)
Error
.

Note a: Recommended; does not require any user defined data definition.

Note b: Requires user defined data definition.

Therefore, to best share data between Simulink and Stateflow, define parameters as exported in Stateflow and as ImportedExtern in Simulink. When the mapping is nonshared, separate instances of that parameter appear in the code generated for Simulink and Stateflow.

As an example, consider the model shown in this picture.In this model, the MATLAB variable Kp is specified in two Gain blocks and as data of machine scope in a Stateflow chart.

When Inline parameters is selected, both Gain blocks share a single instance of Kp, and the Stateflow chart references a second instance. In such cases, the numInstances and mapOffset fields of the ParameterTuning structure are used in conjunction. The numInstances field specifies the number of parameter instances, while mapOffset is the offset into the map vector (rtParametersMap). The map vector determines the actual address of each instance from its source.

The following code shows the rtVariableTuning and rtParametersMap arrays for this case.

When Inline parameters is not selected, Real-Time Workshop creates two instances of Kp in the global parameters structure rtP for the two Simulink Gain blocks referencing Kp, and a third instance as a global variable for the Stateflow chart. All three instances must be updated to reflect any change in Kp. In the code example below, the entries for the Gain blocks in rtBlockTuning correspond to the two instances of Kp for those blocks. In addition, the entry for Kp in rtVariableTuning corresponds to the third instance for the Stateflow chart.

Accessing the Parameter Mapping Structures.

The parameter mapping arrays in model_pt.c are declared static. Pointers to the parameter mapping arrays are stored in a ModelMappingInfo structure, defined as follows in matlabroot/rtw/c/src/mdl_info.h.

The ModelMappingInfo structure is cached in the rtModel data structure. Use the rtmGetModelMappingInfo macro to obtain a pointer to the ModelMappingInfo structure, as in the following example.

In mdl_info.h, Real-Time Workshop provides additional macros that let you access members of the ModelMappingInfo structurevia a ModelMappingInfo pointer.

Using the Example Code

Real-Time Workshop provides example code that uses the parameter tuning API in matlabroot/rtw/c/src/pt_print.c. This file contains three functions:

This code is intended as a starting point for your parameter tuning code. For more information see the function abstracts preceding each function.

To become familiar with the example code, we suggest building a model that displays all the tunable block parameters and MATLAB variables to the screen. You can use ptdemo, the parameter tuning demo model, for this purpose. First, run the demo with Inline parameters on:

  1. Open the ptdemo model.
  1. ptdemo

  1. From the Simulation menu, choose Simulation Parameters.
  2. Select the Advanced tab. Make sure that the Inline parameters option is selected. Click Apply if necessary.
  3. Click on the Real-Time Workshop tab of the Simulation Parameters dialog box. The Real-Time Workshop pane activates. Select Target configuration from the Category menu. Note the System target file edit field contains:
  1. grt.tlc -aParameterTuning=1 -aParameterTuningTestFile="ptinfotestfile.tlc" -p0

    The second argument:

    includes the TLC file matlabroot/rtw/c/tlc/mw/ptinfotestfile.tlc. This file generates code required to display the parameter tuning information.

  1. Click the Build button.
  2. When the build completes, run the executable program:
  1. Parameter information will be displayed in the MATLAB command window. You can inspect the parameter map in the build directory (./ptdemo_grt_rtw/ptdemo_pt.c).

Next, run the demo with Inline parameters off:

  1. Select the Advanced tab of the Simulation Parameters dialog. Make sure that the Inline parameters option is deselected. Click Apply if necessary.
  2. Repeat steps 4-6 above. Note the difference, in the displayed parameter information and the ptdemo_pt.c file, with Inline parameters on versus off.

Restrictions

The parameter tuning C API does not support:

The coefficients of the Transfer Fcn, State-Space, Discrete Filter, Discrete Transfer Function and Discrete State-Space blocks are tunable, subject to requirements described in Tunability of Linear Block Parameters.

Summary of Parameter Tuning Source Files


  Signal Monitoring via Block Outputs Target Language Compiler API for Signals and Parameters