GiantJacob

Add On Instructions Parameters

9 posts in this topic

I am trying to learn how to use add-on instructions, and to get a better understanding I am trying to do a simplified example. I am trying to make two separate add on instructions that alter an integer tag in different ways. I would like one AOI that multiplies the input integer by 3 and another AOI that adds 3. The integer tag is a InOut parameter.  I named these AOIs "Mult3" and "Add3". When trying to use the AOI's it requires me to have an additional input with datatype the same as the AOI ("Mult3" or "Add3"). I have a couple of questions about this:

1. Why is this data type required? It seems to me that I would only need the InOut integer that I want to modify.

2. I want to use these AOI's in a number of places throughout my code on a number of tags. Would I be able to just use one "Mult3" tag for all the AOI's, or would I need a seperate tag for each use of the AOI? 

For Instance if I am using the Mult3 AOI in 3 separate places in my code, two times using "Int1" as the InOut parameter and another time using "Int2" as the InOut parameter, could I just make one tag of "Mult3" datatype and use that for all the instances of the AOI, or would that screw it up in some way.

Thanks for any help.

Edited by GiantJacob

Share this post


Link to post
Share on other sites

An Add On Instruction (AOI) definition is a template. Think of it as the blueprints used to build a house. It has a name just as a specific set of blueprints might have a name. Let's say Joe's Builders created the house design as shown in the the blueprints and called the design 'Aspen Palace'.

Now you, as a home buyer decide you like the 'Aspen Palace' design and you want one built on you lot. They don't just take a set of blueprints and drop them on your lot. They actually build the house. When done your house has a specific address - let's say "201 Main Street, Anytown, USA". Imagine also that each house also has a little plaque saying "Aspen Palace Built by Joe's Builders'. Your built house is an 'Instance' of that specific set of plans. The builder could build a number of these in various places. They would all be Instances of those specific 'Aspen Palace' house plans, but they would each have their own distinct and unique address.

Now that you have created your AOI (Mult3). You want to use one of these at THIS place in your code. In effect the placing of the AOI box and specifying its parameters is building all the code and making space for the variables at that particular placed in your ladder (though it shows up as a box.) The box will have a title - Mult3. This is like the house plaque. It shows the TYPE of AOI. But you have to give a name to this specific instance of the AOI. That is the first 'parameter' at the top of the box. This name is applied to the code and variable space inside this particular box. If you want to use the AOI somewhere else, probably with different parameters, you will be creating another instance of the AOI template. Again it will have the name of the template - Mult3 - but this time the specific instance gets its own unique name to differentiate THIS set of code and memory space from the other instances of this particular AOI.

 

In like manner the User Defined Type (UDT) is a template for a set of variables. If you want to make an Instance of the UDT you create a tag with the TYPE of the UDT but with its own particular name. The name specifies the chunk of memory holding a set of variables as defined by the UDT template.

Edited by b_carlton

Share this post


Link to post
Share on other sites

Thanks for the reply.

So what would happen if I used the same UDT for all instances? In practice how would this produce bad results?

Also does this mean if I want to use the same parameters I could use the same UDT? So in the two instances where I am using "Int1" as the InOut parameter I could use the same UDT (say "Mult3_A") and the other instance using "Int2" would need a seperate UDT (say "Mult3_B")?

Share this post


Link to post
Share on other sites

Each instance of an AOI gets its own name (because it is actually a different chunk of memory - saame type house but differeent lot). I haven't tried but I think the compiler will throw a hissy fit if you try to give two instances thee same name. If you really, Really, REALLY want to have only one chunk of code and call the same code from multiple places in one Program then create a subroutine and pass input and receive output paraameters. I don't think subroutines can do an in/out type of parrameter though. 

The same with two identically named tags (instances) of  the same UDT.

Edited by b_carlton

Share this post


Link to post
Share on other sites

You can't have two tags with the same name and since each instance of the AOI requires a tag, you can't name them the same

However, a UDT is a different situation because a UDT ends up as a dot field for instance X. Ie if I have a UDT structure called Stuff:

X REAL

Y REAL

Z REAL (This auto double space thing is annoying as hell)

And I create Bob of the type Stuff and Trisha of the type stuff the tags that are created will be

Bob.X, Bob.Y, and Bob.Z and

Trisha.X, Trisha.Y, and Trisha.Z

An AOI is a container for a routine. The tags that you create within the routine end up as dot fields of whatever you name the instance of the routine.

AOI's are useful but I wouldn't make one if I was going to use it once or twice. An AOI is something  that you plan to use many, many times in your lifetime as a programmer The AOI's that I've made have been shared with other programmers and I know I've used most of them between 75 and 100 times. 

I don't think I've ever had much luck with using an In/Out parameter in an AOI. Just Local, Input, and Output. I could never get a String tag to work either.

Edited by Michael Lloyd

Share this post


Link to post
Share on other sites

For an AOI, anything not a BOOL, SINT, INT, DINT, REAL will have to be an in/out type.

In the case of BOOL ... REAL the VALUE of the input parameter is passed to the AOI. The AOI cannot alter the actual input parameter. An output parameter of these types receives the final value from the AOI.

The in/out parameters have a pointer to their location passed to the AOI. The AOI can read/write anything in the in/out parameter.

Edited by b_carlton

Share this post


Link to post
Share on other sites

(In my previous post I was mistakenly using "UDT" to refer to the tag with the same type as the AOI, I now understand my mistake in that regard.)

My intial reasoning for using an AOI instead of a subroutine was because as I understand it the JSR call to use a subroutine requires significantly more execution time than an AOI. Is this correct?

If I wanted to replicate the same set of code with different parameters a large amount of times in my project, say 100-300 times, what would be the best way to do such a thing?

I would like to not have to create 300 tags, one for each AOI. But I would also not like to slow down my execution time severely.

Also a subroutine call cannot "block" a rung like an AOI can, correct? For instance if I wanted the subsequent part of the rung/branch that the Mult3 is on to only be examined if the modified integer is an even number (or some other requirment), this could only be achieved though an AOI, correct? If I wanted to replicate this example 300 times in a project, what would be the ideal way to do so?

My main reason to understand this is that if I can reuse the same code (using an AOI or subroutine) I reduce the chance for human errors by mistyping something, especially if I need to do the same thing a large number of times, not to mention the programming time saved.

Thanks again for the help

Share this post


Link to post
Share on other sites

In posting from my phone but when I get home I'll upload an example of a loop that I think solves your problem. The way I (and many others) use it is different than what you describe but I think it's adaptable. 

 

I've used it to loop thru 250+ times and it runs very quickly

Share this post


Link to post
Share on other sites

Almost forgot to post this. Any questions post away

The attached piece of a program is how I scale large blocks. The size of the loop is set in the first line of AIOM. Dig around in it. It should be fairly simple. I included the AOI but I usually just copy that part from another program since it never changes

It's a Rev 19 file

Test.ACD

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