Documentation Home Page RT-LAB Home Page
Pour la documentation en FRANÇAIS, utilisez l'outil de traduction de votre navigateur Chrome, Edge ou Safari. Voir un exemple.

OpConversion

Library

rtlab/Miscellaneous

Block

This block handles the raw-to-engineering and engineering-to-raw conversions according to a set of conversion rules described below.

Figure 1: OpConversion block

Mask

OpConversion mask

Description

The OpConversion block acts as an interface in order to connect RT-LAB I/O blocks to the rest of the simulation.

When the engineering-to-raw mode is selected, the block transforms the engineering value submitted at the input of the block into a raw value that can be insterted for example in the input stream of data of an I/O block. Before applying the conversion rules, the block performs the following validations:

Determination of the length of the engineering valueThe size of the engineering value is determined by the Raw length parameter of the block. This value is specified in bytes. If the Raw length is bigger than the type of 1 element of the engineering value type specified in the parameter Eng type, the block expects an array of values at the inport.
Determination of the sign of the engineering valueIf the Eng type is one of : signed, signed type (short, integer, etc.), or float, the 2' complement representation is used in the conversions.
The conversion rules are then applied using the Scale, Offset, High and Low parameter values:
Scaling
  • If TY_SCALE, TY_HIGH_TY_LOW is 0, no scaling is needed and Scale =1.
  • If TY_SCALE is different from 0, ignore TY_HIGH, TY_LOW and TY_RAW_LENGTH: Scale = TY_SCALE.
  • If TY_SCALE is 0 and TY_HIGH or TY_LOW different from 0, calculate scale as Scale = (TY_HIGH - sTY_LOW) / (2^sTY_RAW_LENGTH - 1), where sTY_RAW_LEN GTH is TY_RAW_LENGTH and sTY_LOW is TY_LOW if the Raw type is unsigned, and sTY_RAW_LENGTH is TY_RAW_LENGTH -1 and sTY_LOW is 0 if the Raw type is signed.
  • If TY_HIGH and TY_LOW is 0, Scale is equal to 1, so conversion is just a type cast between raw and engineering values.
  • If Scale is different from 1, Eng = Raw * Scale. If Eng is an array, the scaling will not be applied.
  • If the scale value specified in the database was different than 1, an error code will be returned to warn the user of this incompatibility.
OffsetThe TY_OFFSET should be added to the Eng value after the Scaling is performed unless Eng is an array in which case the offset will be 0. If Eng is an array (as explained in step 0), and the offset value obtained from the database was different from 0, an error code will be returned to warn the user of this incompatibility.
Raw to engineeringThe raw to engineering performs the processing of the raw value of one element received from the hardware to present it in the engineering unit to the rest of the model. The processing of data follows the conversion rules listed below:
Determination of the length of the engineering value

This step should be done during the initialization of the s-function since it will determine the size of the output ports. The rules are: If TY_ENG_TYPE is a C-type, and TY_RAW_LENGTH is smaller than or equal to TY_ENG_TYPE length, the Eng value is a one-element value.

If TY_ENG_TYPE is a C-type (char, short, integer, etc) and TY_RAW_LENGTH is larger than TY_ENG_TYPE, the output Eng value should be a vector of TY_ENG_TYPE elements. For example If TY_RAW_LENGHT = 48, and TY_RAW_TYPE is defined as unsigned and TY_ENG_TYPE defined as char or unsigned char, it shall create as engineer value a char[6] or unsigned char[6] type, and split the values into each position on the array.

Determination of the sign of the raw and engineering value

Sign of the raw value: From TY_RAW_TYPE, determine if the type is signed or unsigned. If the TY_RAW_TYPE is signed, in order to determine if the Raw value is negative, the sign bit will be defined with the length of the TY_RAW_TYPE. For example the sign bit of an 8-bit integer will be the 7th bit, and not the 31st bit that it would be if the integrer was defined as a 32-bit integer. So, the sign will be + if the last bit of the Element is 0, and - if the last bit is 1. If TY_RAW_TYPE is signed, and TY_ENG_TYPE is unsigned, an error will be generated. If TY_RAW_TYPE is unsigned and the TY_LOW is a negative value, conversion is not allowed and the s-function will return an error and stop.

Sign of the eng value: From TY_ENG_TYPE, determine if the type is one of: signed, signed type (short, integer, etc.), or float. If TY_ENG_TYPE is one of these types, the 2' complement representation should be used. If TY_RAW_TYPE is unsigned and a negative engineering value is applied at run-time, conversion is not allowed and the output of the conversion will be set to 0.

Scalingsame as engineering to raw.
Offset same as engineering to raw.

Parameters

TY_RAW_LENGTHthe value of the raw length of the element.
TY_RAW_TYPEthe raw type of the element.
TY_ENG_TYPEthe engineering type of the element.
TY_SCALEthe value of the scale of the element.
TY_OFFSETthe value of the offset of the element.
TY_HIGHthe high value of the element.
TY_LOWthe low value of the element.

Inputs

When converting from raw to engineering, the s-function inport is a raw data value. In order to prevent data corruption when unsigned values are used, the type of the inport is set to 'uint32_T'.

In the case of conversion from engineering to raw, the inport is one engineering data value and the inport is of type 'double'.

The size of the inport is determined using the Raw length and Raw type of the element.

Outputs

The s-function outport is the Eng value calculated in the s-function when converting from raw to engineering (the outport is the raw value in case of conversion from engineering to raw).

The size of the outport is determined according to the rules presented above, item 0. The type of the outport is 'double' when converting from raw to engineering (the type of the outport is set to 'uint32_T' when converting from engineering to raw in order to prevent data corruption when unsigned values are used).

Characteristics and Limitations

Direct Feedthrough

No

Discrete sample time

Yes

XHP support

Yes

Work offline

Yes

OPAL-RT TECHNOLOGIES, Inc. | 1751, rue Richardson, bureau 1060 | Montréal, Québec Canada H3K 1G6 | opal-rt.com | +1 514-935-2323
Follow OPAL-RT: LinkedIn | Facebook | YouTube | X/Twitter