CNC Macro Programming for Custom Deep Hole Cycles
Standard canned cycles (G73, G83) handle basic deep hole drilling needs, but they have fixed behavior: constant peck depth, constant feed rate, and no conditional logic. For advanced deep hole drilling — where parameters should change as the hole gets deeper — custom macro programming provides the flexibility needed.
This guide covers Fanuc Custom Macro B, the most widely used macro language, with examples adaptable to other controls.
Why Custom Macros for Deep Hole Drilling?
| Standard G83 Limitation | Custom Macro Solution |
|---|---|
| Fixed peck depth (Q) | Variable peck depth — decreasing at depth |
| Fixed feed rate | Variable feed — slower at entry and exit |
| No conditional logic | Monitor depth, adjust parameters in real time |
| No coolant control | M-code before each peck can turn coolant on/off |
| No first-peck adjustment | First peck deeper (or shallower) than subsequent pecks |
| No manual chip evacuation | Programmed dwell or spindle reverse at final depth |
Fanuc Custom Macro B Basics
Variables
| Variable Type | Range | Persistence |
|---|---|---|
| Local | #1–#33 | Cleared on program end |
| Common | #100–#199 | Cleared on power off |
| Common | #500–#999 | Retained on power off |
| System | #1000+ | Machine parameters, position, status |
Macro Call with Arguments
; Call macro O9001 with arguments
G65 P9001 X25.0 Y0 Z-40.0 R2.0 Q4.0 F150
Arguments are passed to local variables:
| Letter | Variable | Meaning |
|---|---|---|
| X | #24 | X position |
| Y | #25 | Y position |
| Z | #26 | Z depth |
| R | #18 | R-plane |
| Q | #17 | Peck depth |
| F | #9 | Feed rate |
Example 1: Variable Peck Depth Macro
This macro decreases peck depth as the hole gets deeper, providing more frequent chip evacuation at depth.
% (MACRO: VARIABLE PECK DRILL)
O9001 (VARIABLE PECK DRILL)
#30=#24 (X POSITION)
#31=#25 (Y POSITION)
#32=#26 (Z DEPTH - FINAL)
#33=#18 (R PLANE)
#34=#17 (INITIAL PECK DEPTH)
#35=#9 (FEED RATE)
#1=#33 (CURRENT Z POSITION - START AT R PLANE)
#2=#34 (CURRENT PECK DEPTH - START AT INITIAL)
WHILE [#1 GT #32] DO1 (LOOP UNTIL REACH FINAL DEPTH)
IF [#1 - #2 LT #32] THEN #2=#1 - #32 (LAST PECK - REDUCED)
G01 Z[#1 - #2] F#35 (FEED TO PECK DEPTH)
G00 Z[#33] (RETRACT TO R PLANE)
G00 Z[#1 - 0.5] (RAPID BACK TO LAST DEPTH - MINUS CLEARANCE)
#1=#1 - #2 (UPDATE CURRENT DEPTH)
#2=#2 * 0.9 (REDUCE NEXT PECK BY 10%)
IF [#2 LT 1.0] THEN #2=1.0 (MINIMUM PECK 1.0 MM)
END1
G00 Z#33 (FINAL RETRACT)
M99
Calling the macro:
G65 P9001 X25.0 Y0 Z-40.0 R2.0 Q6.0 F150
Behavior: First peck 6 mm, second 5.4 mm, third 4.9 mm, down to minimum 1.0 mm.
Example 2: Entry Feed Reduction Macro
This macro uses a reduced feed for the first peck, then increases to the production feed rate.
O9002 (ENTRY FEED REDUCTION DRILL)
#24=#24 (X)
#25=#25 (Y)
#26=#26 (Z)
#18=#18 (R)
#17=#17 (Q)
#9=#9 (PRODUCTION FEED)
#7=#7 (ENTRY FEED - PASSED AS E ARGUMENT)
G00 X#24 Y#25 (MOVE TO POSITION)
G00 Z#18 (RAPID TO R PLANE)
; FIRST PECK - REDUCED FEED
G01 Z[#18 - #17] F#7
G00 Z#18
G00 Z[#18 - [#17 - 0.5]]
; REMAINING PECKS - FULL FEED
#1=#18 - #17
WHILE [#1 GT #26] DO1
IF [#1 - #17 LT #26] THEN #17=#1 - #26
G01 Z[#1 - #17] F#9
G00 Z#18
G00 Z[#1 - 0.5]
#1=#1 - #17
END1
G00 Z#18
M99
Calling the macro:
G65 P9002 X25.0 Y0 Z-40.0 R2.0 Q4.0 E80 F150
Entry feed = 80 mm/min, production feed = 150 mm/min.
Example 3: Coolant-Controlled Deep Hole Cycle
For machines without through-spindle coolant, turning coolant on and off between pecks can improve chip evacuation.
O9003 (COOLANT CONTROL DRILL)
G00 X#24 Y#25
G00 Z#18
#1=#18
WHILE [#1 GT #26] DO1
IF [#1 - #17 LT #26] THEN #17=#1 - #26
M08 (COOLANT ON BEFORE CUT)
G01 Z[#1 - #17] F#9
G04 P200 (DWELL 0.2 SEC)
M09 (COOLANT OFF FOR RETRACT)
G00 Z#18 (CLEAR RETRACT)
M08 (COOLANT ON BEFORE NEXT PECK)
G00 Z[#1 - 0.5]
#1=#1 - #17
END1
M09
M99
Example 4: Position-Specific Parameter Adjustment
For drilling multiple holes at different depths, this macro adjusts feed rate based on hole depth.
O9004 (POSITION-SPECIFIC DRILL)
; X=#24, Y=#25, Z=#26 (FINAL DEPTH)
; FEED = 100 + ABS(Z)/10 (DEEPER = FASTER FEED)
#9 = 100 + [ABS[#26] / 10]
G83 X#24 Y#25 Z#26 R#18 Q#17 F#9
M99
Mapping Custom Cycles to G-Codes
Custom macros can be mapped to user-defined G-codes for easier calling:
| System Variable | Maps To |
|---|---|
| #6050 | Macro O9011 → G-code (e.g., G123) |
| #6051 | Macro O9012 |
| #6052–#6059 | Macros O9013–O9020 |
Setup:
; Set parameter #6050 to 123
; Now G123 calls macro O9011
G123 X25.0 Y0 Z-40.0 R2.0 Q4.0 F150
Siemens Sinumerik Custom Cycles
Siemens controls use a different approach — high-level language (SCL) or SINUMERIK Operate cycles:
; Sinumerik custom cycle example
PROC CYCLE84 (REAL RTP, REAL RFP, REAL SDIS, REAL DP, REAL DPR)
; Custom deep hole cycle
G0 G90 X0 Y0
G1 Z=RFP+SDIS F500
; ... custom logic
M17
Macro Programming Best Practices
| Practice | Why |
|---|---|
| Document variable usage | Macros are hard to debug without documentation |
| Use modal-safe programming | Save and restore G90/G91, G98/G99, and other modal states |
| Include clearance moves | Always retract at least 0.5 mm above the previous depth |
| Test on a simulation first | Macro bugs can cause rapid moves into the workpiece |
| Use WHILE loops, not GOTO | WHILE loops are safer and more readable |
| Set minimum peck depth | Prevent infinite loops with decreasing peck macros |
Summary
Custom macro programming extends the deep hole drilling capability of standard CNC machines far beyond what G73 and G83 offer. Variable peck depth (decreasing at depth), entry feed reduction, coolant control, and position-specific parameter adjustment are all achievable with Fanuc Custom Macro B or similar macro languages on other controls. For production deep hole drilling beyond 15:1 depth ratio, custom macros combined with through-spindle coolant provide a cost-effective alternative to dedicated deep hole drilling equipment.
For G83/G73 fundamentals, see CNC deep hole drilling G-code guide. For control-specific cycles, see CNC drilling cycles by control. For a complete overview, visit the CNC deep hole drilling guide.