A code Newbie

Fishbone

Member
Join Date
Jun 2014
Location
Arkansas
Posts
5
I am needing to capture a value every one sec and then add 60 of those values together and divide by 60 to get the average of these. How do I write code for this?
 
Do you actually need to take 60 samples per minute? That seems like a lot. Also do you need to the average every 60 sec? Can you do a running average?

I will normally sample every 6 seconds and that with give you 10 samples per minute. I use a 6000 ms timer for that.

If you have to take a sample every second then you will have to use a 1000 ms timer.

No matter how fast you sample, every time the your timer is DONE you would add the value you are measuring to a total register and add a 1 to a counter with a preset of 60 (or 6). When the counter is done then you take the total register and divide by 60 or 6. That will give you a average over a minute.

If you can do a running average then instead of using a counter you can just add a 1 to a count register. Then take the total register and divide by the count register to get your average. It will do the average on every scan. Keep in mind that you may want to put a GEQ in front of the DIV instruction to keep the DIV from happening unless the count register is at 1 or above. That keeps the processor from dividing by zero.

Once you get started it goes pretty fast.
 
In Logix5000 Use a FIFO queue (FFL/FFU instruction pair) and the AVE function.

See example below. Once the queue is full the FFU is used to unload the oldest value from the array to make space for a new value. Note that there is a disadvantage to the code shown in that for the first minute the average is not going to be accurate.

See this post http://www.plctalk.net/qanda/showpost.php?p=260919&postcount=5 for two examples of how to do it so that the first minute average is correct and so that you don't have to compute the sum every time by keeping a running total.

tc061114.jpg
 
Last edited:
Quick Running Average

Here is a quick method for maintaining a running average.
If your required data set is 60 samples, then the total of the data set can be calculated by multiplying the previously calculated average by 60. Add a new sample to the data set by multiplying the old average by 59 then adding the new sample.

RunningAvg = (RunningAvg * 59 + Sample) / 60

This method provides a simple filter for analog signals. Here is sample code where the interval and number of samples can be easily modified.

RunningAvg.jpg
 

Attachments

  • RunningAvg_L5X.zip
    1,011 bytes · Views: 13
Be very careful with the quick running average. It will not give you a true average and it has a hysteresis that lasts a lot longer than 60 seconds. Running averages have their place (including as a filter) but they are not the same thing as an average over a rolling time period and it produces a different answer so choose the method that gives you what you actually want. If the plant manager wants to know average production rates then that is not your method. OTOH, if you are trying to deal with a level PV in a turbulent tank then its one way to deal with the problem. This has been hashed ad nauseum on the forum. Do a search for average.
 
The presented 'Quick Running Average' DOES NOT take into account just the previous 60, in this case, measurements.

In the scenario presented the last measurement contributes just 1/60th of the running average. Previous measurements provide the other 59/60ths. But if you look at the previous measurements the immediately previous one contributed 1/60th of THAT previous average. So a slightly smaller amount. This continues back quite a long way, in this particular scenario. A lasting change in the values will take a LONG TIME to show a significant effect in the running average (much longer than 60 samples).
 
You can easily simulate the hysteresis of the "Quick Running Average" with a spread sheet. Start with an imaginary process holding at a steady state average of 5. Introduce a disturbance where the PV jumps to 8 and stays at 8. After 60 samples the new time rolling average should be 8, but with the quick running average as presented the computed value is 6.8. Even after 300 samples, or five times the averaging period, the computed value still doesn't reflect the correct value.

If we were tracking parts made by an operator per minute over a rolling hour and at shift change an 8 part per minute operator replaced a 5 part per minute operator then you will have a coworker who is very unhappy with you.
 
Last edited:
And if you want rate within a couple of cycles, then just calculate rate at every cycle completion, and average the last few if you find the need. Just divide the last cycle time into the time per period, in your case minutes or 60000 milliseconds. Within two or three cycles, you will know the rate at which the machine is producing. This is very useful when manually adjusting speed of anything affecting the cycle rate for quicker feedback. It can also make scheduling and projection math more accurate.
 

Similar Topics

Hi there, I'm new to plc programming and was wondering why I get this error code when I run my simulation for these temperature sensors? What I'm...
Replies
1
Views
54
Hi All, Someone at work has put a PLC system on my desk, that's just been taken off an idle production line. He said "It's an S7 PLC. We don't...
Replies
10
Views
272
hello, I'm a student and for my final grade I have to repare a sepro robot. the only problem I have for now is that i have an error code 3...
Replies
0
Views
40
I received an email from a student with the following code attached. This is supposed to control a floodgate system, and supposed to be written...
Replies
23
Views
793
I have a machine which is undergoing upgradation. As part of the process two SEW drives are being replaced., existing Gen B with new Gen C. The...
Replies
3
Views
214
Back
Top Bottom