Automation studio help: averaging analog sensor values every 2 seconds

afm

Member
Join Date
Aug 2023
Location
Tennessee
Posts
88
Hi, I have a bit of PLC experience in Siemens and have unfortunately been blessed with working on Automation Studio and B&R which has been a big learning curve. I am trying to collect I/O link analog sensor values and compare it to what the theoretical baseline value should be based on a formula. I have the sensor data reading in, but I want to collect and average values over a 2 second period, or collect X number of values into an array, average it, then use that value for further logic, then reset and repeat over every 2 second interval.

I have tried a few techniques with timers, counters, and MOVE blocks and the MTFilterMovingAverage block, but those methods have been unsuccessful. I am probably overcomplicating it, but the lack of resources on AS has made it difficult. Any suggestions on how to approach it?
 
Last edited:
there are many ways to skin this cat.

If you don't need a 2s-average value more frequently than every 2s, then you don't need an array, you can (i) sum values and count values over 2s, and then, when the 2s timer expires, (ii) divide the sum by the count to get the average when the 2s expires, and (iii) reset both sum and count to 0.


Or sum values over some large number of scan cycles and take that average.



If you want a 2s moving average, then you need an array, but everything could be mostly the same e.g.


(i) start with
(i.a) an array of 20 values initialized to 0,
(i.b) a sum of 0,
(i.c) a count of 0
(i.d) an index of 0

(ii) run a repeating 100ms timer
(iii) each time the timer expires
(iii.a) subtract the value of array[index] from the sum
(iii.b) write the reading value into array[index]
(iii.c) add the value of array[index] (or the reading) to the sum
(iii.d) increment index value by 1

(iii.e) if index value > 19, then assign 0 to value of index
(iii.f) if count value < 20, then increment count value by 1
(iii.g) divide sum by count, write result as value of average


I suspect that is what the MTFilterMovingAverage block is doing; perhaps it is using a FIFO and no index which simplifies the logic somewhat at the expense of speed.


On thing to be careful of is overflow and roundoff, but if the analog values are 16-bit integers and the sum is 32-bit, it is not likely to be a problem. Using a REAL for the sum could accumulate roundoff errors.

What language (ladder, ST, ?) is this being written in?
 
This is not complicated; post your code (Press PrintScreen to put the screen image into the copy-paste buffer; paste it into Microsoft Paint; crop and annotate; save as PNG; attach to a post).
 
Why not use a low pass filter? That doesn't require an array and can be updated and read from very flexibly. If there is a timed interrupt, then the averaged value can be updated at the rate of the timed interrupt. For instance. If the time interrupt occurs every 100 ms, there will be 20 values averaged in in 2 seconds. This can be taken to "extremes" by sampling every 10 ms so there is now 200 readings averaged in. With a little mathemagic one can calculate a filtered rate of change too.

Come on drbitboy, jump on the bandwagon. You know a low pass filter is simpler.
 
Why not use a low pass filter? That doesn't require an array and can be updated and read from very flexibly. If there is a timed interrupt, then the averaged value can be updated at the rate of the timed interrupt. For instance. If the time interrupt occurs every 100 ms, there will be 20 values averaged in in 2 seconds. This can be taken to "extremes" by sampling every 10 ms so there is now 200 readings averaged in. With a little mathemagic one can calculate a filtered rate of change too.

Come on drbitboy, jump on the bandwagon. You know a low pass filter is simpler.


Yes, of course it is. But they all do the same thing; all that changes are the weights and the length of the (implied) array.



OP asked for a moving average, and I am more interested in them learning summat than solving their problem. Give a man a fish, and you feed him for a day; teach a man to fish and ... (you never see him on the weekend).
 

Similar Topics

Hi, I have a PLC code that was written using AS v2.5.1. Unfortunately my AS v3.0 does not let me to open that file. Is there any chance that...
Replies
0
Views
3,235
Hi, I have been trying to run drive via Sysmac studio. I can ping the drive. I can see the logic bits going on/off as per command. But, drive is...
Replies
21
Views
570
Hello Experts, I recently stumbled with an issue about ABB VFD ACS800 connected with B&R PLC X20CP1484-1. This ABB VFD ACS800 somehow got...
Replies
0
Views
325
Hi, I would like to prepare a project for the operator panel and I do it like this: New project -> I choose the appropriate panel -> add object ->...
Replies
0
Views
428
Hi, I would like to prepare a project for the operator panel and I do it like this: New project -> I choose the appropriate panel, it seems to me...
Replies
3
Views
640
Back
Top Bottom