Tip:
Highlight text to annotate it
X
Alright, this video is going to cover the WAGO IO Pro CAA programming guide and creating
a function.
We are going to programming an AVERAGE function.
And what this is going to do, its going to pass an array of values and give you the average
of the result.
In this PDF document, you can see it steps you through very easily, through the creation
of this program.
And were also going to be covering, uh, how to create a visualization.
It will allow you to manipulate the variables through a visualization interface.
Alright, we are ready to start programming Codesys.
Go ahead and click on file, new.
And we are going to program a WAGO 750-841.
On the visualizations tab, I want to enable web visualizations.
And click OK.
And our default PLC_PRG is going to be a ladder diagram.
Just want to right click in the white area under the POU's and select add an object.
It's going to be a function, with type integer and we'll program that is structured text.
I'm going to name that 'Average'.
At this point we can start programming our
(pause)
function.
(pause - typing)
So as you start programming your code, it will ask you to declare your variables. Um,
variable 'NUM' is actually an input variable we are going to pass into our function and
it is an integer.
and next we are going to do our FOR loop, which is going to count our, contents of our
array.
Index is a local variable. Its an integer.
Sum is also a local variable and its an integer.
And the values is actually a variable input,
and this is going to be an array of integers.
So again, this is just going to, um, create a loop. And the loop will basically operate
from one, to whatever the value of NUM is. For however many variables we would like to
average.
And each time it runs through this, um, its going to increment sum, based on the, uh,
its going to totalize the contents of all of the, uh, containers that are included in
this array.
At this point we have a sum total of all the values we've sent into this function block,
and now we can create an average based on that.
So, the average is the sum divided by the number of elements.
Notice, average is not shown up here, um, as a variable.
Its actually the return variable for our function.
We can setup an else statement. And we are just going to return a minus 1 if our NUM
equals zero.
And I'll put a comment here.
Alright, thats our function.
Go back to our PLC PRG.
Set up an array for our example here.
One hundred elements of integers.
Setup a variable to accept our mean.
At this point we can insert a box. I am going to change this to AVERAGE.
And you can see here it populated the inputs that we are looking for.
So our values are tied to 'MyValues'. And for NUM, I am just going to put a constant
here of five.
And we will tie this to MEAN.
Again, for all of these we can use the input assistant, um,
if we didn't remember we had mean there, you can press F2 and go to the input assistant,
and select the variable that you're looking for.
Alright, at this point we can go to the visualizations tab.
And we will create a new visualization.
And we will call that 'average'
I'm just going to select the, uh, square input box.
and we are going to go to text,
and I'm going to type in value %s
and this percent little s, is a, uh,
identifies a variable display.
Go to variables, and text display.
And, you can see here, as soon as you type in PLC_PRG period, it will
prompt you with the, ah, inputs to display here. I'm going to select my values.
And I'm going to use the brackets to indicate the first input of that variable.
And
Now I'm just going to cut and paste this a few times.
(pause)
And go into each one of these and change the
array reference.
Alright, and the final box,
will be the mean.
And you can change the colors on these objects, ah, if you want to.
Its totally up to you.
At this point we are going to build the project.
we have no errors and no warnings.
we can go into simulation mode, and
log in.
and I just want to enter some numbers here.
and we are going to run the processor.
and you can see our average is being displayed. average of four.
and I can change these numbers and it will automatically update the result.