To create a FUNCTION BLOCK in Structured Text (ST) for a PLC that uses a 2D lookup table to control 1 output based on 3 inputs (X, Y, and the 2D array), and outputs the interpolated value, you can follow the example below. This example assumes you're working with a PLC that supports the IEC 61131-3 standard, such as those from Beckhoff, Siemens (to some extent), or others that support Structured Text.
The function block will take in the X and Y coordinates and a 2D array, and it will output the interpolated value based on the values in the 2D array at the coordinates closest to X and Y.
Here's a basic implementation:
FUNCTION_BLOCK FB_2DLookup
VAR_INPUT
X : REAL; // X coordinate
Y : REAL; // Y coordinate
arr2D : ARRAY [0..N, 0..M] OF REAL; // 2D array, N and M are dimensions
X_table : ARRAY [0..N] OF REAL; // X values of the table
Y_table : ARRAY [0..M] OF REAL; // Y values of the table
END_VAR
VAR_OUTPUT
Output : REAL; // Interpolated output value
END_VAR
VAR
i, j : INT; // Indices for X and Y in the table
x_ratio : REAL; // Ratio for interpolation in X direction
y_ratio : REAL; // Ratio for interpolation in Y direction
x1, x2 : REAL; // X values for interpolation
y1, y2 : REAL; // Y values for interpolation
Q11, Q12, Q21, Q22 : REAL; // Values from the 2D array for interpolation
END_VAR
BEGIN
// Find the indices i and j such that X_table[i] <= X < X_table[i+1] and Y_table[j] <= Y < Y_table[j+1]
FOR i := 0 TO N-1 DO
IF X >= X_table[i] AND X < X_table[i+1] THEN
EXIT;
END_IF
END_FOR
FOR j := 0 TO M-1 DO
IF Y >= Y_table[j] AND Y < Y_table[j+1] THEN
EXIT;
END_IF
END_FOR
// Check if X and Y are within the table bounds
IF i >= N OR j >= M THEN
// Handle out-of-bounds condition, e.g., by setting Output to a default value or an error
Output := 0.0; // Example default value
ELSE
// Get the values for interpolation
Q11 := arr2D[i, j];
Q12 := arr2D[i, j+1];
Q21 := arr2D[i+1, j];
Q22 := arr2D[i+1, j+1];
x1 := X_table[i];
x2 := X_table[i+1];
y1 := Y_table[j];
y2 := Y_table[j+1];
// Calculate the ratios for interpolation
x_ratio := (X - x1) / (x2 - x1);
y_ratio := (Y - y1) / (y2 - y1);
// Perform bilinear interpolation
Output := Q11 * (1 - x_ratio) * (1 - y_ratio) +
Q21 * x_ratio * (1 - y_ratio) +
Q12 * (1 - x_ratio) * y_ratio +
Q22 * x_ratio * y_ratio;
END_IF
END_FUNCTION_BLOCK
Key Points:
- N and M: You need to define
N and M as constants representing the dimensions of your 2D array minus one (since array indices start at 0). For example, if your array is ARRAY [0..5, 0..7] OF REAL;, then N = 5 and M = 7.
- X_table and Y_table: These arrays represent the X and Y coordinates of the points in your 2D lookup table. They should be sorted in ascending order.
- Bilinear Interpolation: The code performs bilinear interpolation, which is a technique for interpolating functions of two variables (here, X and Y) on a 2D grid.
To use this function block, you would instantiate it in your PLC program, pass the required inputs (X, Y, and the 2D array along with X_table and Y_table), and read the interpolated output from the Output variable.
PROGRAM Main
VAR
my2DLookup : FB_2DLookup;
my2DArray : ARRAY [0..N, 0..M] OF REAL := [...]; // Initialize with your data
X_values : ARRAY [0..N] OF REAL := [...]; // X coordinates of the table
Y_values : ARRAY [0..M] OF REAL := [...]; // Y coordinates of the table
interpolatedValue : REAL;
END_VAR
BEGIN
my2DLookup(X := 4.5, Y := 2.7, arr2D := my2DArray, X_table := X_values, Y_table := Y_values, Output => interpolatedValue);
// Use interpolatedValue as needed
END_PROGRAM
Make sure to adjust N, M, my2DArray, X_values, and Y_values according to your specific application.