ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Analog Input Scaling to 0-10V

PLC Function Block Diagram (FBD)

Goal -- WPM

Ready
Exercise Algorithm Area
1FUNCTION_BLOCK Analog_Input_Scaling
2
3VAR_INPUT
4// Raw analog input value from sensor (e.g., 0-32767 for 16-bit ADC)
5RawInput : INT;
6// Minimum raw value corresponding to the minimum engineering unit
7RawMin : INT := 0;
8// Maximum raw value corresponding to the maximum engineering unit
9RawMax : INT := 32767;
10// Minimum engineering unit value (e.g., 0 for 0-100%)
11EngUnitMin : REAL := 0.0;
12// Maximum engineering unit value (e.g., 100 for 0-100%)
13EngUnitMax : REAL := 100.0;
14END_VAR
15
16VAR_OUTPUT
17// Scaled value in engineering units
18ScaledOutput : REAL;
19END_VAR
20
21VAR
22// Intermediate variables for calculation
23RawRange : REAL;
24EngUnitRange : REAL;
25ScaledValue : REAL;
26ClampedRawInput : INT;
27ClampedEngUnitMin : REAL;
28ClampedEngUnitMax : REAL;
29END_VAR
30
31// --- Input Validation and Clamping ---
32
33// Ensure RawMin is not greater than RawMax to prevent division by zero or incorrect scaling
34IF RawMin >= RawMax THEN
35RawMin := 0;
36RawMax := 32767;
37// Consider logging an error or setting a specific error flag in a real system
38END_IF
39
40// Ensure EngUnitMin is not greater than EngUnitMax
41IF EngUnitMin >= EngUnitMax THEN
42EngUnitMin := 0.0;
43EngUnitMax := 100.0;
44// Consider logging an error or setting a specific error flag
45END_IF
46
47// Clamp the raw input to the defined raw range to handle out-of-bounds sensor readings
48IF RawInput < RawMin THEN
49ClampedRawInput := RawMin;
50ELSIF RawInput > RawMax THEN
51ClampedRawInput := RawMax;
52ELSE
53ClampedRawInput := RawInput;
54END_IF
55
56// --- Scaling Calculation ---
57
58// Calculate the range of the raw input values
59RawRange := REAL_FROM_INT(RawMax) - REAL_FROM_INT(RawMin);
60
61// Calculate the range of the engineering unit values
62EngUnitRange := EngUnitMax - EngUnitMin;
63
64// Check if RawRange is zero to prevent division by zero. This case should be caught by RawMin >= RawMax check, but as a safeguard.
65IF RawRange = 0.0 THEN
66ScaledValue := EngUnitMin; // Default to minimum if range is zero
67ELSE
68// Perform the linear scaling calculation
69// Formula: ScaledValue = EngUnitMin + (ClampedRawInput - RawMin) * (EngUnitRange / RawRange)
70ScaledValue := EngUnitMin + (REAL_FROM_INT(ClampedRawInput) - REAL_FROM_INT(RawMin)) * (EngUnitRange / RawRange);
71END_IF
72
73// --- Output Clamping ---
74
75// Ensure the final scaled output is within the defined engineering unit range
76// This step is crucial if the EngUnitMin/Max were adjusted due to invalid input, or if the calculation somehow produced an out-of-bounds value.
77IF ScaledValue < EngUnitMin THEN
78ClampedEngUnitMin := EngUnitMin;
79ELSIF ScaledValue > EngUnitMax THEN
80ClampedEngUnitMax := EngUnitMax;
81ELSE
82ClampedEngUnitMax := ScaledValue;
83END_IF
84
85ScaledOutput := ClampedEngUnitMax;
86
87END_FUNCTION_BLOCK
Algorithm description viewbox

Analog Input Scaling to 0-10V

Algorithm description:

This Function Block scales a raw analog input signal (typically from a sensor) to a desired range of engineering units. It takes the raw input value, its corresponding minimum and maximum raw values, and the desired minimum and maximum engineering unit values. The block first validates and clamps the input to ensure it's within the expected raw range. Then, it performs a linear interpolation to map the clamped raw value to the target engineering unit range. Finally, it clamps the output to ensure it stays within the specified engineering unit bounds. This is commonly used for converting sensor readings like pressure, temperature, or flow into understandable units for control systems.

Algorithm explanation:

The algorithm implements linear scaling, a fundamental technique for converting raw sensor data into meaningful engineering units. It uses the formula: `Output = OutputMin + (Input - InputMin) * (OutputMax - OutputMin) / (InputMax - InputMin)`. The algorithm includes several checks to ensure robustness. It validates that `RawMin` is not greater than `RawMax` and `EngUnitMin` is not greater than `EngUnitMax`, defaulting to safe values if invalid ranges are provided. Crucially, it clamps the `RawInput` to the `[RawMin, RawMax]` range before calculation to handle sensor readings that might be slightly outside their nominal operating range. The final `ScaledOutput` is also clamped to `[EngUnitMin, EngUnitMax]` to guarantee it adheres to the specified output bounds, even if minor floating-point inaccuracies occur. The time complexity is O(1) as it involves a fixed number of arithmetic operations and comparisons. Space complexity is also O(1) as it uses a fixed number of variables.

Pseudocode:

FUNCTION_BLOCK Analog_Input_Scaling
  INPUTS: RawInput (INT), RawMin (INT), RawMax (INT), EngUnitMin (REAL), EngUnitMax (REAL)
  OUTPUTS: ScaledOutput (REAL)

  // Input validation
  IF RawMin >= RawMax THEN
    RawMin = 0
    RawMax = 32767 // Default safe values
  END IF
  IF EngUnitMin >= EngUnitMax THEN
    EngUnitMin = 0.0
    EngUnitMax = 100.0 // Default safe values
  END IF

  // Clamp raw input
  ClampedRawInput = Clamp(RawInput, RawMin, RawMax)

  // Calculate ranges
  RawRange = ConvertToReal(RawMax) - ConvertToReal(RawMin)
  EngUnitRange = EngUnitMax - EngUnitMin

  // Scaling calculation
  IF RawRange == 0.0 THEN
    ScaledValue = EngUnitMin
  ELSE
    ScaledValue = EngUnitMin + (ConvertToReal(ClampedRawInput) - ConvertToReal(RawMin)) * (EngUnitRange / RawRange)
  END IF

  // Clamp output
  ScaledOutput = Clamp(ScaledValue, EngUnitMin, EngUnitMax)

END FUNCTION_BLOCK

FUNCTION Clamp(Value, MinVal, MaxVal)
  IF Value < MinVal THEN
    RETURN MinVal
  ELSIF Value > MaxVal THEN
    RETURN MaxVal
  ELSE
    RETURN Value
  END IF
END FUNCTION