Real-Time Workshop    

Parameter Objects

This section discusses how to use parameter objects in code generation.

Configuring Parameter Objects for Code Generation

In configuring parameter objects for code generation, you use the following code generation and parameter object properties:

Effect of Storage Classes on Code Generation for Parameter Objects

Real-Time Workshop generates code and storage declarations based on the RTWInfo.StorageClass property of the parameter object. The logic is as follows:

See Table 5-6 for examples of code generated for each possible setting of RTWInfo.StorageClass.

Example of Parameter Object Code Generation

In this section, we use the Gain block computations of the model shown in the figure below as an example of how Real-Time Workshop generates code for a parameter object.

Figure 5-5: Model Using Parameter Object Kp As Block Parameter

In this model, Kp sets the gain of the Gain1 block.

To configure a parameter object such as Kp for code generation:

  1. Define a subclass of Simulink.Parameter. In this example, the parameter object is an instance of the example class SimulinkDemos.Parameter, which is provided with Simulink. For the definition of SimulinkDemos.Parameter, see the directory
    matlabroot/toolbox/simulink/simdemos/@SimulinkDemos.
  2. Instantiate a parameter object from your subclass. The following example instantiates Kp as a parameter object of class SimulinkDemos.Parameter.
  1. Make sure that the name of the parameter object matches the desired block parameter in your model. This ensures that Simulink can associate the parameter name with the correct object. For example, in the model of Figure 5-5, the Gain block parameter Kp resolves to the parameter object Kp.

  1. Set the object properties.You can do this via the Simulink Data Explorer. Alternatively, you can assign properties via MATLAB commands, as follows:

Table 5-6 shows the variable declarations for Kp and the code generated for the Gain block in the model shown in Figure 5-5, with Inline parameters on. (Due to expression folding optimizations, the gain computation is included in the output computation.) An example is shown for each possible setting of RTWInfo.StorageClass.

Table 5-6: Code Generation from Parameter Objects (Inline Parameters ON)
StorageClass Property
Generated Variable Declaration
and Code
Auto
  • rtY.Out1 = (5.0 * rtb_u)
    
Simulink Global
  • typedef struct Parameters_tag {
      real_T Kp;
    .
    .
    Parameters rtP = {
      5.0 
    };
    .
    .
    rtY.Out1 = (rtP.Kp * rtb_u);
    
Exported Global
  • extern real_T Kp;
    .
    .
    real_T Kp = 5.0;
    .
    .
    rtY.Out1 = (Kp * rtb_u);
    
    
Imported Extern
  •  extern real_T Kp;
    .
    .
    rtY.Out1 = (Kp * rtb_u);
    
Imported Extern Pointer
  • extern real_T *Kp;
    .
    .
    rtY.Out1 = ((*Kp) * rtb_u);
    


  Simulink Data Objects and Code Generation Parameter Object Configuration Quick Reference Diagram