Simulink-Targetlink Datatypes Defnt Problem
Simulink-Targetlink Datatypes Defnt Problem
Simulink-Targetlink Datatypes Defnt Problem
September 2004
1
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
I Production code is generated according to specified types and scaling parameters and run on the selected target. Thus, fixed-point effects are investigated using blocks and the Simulink simulation engine.
Simulink-to-TargetLink Conversion
During Simulink-to-TargetLink conversion, Simulink blocks are replaced by corresponding TargetLink blocks. In the replacement process, block parameters are read from the blocks being replaced and written to the replacing blocks. For example, when a Simulink Gain block is replaced, the Gain parameter is written to the TargetLink Gain block. The rule is that after conversion, the converted subsystem with TargetLink blocks shows the same simulation behavior as the original subsystem. However, due to TargetLinks approach to data types, this rule does not hold if non-double data types are involved. Simulink data types are read from the blocks being replaced and written to the TargetLink blocks. For TargetLink, data types are only tagged to the blocks to be evaluated for production code generation. Unlike Simulink data types, TargetLink data types have no impact on the simulation bevaviour of the blocks.
2
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
A subsystem is converted to TargetLink. Inside the subsystem is a Simulink Gain block whose output signal type is specified to be uint16 . Consequently, the output data type ( output.type ) property of the replacing TargetLink block is set to UInt16, and this type is used during production code generation. However, the TargetLink Gain block will not simulate uint16 using integer arithmetic, but will produce doubles. This applies even if the Gain blocks input signal has an integer data type. During MIL simulation, the output is checked for compliance with the UInt16 range (overflow detection). This has two consequences: I When integer data types are involved, the simulation behavior may be different after conversion. I Since signal data types have changed, conversion might introduce data type conflicts, which makes simulation after conversion impossible without additional manual work, i.e. insertion of DataTypeConversion blocks. The latter could be the case if, for example, the Gain blocks output is fed to another block outside the converted subsystem, whose input is required to be uint16 . After conversion, the Gain blocks output is double, so the model cannot be initialized.
September 2004
3
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
The precondition for this mode is that the model can be initialized. This is the main reason why Simulink-to-TargetLink conversion of subsystems in models requires that the model can be initialized, and why conversion does not proceed if the Conversion Tool detects that the model is not initializable. This mode is also used to evaluate signal widths. Simulink libraries cannot be set to compiled mode. Thus, if a library (or a part of a library) is converted to TargetLink, data types cannot be evaluated using compiled mode. The same problem arises with signal widths. Evaluating data types in compiled mode has a big advantage: inherited data types can be considered as well as data types which are explicitly set. In compiled mode, it makes no difference, for example, whether the output type of a Product block is int16 because it has been explictly set, or because the block context and inheritance rules result in an int16 output type. Evaluating compiled data types also has a big disadvantage (Simulink Version 5 and 6 only): The resulting types depend on whether Simulink runs with or without a Fixed-Point license. With a license, all fixedpoint settings are taken into account, and signals can be fixed-point with or without scaling. Without a license, the user must set modelspecific settings so that all signals are double, as otherwise the model fails to initialize. Thus, without a license, Simulink fixed-point settings have no impact on the types in compiled mode. Moreover, this behavior differs in some details between Simulink 5 and Simulink 6. In other words, which scaling parameters are set during Simulink-toTargetLink conversion depends on whether there is a Fixed-Point license or not. If Simulink fixed-point types are used, it is therefore strongly recommended to invoke conversion on Simulink installations with Fixed-Point licenses.
4
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Gain
5.0+
Yes
Constant
5.0+
Yes
September 2004
5
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Simulink properties OutDataTypeMode LogicDataType OutDataTypeMode OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow LogicOutDataTypeMode LogicDataType OutputDataTypeScalingMode OutDataType OutScaling ConRadixGroup DataType OutDataType OutScaling
Remarks
DiscreteIntegrator
6.0+
Yes
Math
6.0+
Yes
MinMax
6.0+
Yes
Product
5.0+
Yes
Sum
5.0+
Yes
RelationalOperator Relay
5.0+ 5.1+
No Yes
Inport
6.0+
Yes
Note that Simulink parameters that have the same name do not always mean the same, and vice versa.
6
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Mapping Rules
The following table shows how Simulink data types and scaling parameters are mapped to TargetLink scaling parameters. The rules apply both to compiled and block-specific scaling parameters. The rules apply to Simulink scaling parameters evaluated during model compilation as well as on a block-by-block basis. Simulink data types int8, int16, int32, uint8, uint16, uint32, single, boolean data type double TargetLink Int8, Int16, Int32, UInt8, UInt16, UInt32,.Float32, Boolean To the currrent default TargetLink data type according to the current data dictionary (Int16 in a default installation) output.lsb and output.offset To corresponding signed or unsigned integer or floating-point type Integers only if number of bits is 8, 16, or 32 To output.checkmin and output.checkmax Remarks For Math blocks, integer types are only set if supported for current function For Math blocks, integer types are only set if supported for current function
If the block is vectorized, all elements are set to the values specified in Simulink Types like ufix(4) are not mapped to TargetLink, since 4bit integers are not supported in TargetLink Only if Simulink type is integer. Note that Simulink does not support saturation to one bound only.
SaturateOnIntegerOverflow
Typing Parameters
The output of Const blocks and the gain of Gain blocks are parameters as opposed to block outputs and states. Parameters in this sense are variables which keep a constant value during simulation. The value is specified by a MATLAB expression.
September 2004
7
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
In Simulink, both Constant block outputs and gains can have scaling parameters. In TargetLink, they are scaled only if the associated variable class (output.class or gain.class property) is not set to default. default means that in generated production code, the variable (gain or const) will not be represented by a variable but merged into operations. If a Simulink Const or Gain is detected to be scaled, the Conversion Tool selects a suitable variable class and writes it to the block. For this purpose, the first dSPACE Data Dictionary variable class object which complies with the following requirements is selected: I The property Const is on, I The property Scope is Global, I The property Macro is off, I The property Storage is default, I The property Info is none , I The property Optimization is movable or erasable. Additionally, a message such as the following is output: To enable setting of scaling parameters, the gain.class property was set to Const.
Reconversion to Simulink
Simulink fixed-point settings which applied before a subsystem was converted to TargetLink ought to be restored by the reconversion process. However, there are some differences between TargetLink and Simulink which make it difficult to unambiguously map TargetLink scaling parameters to Simulink scaling parameters: I Various TargetLink scaling parameter sets (that is, data type, LSB and offset values) can be mapped to different Simulink parameter sets. For example, TargetLinks UInt16, LSB = 2^0, Offset = 0 can be mapped either to Simulink ufix(16), [Slope Bias] = [2^0 0], or to uint16. There are many cases where mapping is similarly ambiguous.
8
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
I For vectorized variables, TargetLink LSB and offset values can be set for each vector element individually, while in Simulink the associated parameters are scalars which apply to all vector elements. I Saturation on integer overflow in Simulink cannot be specified for the lower and upper bounds separately, as is possible in TargetLink. To resolve these issues, the following approach has been implemented: I During Simulink-to-TargetLink conversion, all data type and scaling related parameters are saved to the replacing TargetLink block. This applies only to Simulink 5 and 6. The saved Simulink scaling parameters are retrieved from the TargetLink blocks during TargetLink-to-Simulink conversion. In many cases, there are none, either because the block concerned does not originate from a conversion (but was directly copied from TargetLinks blockset), or because the conversion was done with Simulink 4. If there are no scaling parameters, no such parameters are written to the Simulink block, which therefore has Simulinks default settings for scaling parameters. If there are any scaling parameters, the conversion routine checks whether they correspond to the current TargetLink parameters according to the table above. If they do, they are written to the replacing Simulink block. This resolves most ambiguity problems. If they do not, the table below applies.
September 2004
9
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Simulink Saved Simulink data type was int8, int16, int32, uint8, uint16, uint32, single, double , or boolean. Saved Simulink data type was specified with an sfix function call, or an uint, sint , ufrac or sfrac function call and [LSB offset] is not [20 0] or [2-wordwidth 0], respectively.
TargetLink TargetLink data type is Int8, Int16, Int32, UInt8, UInt16, UInt32,.Float32, Float64, Boolean or a user-defined type whose base type is one of these types. TargetLink data type is Int8, Int16, Int32, UInt8, UInt16, UInt32,.Float32, Float64, Boolean or a user-defined type.
Results The Simulink data type is written to the block. The TargetLink LSB and offset are not used.
Int8 -> sfix(8) Int16 -> sfix(16) Int32 -> sfix(32) UInt8 -> ufix(8) UInt16 -> ufix(16) UInt32 -> ufix(32) Float32 -> float(single) Float64 -> float(double) [LSB offset] -> [Slope Bias] If the LSB or offset is different across vector, the first values are written and a warning is issued. Int8 -> sint(8) Int16 -> sint(16) Int32 -> sint(32) UInt8 -> uint(8) UInt16 -> uint(16) UInt32 -> uint(32) Int8 -> sfrac(8) Int16 -> sfrac(16) Int32 -> sfrac(32) UInt8 -> ufrac(8) UInt16 -> ufrac(16) UInt32 -> ufrac(32) If Simulink type is integer: SaturateOnIntegerOverflow is set to the value of output.checkmin if output.checkmin == output.checkmax. Otherwise, a warning is issued.
Saved Simulink data type was specified with an uint or sint function call, and [LSB offset] is [20 0]. Saved Simulink data type was specified with an ufrac or sfrac function call, and [LSB offset] is [2-wordwidth 0]. SaturateOnIntegerOver flow
TargetLink data type is Int8, Int16, Int32, UInt8, UInt16, UInt32 or a user-defined type with integer base type.
TargetLink data type is Int8, Int16, Int32, UInt8, UInt16, UInt32 or a user-defined type with integer base type.
10
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
4 5 6 7 8 9
Counter/Alarm Custom Code D Flip-Flop D Latch Data Store Memory Data Store Read
double All Simulink types All Simulink types All Simulink types Same as Simulink
No No Yes Yes No No
September 2004
11
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Nr 10 11 12 13 14 15 16 17 18 19
Block Type Data Store Write Direct Look-Up Table (n-D) Discrete Filter Discrete StateSpace Discrete Transfer Function Discrete-Time Integrator FIR Filter Fcn Gain Inport
Input(s) Same as Simulink All Simulink types double double double double double Same as Simulink Same as Simulink All Simulink types
Output(s) Same as Simulink double double double double double double Same as Simulink double If set to virtual: same as input type otherwise double double
Typecast Yes Yes Yes Yes Yes Yes No Yes Only if nonvirtual Yes
Remarks
20
UInt32 or double
UInt32 input holds index and fraction. The common variable for index and fraction is TargetLinkspecific and not supported by Simulink
21 22
23
Look-Up Table
double
Yes
12
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Nr 24 25 26 27 28 29
Block Type Look-Up Table (2D) Math Merge MinMax Multiport Switch Outport
Input(s) All Simulink types double Same as Simulink All Simulink types All Simulink types All Simulink types
Output(s) double double Same as Simulink double double If set to virtual: same as input type otherwise double double or UInt32
Remarks
30
UInt32 output holds index and fraction. The common variable for index and fraction is TargetLinkspecific and not supported by Simulink.
31 32 33 34
Same as Simulink All Simulink types Same as Simulink All Simulink types
35 36 37
Yes No Yes
September 2004
13
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Nr 38 39 40 41 42 43 44
Block Type Sign Sink Sum Switch Trigonometric Function Unit Delay Unit Delay Reset Enabled
Input(s) All Simulink types All Simulink types All Simulink types All Simulink types All Simulink types All Simulink types All Simulink types
Remarks
14
I I I I I I I I I I I I I M I I
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Note that scaled fixed-point signal types are generally not supported by TargetLink blocks.
September 2004
15
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Thus, the signal data type behavior in TargetLink stand-alone mode does not exactly correspond to the signal data type behavior in full-featured mode. It also does not correspond to Simulinks behavior , either. This might result in invalid models after the mode has been switched.
16
I I I I I I I I I I I I I M I I
September 2004