You are on page 1of 2

Custom macro B provides many tools to help users with CNC programming, including

variable capabilities, arithmetic calculations and program flow control. One of


the most powerful functions allowed by this custom macro (and for that matter,
any computer programming language) is looping.
Columns: 6/1/2006
MIKE LYNCH
Custom macro B provides many tools to help users with CNC programming, including
variable capabilities, arithmetic calculations and program flow control. One of
the most powerful functions allowed by this custom macro (and for that matter,
any computer programming language) is looping. Looping involves having the machi
ne repeat a series of commands and can be compared to repeating the execution of
subprograms. When you repeat a subprogram, it will be executed in exactly the s
ame fashion all four times. That is, nothing in the subprogram can change betwee
n executions. When you set up a loop, however, anything can change between execu
tions.
Loops are used to minimize the number of redundant commands needed for your appl
ication. Consider this loop, which sets offsets 1 through 100 to zero.
#100 = 1 (Loop counter and offset number)
N1 IF [#100 GT 100] GOTO 50 (Test if finished)
G90 G10 L1 P#100 R0 (Set current offset to zero)
#100 = #100 + 1 (Step counter and offset number)
GOTO 1 (Go back to the test)
N50 (Continue with program)
This loop illustrates the contrast to a time when the commands were written out
in longhand, requiring many (100) commands. As you can see, this loop requires o
nly six commands.
Loops are also used when the number of executions for the loop changes from appl
ication to application. With a bolt circle loop, for example, the number of hole
s on the bolt circle can change from one bolt circle to another. You can see thi
s loop on my company s Web site at www.cncci.com/resources/tips/boltcircle.htm.
Loops become more complicated when it is not easy to determine the number of loo
p executions. Consider, for example, a peck drilling custom macro. You have a 3inch deep hole and a peck depth of 1 inch. In this case, the loop is pretty easy
to program. The hole depth of 3 inches is evenly divisible by the peck depth of
1 inch. This means that three executions of the peck depth will be required. Bu
t what if you have a 3.25-inch deep hole and want a 1-inch peck? This loop requi
res a little more thought.
I always recommend determining an even number of executions, even if it means mo
difying certain machining criteria. For example, if you use the round function (wh
ich rounds to the closest integer), you can come up with an even number of passe
s. Consider the command:
#100 = ROUND[3.25 / 3.0]
The result will be that #100 will be 3. However, you cannot simply peck 1 inch d
eep three times. This will not render the correct depth.
Let s recalculate the peck depth based upon the number of passes:
#101 = 3.25 / #100
The peck depth will be slightly more than 1 inch (in this example). However, you
can now set up a loop that has an even number of passes.

The seven steps to setting up a loop are:


Initialize the counter and, if needed, anything that changes each time within th
e loop.
If necessary, initialize constants in the loop.
Test to see if the loop is finished.
If necessary, calculate anything needed for the current time through the loop.
If necessary, make motions needed for the current time through the loop.
Step the counter and, if needed, anything that changes each time through the loo
p.
Go back to the test.
There is a special custom macro function for looping, WHILE. However, anything y
ou can do with a WHILE statement loop can be done with an IF statement loop. Her
e is the offset clearing loop shown earlier using the WHILE statement:
#100 = 1 (Loop counter and offset number)
WHILE [#100 GT 100] DO 1 (Start of loop)
G90 G10 L1 P#100 R0 (Set current offset to zero)
#100 = #100 + 1 (Step counter and offset number)
END 1 (End of loop)
N50 (Continue with program)
Note that the counter (#100) must still be initialized and stepped. The DO 1 at
the end of the WHILE statement tells the machine the location of the loop s ending
(corresponding to END 1). However, if you are nesting loops, only three loops c
an be running. This means you can use DO1/END1, DO2/END2 and DO3/END3. There is
no such limitation to an IF statement loop.
Columns
Automating Surface Metrology for the Shop Floor
The Art of Change
Developing an In-House Training Curriculum
A System for Estate Planning
Impressions of IMTS 2016

You might also like