Writing Your First Machining Macro

Figure 1. Part being held in a three-jaw lathe chuck with the part zero on the front face. (Image courtesy of the author.)

The basic peck drilling cycle, which is included with CNC FANUC-controlled lathes, does not have the drill retract fully after each peck, but this is very easy and quick to do with a macro subprogram. Put in a few lines of code and the peck drilling cycle will behave like a G83 drilling cycle.

  • O0083
  • N1 (Peck drilling full retract to R)
  • N2 #100 = ABS [#26-#18] (Total incremental distance)
  • N3 #101 = #18 - #17 (First peck position)
  • N4 #100 = #100 - #17
  • N5 WHILE [#100 GT 0] DO1
  • N6 G01 Z#101 F#09
  • N7 G00 Z#18
  • N8 Z = #101 + 0.03
  • N9 #101 = #101 - #17
  • N10 #100 = #100 - #17
  • N11 END1
  • N12 G01 Z#26 F#09
  • N13 G00 Z#18
  • N14 M99

The line of code in the main program to initiate this macro as described in Part 1 is shown below:

G65 P0083 X0 Z-1.1 R0.1 Q0.15 F0.012

Figure 2. Mild steel part being held in a collet chuck prior to the peck drilling cycle. (Image courtesy of Otterview Machine.)

Prior to calling the macro, the tool should be at X0 and no closer to the front of the part than Z0.1. The values for R, Q, Z and F will be unique for each application. The order in which the code appears on the line after the G65 P0083 is irrelevant; each value is assigned to the appropriate local variable as explained previously. The purpose of each code is described below:

X0 ensures the drill is on the center of the turned part.

Z-1.1 is the final drilling depth.

R0.1 is the Z coordinate at which the drill will start its peck drilling and to which it will return after every peck. 

Q0.15 is the amount of material that will be removed with each peck. 

F0.012 is the feed that the drill will advance into the part each revolution.

This is a very basic macro and it is important to understand that the way it has been written means that the following conditions must be met before running the macro:

  1. The part zero must be the front of the part as shown in figures 1 and 2.
  2. The R and Z values in the G65 line must be programmed in absolute.

Macro Operators

Macros contain logic statements that the CNC machine solves almost instantaneously before executing the machining code. The logic statements are often contained within square brackets [logic condition]. Table 1 lists the operators employed with in a macro program.

Line-by-line description of the macro:

N1: Program description.

N2: In common variable #100 stores the absolute value of local variable 26 minus local variable 18, or in the sample case, #100 = |-1.1-0.1| = 1.2. This value, 1.2, is the total incremental distance that the tool will travel to drill this hole.

N3: In common variable #101 stores the value of 0.1 – 0.15 = -0.05. This is the absolute depth the drill will go to at the end of the first peck.

N4: Recalculates the value stored in common variable #100 to be the amount of material yet to be drilled after the first peck (i.e., #100 = 1.2-0.15 = 1.05). This is a form of error checking to make certain that you have not told the drill to drill too deep on the first peck.

N5: The “While [Condition] Do” statement is a great tool for non-FANUC controllers and the same functionality can be performed with the “If Goto” statements, which will be examined in the next article. The “While [Condition] Do” statement tells the machine to continue to repeat all of the lines of codes found between the “While [Condition] Do” and the “End1” statement until the condition in the square brackets is no longer true. In this case, as long as the value stored in common variable #100 is greater than zero, keep repeating the lines of code from N6 through N10.  Each time before the loop begins, the value stored in common variable #100 will be checked to see if it is larger than zero; once it is no longer larger than zero, the logic statement will no longer be true and the loop will end. The line of code immediately following the “End1” statement will then be executed—in this case, N12.

N6: Feed at 0.01 inches per revolution to the absolute value stored in common variable #101, in the case of the first peck -0.05.

N7: Rapid the Z axis to the value stored in local variable #18, in this case 0.1.

N8: Rapid the Z-axis to the value stored in common variable #101 plus 0.03, in this case rapid the Z-axis to -0.02 = -0.05+0.03. In other words, rapid the tool tip to an incremental value of 0.03 inches away from the current depth of the hole.

N9: Calculate the next drill depth and store that value in common variable #101, in this case #101 = -0.05-0.15 = -0.2.

N10: Calculate the amount of material left to remove and store it in common variable #100, in the case of the first peck #100 = 1.05-0.15 = 0.95.

N11: Check if the “While [Condition] Do1” statement is true.

N12: Feed to the final hole depth, since the “While [Condition] Do1” loop will not allow the tool to go to the required depth but will always stop it at a depth less than or equal to one peck increment. In this case, the last peck will take the tool to an absolute depth of -0.95, leaving 0.05 for the final peck.

N13: Rapid out of the hole to the value stored in local variable #18, or an absolute position of Z0.1.

N14: Return to the main program. The M99 will also delete all macro local variable values. It is important to understand that the local variables are all emptied and not just set to zero.

You can also remove any data stored in a local variable by setting it equal to #0, the “null” variable. I.e., if #2 = #0 is programmed, then local variable #2 will be cleared of all data.

It is important to note that there are many ways to write a macro such as this, but this one was developed to help teach macro programming logic and to work efficiently. Table 2 shows all of the values for each variable as the macro steps through the sample code.

A video of this macro being used to drill a hole.

The test piece shown in the video was machined at Otterview Machine near Tillsonburg, Ontario. It was machined on a Doosan Lynx 220L with a FANUC i-series controller as shown in figure 3. 

Figure 3. Doosan Lynx 220L with a FANUC i-series controller used to test the macro. (Image courtesy of Otterview Machine.)

Not all FANUC-based controllers are capable of running the WHILE [ ] DO logic. In the next article, I will examine how to perform the same task, but using IF [ ] GOTO logic.

This is Part 2 of a 3-part series. For more information, read Part 1.

About the Author






Fred Fulkerson is a graduate of the Faculty of Education, University of Western Ontario, and of the general machining program at Conestoga College in Ontario. He is a Canadian Red Seal certified general machinist and CNC programmer and a certified Mastercam and SOLIDWORKS instructor.