Setting Up Loops With Custom Macro B
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.
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 machine 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 same fashion all four times. That is, nothing in the subprogram can change between executions. When you set up a loop, however, anything can change between executions.
Loops are used to minimize the number of redundant commands needed for your application. 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 only six commands.
Loops are also used when the number of executions for the loop changes from application to application. With a bolt circle loop, for example, the number of holes on the bolt circle can change from one bolt circle to another. You can see this 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 loop executions. Consider, for example, a peck drilling custom macro. You have a 3-inch 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. But what if you have a 3.25-inch deep hole and want a 1-inch peck? This loop requires a little more thought.
I always recommend determining an even number of executions, even if it means modifying certain machining criteria. For example, if you use the “round” function (which rounds to the closest integer), you can come up with an even number of passes. 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 deep 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 the 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 loop.
- Go back to the test.
There is a special custom macro function for looping, WHILE. However, anything you can do with a WHILE statement loop can be done with an IF statement loop. Here 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 can be running. This means you can use DO1/END1, DO2/END2 and DO3/END3. There is no such limitation to an IF statement loop.
Read Next
Inside Machineosaurus: Unique Job Shop with Dinosaur-Named CNC Machines, Four-Day Workweek & High-Precision Machining
Take a tour of Machineosaurus, a Massachusetts machine shop where every CNC machine is named after a dinosaur!
Read MoreThe Future of High Feed Milling in Modern Manufacturing
Achieve higher metal removal rates and enhanced predictability with ISCAR’s advanced high-feed milling tools — optimized for today’s competitive global market.
Read MoreIncreasing Productivity with Digitalization and AI
Job shops are implementing automation and digitalization into workflows to eliminate set up time and increase repeatability in production.
Read More