Averaging numbers

saultgeorge

Lifetime Supporting Member
Join Date
Jul 2015
Location
Detroit
Posts
535
Hi, folks. Thanks for taking the time to read this.
I need to average 5 numbers, they being an accumulated value (mSec) from a timer.
My director of controls and the customer don't really want me to use a compute (CPT) or the average (AVE) instructions in rs5000. So far I have tried using copies and moves to get the value in a DINT[6] array, using 1 through 5 to hold the ever changing values and then a divide (DIV) to average the results. I can't figure out how to get the total of the 5 values into position 0 in the array easily, or even how to get the 5 added up, and most importantly how to write a value to each bit in the DINT[6]. If I write to the same position in the array every time, will the existing value in there move to the next position, like a musical chairs type thing?
The time comes from a process that gets timed from when it leaves home to at home. When it sets the "at home" bit, it moves the .acc to a member in a UDT and resets the timer.
Any ideas?? Thanks, guys :cry:
 
Sooo... Don't use a screwdriver for a screw, or a wrench for a bolt... Gotcha.

Use a FIFO with 5 elements. That will require defining up the control block for the FFL/FFU instructions, defining a one-shot trigger to load, determining based on the one shot and the control block .DN bit when to unload, then simply add up all 5 values in the FIFO Queue every time unload is triggerred, divide by 5 (lose some data for integer math) and you are done.

Simple.
 
Why would your 'Director of Controls' or the customer care which way you accomplished this?

The only person I can think of that would want this done some way other than the normal, easy way would be a PLC teacher that wants you to learn, not be told, different aspects of PLC programming. I.E. - homework

My 2 cents.
 
XIC RUNNING XIO cyclecomplete TON TIME
GEQ N 5 MOV 0 N
xic cyclecomplete ons[0] mov time.acc avgtime[n]
GEQ avgtime[n] 0 DIV avgtime[n] 5 AVGCYCLETIME
xic cyclecomplete BST add n 1 n NXB MOV 0 TIME.ACC BND

I can't check syntax because I don't have RSL5k on my home computer.
the first 5 cycles will be wrong but you can tweek the divisor to only count actual cycle times by changing it to divide by the number of samples.
 
Sooo... Don't use a screwdriver for a screw, or a wrench for a bolt...
Not exactly. For me, it's more like "Don't use an impact driver in our shop".
I prefer myself to use basic instructions, because all the arguments are visualized right at the spot for monitoring, the code is easily transferable to different platforms, and often executes faster than the same logic using advanced instructions.
Also, an occasional user working on multiple platforms (like an average maintenance guy) does not have to browse through manuals to understand the syntax of advanced instructions.
For 5 values only, my FIFO definitely would be 5 MOV instructions.
These are, however, programmer's arguments, not usual director's or customer's.
 
Last edited:
I am struggling to understand how one instruction, with the mnemonic name AVE, and the description "Average File" can be less clear what it's function is to a casual onlooker, than a bunch of instructions adding several values then dividing by another number...
 
It's not too difficult to realize, that AVE stands for average (although the "Average File" description is somewhat confusing; I would say "Average Value").
The syntax and functionality, however, are not that obvious.
Here is the one from Omron: AVG D0 &10 D10.
Unless you are an Omron expert, try to describe what and how it does, what is the source and what is the destination of the data, before looking into the manual.
Then try the same for Mitsubishi MEAN D0 D10 K10.
And how about Panasonic MEAN DT0 DT10 DT20 ?
And only one of the source words will be monitored directly in the instruction.
Not to mention controllers with no instruction for averaging.
 
It's not too difficult to realize, that AVE stands for average (although the "Average File" description is somewhat confusing; I would say "Average Value").
The syntax and functionality, however, are not that obvious.
Here is the one from Omron: AVG D0 &10 D10.
Unless you are an Omron expert, try to describe what and how it does, what is the source and what is the destination of the data, before looking into the manual.
Then try the same for Mitsubishi MEAN D0 D10 K10.
And how about Panasonic MEAN DT0 DT10 DT20 ?
And only one of the source words will be monitored directly in the instruction.
Not to mention controllers with no instruction for averaging.

