Add elements of an array Logix5000

tworst

Member
Join Date
Mar 2016
Location
x
Posts
15
I have an array with 24 elements in it and 3 shifts in a day. The elements represent downtime for each hour in the day. I want to add the various element of the array for each shift period. For example:

Elem 0-7 A shift
Elem 8-15 B shift
Elem 16-23 C shift

Is there a function that will execute on a single scan to pick out the correct hours and add them up? I am using Logix5000 on a Contrologix PLC.
 
You could use a compute (CPT) statement and add it that way. Probably the simplest way to do it.

You could also do a FOR loop but that's more complicated that it needs to be.
 
The FAL instruction is made for this.

Doug - After I responded originally to this post my boss assigned me a project where I had to add up almost the exact same thing as the OP. The FAL worked great. Thanks for that. While I would probably still use a CPT for adding together only a few elements, for larger arrays the FAL is a great tool to have.
 
You could also use the AVE (Average) instruction, it might be faster....

There isn't an instruction to give you the SUM of values in an array, but there is the AVERAGE instruction...

AVERAGE = SUM / LENGTH

So, multiply the AVERAGE by the LENGTH, gives you the SUM
 
The FAL instruction is made for this.

I looked at the FAL instruction, but it looks like it only does incremental mode and I would have to loop around it. I was hoping for just a direct calculation of the sum. Also, my shift times change, so I have to use indexes for the shift time and match that to array elements.

If you don't mind, could you post the code you used with the FAL to get the sum?

I will also look at the AVG/# ELEM method and see if that might be easier.

Thanks
 
I looked at the FAL instruction, but it looks like it only does incremental mode and I would have to loop around it. I was hoping for just a direct calculation of the sum. Also, my shift times change, so I have to use indexes for the shift time and match that to array elements.

If you don't mind, could you post the code you used with the FAL to get the sum?

I will also look at the AVG/# ELEM method and see if that might be easier.

Thanks

This adds up the hourly totals over a 24 shift:

Datatypes are all DINTS, other than the control. The cv_HourlyCycles tag is a DINT[24] array. The FAL can take more than one scan to complete which is why I'm using the temp value and only moving the summed value into the working register when the FAL is done.

Basically, what I'm doing is initializing a tag to 0, then adding each array element to it (creating a running total), then moving that tag into my answer when the adding is done.

Code:
DINT temp;
temp = array[0] + temp;
temp = array[1] + temp;
and so on...

Untitled.jpg
 
I looked at the FAL instruction, but it looks like it only does incremental mode and I would have to loop around it. I was hoping for just a direct calculation of the sum. Also, my shift times change, so I have to use indexes for the shift time and match that to array elements.

If you don't mind, could you post the code you used with the FAL to get the sum?

I will also look at the AVG/# ELEM method and see if that might be easier.

Thanks
Don't have any code. Don't have a CLX. But, the FAL functions like it did in the PLC-5. Choose ALL mode rather than INCREMENTAL.

You can manipulate the length and POS parameters to control which elements are used in the instruction.
 
On closer inspection, I did see you could change from incremental to all so I will give that a try.

I will use the FAL instruction to add up downtime in shifts over the 7 days of the week. Problem now is that I can not accurately get the DOW. First I used the DOW subroutines from sample code until I learned there is an error due to rounding when using on a control logix processor. So then I used the T_Dow ladder instruction. It kind of works, in that I get the right day of week, but it updates the changeover from one day to the next at 19:00 hours instead of 0:00 hours. Still looking into this one. Has anybody used T_Dow?
 
Last edited:
Arg, had my GSV to get the wallclock time set to attribute DateTime instead of LocalDateTime.
 
OK, so I tried the FAL instruction in the ALL mode, but it did not work as I expected. I have an array that is 168 elements, (24 hrs * 7 days). Element 0 is Sunday day of week 0 and midnight hour 0. Element 167 is Saturday day of week 6 and 11:00PM which is hour 23. I have various shifts across these days and hours. I simply want to pick out any contiguous group of hours of a shift starting at the hour of the beginning of the shift. This could be something like array elements 7 to 18 for Sunday shift from 7:00 am to 7:00 pm, total of 12 hours. So I thought I could just put in 7 for .POS and 12 for .LEN and use the ALL mode and the FAL would cycle through these elements. But it doesn't. It appears that .Len is referenced to the 0 element in the array, NOT the starting position .Pos. So it only cycles through elements 7 to 12. But I need it to cycle through elements 7 to 18 (7+12-1). So, not sure if there is a way to use the FAL for this purpose.
 
Calculate an offset ((day * 24) + (shift * 12)) and add that to the .POS in the expression. Set your 'length' to 12.

This is assuming 'shift' is expressed by '0' or '1'.

So the FAL just generates the numbers 0-11. Your calculations move the retrieved info to the proper place in the array.
 
Last edited:
I calculate the minutes of downtime in each hour of each day of the week and move that value into my 168 element array (24hr * 7day). The array doesn't know anything about shifts. Its just knows when downtime had occurred on an hourly basis over a 7 day week. Also, my shift schedules change sometimes.

But just to make this simpler, so I can just understand the functionality of the FAL instruction, lets just say I want to add up elements 2, 3, and 4 in a 7 element array.

ELEM VAL
0 1
1 1
2 1 .Pos .Len
3 1 .Len
4 1 .Len
5 1
6 1

If .Pos =2, .Len=3, Mode=ALL it will not work. Since the .Len =3, it will only ever operate on elements 0,1,2 and ignore the other elements. Since .Pos=2, it will only ever reference element 2 and my sum will be 1. As far as I can tell, there are no .Pos .Len values I can enter into the FAL instruction to get it to add elements 2, 3 and 4.
 
As I noted, BEFORE calling the FAL instruction calculate the needed OFFSET according to the day and shift you need.

Don't worry about the .POS - leave it at zero. If you want all 12 hours in a shift leave the .LEN at 12.

It's within the expression, using the pre-calculate OFFSET, that you will pick out the appropriate 12 readings.

Maybe put the FAL stuff into a subroutine. Bring in Day (0-6) and Shift (0-1) as arguments. Return the accumulated values.

Thus if you wanted Tuesday (2) second shift (1) your 'OFFSET' would be (2 * 24) + (1 * 12) = 60

Your 'expression' in the FAL command would be cv_HourlyCycles[cv_TotalCyclesTodayCTRL.POS + OFFSET] + cv_TotalCyclesTodayTemp
 
Last edited:

Similar Topics

Hi, I have questions. I have Analog Input that need to put into Ignition Designer. But I don't know how to put?
Replies
1
Views
119
I have just installed Studio 5000 V30.11 on my Windows 11 Pro machine. First I had an "Invalid Pointer" error that required me to update Factory...
Replies
2
Views
118
Im trying to create a level indicator for water Tank i have used the ADD function while the pump is on and level increasing everything works...
Replies
33
Views
1,022
We are trying to poll data coming from a PLC for remote monitoring we have the IP address of the PLC and the default port number and the path is...
Replies
25
Views
579
Back
Top Bottom