IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII IIIIIIIIIIIII
Simulink Scaling Parameters and TargetLink
This application note is about Simulink data types and how they are dealt with in the TargetLink environment. This includes fixed-point settings introduced with Simulink 5 .
TargetLinks General Approach
TargetLinks approach to production code development is as follows: I The control strategy is designed either with Simulink with subsequent conversion to TargetLink, or with TargetLinks blockset from scratch. In either case, the algorithm is simulated with Simulinks simulation engine using TargetLink blocks (MIL simulation mode). All signals are computed in floating-point. Scaling parameters are tagged to the TargetLink blocks. During MIL simulations, the resulting ranges are compared to the actual floating-point signal ranges (overflow detection). I Production code is generated according to the scaling parameters and run on the host (SIL mode) or on a target EVB (PIL mode). This allows fixed-point effects to be investigated directly. Thus, fixed-point effects are not investigated using blocks and the Simulink simulation engine, but using the production code itself. There is no fixed-point arithmetic in MIL mode.
Simulinks General Approach
Simulinks approach to production code development is as follows: I The control strategy is designed with Simulink blocks. To allow the generation of fixed-point code, integer data types are supported. To scale signals, a Fixed-Point license is needed. When signals are scaled, the associated blocks mimic fixed-point arithmetic. The objective of mimicking fixed-point arithmetic is to predict how the code to be generated will behave on ECU targets.
TargetLink Data Types Application Note
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.
Comparison of Simulink and TargetLink
The two approaches sometimes conflict and cause problems with models that were converted to TargetLink (see below). Some of these problems have been solved, for example, by introducing the Cast output signal to TargetLink type feature. Others cannot currently be solved.
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
TargetLink Data Types Application Note
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 ( [Link] ) 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.
Evaluation of Simulink Data Types
During conversion, Simulink data types of block outputs are evaluated and used when the data types ( [Link] property) of the replacing TargetLink blocks are set. Two approaches to evaluating Simulink data types have been implemented, as described below.
Compiled Scaling Parameters
During conversion, a model can be set to the so-called compiled mode. In this mode, the data types and scaling parameters of all signals (block outputs) can be read out using the Simulink API.
TargetLink Data Types Application Note
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
TargetLink Data Types Application Note
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Block-specific Scaling Parameters
After compiled data types have been evaluated and TargetLink types set accordingly, scaling parameters are retrieved on a block-by-block basis. This is possible without compiling the model, for both model and library conversion. However, if a blocks output inherits, for example, an integer data type, this method will fail to detect it. Additionally, since Simulink blocks do not have explicit scaling parameters in Simulink 4, this step is confined to version 5 and later. This is the second step in the conversion process. Thus, if model compilation is not possible (for libraries), or if no Fixed-Point license is available, Simulink scaling parameters will be mapped to TargetLink scaling parameters during this step. In Simulink 6, more blocks support block-specific types than in Simulink 5. The overall number will most likely increase in future Simulink versions. The following table shows which Simulink block types support fixedpoint data types. Some blocks additionally support scaling parameters, i.e., the output can be scaled. Output scaling is also taken into account during conversion. The Simulink properties which define the fixedpoint settings are listed in the third column. Block Type DataStoreMemory Ver. 6.0+ Simulink properties DataType OutDataType OutScaling ParameterDataTypeMode ParameterDataType ParameterScalingMode ParameterScaling OutDataTypeMode OutDataType OutScaling SaturateOnIntegerOverflow OutDataTypeMode OutDataType OutScaling ConRadixGroup Scaling Yes Remarks
Gain
5.0+
Yes
Both output and gain can be typed
Constant
5.0+
Yes
TargetLink Data Types Application Note
September 2004
5
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Block Type Logic Lookup1D Lookup2D
Ver. 5.0+ 5.0+ 5.0+
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
Scaling No Yes Yes
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
Written to associated TargetLink Inport block
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
TargetLink Data Types Application Note
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) [Link] and [Link] To corresponding signed or unsigned integer or floating-point type Integers only if number of bits is 8, 16, or 32 To [Link] and [Link] 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
Slope and Bias
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.
sfix, ufix, sint, uint, float, sfrac, ufrac
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.
TargetLink Data Types Application Note
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 ([Link] or [Link] 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 [Link] 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
TargetLink Data Types Application Note
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.
TargetLink Data Types Application Note
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 [Link] if [Link] == [Link]. 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.
[Link] and [Link]
10
I I I I I I I I I I I I I M I I
TargetLink Data Types Application Note
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Signal Data Types of TargetLink blocks
As stated before, TargetLink blocks behave differently than corresponding Simulink blocks with regard to non-double data types. Simulink blocks support numerous inheritance schemes, and for many blocks the signal output can be set explicitly, which in turn influences the type of the input [Link] blocks generally emit doubles and accept any nonscaled type at their inputs. However, some TargetLink blocks deviate from this in specific details. In any of these cases, the different behavior might result in models which cannot be initialized after Simulink-to-TargetLink conversion. The following list provides some details. All Simulink types means int8, int16, ..., single, double, boolean. Scaled signals accepted means that signals which are scaled in Simulink can be fed into the block. Same as Simulink means that the block behaves exactly like the corresponding Simulink block. The Typecast column indicates whether the block supports the Cast output signal to TargetLink type feature. Nr 1 2 3 Block Type Abs Assignment Constant Input(s) All Simulink types Same as Simulink Output(s) double Same as Simulink According to type of Constant value double double double double Same as Simulink Typecast Yes No No Remarks
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
TargetLink Data Types Application Note
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
Interpolation (nD) using PreLookUp
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
J-K Flip-Flop Logical Operator
All Simulink types All Simulink types
double double or boolean
No No Boolean if modelspecific setting BooleanDataType is set
23
Look-Up Table
All Simulink types
double
Yes
12
I I I I I I I I I I I I I M I I
TargetLink Data Types Application Note
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
Typecast Yes Yes No Yes Yes Only if nonvirtual Yes
Remarks
30
PreLook-Up Index Search
All Simulink types
UInt32 output holds index and fraction. The common variable for index and fraction is TargetLinkspecific and not supported by Simulink.
31 32 33 34
Preprocessor If Product Rate Limiter Relational Operator
Same as Simulink All Simulink types Same as Simulink All Simulink types
Same as Simulink double double double or boolean
No Yes Yes No Boolean if modelspecific setting BooleanDataType is set.
35 36 37
Relay S-R Flip-Flop Saturation
Same as Simulink All Simulink types All Simulink types
double double double
Yes No Yes
TargetLink Data Types Application Note
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
Output(s) double double double double double double
Typecast No Yes Yes Yes Yes Yes
Remarks
14
I I I I I I I I I I I I I M I I
TargetLink Data Types Application Note
September 2004
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII M L IIIIIIIIII
Note that scaled fixed-point signal types are generally not supported by TargetLink blocks.
TargetLink Data Types Application Note
September 2004
15
L I I I I I I I I I I I I I I I
IIIIIIIIIIIIM I L IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
TargetLink Stand-Alone Blockset
In TargetLinks Stand-Alone Blockset, the blocks are basically just masked Simulink blocks. Thus, their data type behavior generally corresponds to Simulink. There are some details to note: I The TargetLink data type ([Link] property) has no impact on the signal type during MIL simulations. The Cast output signal to TargetLink type feature is not supported. I Custom Code blocks behave identically in full-featured and standalone mode (except that signal logging is generally not possible in stand-alone mode). I TargetLink Inport and Outport blocks are always virtual. Their input signals are simply fed through the output. The signal data type behavior of Index Search and Pre Look-up Interpolation blocks is the same in full-featured and standalone mode, except that in stand-alone mode, the Cast output signal to TargetLink type feature is not supported.
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
TargetLink Data Types Application Note
September 2004