Rolling average of a DINT tag

scushman32

Member
Join Date
Nov 2014
Location
Sarnia
Posts
5
Is it possible to set up a rolling average of a DINT tag using a AB 1756 controllogix plc that's programmed using ladder logic? We seen that there is a MAVE block in function block programming, is there an equivalent in ladder logic?


We want to set up an alarm on our VFD output speeds to show that pumps are deteriorating anyone have any ideas for us? maybe an average isn't the best way to go.
 
I've programmed a rolling average with a MicroLogix 1400 in ladder. It should be the same? I haven't got the program here but this is how i think it was done:

Make a array of x number of dint, in this example we take 10.
Make a variable to use as "total value" of the array
Then you should determine how often you want to take a sample

So everytime you want a sample:

Subtract "total value" - "10th array value"
shift array to the right
add the "sample value" at the first array
Divide "total value"/10 (as in you have 10 variables in the array) = this result is the average
 
In Logix 5000 it's even neater...

Use an FFL/FFU pair to load data in and out of an array, and then use the standard AVE instruction on the array.

The order is somewhat important in order to get it to respond the right way, but it's reasonably simple. Each time you want to calculate the average:

1. Check if the array is full of data (FFL .DN bit set). If so, execute FFU to unload the oldest data
2. Load the newest data into the array using the FFL
3. Perform the AVE on the array, using the .POS of the FFL as the .LEN of the AVE (this prevents empty array elements from throwing out your average calculation, though of course in theory, once your array is full, it should never empty back out. So you could consider that part optional)
 
Come to think of it, you could probably do it the same way in a Micrologix 1400. I'm fairly certain it supports at least FFL/FFU, if not AVE. You could substitute a CPT for the AVE if it's not available in any case; it's a simple calculation.
 
No need for FFL/FFU and all the complications that inevitably ensue.

Just use a COP (or CPS) to shift the samples array, MOV the new reading into the vacated "slot", then AVE the array.

Example : An average of the last ten readings in array Data[0..9]

CPS Data[9] Data[8] 9
MOV NewData Data[9]
AVE Average Data[0] 10

This method of averaging an analog signal will always suffer from an erroneous or spurious reading taking n cycles to be removed from the averaging.

Another technique I came across a long time ago was to insert a SoRT before the averaging, then only averaging the middle n-2, or n-4 values, thus effectively always removing "spikes" from the averaging.

This however, is a poor man's attempt at a proper filtering algorithm, such as Kalman filtering, but has been used most effectively on analog signals that fluctuate wildly.
 
won't that fault the controller?

YES !! I got it wrong...

The CPS should have read

CPS Data[1] Data[0] 9

....which does the following in this order...

Data[1] -> Data[0]
Data[2] -> Data[1]
Data[3] -> Data[2]
Data[4] -> Data[3]
Data[5] -> Data[4]
Data[6] -> Data[5]
Data[7] -> Data[6]
Data[8] -> Data[7]
Data[9] -> Data[8]


My apologies, and thanks for pointing it out.

The reason for moving the data "backwards" in the array is to prevent the array being filled with the data from Data[0], as the CPS would do...

Data[0] -> Data[1]
Data[1] -> Data[2]
Data[2] -> Data[3]
Data[3] -> Data[4]
Data[4] -> Data[5]
Data[5] -> Data[6]
Data[6] -> Data[7]
Data[7] -> Data[8]
Data[8] -> Data[9]
 
Last edited:
No need for FFL/FFU and all the complications that inevitably ensue.

Just use a COP (or CPS) to shift the samples array, MOV the new reading into the vacated "slot", then AVE the array.

Example : An average of the last ten readings in array Data[0..9]

CPS Data[9] Data[8] 9
MOV NewData Data[9]
AVE Average Data[0] 10

This method of averaging an analog signal will always suffer from an erroneous or spurious reading taking n cycles to be removed from the averaging.

Another technique I came across a long time ago was to insert a SoRT before the averaging, then only averaging the middle n-2, or n-4 values, thus effectively always removing "spikes" from the averaging.

This however, is a poor man's attempt at a proper filtering algorithm, such as Kalman filtering, but has been used most effectively on analog signals that fluctuate wildly.

Well now I've spent the morning reading about Kalman filtering heh. Have you used anything like that in a PLC application before? Seems like quite a bit of setup, without a ton of improvement over a tuned MAVE. Maybe I'm just not seeing it?
 
Just make AOI of the MAVE and use it in ladder

I know this is a bit of a cop-out for this question, but whenever I see a useful function that exists in FBD that I want to use in ladder, I just create an Add-on instruction wrapper for it and then use an instance of that in ladder. Since it's relatively easy to do that (assuming you have the FBD license that is) it's always made me wonder why Rockwell doesn't have all instructions in all languages...seems to me there is no reason not to, it all compiles down to same assembler under the hood...must just be marketing...has to be some reason to get people to pay for that license!
 
Well now I've spent the morning reading about Kalman filtering heh. Have you used anything like that in a PLC application before?

Haven't got my head around implementing it in a PLC application yet - to be honest the math looks a bit strange and incomprehensible to me....

I'm doing a fair bit with 5.8GHz receivers (R/C models with FPV camera), and working on a tracking antenna to receive the video signals from a distance. I've always suffered from "noise", perhaps from reflected signals, or multiple signal paths, from the software getting a "fix" on where it should be pointing, but applying Kalman filtering is showing great promise.

You've read up on it, don't you think it is a great algorithm... "whoa, hold on, is that a real measurement, or should we just factor it in"... ??
 
