Ladder logic 402: Data manipulation and filtering

Once values from an analog card have been captured, there are methods to filter the data and methods to simulate a filter using Microsoft Excel before it is coded into a PLC.

03/29/2016


The prior post on ladder logic discussed using a programmable logic controller (PLC) to capture values from an analog card. Once those values have been captured, there are methods to filter the data and methods to simulate a filter using Microsoft Excel before it is coded into a PLC. There are several filtering algorithms that can be found on the Internet currently being used on a PLC (see links below). For the formulas, the same variable names were used even though they differed extensively in the various posts:

N = New value calculated and substituted for the input value. In other words, the calculated filtered value.

L = The previous captured input value.

F = The previous calculated filtered value.

k = The manipulated factor for all calculations (sometimes known as Alpha).

i = The current analog input value.

Avg = A running average over x number of previous samples, (3 for x was used in this example.)

The formula for a first order filter is New Filtered = Last Filtered + Factor * (Input - Last Filtered).

This equates to N=F+k(i-F) according to the variables listed above. The actual formula uses Alpha instead of k. Results from this formula are listed as Formula 6 below in the simulation.

To simulate a signal analog values were listed in one column on an Excel spreadsheet. These values are typical for a 13-bit signed analog signal from a PLC card; notice that they increment or decrement by a factor of eight. This would mean that on a 0 to 10v or 0 to 20mA signal with a range of 0-32,767, there would be 4096 possible values for the signal. The other half of the range, -32768 to -1, is not obtainable from the field device.

Simulation of a signal with listed some analog values in one column on a Microsoft Excel spreadsheet. These values are typical for a 13 bit signed analog signal from a PLC card; notice that they increment or decrement by a factor of eight. Courtesy: Frank

The first column shown above are the input values. The value of 20168 simulates a big noise spike that the user might want to filter for. The following columns show results after using the various filtering formulas found. The reason the input column has earlier values in it is to ptovide a running average of up to five values in the formulas. Formula 6 (shown below) also needs to use the previous filtered value, so that's why it has an extra value in it also.

This is a key with the variables listed and the formulas. The k values are linked to cells used in the table, so they can be changed as needed. Courtesy: Frank Lamb, Automation Primer

This is a key with the variables listed and the formulas. The k values are linked to cells used in the table.

Formulas 1 to 3 in graph form after they have been computed. Courtesy: Frank Lamb, Automation Primer

Formulas 4 to 6 in graph form after they have been computed. Courtesy: Frank Lamb, Automation Primer

Formula 1 actually looks like it makes the signal worse. Formula 2 and Formula 4 did nearly the same things; in the instructions Formula 2 was "k=1.0, no filtering, k=0, output freezes." Formula 4′s instructions said "k=1.0, no filtering, 2.0, 4.0 and 8.0 give increasing amounts of filtering with more dampening, but more delay."

Formula 3 is a straight running average, and seems to actually work pretty well. The current and previous values were used for an average of three; when using more there was more lag. Formula 5 also uses a running average. Three samples were used in the average also even though the intent was to use four or five. When four or five were used, the delayed reaction (lag) got worse.

The formal definition of a first order filter is represented by Formula 6. Sometimes this is known as a first derivative filter; if the points are equated to position, this would represent a velocity based filter. A second order filter would then represent Accel/Decel and a third order filter would represent "jerk."

The filters would work off of the delta between the current and previous values. Simulating formulas can be done very easily in Microsoft Excel, but the user may want to check things out before coding it into the PLC as illustrated by Formula 1.

Putting a formula into PLC form will vary a lot between platforms. In ladder logic, a "calculate" type command lets the user type the formula in directly, though you may still need to use first in, first out (FIFOs) instructions, especially when averaging.

Frank Lamb is the founder of Automation Consulting Services Inc. This article originally appeared on the Automation Primer blog. Automation Primer is a CFE Media content partner. Edited by Chris Vavra, production editor, Control Engineering, CFE Media, cvavra@cfemedia.com.

ONLINE extra

For more articles on PLCs and programming, see related articles below.



WILLIAM , CA, United States, 05/04/16 10:15 AM:

Simplest is #6, usually written: xf = w*x + (1-w)xfL, where xf = filtered value, x = current reading, xfL = last xf (at last sample), w= weighting factor. This approximates an analog 1st-order low-pass filter (R-C) and w can be related to a time constant. Simplest because you need store only the last value. Many engineers term "running average", but Statistics terms "exponential averaging". I term #3 "block average" or "moving window average".
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by...
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
The Engineering Leaders Under 40 program identifies and gives recognition to young engineers who...
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
Sensor-to-cloud interoperability; PID and digital control efficiency; Alarm management system design; Automotive industry advances
Make Big Data and Industrial Internet of Things work for you, 2017 Engineers' Choice Finalists, Avoid control design pitfalls, Managing IIoT processes
Engineering Leaders Under 40; System integration improving packaging operation; Process sensing; PID velocity; Cybersecurity and functional safety
This article collection contains several articles on the Industrial Internet of Things (IIoT) and how it is transforming manufacturing.

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

SCADA at the junction, Managing risk through maintenance, Moving at the speed of data
Flexible offshore fire protection; Big Data's impact on operations; Bridging the skills gap; Identifying security risks
The digital oilfield: Utilizing Big Data can yield big savings; Virtualization a real solution; Tracking SIS performance
click me