This code performs a series of operations to scale and...

June 30, 2025 at 12:10 PM

IF #ScaleOn AND NOT #SimulationOn AND NOT #WBT AND (INT_TO_REAL(#MaxRaw) - INT_TO_REAL(#MinRaw)) <> 0 THEN // Scale #ScaleTemp := (INT_TO_REAL(#InRaw) - INT_TO_REAL(#MinRaw)) / (INT_TO_REAL(#MaxRaw) - INT_TO_REAL(#MinRaw)) * (#MaxScaledVal - #MinScaledVal) + #MinScaledVal + #Offset; // Digital filtering IF #FilterFactor > 0 AND #FilterFactor < 1.0 THEN #Value := (#Value - #ScaleTemp) * #FilterFactor + #ScaleTemp; ELSE #Value := #ScaleTemp; END_IF; IF #MaxScaledVal >= #MinScaledVal THEN // Positive IF #Value < #MinScaledVal - (#MaxScaledVal - #MinScaledVal) * #OverRange THEN #Value := #MinScaledVal - (#MaxScaledVal - #MinScaledVal) * #OverRange; ELSIF #Value > #MaxScaledVal + (#MaxScaledVal - #MinScaledVal) * #OverRange THEN #Value := #MaxScaledVal + (#MaxScaledVal - #MinScaledVal) * #OverRange; END_IF; ELSE // Negative IF #Value > #MinScaledVal + (#MinScaledVal - #MaxScaledVal) * #OverRange THEN #Value := #MinScaledVal + (#MinScaledVal - #MaxScaledVal) * #OverRange; ELSIF #Value < #MaxScaledVal - (#MinScaledVal - #MaxScaledVal) * #OverRange THEN #Value := #MaxScaledVal - (#MinScaledVal - #MaxScaledVal) * #OverRange; END_IF; END_IF; //High Limit Value IF #Value > #MaxScaledLim THEN #Value := #MaxScaledLim; END_IF; //Low Limit Value IF #Value < #MinScaledLim THEN #Value := #MinScaledLim; END_IF; END_IF;

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.
  • For negative bounds:

    • Similar conditions are checked, but the calculation accommodates inversely ordered #MaxScaledVal and #MinScaledVal.

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:

  1. Scales the raw input value (#InRaw) into a new range while applying an offset.
  2. Optionally applies a digital filtering process to smooth transitions in the output.
  3. 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.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node