User's Manual

PMAC User Manual
Writing a PLC Program 235
PLC Program Structure
The important thing to remember in writing a PLC program is that each PLC program is effectively in an
infinite loop; it will execute over and over again until told to stop. (These are called PLC because of the
similarity in how they operate to hardware Programmable Logic Controllers, the repeated scanning
through a sequence of operations and potential operations.)
Calculation Statements
Much of the action taken by a PLC is done through variable value assignment statements:
{variable}={expression}. The variables can be I, P, Q, or M types, and the action thus taken can
affect many things inside and outside the card.
As shown in the Getting Started section of the manual, perhaps the simplest PLC program consists of one
line:
P1 =P1 + 1
Every time the PLC executes, usually hundreds of times per second, P1 will increment by one. Of course,
these statements can get a lot more involved. The statement:
P2 = M162/I108 32 10000)*COS(M262/(I208 32 100))
could be converting radial (M162) and angular (M262) positions into horizontal position data, scaling at
the same time. Because it updates this very frequently, whoever needs access to this information (e.g.,
host computer, operator, motion program) can be assured of having current data.
Conditional Statements
Most action in a PLC program is conditional, dependent on the state of PMAC variables, such as inputs,
outputs, positions, counters, etc. The action can be level-triggered or edge-triggered; both can be done,
but the techniques are different.
Level-Triggered Conditions.
A branch controlled by a level- triggered condition is easier to implement. Taking the incrementing
variable example and making the counting dependent on an input assigned to variable M11:
IF (M11=1)
P1=P1+1
ENDIF
As long as the input is true, P1 will increment several hundred times per second. When the input goes
false, P1 will stop incrementing.
Edge-Triggered Conditions
To increment P1 once for each time M11 goes true (triggering on the rising edge of M11 sometimes called
a one-shot or latched). To do this, there must be need a compound condition to trigger the action, then as
part of the action, set one of the conditions false, so the action will not occur on the next PLC scan. The
easiest way to do this is using a shadow variable, which will follow the input variable value. Action is
only taken when the shadow variable does not match the input variable. The code could become:
IF (M11=1)
IF (P11=0)
P1=P1+1
P11=1
ENDIF
ELSE
P11=0
ENDIF
Make sure that P11 can follow M11 both up and down. Set P11 to 0 in a level-triggered mode. This could
have been done this edge-triggered as well, but it does not matter as far as the final outcome of the routine
is concerned. It is about even in calculation time, and it saves program lines.