Well now I've spent the morning reading about Kalman filtering heh. Have you used anything like that in a PLC application before? Seems like quite a bit of setup, without a ton of improvement over a tuned MAVE. Maybe I'm just not seeing it?

MAVE will include ALL of the "spurious" value in all calculations until it is historically irrelevant.

Kalman says "doesn't look like this is right, let's give it a low confidence value", the subsequent averaging takes these confidence values into account.

Basically Kalman filtering is using history to deduce the likelihood of subsequent readings being valid - I like the thinking behind it....
 
The Madgwick or Mahoney filters are used extensively in Arduino for filtering of the data from IMU's as the load on the processor is way less and the Kalman filter offers only slightly better performance, but with better/faster boards like the Teensey 32 bit and double precision floating maths, people are now able to use the Kalman filter to get as good as performance as possible.
Not sure how well a PLC will do Kalman filter, are they fast enough.
Link showing the speed of some of the Arduino boards doing maths
https://hmbd.wordpress.com/2016/08/24/speed-comparisons-for-arduino-unonano-and-due/
 
Last edited:
The Madgwick or Mahoney filters are used extensively in Arduino for filtering of the data from IMU's as the load on the processor is way less and the Kalman filter offers only slightly better performance, but with better/faster boards like the Teensey 32 bit and double precision floating maths, people are now able to use the Kalman filter to get as good as performance as possible.
Not sure how well a PLC will do Kalman filter, are they fast enough.
Link showing the speed of some of the Arduino boards doing maths
https://hmbd.wordpress.com/2016/08/24/speed-comparisons-for-arduino-unonano-and-due/

In terms of speed, the A/D conversion rate and I/O update exchange rate of the analog input module used would be the limiting factor, the maths would be much much faster.

Are you saying Kalman is "the best" ?

I'd love to see a Kalman AOI written for Logix5000, but the maths involved is beyond me. I know of one person on this forum who might have the skills, but I'm not goading a response.
 
Yes the Kalman is the best available filter it was designed for the space industry.
But as l said it is very processor hungry.
I don't profess to be a code warrior but the beauty of c+ is that the arduino madgwick and mahoney filters are all well documented libraries and l would imagine someone of your skills could adapt either of these filters.
I have seen better tilt compensation on a IMU used as a 9 degree of freedom compass ( 3 axis Accelerometer, 3 axis Gyro, 3 axis Magnetometer) using the Madgwick filter over the Mahoney.
Attached the Madwick library, so this is for quaternions to get the 3D position of the IMU.
Not sure how much experience you have had with Arduino, but there are other libraries that are called in the filter function, i.e Maths, which need to be included to be able to get the final result. Enjoy
P/S I will add that the Arduino community is very heavily weighted to offering help and is 100's of times larger than the PLC community and l believe a lot more intellectual projects covering just about everything, as an example, l don't think there are many drones flying around that have AB PLC'c in them (maybe Military? but l doubt that (or will be corrected))
 
Last edited:
Yes the Kalman is the best available filter it was designed for the space industry.
But as l said it is very processor hungry.
I don't profess to be a code warrior but the beauty of c+ is that the arduino madgwick and mahoney filters are all well documented libraries and l would imagine someone of your skills could adapt either of these filters.
I have seen better tilt compensation on a IMU used as a 9 degree of freedom compass ( 3 axis Accelerometer, 3 axis Gyro, 3 axis Magnetometer) using the Madgwick filter over the Mahoney.
Attached the Madwick library, so this is for quaternions to get the 3D position of the IMU.
Not sure how much experience you have had with Arduino, but there are other libraries that are called in the filter function, i.e Maths, which need to be included to be able to get the final result. Enjoy
P/S I will add that the Arduino community is very heavily weighted to offering help and is 100's of times larger than the PLC community and l believe a lot more intellectual projects covering just about everything, as an example, l don't think there are many drones flying around that have AB PLC'c in them (maybe Military? but l doubt that (or will be corrected))

I have implemented kalman filter in my tracking antenna, it was easy as it was just a single .h file to include, no additional maths libraries needed.

Actually I've just opened up the documentation file, and it states...

"A simplified one dimensional Kalman filter implementation for Arduino.<br>
It is based on: http://interactive-matter.eu/blog/2009/12/18/filtering-sensor-data-with-a-kalman-filter/ and is actually a low pass filter for one single variable (but still very useful)"

And here's the formulae it says it uses...

Kalman Formulas
---------------

x = x
p = p + q;
k = p / (p + r);
x = x + k * (measurement – x);
p = (1 – k) * p;

Where:
q = process noise covariance
r = measurement noise covariance
x = value of interest
p = estimation error covariance
k = Kalman gain


So it is very much simplified, but I am achieving great results with it, even so.
 

Similar Topics

Does anyone have any logic for Allen Bradley for parts per minute, and rolling averages. Im trying to write logic for uptime / downtime /...
Replies
6
Views
5,337
Hello, I a would like to create a rolling average for a flowmeter signal. I'm quite new to Siemens Step 7 - does anyone have a FB or can paste in...
Replies
5
Views
8,978
Afternoon all, I'm working on setting up a large excel recipe table for porting updates through the Linx Gateway RTD/DDE function into my recipe...
Replies
2
Views
110
Compactlogix controller, program has 28 conveyors that use TON's to start the conveyors. The TT sounds a warning horn during start and the DN...
Replies
10
Views
485
I'm a Manufacturing Engineer with limited PLC experience and I am doing some research for our Controls Department. Does anyone have experience...
Replies
4
Views
177
Back
Top Bottom