Yaskawa
eLearning
You're watching the IEC 61131 Basic Series
This is Programming Quick Start Part 2
Hi, I'm Matt Pelletier and in this
segment we will build on the example
from the previous video by creating
our own Task, Instance,
start some simple
Structured Text programming and work
more closely with Variable Usage and
Data Types.
Building on our example application
of a parking gate control we
had a counter and we're counting the
number of cars that come through the
parking lot with a Sensor and maybe
this is our business we want to make some
money, we'd like to be able to calculate
how much money should be coming in.
Well, you could do this in any of the languages,
let's do it using a Structured Text Program.
This is the perfect example
of when Structured Text lends itself
well to programming over function block
and in our template we do not yet have
any Structured Text Program
so let's go through the process of creating
a new Structured Text Program
you'll right click on the LogicalPOUs,
insert the Program and let's demonstrate that here
OK so with a Resource window closed
and Debug OFF, we'll go to the LogicalPOUs
in the project tree and right click,
insert Program, type in the name
in this case I'm going to call it Money
and you choose the language ST,
for Structured Text, click OK,
and it gives you the Structured Text code
notice there's a even a slightly different
color and look to the pieces of the,
the POU here, however I ask you...
is this POU, does it exist
in the PLC by creating it here
if you look over to the PLC which
is the Resource you can see
that it's not in one of the Tasks
and so this is the second step
anytime you insert a POU,
a program POU, is a program POU
runs in a Task and so you'll have to insert it
manually into one of the Tasks
or if not create a new Task
in this example we will add it to
the existing Task by right clicking
Insert Program Instance so let me
demonstrate that, the POU's name is Money
right click Insert Program Instance
into this task
I'm going to call it Money
don't stop here how many
times have I seen people type in the
name and click OK, that's not right!
You have to choose the Program Type
all of the existing Logical POUs for the project
will be listed here in alphabetical order
and so you need to choose the title
and the type of program
we typically make those match
so there it is... Money, Money
the names under twice because one
of them we typed, and the other
one is the Program Type.
So now the Logical POU called Money
is running in the cyclic task.
This separation of the Logical POUs
and the physical hardware
is an important concept in IEC 61131
and the reason for it is to allow modularity
and reusability of the parts of the program
between one hardware platform and another.
However, if we open up Money
you'll see that it's completely blank
because we have not programmed it yet.
So programming in the Structured Text language
involves much use of the F5 key
because the F5 key is used to declare
variables and put those variables into
the Variable List of the POU.
It's really important to use F5 and not just
type in the word that you want for the variable.
So let's go through it here we'll hit F5,
choose Income, can refer to this page
of the training guide,
we'll use data type LREAL in this case
that we can handle decimal places
REAL or LREAL would work
we'll use LREAL, we have
Income: = NumberOfCars * LREAL# 29.99
this is the format for the literal data, a constant.
OK, so let's do it...we'll type F5
my variable is called Income
with the Data Type
I'll tab over to Data Type here LREAL
stick with Usage VAR for now
Income and the syntax here is
colon equal, I mean set the
data equal to
now I'll hit a space here and F5 again
if you don't the space it
may automatically just select some text
so I usually hit a space
and NumberOfCars, stick with LREAL again
and then times, all caps, LREAL you may not need
all caps, but that's what I do
pound and let's say we're in Chicago here
we charge $29.99 to park, a good deal
and you terminate every line with a semicolon.
A quick check here, if you look at MoneyV
the variable list you better see
Income and NumberOfCars
if you don't see it there, maybe it's because
you didn't type F5 when you declared the variable
a very common mistake.
So once you have this typed in
you can go to Download Changes again
and let's go to
Debug Mode and see if it worked.
Alright now I have zero income
you can always hover over these here to see what
what the variable data is
NumberOfCars there is zero
so let's go back to Main
and let's trip the Sensor a few times here.
Now what's going on? I didn't, you know what?
we are using the variable here
called Current Value,
I think we need to make an update now
to call it NumberOfCars
So let's do that, call it NumberOfCars
Download Changes.
If you guys who know IEC might notice
that there's a mistake going on here
but I'm going to be using this mistake to prove
and teach a point.
OK so now...
now we got NumberOfCars two, three
all right I should be making tons
of money over in the Money POU
wait a minute still says zero
I'm at NumberOfCars here says zero
NumberOfCars here says three
so what could be going on
and what's going on here is that
NumberOfCars is not the same value in both POUs
because it's not the same variable.
If you look at the Variable List, you'll see
that NumberOfCars in the main POU is an
Integer Data Type and that NumberOfCars
is an LREAL and it's just
a completely separate variable.
Each local variable list
has its own variable that you can
reuse the names of variables
between the different POUs.
So what we need to do is to
connect the two POUs to be able to
share the data in this variable
by using a Global Variable
and one convention that we have
is to make that very clear by putting
a G underscore in the name
it's a naming convention for variables
and then you choose VAR_GLOBAL
when you create the variable
and that confirms the fact that
using a Global Variable
so a Global Variable allows data to be passed
between different POUs
so let's implement this
I'll change my Counter from NumberOfCars
and I'll put in a new variable
called G_NumberOfCars
and then let's also update the text
NumberOfCars
one thing you can do is hit F5 here
and NumberOfCars, change it to again
G_NumberOfCars, this is it.
OK, so now we have if you look at the Variable List
we have G_NumberOfCars
and Main has G_NumberOfCars
and you maybe look at the Global Variable list
haven't showed this yet, but let's look at it
Global Variables
I also have G_NumberOfCars
so this is where the data is truly
stored is in this Global Variable list.
So now let's Download Changes.
All right and now with this Global Variable
let's look at Debug Mode.
My NumberOfCars here are at 3
G_ NumberOfCars is still zero
so why is this?
I think I know what I did wrong here
at an additional thing wrong is I didn't
didn't not do what I said I would do
I chose G_ NumberOfCars
and I gave it the name G for Global
but I did not change the Usage
itself to Global
so let's go back and do that change the Usage to Global
let's be sure that both Main is
Global and Money variable is Global
Global will come up as VAR_EXTERNAL
I'll explain more about that and then
(later on) but let's just go with it for now
so I have them both now as Global
and now I have an error
and the error is Errors, 2 Warnings
and go to this Error tab
it says here there are no matching Global Variable
found for Money G_NumberOfCars
and missing resource in Resource
so you double click on that it highlights
the area of problem it says it can't find a
G_NumberOfCars LREAL in the Resource.
Well here's the physical hardware resource
it's the Global Variable list
can't find a matching variable, well there it is
doesn't it match? G_NumberOfCars
oh INT, so it's an Integer in Global Variables
in the Main Variable list
G_NumberOfCars is an Integer
but in Money G_NumberOfCars LREAL
so this is where Data Type is very important
in the IEC world.
This is going to be true for any
of the programming interfaces that you use
is you have to
have agreement of data types
and data type is very important.
When you do mathematical operations on data
they have to be of the same data type only
and so if you don't have the same data type
you'll have to convert it
there is no automatic conversion
in some senses it would be nice
to think oh well I know that a 3
is the same as a 3.0
well in IEC it is not IEC 61131
that says the data types must be the same
and there will be no assumptions
may the assumptions must be
part of the program
and that's considered a best programming practice
and so we will have to do that here
ourselves and convert that over.
So I will keep G_NumberOfCars as an Integer
and then in the Money POU
we will not use directly that Integer called
G_NumberOfCars, instead let's create
we'll use the variable we had before
called NumberOfCars so F5
NumberOfCars that's an LREAL
colon equals
and now we need to convert G_NumberOfCars
from an Integer to an LREAL
so let's get the
conversion from the Edit Wizard
there's a function for converting called
Type Convert functions and we want to convert
INT to LREAL, I'll type I here INT to LREAL
lNT to LREAL
is given as part of what we already have
the result is an LREAL is NumbeOfCars
and now what is the input as an Integer
the input as an Integer is
the G_ I better use F5 here
make some space here and hit F5
G_NumbeOfCars that's the Integer
OK and then for the code
we will not use G_NumbeOfCars
we'll use (hit F5 again here) NumbeOfCars
which is LREAL.
So now we have an LREAL times an LREAL
because we first converted one to the other.
So let's try that Download Changes
and see if that works.
It says there's been an error
and that download changes has been aborted.
Let's click OK it says see PLC errors
there's an error, whenever you see an
error flashing the PLC you need to click on it
it says error, instruction not implemented
or error exceeded a Data Type
it doesn't like this line
and for whatever reason
there's a little trick you have to do this is
I believe this is a minor issue
with the PLC Simulator
and the fix here is to go
in to the Simulator by right-clicking the
Resource and its settings and just
point this to Simulation2
and click OK
and I believe that will fix this issue.
Let's do Download Changes work
for simulation two
so we will Download
I'll do a full Download here
and says Warmstart is not possible
let's do Cold Start
we could even stop it and do a Warm Start
and I think now will be good.
OK so that one more time was you right-click
on the Resource,
look at Settings and choose Simulation 2
and that seems to fix the issue.
There's nothing wrong with this code
this is exactly how you do a
conversion of Integer to LREAL.
So now in Debug Mode let's see what we've got
go back into the Main POU
and let's have a couple of cars going there
I'll click on the Sensor
let's open ourselves up for business here
and we've got three cars
let's go into the Money POU
there's the calculation, everything seems to be working
we've got three cars
three converts to 3.0
through this line and our Income
is multiplied and we have $89.97
So this is as far as we'll take it
we created a new POU called Money,
we put that in the Task as an Instance,
we used a Global Variable
to convert it into a Local Variable with a different
data type, the LREAL data type,
and we did calculation based on that data
from the Main POU.
So with this we will conclude
Programming Quick Start 2
and we'll see in the next video.
Thank you!
you