Email us at: sales@trutegra.com

Trusted. Reliable. Soultions.

Handling Different Array lengths with One AOI.

By An Nguyen

A PLC programmer may want to create an Add-on Instruction as part of a project.  This can make code re-usable, more compact or just more readable.  Passing an array into an AOI is possible, but when the array to be passed is of unknown length, some tricks or additional understanding is required.  This article explains how this can be easily done.

The programmer wants to create a simple AOI that finds the average of all elements in an array.  Finding an average is a very simple case, but it illustrates the point, which is that arrays of different lengths can be passed into an AOI. This technique promotes standardization and more re-usable code.

Figure 1: Rockwell gives us the ability to do this with AOI

 

Rockwell gives us the option to do this with AOI

Reading between the lines, we discover the key insight, and the trick which allows our AOI to accept any size array we want to it.  This allows us to avoid writing many versions of an AOI for various sized arrays. 

First, setup AOI parameter based on Figure 1.

 

Add-On Instruction Definition 

Notice that:  The data type can be bool, real or even your own UDT as long as it as an array length of 1 and the usage parameter is set to “InOut”.  

Expert tip: When usage is set to “InOut”, the array is passed by reference, not by value.  Parameters defined as “Input” are passed by value.   

Then within the AOI code, we must determine the size of the array before we do any processing on it. 

The example is given in structured text:

//Determine the size of the passed in array

size(Array_In,0,array_size);

//Initialize a sum variable… 

sum := 0;

//Remember index of array range from 0 to size of array – 1 because 0 is inclusive 

for index := 0 to array_size - 1 do

sum := sum + Array_In[index]; 

end_for; 

Average := sum/array_size;

Now you can use the same AOI to take the average of an array of any length

An example using the AOI in a ladder diagram is as follows: 

Ladder logic showing AOI

Notes: 

Edits inside an AOI require going offline, editing, then downloading.  By building the AOI flexibly using this technique (passing variable length arrays) it may be that offline editing in the future can be reduced.

Also, the example above uses the same tag name (Test_Avg) for both instances of the AVG AOI.  This is not advised. 

Key Points, Restated: 
To pass an array of undetermined size (number of elements) into an AOI, set the parameter to “InOut” and in the AOI definition, declare it as having array size of 1.  There are side benefits to this, including flexibility, and standardization of code.

Happy Programming!

 

Connect With Us

Stay up to date with what's going on at Trutegra by liking us on Facebook!

Join Trutegra's ever-growing professional network by connecting with us on LinkedIn!

Watch the latest videos from Trutegra by subscribing to our YouTube channel!



Work For Us.

Trutegra Careers

Interested in working for Trutegra? We are always seeking new engineers to grow our staff of innovative problem solvers. Trutegra offers a very flexible and creative work environment, with the opportunity to learn many new technologies. A competitive salary and great benefits await you. Click below to submit your resume today!

Apply Now  

Contact Us

901 Blairhill Road Suite 500, Charlotte, NC
Phone: 704.529.0123 | Fax: 704.529.0139 | sales@trutegra.com