To design and implement a second-order Butterworth low-pass filter with a cutoff frequency of 0.01 Hz on an Allen Bradley PLC with a scan time of 1 Hz, we need to derive the discrete-time filter coefficients and then translate them into Structured Text code suitable for the PLC.
1. Deriving the Filter Coefficients
a. Continuous-Time Transfer Function:
The continuous-time transfer function of a second-order Butterworth low-pass filter with cutoff frequency ( \omega_c ) is:
[ H(s) = \frac{\omega_c^2}{s^2 + \sqrt{2}\omega_c s + \omega_c^2} ]
Given the cutoff frequency ( f_c = 0.01 ) Hz, the angular frequency ( \omega_c ) is:
[ \omega_c = 2\pi f_c = 2\pi \times 0.01 = 0.0628319 \ \text{rad/s} ]
b. Pre-warping the Frequency:
When using the bilinear transform, frequency warping occurs. To compensate, we pre-warp the cutoff frequency:
[ \omega_p = \frac{2}{T} \tan\left( \frac{\omega_c T}{2} \right) ]
Given the sampling period ( T = 1 ) s:
[ \omega_p = 2 \tan\left( \frac{0.0628319 \times 1}{2} \right) = 0.0628526 \ \text{rad/s} ]
c. Bilinear Transformation:
We substitute ( s ) in the continuous transfer function with the bilinear transform:
[ s = \frac{2}{T} \cdot \frac{z - 1}{z + 1} ]
Substituting ( s ) and ( \omega_p ) into ( H(s) ) and simplifying leads to the discrete-time transfer function ( H(z) ):
[ H(z) = \frac{b_0 z^2 + b_1 z + b_2}{z^2 + a_1 z + a_2} ]
d. Calculating the Coefficients:
After algebraic manipulation and simplification, we calculate the coefficients:
- Numerator coefficients:
- ( b_0 = 0.00094588 )
- ( b_1 = 0.00189176 )
- ( b_2 = 0.00094588 )
- Denominator coefficients:
- ( a_1 = -1.911195 )
- ( a_2 = 0.914973 )
The difference equation representing the filter is:
[ y[n] = b_0 x[n] + b_1 x[n-1] + b_2 x[n-2] - a_1 y[n-1] - a_2 y[n-2] ]
2. Structured Text Implementation
Below is the Structured Text code that implements the derived filter:
VAR
// Input and output sample variables
x0 : REAL; // Current input sample x[n]
x1 : REAL := 0.0; // Previous input sample x[n-1]
x2 : REAL := 0.0; // Input sample x[n-2]
y0 : REAL; // Current output sample y[n]
y1 : REAL := 0.0; // Previous output sample y[n-1]
y2 : REAL := 0.0; // Output sample y[n-2]
// Filter coefficients
b0 : REAL := 0.00094588;
b1 : REAL := 0.00189176;
b2 : REAL := 0.00094588;
a1 : REAL := -1.911195;
a2 : REAL := 0.914973;
END_VAR
// Read the current input sample into x0
x0 := INPUT_SIGNAL; // Replace with actual input signal variable
// Compute the current output sample using the difference equation
y0 := b0 * x0 + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
// Update the stored input samples for the next iteration
x2 := x1;
x1 := x0;
// Update the stored output samples for the next iteration
y2 := y1;
y1 := y0;
// Output the current output sample
OUTPUT_SIGNAL := y0; // Replace with actual output signal variable
Notes:
- Variable Initialization: The previous input and output samples (
x1, x2, y1, y2) are initialized to zero. This assumes the system starts at rest. Adjust if you have initial conditions.
- Reading Input: Replace
INPUT_SIGNAL with the actual variable or function that provides the current sensor input.
- Writing Output: Replace
OUTPUT_SIGNAL with the variable or output register where the filtered signal should be sent.
- Data Types: Ensure that the REAL data type provides sufficient precision for your application. If higher precision is needed, and supported by the PLC, consider using DOUBLE or LREAL.
- Execution Rate: This code should execute at every scan cycle (every 1 second) to maintain the correct sampling rate.
3. Implementation Considerations
- Precision: Floating-point precision is crucial for filter stability and performance, especially with small coefficient values.
- Coefficient Signs: Pay attention to the signs of the coefficients in the difference equation. The feedback coefficients
a1 and a2 are subtracted in the equation.
- Stability: Ensure that the coefficients are calculated accurately. Small errors can lead to filter instability.
- Testing: Before deploying, test the filter with known signals to verify that it behaves as expected.
Summary:
You've derived the necessary coefficients for a second-order Butterworth filter and provided Structured Text code that can be implemented in an Allen Bradley PLC to filter a noisy level signal with a 0.01 Hz cutoff frequency at a 1 Hz sampling rate.