Why do I get an "Incorrect data type" error or "Parameter precision loss" warning when tuning an integer or fixed-point type parameter with Simulink Real-Time & Speedgoat?

I am trying to change a Gain parameter during my Simulink Real-Time (SLRT) simulation, but I get an "Incorrect data type" error when I use the setparam target function as follows:
>> tg = slrealtime;
>> tg.setparam('MyModel/Gain','Gain',-2)
Unable to set 'Gain' parameter value on target computer 'TargetPC1': Incorrect data type.
When I try changing the parameter in the block dialog while using the external mode ("Run on Target"), I see warnings such as the following in the MATLAB prompt:
Warning: Parameter precision loss occurred for 'Gain' of 'MyModel/Gain'. The
original value of the parameter, 1, cannot be represented exactly using the run-time data type
sfix32_En31. The value is quantized to 0.9999999995343387. Quantization error occurred with an absolute
difference of 4.656612873077393e-10 and a relative difference of 4.65661287307739e-08%.
Suggested Actions:
To disable this warning or error for all parameters in the model, set the 'Detect precision loss'
option to 'none'. - Apply
Inspect details in the Parameter Quantization Advisor. - Open
- Suppress
> In slrealtime/Instrument/dataAvailableFromObserverViaTarget (line 1413)
In slrealtime.Target/dataAvailableFromObserver (line 66)
In slrealtime.Instrument>@(varargin)tg.dataAvailableFromObserver(varargin{:}) (line 1332)
In slrealtime.internal.instrument.StreamingCallBack.NewData (line 25)

 Accepted Answer

Option 1: Use the MATLAB command line
You can first query the parameter using the getparam target function to get clarity on the data type:
>> val = tg.getparam(gcb,'Gain')
val =
int8
-1
In this case, you need to pass an explicit int8 Type:
>> tg.setparam('MyModel/Gain','Gain',int8(-2));
If the parameter type is fixed-point, you will see the following output for getparam:
>> val = tg.getparam(gcb,'Gain')
val =
-1
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 32
FractionLength: 31
In this case, it is easiest to re-use the same fi object returned by getparam:
>> val.Value = -2;
>> tg.setparam('MyModel/Gain','Gain',val);
Option 2: Use SLRT Explorer
For an interactive approach, you can also check the parameter data types in the SLRT Explorer "Parameters" tab and "Type" column:
Additional notes:
  1. To avoid fixed-point types for Gain parameters, enable the model setting Gain parameters inherit a built-in integer type that is lossless.
  2. The Parameter Quantization Advisor app (R2022b+) may also be useful.
  3. More information on controlling block parameter data types in Simulink can be found in the documentation Control Block Parameter Data Types.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!