> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/rt-lab/simulink-blocks-reference/rt-lab-library/opconversion.md).

# OpConversion

## Description

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-da4f47b1dbf0f15ffe038bffb716337a44194bc4%2FRTL_opconversion_block.png?alt=media" alt=""><figcaption></figcaption></figure>

The **OpConversion** block, located in the **RT-LAB/Miscellaneous** library, converts values between raw and engineering units to interface RT-LAB I/O blocks with the rest of the simulation.

**Engineering-to-raw mode**: The block converts the engineering value at the input into a raw value that can be inserted in the input stream of an I/O block. Before applying the conversion rules, it validates:

**Determination of the length of the engineering value**: The engineering value size is defined by the Raw length parameter (in bytes). If Raw length is larger than the size of a single element of the Eng type, the block expects an array at the input.

**Determination of the sign of the engineering value**: If Eng type is one of: signed, signed type (short, integer, etc.), or float, two’s complement is used in the conversions.

**Conversion rules (Scale, Offset, High, Low)**

**Scaling**:

* If TY\_SCALE and TY\_HIGH/TY\_LOW are 0, no scaling is applied and Scale = 1.
* If TY\_SCALE is not 0, ignore TY\_HIGH, TY\_LOW, and TY\_RAW\_LENGTH: Scale = TY\_SCALE.
* If TY\_SCALE is 0 and TY\_HIGH or TY\_LOW is not 0, compute Scale = (TY\_HIGH - sTY\_LOW) / (2^sTY\_RAW\_LENGTH - 1), where sTY\_RAW\_LENGTH is TY\_RAW\_LENGTH and sTY\_LOW is TY\_LOW if Raw type is unsigned, and sTY\_RAW\_LENGTH is TY\_RAW\_LENGTH - 1 and sTY\_LOW is 0 if Raw type is signed.
* If TY\_HIGH and TY\_LOW are 0, Scale = 1 and conversion is a type cast between raw and engineering values.
* If Scale is not 1, Eng = Raw \* Scale. If Eng is an array, scaling is not applied.
* If the scale value defined in the database is different from 1, an error code is returned to warn of this incompatibility.

**Offset**: TY\_OFFSET is added to Eng after scaling, unless Eng is an array, in which case the offset is 0. If Eng is an array and the database offset is not 0, an error code is returned.

**Raw-to-engineering mode**: The block processes a raw value received from hardware and outputs the value in engineering units using the rules below.

**Determination of the length of the engineering value**: Performed at initialization to size the outputs. If TY\_ENG\_TYPE is a C-type and TY\_RAW\_LENGTH is smaller than or equal to the TY\_ENG\_TYPE length, Eng is a single value. If TY\_ENG\_TYPE is a C-type and TY\_RAW\_LENGTH is larger than the TY\_ENG\_TYPE length, the output Eng value is a vector of TY\_ENG\_TYPE elements. For example, if TY\_RAW\_LENGTH = 48, TY\_RAW\_TYPE is unsigned, and TY\_ENG\_TYPE is char or unsigned char, the engineering value is a char\[6] or unsigned char\[6], with values split across the array.

**Determination of the sign of the raw and engineering value**:

**Sign of the raw value**: From TY\_RAW\_TYPE, determine signed or unsigned. If TY\_RAW\_TYPE is signed, the sign bit position is defined by the TY\_RAW\_TYPE length (e.g., bit 7 for an 8-bit integer). The sign is positive if the most significant bit is 0, negative if it is 1. If TY\_RAW\_TYPE is signed and TY\_ENG\_TYPE is unsigned, an error is generated. If TY\_RAW\_TYPE is unsigned and TY\_LOW is negative, conversion is not allowed; an error is returned and processing stops.

**Sign of the engineering value**: From TY\_ENG\_TYPE, if the type is one of: signed, signed type (short, integer, etc.), or float, two’s complement is used. If TY\_RAW\_TYPE is unsigned and a negative engineering value is applied at run time, conversion is not allowed and the output is set to 0.

**Scaling**: Same rules as engineering-to-raw.

**Offset**: Same rules as engineering-to-raw.

## Mask parameters

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-aec7517be6d6eb74a105b344689aa048e6b4c6ed%2FRTL_opconversion_mask.png?alt=media" alt="" width="329"><figcaption></figcaption></figure>

<table><thead><tr><th width="219.4000244140625">Parameter</th><th>Description</th></tr></thead><tbody><tr><td><strong>TY_RAW_LENGTH</strong></td><td>The raw length of the element (in bytes).</td></tr><tr><td><strong>TY_RAW_TYPE</strong></td><td>The raw type of the element.</td></tr><tr><td><strong>TY_ENG_TYPE</strong></td><td>The engineering type of the element.</td></tr><tr><td><strong>TY_SCALE</strong></td><td>The scale value of the element.</td></tr><tr><td><strong>TY_OFFSET</strong></td><td>The offset value of the element.</td></tr><tr><td><strong>TY_HIGH</strong></td><td>The high value of the element.</td></tr><tr><td><strong>TY_LOW</strong></td><td>The low value of the element.</td></tr></tbody></table>

## Inputs

<table><thead><tr><th width="220.99993896484375">Input</th><th>Description</th></tr></thead><tbody><tr><td><strong>Raw-to-engineering</strong></td><td>One raw data value of type uint32_T. The type prevents data corruption when unsigned values are used. Size is determined by Raw length and Raw type.</td></tr><tr><td><strong>Engineering-to-raw</strong></td><td>One engineering data value of type double. Size is determined by Raw length and Raw type.</td></tr></tbody></table>

## Outputs

<table><thead><tr><th width="221">Output</th><th>Description</th></tr></thead><tbody><tr><td><strong>Raw-to-engineering</strong></td><td>The engineering value, of type double, computed by the block. Size is determined by the “Determination of the length of the engineering value” rules above.</td></tr><tr><td><strong>Engineering-to-raw</strong></td><td>The raw value, of type uint32_T, computed by the block. The type prevents data corruption when unsigned values are used.</td></tr></tbody></table>

## Characteristics and limitations

<table><thead><tr><th width="221">Characteristic</th><th>Value</th></tr></thead><tbody><tr><td>Direct Feedthrough</td><td>No</td></tr><tr><td>Discrete sample time</td><td>Yes</td></tr><tr><td>XHP support</td><td>Yes</td></tr><tr><td>Work offline</td><td>Yes</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/rt-lab/simulink-blocks-reference/rt-lab-library/opconversion.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
