Ladder Logic 307: Data Average and Archive

Today’s post is in response to Yusuf Shaikh, who asked a question in a Facebook forum on PLCs. He asked “How to calculate average of drive current tag for every 15 seconds in Micrologix 1400 PLC?

Of course there are various questions that need to be asked about an application like this, and it is very difficult to explain something like this in text on Facebook. He did state that the current was a floating point value, which means it has been scaled from an Integer from an analog input. For the purpose of demonstration I used F8:0.

15 Second Pulse Timer

I have explained this rung previously both on this blog and in my book. Sometimes you have to move this rung to after the calculation logic, but I didn’t have time to download this and test it. Suffice it to say, if you don’t get values in the next few rungs, move this to the end of the logic.

“Cascade” of moves

I have explained this logic also before, it’s also in Advanced PLC Hardware and Programming, which discusses different ways of moving data. Since there are only 4 values to average, this is the easiest way.

Add 4 values for averaging

This logic simply adds the four floating point values together for averaging. It can be combined with the next rung, but I didn’t have a way to easily copy such a big rung, so I separated them. And yes, you can continuously overwrite F8:5 in successive rungs.

Divide for the final average value

Divide by 4 and you have calculated a running average of current for a one minute period that updates every 15 seconds. So hopefully that is what Yusuf was looking for.

The next few rungs assumes that I also want to archive the data, perhaps so I can trend it. So I am going to save the last 100 averaged values. one value every minute, continuously.

Rung 4 is a new pulse occurring every minute. Again, you may have to move this rung to the end. Unfortunately in a Micrologix and in RSLogix500 itself, you can only use the FFL and FFU instructions on Integers. So I reverse scaled the Floating point number, multiplying it by 100 with an implied decimal point. This allows values to be displayed on a touchscreen as XXX.XX, but you can’t use this technique if current values are higher than 327 amps or so, since the highest value allowed in a signed integer is 32,767.

This is the FIFO Load and Unload Instruction, the core of archiving the data. Newest values enter the stack pushing all of the other values down, until full. So this would keep a running list of your averaged current values for 100 minutes. You could also archive these values, average them again, or display them. The FFU is needed because when the stack gets full, it won’t let you put any more on. When you unload, you remove the oldest value.

I mentioned on the Facebook forum that answering questions like this is time consuming, and of course people charge good money for programming. At the same time, I do have this blog, and I’m stuck at home… and I like to teach… so there it is.

I hope that answers your question Yusuf!

Share
About

Electrical Engineer and business owner from the Nashville, Tennessee area. I also play music, Chess and Go.

4 Comments on “Ladder Logic 307: Data Average and Archive

    • You’re welcome Yusuf! I am always looking for ideas to post here, and things that come from people who have a real application are always best. If you or anyone else has any applications on Allen-Bradley or Siemens products let me know!

  1. I almost always use OUT = (IN + LAST(N-1))/N
    translated into Excel-ese: =(D12*($H$4-1)+C13)/$H$4

    or in anachronistic Ladder-ese, with the capability of dynamic filter constant adjustment:
    SUBTRACT (N ,1 )-> M
    MULT(OUT, M) -> OUTM
    ADD(OUTM, IN) -> OUTN
    DIV(OUTN,N) -> OUT
    unless of course one simply abandons ladder-bullshit and goes directly,
    not passing GO, not collecting $200 to IEC61131 Structured Text
    in which case GOTO Line 1

    • Hi Kurt, I understand your point about ST, so many people are pushing it on forums and Linked In. However…

      Many platforms, including RSLogix500 and Automation Direct’s DL2xx series (which the person this article was targeted to has), don’t have Structured text. And ST costs more if you want it on Siemens S7 or Allen-Bradley ControlLogix.

      I use ST when needed, but in my opinion the reason so many people are jumping on the ST bandwagon is that they took high-level coding classes in college and liked it. There ARE no good ladder classes in college, the only people who know ladder well are people who have used it extensively in industry. And they don’t teach or write books.

      I have written code for many multi-million dollar machines entirely in Ladder. AB Logix5000 lends itself to ladder, and many plants require it. Templates are written that way, and machine builders in the US have standardized on it. When using Siemens I use a lot of STL also, again, standards.

      I am probably going to write a nice controversial post on this soon, I’m sure it will get a lot of comments!