# LGF_ScaleLinear (FC / V3.0.1)

Author: Siemens Digital Industry Support

## Short description

This function scales an input variable (`LReal`) via a linear straight-line equation.

### Block Interface

| LGF_ScaleLinear (FC) |
--- | --- | ---
LReal |  |  | x |  | Ret_Val |  |  | LReal
LReal |  |  | x1 |  | error |  |  | Bool
LReal |  |  | y1 |  | status |  |  | Word
LReal |  |  | x2 |  |  |  |  |
LReal |  |  | y2 |  |  |  |  |
LReal |  |  | yMin |  |  |  |  |
LReal |  |  | yMax |  |  |  |  |

### Input parameter

Identifier | Data type | Description
--- | --- | ---
x | LReal | Input value `x` to be scaled.
x1 | LReal | Point 1 (P1) -`x` coordinate of the linear function.
y1 | LReal | Point 1 (P1) -`y` coordinate of the linear function.
x2 | LReal | Point 2 (P2) -`x` coordinate of the linear function.
y2 | LReal | Point 2 (P2) -`y` coordinate of the linear function.
yMin | LReal | Lower limit value of the output.
yMax | LReal | High limit value of the output.

### Output parameter

Identifier | Data type | Description
--- | --- | ---
Ret_Val | LReal | Scaled output value `y`
error | Bool | FALSE: No error TRUE: An error occurred during the execution of the FB
status | Word | 16#0000-16#7FFF: Status of the FB 16#8000-16#FFFF: Error identification (see following Table)

### Status & Error codes

Code / Value | Identifier / Description
--- | ---
16#0000 | STATUS_FINISHED_NO_ERROR Status: Execution finished without errors.
16#6001 | WARN_Y_LIMITED_TO_YMIN Warning: Output value limited to `yMin`
16#6002 | WARN_Y_LIMITED_TO_YMAX Warning: Output value limited to `yMax`
16#8200 | ERR_LOW_LIM_OVER_UP_LIM Error: Lower limit value `yMin` is greater than high limit value `yMax`.

## Functional description

The function linearly scales an input variable (e.g. an analog input value) to a specific output variable (e.g. level).

To determine the output variable, the following linear equation is used in the function:

x = \frac{y_2 - y_1}{x_2 - x_1} * (x - x_1) + y_1

The straight line is described by the two points, P1 and P2. You specify the points as a Cartesian coordinate system using x and y coordinates.

Note
  If the values of the parameters `x1` and `x2` are the same, the value of `y1` is output on output `y`.

By specifying `yMin` and `yMax` you can restrict the calculated value of `y` to a range limited at top and bottom. Thus, you avoid override and underride ranges.

Figure: Graphical representation

###### Example

A signal from 4 to 20mA is applied on an analog input module. This signal is converted to the CPU internal value from 0 to 27648 to measure a level. 0 corresponds to a level of 0.0m and 27648 to a level of 1.7m.

The block must then be parameterized as follows:

- P2: x1 = 0; y1 = 0.0
- P2: x2 = +27648; y2 = 1.7
- yMin = 0.0; yMax = 1.7;

## Change log

Version & Date | Change description
--- | ---
| 01.00.00 | Siemens Industry Online Support
| 27.01.2017 | First released version
| 01.00.01 | Siemens Industry Online Support
| 17.08.2018 | Upgrade: TIA V15 Update 2
| 01.00.02 | Siemens Industry Online Support
| 23.11.2018 | Upgrade: TIA V15.1
| 02.00.00 | Simatic Systems Support
| 25.01.2019 | Data type changed from Variant to LReal
| 02.00.01 | Simatic Systems Support
| 25.06.2019 | Standard header and block parameters update, status parameter added LReal value comparison added Result parameter changed to return value of FC for use in SCL Warning number changed to range of 16#6xxx refactor variable handling and extract returns in between the code add ENO handling
| 03.00.00 | Simatic Systems Support
| 23.04.2020 | Set version to V3.0.0 harmonize the version of the whole library
| 03.00.01 | Simatic Systems Support
| 12.11.2020 | Insert documentation Move to folder "Math operations"