The OP specifically mentioned rs5000

saultgeorge said:
My director of controls and the customer don't really want me to use a compute (CPT) or the average (AVE) instructions in rs5000

I see nothing wrong with "Average File" - because it is a "File" or Array of numbers that will be averaged - the starting element is specified, and the length, so it's not hard to determine which source data is being acted upon.
 
Here is the one from Omron: AVG D0 &10 D10.
Unless you are an Omron expert, try to describe what and how it does, what is the source and what is the destination of the data, before looking into the manual.
I'm not an Omron expert and I haven't checked the manual.
I interpret it to mean take the average of the values in ten memory locations D0 through D9 and put the result in D10. Is that correct?
 
The OP specifically mentioned rs5000
And I specifically mentioned a maintenance guy not specializing on any specific platform, and far from being an expert in any one.

I'm not an Omron expert and I haven't checked the manual.
I interpret it to mean take the average of the values in ten memory locations D0 through D9 and put the result in D10. Is that correct?
Now check the manual and you will be surprised.
It averages the D0 value through 10 scans, writes the result into D10 and sets the done flag bit in D11.
Until the flag is set, the D0 value is copied to the D10 unchanged.
Not so obvious, right? Despite the clear meaning of the "AVG".
 
Last edited:
You're right, it's not obvious. Which is why clear rung comments are important.
Of course, if you had posted it as AVG D0 &10 D2, I probably would not have interpreted as I did, although I doubt if I would have come up with the ten scans thing.
 
And I specifically mentioned a maintenance guy not specializing on any specific platform, and far from being an expert in any one.


Now check the manual and you will be surprised.
It averages the D0 value through 10 scans, writes the result into D10 and sets the done flag bit in D11.
Until the flag is set, the D0 value is copied to the D10 unchanged.
Not so obvious, right? Despite the clear meaning of the "AVG".

Are you saying that to average 100,000 values it will take 100,000 scans ??? Or is there a "do it all now" mode.

As for the maintenance guy, I'm sure he would be happier with instructions that do what they say they do, as and when they are executed. There's no "done" bit to test to validate the result is accurate. "It does what it says on the tin"
 
And I specifically mentioned a maintenance guy not specializing on any specific platform, and far from being an expert in any one.


Now check the manual and you will be surprised.
It averages the D0 value through 10 scans, writes the result into D10 and sets the done flag bit in D11.
Until the flag is set, the D0 value is copied to the D10 unchanged.
Not so obvious, right? Despite the clear meaning of the "AVG".

/rant on
Then don't use a PLC at all. Use discrete relay logic. The time for "Bubba" to be diving into PLC code is long over. Write the program for the process, verify it, commission it, done. Bubba doesn't need to know ANYTHING AT ALL about the actual logic.

If you are intent on dumbing things down, then don't use any analogs, Bubba probably can't use a meter for anything but "Power/No Power". Don't use any process functions, PID's, Ramps, Math (Bubba ain't so gud at figgers). Don't use any networks or communications (Bubba can barely manage to dial a phone). Don't use an HMI in any form (it can't be a button if it ain't gots no wires). In short, don't use any modern PLC. Go find an old Modicon 084, and code only according to what was possible with that.

/rant off
 

Similar Topics

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...
Replies
5
Views
1,360
The PLC code calculates oil flow rate and outputs it on D420. It is represented on the HMI as a vertical bar. Problem is that the operator says...
Replies
33
Views
4,897
Hello, I am working with a laser encoder that is providing the speed of a fiber strand to our AB 1756-L61 PLC. In order to calculate the amount...
Replies
3
Views
3,132
Hi ;Configure A1s64AD card in slot 3 is as attached. This is averaging count. my signals are coming in at channel 3 and 4. There are spiking so i...
Replies
1
Views
1,630
Hi folks, First and foremost, credit where credit's due, thank you daba for the averaging sample provided in a separate post back in 2013. I've...
Replies
6
Views
3,902
Back
Top Bottom