This code performs a series of operations to scale and...
This code performs a series of operations to scale and filter a raw input value, apply range limitations, and enforce bounds on the scaled value. Here's a breakdown of what each part of the code does:
1. Condition Check
The first IF
statement checks whether the following conditions are all true:
#ScaleOn
: Scaling is enabled.NOT #SimulationOn
: The system is not in simulation mode.NOT #WBT
: A particular condition (potentially a watchdog timer or a flag) is not active.(INT_TO_REAL(#MaxRaw) - INT_TO_REAL(#MinRaw) <> 0)
: The difference between the maximum raw value and the minimum raw value is not zero (to avoid division by zero during scaling).
If all these conditions are true, the execution proceeds with the scaling and filtering logic.
2. Scaling the Raw Input
The #InRaw
value is scaled from its raw input range (defined by #MinRaw
and #MaxRaw
) to a desired output range (defined by #MinScaledVal
and #MaxScaledVal
), using the formula:
#ScaleTemp = (RawInput - MinRaw) / (MaxRaw - MinRaw) * (MaxScaledVal - MinScaledVal) + MinScaledVal + Offset
This converts the raw value into a scaled value (#ScaleTemp
) based on the target scaling range.
3. Digital Filtering
Next, the code checks whether a filter factor is set and within valid bounds (0 < #FilterFactor < 1.0
). If so, it applies a simple digital filter to smooth the output. The formula is:
#Value := (#Value - #ScaleTemp) * #FilterFactor + #ScaleTemp;
If the filter factor is invalid or not set, the scaled value #ScaleTemp
becomes the direct result (#Value
).
4. Bounding the Scaled Value
The code then ensures the scaled value remains within bounds, taking into account whether the scaling range is positive (#MaxScaledVal >= #MinScaledVal
) or negative (flipped bounds).
-
For positive bounds:
- If
#Value
is below a calculated lower over-range threshold, it is clamped to that lower threshold. - If
#Value
exceeds a calculated upper over-range threshold, it is clamped to that upper threshold.
- If
-
For negative bounds:
- Similar conditions are checked, but the calculation accommodates inversely ordered
#MaxScaledVal
and#MinScaledVal
.
- Similar conditions are checked, but the calculation accommodates inversely ordered
The over-range limit (#OverRange
) adds tolerance, allowing values to extend slightly beyond the scaled range before clamping.
5. Enforcing Absolute Limits
Finally, the scaled value (#Value
) is clamped between absolute limits defined by #MaxScaledLim
(maximum limit) and #MinScaledLim
(minimum limit). This ensures the output value does not exceed predefined bounds.
Summary of What the Code Does:
- Scales the raw input value (
#InRaw
) into a new range while applying an offset. - Optionally applies a digital filtering process to smooth transitions in the output.
- Enforces range checks and clamps the scaled value (
#Value
) to ensure it respects configured limits, both within the scaling range and absolute bounds.
This code is typically used in industrial automation or control systems where input values (e.g., sensor readings) need to be normalized and constrained before further processing or output.