How-to
Port a PLC-5 filter routine to ControlLogix structured text
Short answer
The filter is a first-order lag whose only state is the previous filtered value. Give every filtered signal its own instance of one filter UDT that also holds the exponent and coefficient, translate the rungs to ordered assignments in a periodic task, and preload the state on the first pass. Prove the port with a step: 63.2% of the change after one time constant, about 95% after three.
On this page
The PLC-5 filter subroutine is a first-order lag, not branching logic: it computes a coefficient from the time constant, blends the new input with the previous filtered value, and returns the blend. Its only state is that previous value. The port is therefore data layout plus ordered assignments: one filter UDT holding the input, the state, the time constant and the two intermediates, one tag instance per filtered signal, and the rungs translated line for line into structured text that a periodic task executes at a fixed rate. Structured text, one of the programming languages IEC 61131-3 defines, runs statements in order, which is the same rung order the .RSP file already uses. Below are the type, the routine, a 100 ms worked example and the step check that proves the new output against the old.
What the .RSP rungs compute #
The subroutine takes three values - the value to filter, the previous filtered value and the filter's time constant - computes an exponent and a coefficient from the time constant, and returns the filtered value. Term by term that is the standard first-order lag:
Exponent = -SampleTime / TimeConstant
Coef = e ** Exponent
Out = Previous * Coef + Actual * (1.0 - Coef)
Previous = Out
Hold this form against the actual rungs before you trust the port, because the ladder decides two things the notation leaves open. One is the unit of the time constant: this port assumes seconds. The other is where SampleTime comes from: this port fixes it to the task rate instead of a measured scan time. The only value that must survive from pass to pass is the previous filtered value, which makes it per-instance state: one private copy per filtered signal, never a shared scratch tag.
A filter port is usually one subroutine inside a bigger controller swap, and the translate-then-prove order used here is the same order that drives migrating a MicroLogix 1500 machine to a current PLC.
Build one UDT per filtered signal #
Keep the three members from the original design - FilterValueActual, FilterValuePrevious and FilterTimeConstant - and add the calculated values, the output and a first-pass flag as members of the same type, FilterFlt. Loose controller-scoped intermediates would give every caller one shared copy, which is exactly the cross-contamination this layout prevents: each tag instance carries its own exponent, coefficient and state.
| Member | Type | Role |
|---|---|---|
| FilterValueActual | REAL | Raw input, written before each pass |
| FilterValuePrevious | REAL | The state: filtered value carried between passes |
| FilterTimeConstant | REAL | Filter time constant in seconds |
| FilterExponent | REAL | Intermediate: -SampleSec / FilterTimeConstant |
| FilterCoef | REAL | Intermediate: e to the exponent |
| FilterValueOut | REAL | Filtered result |
| FilterInit | BOOL | FALSE until the first pass preloads the state |
FilterInit gives the routine its own first-pass flag, so the preload does not depend on how the task or the controller mode change is configured. Create the tag at program scope as an array of FilterFlt with one element per filtered signal; this article uses indexes 0 through 4 as an example for five signals, so size the array to the channel count in your project.
Port the rungs in this order #
- Open the .RSP file and read the two ladder decisions: the unit of the time constant (seconds in this port) and the source of SampleTime (a fixed rate here).
- Create the FilterFlt type with the seven members in the table above.
- Create the program-scoped tag Flt as an array with one element per filtered signal.
- Create a periodic task, set its period to 100 ms, and put the program in it, so SampleSec is a constant 0.1 s.
- Enter the routine below in a structured text routine named FilterRoutine.
- Call FilterRoutine from the main routine with a JSR that passes no parameters: the array carries the inputs and results, so nothing moves through the call. Write Flt[n].FilterValueActual and Flt[n].FilterTimeConstant before the call and read Flt[n].FilterValueOut after it.
The routine #
SampleSec, Flt, i and e are this example's names: declare SampleSec as REAL at 0.1, i as DINT, e as REAL at 2.71828183, and rename them to suit the project. The ** operator is the exponentiation operator IEC 61131-3 structured text defines; if your rungs built the coefficient a different way, mirror the rungs instead, because the PLC-5 output is the target. Statement order is execution order, so the guard, the preload and the state store run exactly as written:
SampleSec := 0.1;
FOR i := 0 TO 4 DO
IF Flt[i].FilterTimeConstant > 0.0 THEN
IF NOT Flt[i].FilterInit THEN
Flt[i].FilterValuePrevious := Flt[i].FilterValueActual;
Flt[i].FilterInit := TRUE;
END_IF;
Flt[i].FilterExponent := -SampleSec / Flt[i].FilterTimeConstant;
Flt[i].FilterCoef := e ** Flt[i].FilterExponent;
Flt[i].FilterValueOut := Flt[i].FilterValuePrevious * Flt[i].FilterCoef
+ Flt[i].FilterValueActual * (1.0 - Flt[i].FilterCoef);
Flt[i].FilterValuePrevious := Flt[i].FilterValueOut;
ELSE
Flt[i].FilterValueOut := Flt[i].FilterValueActual;
Flt[i].FilterValuePrevious := Flt[i].FilterValueActual;
END_IF;
END_FOR;
The guard keeps a zero or negative constant from producing an invalid REAL and storing it as state; the ELSE branch passes the input through and refreshes the state, so the output recovers cleanly once a valid constant returns. Change 0 TO 4 to your own array bounds.
Worked example: 100 ms task #
Enter FilterTimeConstant = 2.0 s on one element and step FilterValueActual from 0.0 to 100.0. The arithmetic:
FilterExponent = -0.1 s / 2.0 s = -0.05 (seconds cancel, the exponent is dimensionless)
FilterCoef = e^(-0.05) = 0.95123
1.0 - FilterCoef = 0.04877, so each pass moves the output 4.877 percent of the remaining gap
First pass after the step: Out = 0.0 * 0.95123 + 100.0 * 0.04877 = 4.877
After 20 passes, one time constant: Out = 100.0 * (1 - 0.95123^20) = 100.0 * (1 - e^(-1)) = 63.21
After 60 passes, three time constants: Out = 100.0 * (1 - e^(-3)) = 95.02
Reaching 63.2 percent of the step at one time constant and about 95 percent at three is a first-order lag responding as designed, so those two trend points are the acceptance numbers for this port: 63 percent at the 20-pass mark and 95 percent at the 60-pass mark.
Prove the port and catch the classic faults #
One safety note before anything runs: a filter delays its signal by design, so confirm no stop, interlock or alarm consumes FilterValueOut, because the port's lag matches the PLC-5 only when the sample rate and the time-constant unit match.
Then prove it:
- Feed the same recorded input sequence to the old rungs and the new routine and compare outputs sample for sample; they should agree within float rounding.
- Apply the step from the worked example and read the one- and three-time-constant points off the trend.
- Set FilterTimeConstant to 0 online: the output follows the input directly and recovers when the original constant is restored.
- Cycle the controller to Run: the output starts at the input value, not at zero, because FilterInit preloads the state.
- Drive two array elements with different inputs and confirm the outputs stay independent.
| What you see | Why | Fix |
|---|---|---|
| Output ramps up from 0 after download | FilterValuePrevious was still 0 on the first pass | The FilterInit branch preloads it; make sure nothing sets FilterInit early |
| Several channels show one blended value | Every caller shares one tag | One array element per signal |
| Output sticks at a nonsense value | An invalid REAL, usually from a zero or negative constant, became the state | The guard passes the input through; restore a positive constant |
| Response speed drifts with program load | The routine runs in the continuous task, so sample time wanders | Move it to the periodic task and keep SampleSec equal to its period |
| Response far too fast or too slow | Time-constant unit differs from the .RSP rungs | Match the rungs' unit; the scans case sets SampleSec to 1.0 |
The decision for each case #
- One program filtering several signals: the program-scoped Flt array, one element per signal, a 100 ms periodic task with SampleSec = 0.1, and a zero-parameter JSR. That is this port.
- The same filter called from more than one program, or state you want packaged with the indexing hidden: the same members and arithmetic as an Add-On Instruction, one instance per signal, so no caller can touch another's state.
- A whole .RSP file to move: decide machine translation versus rewrite before porting rungs one at a time, which is the choice set out in converting PLC-5 to ControlLogix: translate utility versus rewrite.
- Never force the arithmetic through an IF/ELSE chain; guards are the only branches this filter needs, and there is no shared scratch tag in any of the three cases.
For the five-signal case this article built, the port is done when the FilterFlt elements 0 through 4 run in the periodic task and the trend shows the acceptance numbers: 63 percent at the 20-pass mark and 95 percent at the 60-pass mark.
Questions asked
How do I convert the time constant if the PLC-5 rungs counted scans instead of seconds?
Enter the constant in scans and set SampleSec to 1.0. The exponent comes out identical: -1.0 / 20.0 scans is the same -0.05 as -0.1 s / 2.0 s, so a 20-scan constant on 100 ms scans still reaches 63.2 percent of a step after 20 passes. Pick one unit and use it on every channel: a channel left in scans while the others run in seconds responds at the scan rate instead of the wall clock.
Can I store FilterValuePrevious as a DINT to save memory?
Keep it REAL. Rounding the state to whole numbers throws away exactly the fraction the coefficient is carrying: at the worked example's 4.877 percent per pass, a 5-unit input change moves the stored state 0.24 units per pass, which a DINT rounds to 0 forever, so the output stalls while the PLC-5 output crept up. The same applies to FilterExponent and FilterCoef, and to the output, since later comparisons run against float resolution.
The input is too noisy for a clean step. How do I check the response then?
Use the 10 to 90 percent rise time instead. A first-order lag crosses that band in ln(9), about 2.2 time constants: with the example constant, the output passes 10 percent of the step about 0.21 s after the step and 90 percent about 4.61 s, a rise of about 4.39 s. Read the same two crossings from the PLC-5 trend; if the new crossings differ by more than a scan or two, the sample time or the time-constant unit is wrong.
Two programs in different tasks need the same filter. Where does the state go?
Program-scoped tags are visible only inside their own program, so a program-scoped array serves one program and no other. Either make one controller-scoped array and have both programs index disjoint elements, or package the members and arithmetic as an Add-On Instruction and give each program its own instance. Before packaging, read the local-tag rules for structured and array types in the Logix 5000 Add-On Instructions manual, publication 1756-PM010, because a structured local tag is not treated like an InOut parameter.