Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Generated Code for Parameter Data Types

For an example of the code generated from Simulink parameters with different data types, run the demo model rtwdemo_paramdt. This demo model shows options that are available for controlling the data type of tunable parameters in the generated code. The model's subsystem includes several instances of Gain blocks feeding Saturation blocks. Each pair of blocks uses a workspace variable of a particular data type, as shown in the next figure.

The Simulink engine initializes the parameters in the demo model by executing the script rtwdemo_paramdt_data.m. You can view the initialization script and inspect the workspace variables in Model Explorer by double-clicking the appropriate yellow boxes in the demo model.

In the demo model, note that the Inline parameters option on the Optimization pane of the Configuration Parameters dialog box is selected. The Model Parameter Configuration dialog box reveals that all base workspace variables (with the exception of Kinline) have their Storage class property set to ExportedGlobal. Consequently, Kinline is a nontunable parameter while the remaining variables are tunable parameters.

To generate code for the demo model, double-click the blue boxes. The following table shows both the MATLAB code used to initialize parameters and the code generated for each parameter in the rtwdemo_paramdt model.

Parameter & MATLAB Code

Generated Variable Declaration and Code

Kinline

Kinline = 2;
rtb_Gain1 = rtwdemo_paramdt_U.In1 * 2.0F;
.
.
rtwdemo_paramdt_Y.Out1 = rt_SATURATE(rtb_Gain1, 0.0F, 2.0F);

Kcs

Kcs = 3;
real32_T Kcs = 3.0F;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In2 * Kcs;
.
.
rtwdemo_paramdt_Y.Out2 = rt_SATURATE(rtb_Gain1, 0.0F, Kcs);

Ksingle

Ksingle = single(4);
real32_T Ksingle = 4.0F;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In3 * Ksingle;
.
.
rtwdemo_paramdt_Y.Out3 = rt_SATURATE(rtb_Gain1, 0.0F, Ksingle);

Kint8

Kint8 = int8(5);
int8_T Kint8 = 5;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In4 * ((real32_T)( Kint8 ));
.
.
rtwdemo_paramdt_Y.Out4 = rt_SATURATE(rtb_Gain1, 0.0F,
  ((real32_T)( Kint8 )));

Kfixpt

Kfixpt = Simulink.Parameter;
Kfixpt.Value = 6;
Kfixpt.DataType = ...
 'fixdt(true, 16, 2^-5, 0)';
Kfixpt.RTWInfo.StorageClass = ...
 'ExportedGlobal';
int16_T Kfixpt = 192;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In5 *
  (((real32_T)ldexp((real_T)Kfixpt, -5)));
.
.
rtwdemo_paramdt_Y.Out5 = rt_SATURATE(rtb_Gain1, 0.0F,
  (((real32_T)ldexp((real_T)Kfixpt, -5))));

Kalias

aliasType = ...
 Simulink.AliasType('single');
Kalias = Simulink.Parameter;
Kalias.Value = 7;
Kalias.DataType = 'aliasType';
Kalias.RTWInfo.StorageClass = ...
 'ExportedGlobal';
typedef real32_T aliasType;
.
.
aliasType Kalias = 7.0F;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In6 * Kalias;
.
.
rtwdemo_paramdt_Y.Out6 = rt_SATURATE(rtb_Gain1, 0.0F, Kalias);

Kuser

userType = Simulink.NumericType;
userType.DataTypeMode = ...
 'Fixed-point: slope and bias scaling';
userType.Slope = 2^-3;
userType.isAlias = true;
Kuser = Simulink.Parameter;
Kuser.Value = 8;
Kuser.DataType = 'userType';
Kuser.RTWInfo.StorageClass = ...
 'ExportedGlobal';
typedef int16_T userType;
.
.
userType Kuser = 64;
.
.
rtb_Gain1 = rtwdemo_paramdt_U.In7 *
  (((real32_T)ldexp((real_T)Kuser, -3)));
.
.
rtwdemo_paramdt_Y.Out7 = rt_SATURATE(rtb_Gain1, 0.0F,
  (((real32_T)ldexp((real_T)Kuser, -3))));

The salient features of the code generated for this demo model are as follows:

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS