Modicon Running Average code required

otiovogon

Member
Join Date
Nov 2008
Location
Derbyshire
Posts
7
Just updated my customer from 984 to Quantum.
We have an analogue I/P which jumps around somewhat and I need to do a running average on it.
I seem to remember a code example in a Modicon Prog. Manual from a long time ago (might even have been 584) which used a FIN / FOUT combination to move the latest reading into say a 5 deep stack then average the 5 registers all in one scan.
I intend to use a toggling flag to solve the logic every other scan.
Any help would be very welcome as I don't have much time on site to de bug and I don't have a PLC here to play with.
Thanks
 
otiovogon:

You´are on the track, your approach is the right way to go, you have to decice how fast you capture data and put it into the queue ( using a timer is the best way), every time you put a new data into the queue sum what is inside the queue and divide the result by queue size, this way you get an average.
 
Thanks Brian, I had seen this thread but it's not quite what I'm looking for.
I think if I had a big table it would be my only option (to sum the elements every other scan then divide at the end)but in this case I'm only looking for a small table.
I'm controlling a weigher which fills rapidly but has a certain "bounce" in it so odd readings are spurious.
My idea is to use say a 5 element FIFO to push the latest reading in and delete the oldest then add the 5 elements and divide by 5 all in the same scan.
I know these things can take a bit of massaging on site to make them work so I wondered if anyone had a bit of tried and tested code which would fit the bill.
Thanks again
Much appreciated
John
 
The table averaging subroutine I refered to would take 5 scans to update the average value on a 5 register table. If your table FIFO update is every scan and you need instant response I can see your point. Otherwise 5 scans is fairly quick in most applications.

Good Luck.
Brian
 
Last edited:
Yes thanks Brian, perhaps this is the way to go.

It's a big plant with 2200 networks so the scan time is reasonably long.

I thought if I could read the analogue every scan and just smooth it a bit (say over 5 readings) then I wouldn't impact the responsiveness of the weigher too much.

Looks like I'll be doing some homework on FIFOs before some fiddling on site.

Thanks again for your input

John
 
Alternative filter

Can you do this in your PLC?

Old_Val = Filter_Val

Filter_Val = (20*analogue_in + 80*Old_Val) / 100

Then you only need 2 variables and can alter the filter effect easily.
 
Thanks Jason,

Elegant solution but not one open to me I'm afraid.

I only have First in and out blocks together with add and divide blocks.

Thanks for taking the time to reply, much appreciated.

John
 
Because 984LL scans a network from the "top to bottom" and then "left to right", careful placement of your FIN and FOUT functions may allow you to accomplish your averaging in one scan.
 
Thanks to all.
I will get the info out on FIFO blocks and sketch something out then take it to site and have a play.
Thanks again
John
 
A crude method I have used with a couple of variables X & Y
Compare your analog value to a variable (X) using the subtract block.
If analog > X add Y, if Analog < X subtract Y
Variable X can only change by Y each scan
Y can be fixed or adjustable depending on the magnitude of error
Roy
 
Old_Val = Filter_Val

Filter_Val = (20*analogue_in + 80*Old_Val) / 100

984 Ladder logic can easily perform this calculation. I have done similar many times. The standard multiply function gives a double precision result and the divide function can directly use this result (a double precision add can be tricky so you may have to resort to the EMTH functions to perform the double precision add). Or you could do it all in floating point using the EMTH functions
 
Thanks Roy.
This would indeed be a simple solution and would prove if the analogue junping around is causing our problems.
My take on your suggestion would be to take a copy of the analoge and compare it with the new reading on the next PLC scan. Then add an amount to the result if the new reading is bigger or take some away if its smaller then take a new copy of the analoge for the next scan.
Thanks
John
 
I think i'm missing the point but why not just use your filtering function on the analogue cards configuration. You can set the time
 

Similar Topics

i have plc modicon m258 its CANopen bus not running and give status on SoMachine v3.1 red triangle after i make download to it so please how...
Replies
4
Views
1,809
Hi, I've been asked to display the pump efficiency on our HMI system (iFIX). They have requested a kind of bar graph to indicate if it's running...
Replies
6
Views
2,451
Going to be Connecting a Siemens G120 over Ethernet to a Modicon CPU. Programing in Unity Pro. I am having a really hard time finding any sort of...
Replies
4
Views
2,244
Everything was working fine, but suddenly CPU went into error mode, and the ERR and TER LEDs lit up. Now I can't download or connect with the PLC...
Replies
0
Views
47
Hello All, Was hoping I could get a little help with Modicon ladder logic. This is the first time I have seen Modicon logic and currently trying...
Replies
6
Views
277
Back
Top Bottom