Sign in to follow this  
Followers 0
claretwelly

Adding integer values

8 posts in this topic

I'm trying to add together the total value of the contents of 128 integer files using RSLogix 500. I want to know if there is a quick way rather writing 128 Add commands. Hope someone can help.

Share this post


Link to post
Share on other sites
Not sure which processor you are using but take a look at the FAL instruction if it's available for your particular processor. You can do a giant totalizer with this instruction by itself. Alternatively, you can use the FOR/NXT instructions in a loop and used indexed or indirect addressing to do the trick. Most often though there is an easier way. If you can change your logic from a block form to an incremental form, you can do what you want with just one add. Let's say for instance we want a running average of say 10 values. The easiest way to calculate this is to put all 10 values into a buffer such as a ring buffer. Then add all 10 values together every time a new value is added, and divide the total by 10. This takes 10 executions of an add instruction, whether it is in an FAL instruction, a FOR/NXT loop or 10 individual add instructions. Alternatively, you can maintain a value called "running average" in addition to the buffer. Whenever you add a new value to the buffer, subtract the value that is being discarded (divided by 10) from the running average, and add the new value (divided by 10) to the buffer. This requires two arithmetic operations (ADD & SUB), executed every time a new value is added. You can either do both divisions, a single division at the end, or use a multiply (which is often faster) if you are careful how you handle the results. The incremental approach is 5 times faster in this example but scales to as many values as you want. Edited by paulengr

Share this post


Link to post
Share on other sites
You mention 'integer files'. I'm going to presume you meant 'integer values'. Beware of possible overflow while doing the addition. Even with 128 values thei becomes a definite possibility.

Share this post


Link to post
Share on other sites
FYI, RSLogix500 does not support the "FAL" instruction. Another option would be to use a compute "CPT" instruction. It would cut down on the number of rungs. One big problem with a FOR/NEXT loop is scan time issues if you try to do it all in one scan. Edited by Mickey

Share this post


Link to post
Share on other sites
Thanks for everyones suggestions so far. It is on an SLC 5/05 processor I am trying to do this. When using CPT command will i just need to write in an equation like n7:0+n7:1+n7:2........n7:128. Or is there a quicker, shorter way. In response to going over the overflow value, the highest value in any one integer value is 16. The root of this problem is that I need to know when the values from N7:0 to N7:128 are all equal to zero. As this will tell me there is no product in the machine and allow me to switch off burners and other parts of the machine for energy saving.

Share this post


Link to post
Share on other sites
How about...every time you change one of your integers to a non-zero, add one to a counter. Whenever you change it to a zero value, subtract one. When the counter equals 0, you know all the values are 0. Sounds like there's a queue in the background somewhere and the incremental approach is tailor made for this. Edited by paulengr

Share this post


Link to post
Share on other sites
I know this would require re-working part of your code but investigate the use of the FIFO commands. Used correctly you will always know if the shift register is empty or not.

Share this post


Link to post
Share on other sites
Counter and FIFO are good option but if value can be write from HMI or another PLC, you may consider to check all integer when you need to meet the "all 0 value". I send you a example with NEQ instruction as suggestion. A good point is N7:100 will give you witch integer is not egal to zero. You can use it in your program to prompt warning or anyting else... Edited by slcman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0