Announcement

Collapse
No announcement yet.

Using 'Average' Instruction in a LOOP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts


  • Using 'Average' Instruction in a LOOP

    Hi,

    Has anyone else had trouble with this? I have an array of values and I was a running average of each individual value over a 15min period... when I place the average instruction in a loop, all the outputs match... it does not individually process them.

    Thanks


  • #2
    Can you post your code? It sounds like a timing issue with the scan.
    Regards,
    Garry

    Comment



    • #3
      Hi, Sure thing.

      The code is below. The issue is that the entire STEL and TWA array end up with the same value.


      Click image for larger version

Name:	Capture.PNG
Views:	112
Size:	5.0 KB
ID:	122358

      Thanks!

      Comment



      • #4
        The Average Instruction is in a loop of 64 iterations.
        All of these instructions will loop 64 times in one scan, which is a small amount of time. Probably just a few milliseconds.

        Since the instruction starts and stops in just one scan, it can never get the minimum amount of time to make a change to the output.
        Does the code work if left out of a loop?

        From the help file.
        This instruction requires one times the Average Time Constant in order for 63% of an input change to be achieved at the output.
        It also requires five times the Average Time Constant value in order for 100% of the input change to be achieved at the output.

        Comment



        • #5
          Originally posted by RogerR View Post
          The Average Instruction is in a loop of 64 iterations.
          All of these instructions will loop 64 times in one scan, which is a small amount of time. Probably just a few milliseconds.

          Since the instruction starts and stops in just one scan, it can never get the minimum amount of time to make a change to the output.
          Does the code work if left out of a loop?

          From the help file.
          This instruction requires one times the Average Time Constant in order for 63% of an input change to be achieved at the output.
          It also requires five times the Average Time Constant value in order for 100% of the input change to be achieved at the output.
          Yes, the goal here is to generate 64 new averages from the 64 PV values.

          The code works if I explode this loop into rungs. (The code below is an example and is now my workaround... I suspect that there is background memory which is not exposed keeping count of how many times the instruction is executed in the given time constant. This is required for calculating an average without actually building an array, however, since it's not exposed, I cannot index it in the loop... again, this is just a suspicion)

          Click image for larger version

Name:	Capture.PNG
Views:	125
Size:	17.6 KB
ID:	122362

          Thanks for the responses so far!

          Comment



          • #6
            Could you build your own own average instruction with 2 dimensional arrays and shift registers and math instructions? I have an idea in my head but am not sure if it will work.


            edit
            Help topic P112 in the productivity manual has the formula that the average instruction uses. You could probably recreate it in the math instruction with the indirect addressed tags
            Last edited by z28z34man; 05-07-2019, 06:10 AM.

            Comment



            • #7
              The formula looks like a form of the Exponentially Weighted Moving Average (EWMA) filter. There are other ways to write the filter that don't use the scan time and time constant, but it always depends on the sampling rate. See Google...

              Comment

              Working...
              X