To implement a first-order low-pass Butterworth filter in an Allen Bradley PLC with a scan time of 1 Hz and a cutoff frequency of 0.00245 Hz, we'll derive the discrete-time filter formula and provide Structured Text (ST) code for implementation.
1. Deriving the Filter Formula
Continuous-Time Filter
The transfer function of a first-order low-pass Butterworth filter is:
[ H(s) = \frac{\omega_c}{s + \omega_c} ]
Where:
- ( \omega_c = 2\pi f_c ) is the cutoff angular frequency in radians per second.
- ( f_c ) is the cutoff frequency in Hz.
Differential Equation
The corresponding differential equation in the time domain is:
[ \tau \frac{dy(t)}{dt} + y(t) = x(t) ]
Where:
- ( \tau = \frac{1}{\omega_c} ) is the time constant.
- ( x(t) ) is the input signal.
- ( y(t) ) is the output signal.
Discretizing the Filter
To implement the filter in a PLC with discrete time steps, we discretize the differential equation using the exponential smoothing method. The discrete-time difference equation becomes:
[ y[n] = y[n - 1] + \alpha \left( x[n] - y[n - 1] \right) ]
Where:
- ( y[n] ) is the filtered output at the current time step.
- ( y[n - 1] ) is the filtered output at the previous time step.
- ( x[n] ) is the current input.
- ( \alpha ) is the filter coefficient.
Calculating the Filter Coefficient ( \alpha )
The filter coefficient ( \alpha ) is calculated based on the sampling period ( T ) and the time constant ( \tau ):
[ \alpha = 1 - e^{-T / \tau} ]
Given:
- Cutoff frequency ( f_c = 0.00245 ) Hz.
- Sampling period ( T = 1 ) s (since the scan time is 1 Hz).
- Time constant ( \tau = \frac{1}{2\pi f_c} ).
Compute ( \tau ):
[ \tau = \frac{1}{2\pi (0.00245)} \approx \frac{1}{0.0154} \approx 64.724 \text{ s} ]
Compute ( \alpha ):
[ \alpha = 1 - e^{-T / \tau} = 1 - e^{-1 / 64.724} ]
[ \alpha = 1 - e^{-0.01545} \approx 1 - 0.9847 \approx 0.0153 ]
Final Discrete-Time Filter Formula
[ y[n] = y[n - 1] + 0.0153 \left( x[n] - y[n - 1] \right) ]
2. Structured Text Code Implementation
Below is the Structured Text code to implement the filter:
// Variable declarations
VAR
// Constants
f_c : REAL := 0.00245; // Cutoff frequency in Hz
T : REAL := 1.0; // Sampling period in seconds
tau : REAL; // Time constant
alpha: REAL; // Filter coefficient
// Variables
x : REAL; // Input signal
y : REAL; // Filtered output
y_prev : REAL := 0.0; // Previous output (initialized to 0)
END_VAR
// Initialization block (execute once at startup)
IF NOT Init_Done THEN
// Calculate time constant
tau := 1 / (2 * 3.14159265359 * f_c);
// Calculate filter coefficient
alpha := 1 - EXP(-T / tau);
// Set initialization flag
Init_Done := TRUE;
END_IF
// Main program loop (execute every scan cycle)
// Read the current input signal (replace with actual input)
x := <Your_Input_Signal>;
// Apply the filter
y := y_prev + alpha * (x - y_prev);
// Update the previous output for the next cycle
y_prev := y;
// The variable 'y' now contains the filtered output
// You can use 'y' as needed in your program
Explanation of the Code:
-
Variable Declarations: We declare variables for the cutoff frequency f_c, sampling period T, time constant tau, filter coefficient alpha, and input/output signals x, y, and y_prev.
-
Initialization Block: This block calculates the time constant tau and filter coefficient alpha based on the given cutoff frequency and sampling period. The Init_Done flag ensures this computation runs only once during initialization.
-
Main Program Loop:
- Reading Input:
x should be set to the current value of the input signal you wish to filter.
- Filtering: The filter equation
y := y_prev + alpha * (x - y_prev); applies the first-order low-pass filter to the input.
- Updating Previous Output:
y_prev is updated to the current output y for use in the next loop iteration.
- Using Filtered Output: The variable
y contains the filtered signal and can be used elsewhere in your program.
Notes:
- Input Signal: Replace
<Your_Input_Signal> with the actual input reading from your PLC (e.g., an analog input).
- Initialization Flag: You may need to declare and manage an initialization flag (
Init_Done) to ensure the initialization block runs only once. This depends on your PLC's programming environment.
- Constants: Since the cutoff frequency and sampling period are constants, you might choose to precompute
alpha and assign it directly if your application doesn't require adjusting f_c at runtime.
- Stability and Accuracy: Using
alpha = 1 - EXP(-T / tau) provides better accuracy, especially when T is not much smaller than τ.
Conclusion:
By using this filter equation and implementing it in Structured Text, you can effectively filter out noise from your signal using a first-order low-pass Butterworth filter in your Allen Bradley PLC with a 1 Hz scan time.