44 Little Cahill Road

Cary, IL  60013

Ph: 847-639-8847

  • Facebook Social Icon

Six steps for creating a loop

One of the most powerful features of custom macro B is looping. With just a few commands, a loop can generate hundreds, even thousands of motion commands. This can dramatically shorten programs while still providing the flexibility of parametric (variable) programming.

Loops can be generated in many ways. Indeed, if you start “hacking away” at your program, you can probably make just about any series of looping commands work. But because loops can get pretty complex, I recommend following a more structured approach to creating them.


Though Fanuc has provides special commands for looping (the WHILE an DO statements), I prefer generating loops with the conditional branching (IF) statement. I find it just as easy to use the IF statement as the WHILE statement.


Here are the six steps I recommend followed by a simple example.


Step one: Initialize a counter and number of executions – as well as anything that changes each time through the loop


Make the counter an integer (whole number) and count up to a “number of executions” in the loop. This sometimes means you must calculate the number of repetitions and that you must modify the step amount/s each time through the loop. For a deep-hole pecking cycle, for example, you may have input variables for depth per peck and total depth. The total depth may not be evenly divisible by the peck depth, so you must modify the depth per peck to come up with an whole number of evenly-spaced pecks, like this:

  • #101 = 1 (Counter)

  • #102 = ROUND[#26/#17] (Number of pecks - #26 is total depth, #17 is specified depth per peck)

  • #103 = #26 / #102 (Recalculated depth per peck)

This will allow you to count from one (#101) to #102 and peck an even amount #103 per peck to the total hole depth (#26).


You must also initialize anything that changes each time through the loop. For peck drilling, for example, the current approach position and the current peck bottom position must be initialized, like this:

  • #104 = 0.1 (Current approach position)

  • #105 = #103 (Current peck bottom position)

Again, these values will be changing each time through the loop, so they must be initialized.


Step two: Test if finished

This is the IF (or WHILE) statement. Using my recommendation for counting up to a “number of executions”, you’ll always be using the greater-than (GT) logical operator in the if statement, like this:

  • N1 IF [#101 GT #102] GOTO 99

A sequence number (N1 in our case) is included because you must come back to this command in the last command of the loop.


The first time through the loop, of course, the counter (1) will not be greater than the number of executions, so the loop will be entered. Eventually, the counter will be greater than the number of executions and the loop will be finished. N99 (in our example) will send execution to the command after the last command of the loop.


If you elect to use the WHILE statement, you’ll always be using the less-than-or-equal-to logical operator (LE), like this:

  • WHILE [#101 LE #102] DO 1

Step three: If necessary, perform calculations needed for this time through the loop.

Some loops require calculations within each execution. Consider, for example, a loop to machine a bolt hole pattern of holes. The X and Y coordinates for each hole will change based upon a (changing) angle and the radius of the bolt hole pattern.


Step four: Write the commands for one execution of the loop.

If your loop is machining something, this means writing the motion commands for one execution of the loop. If your loop is setting offsets, this means writing the G10 command for the current offset. Again, what ever your loop is doing, write the commands for it in this step. With the peck drilling example, this means making one peck, like this:

  • G00 Z#104 (Rapid to current approach position)

  • G01 Z-#105 F4.0 (Machine to current peck bottom)

  • G00 Z0.1 (Retract from hole)

Step five: Step the counter and anything that changes each time through the loop.

For the next execution of the loop, of course, you’ll want the loop to do something differently, meaning something must be changed before the loop can be executed again. For the peck drilling example, the counter must be stepped (by one). The current approach position and the current peck bottom position must be stepped by the recalculated peck depth amount, like this:

  • #101 = #101 +1 (Step counter)

  • #104 = #104 + #103 (Step current approach position)

  • #105 = #105 + #103 (Step current peck bottom position)

Step six: Go back to the test.

For an IF statement loop, this means including a GOTO statement that sends execution back to the IF statement, like this:

  • GOTO 1

If you elect to use a WHILE statement loop, the command will be:

  • END 1

The number (1 in our case) must match the DO value in the WHILE statement (and must be between one and three).


A complete example

Here is the full peck drilling example loop. Example calling program:

  • O0001

  • N005 T01 M06

  • N010 G90 G54 S500 M03 T02

  • N015 G00 X0 Y0

  • N020 G43 H01 Z2.0 M08

  • N025 G65 P2000 X1.0 Y1.0 R0.1 Z-3.54 Q0.6 F5.0

  • N030 G91 G28 Z0

  • N035 M30

In line N025, X and Y (#24 and #25) specify the hole’s position. R (#18) specifies the rapid plane. Z (#26) specifies the hole bottom. Q (#17) specifies the (approximate) peck depth. F (#9) specifies the feedrate.

  • O2000 (Peck drilling custom macro)

  • G00 X#24 Y#25 (Move to hole location)

  • (Step 1)

  • #100 = ABS[#18 - #26] (Calculate total travel distance)

  • #101 = 1 (Counter)

  • #102 = ROUND[#100/#17] (Number of pecks)

  • #103 = #100 / #102 (Recalculated depth per peck)

  • #104 = #18 (Current approach position)

  • #105 = #18 - #103 (Current peck bottom position)

  • (Step 2)

  • N1 IF [#101 GT #102] GOTO 99

  • (Step 3 No calculations required in this loop)

  • (Step 4)

  • G00 Z#104 (Rapid to current approach position)

  • G01 Z#105 F4.0 (Machine to current peck bottom)

  • G00 Z#18 (Retract from hole

  • (Step 5)

  • #101 = #101 +1 (Step counter)

  • #104 = #104 - #103 (Step current approach position)

  • #105 = #105 - #103 (Step current peck bottom position)

  • (Step 6)

  • GOTO 1

  • N99 M99 (End of custom macro)

cnc reverse logo.jpg
0