; MACROCOMMAND= Drill Diameter,Finish Depth,SFM,FPR,Tool Number,Peck Depth,Retract Depth
; MACRODEFAULTS=1.0,-1.0,300.0,.007,1,0.2,.1
; MACROSVG=LatheMacro.svg,6
; O<drill> call [${drill.diameter-f}] [${drill.zdepth-f}] [${drill.sf-f}] [${drill.feed-f}] [${drill.tool-s}] [${drill.peck-f}] [${drill.retract-f}]

; #1 drill diameter
; #2 zdepth
; #3 surface speed
; #4 feed/rpm
; #5 tool number
; #6 peck amount
; #7 peck retract amount - not used right now

O<drill> sub

O100 IF [#<_imperial>]
    (MSG, IMPERIAL sizes)
    (MSG, )
    #<diam> = [#1 * 25.4]
    #<zDepth> = [#2 * 25.4]
    #<surfaceSpeed> = [#3 * .3048] ; sf/m to m/m
    #<fpr> = [#4 * 25.4]
    #<peck> = [#6 * 25.4]
    #<retract> = [#7 * 25.4]
    #10 = [[#3 *12] / [3.1415 * #1]]	; RPM from sfm request

    (debug, Drill Diameter:    %d#1 in)
    (debug, Peck Increment:    %.3f#6 in)
    (debug, Start Z:    %.3f#<_z> in)
    (debug, Finish Z:    %.3f#2 in)
    (debug, Feed:    %.3f#4 in/rev)
    (debug, Tool:    %d#5)
    (debug, Speed:    %d#3 sfm @ %d#10 rpm)
O100  ELSE
    (MSG, METRIC sizes)
    #<diam> = #1
    #<zDepth> = #2
    #<surfaceSpeed> = #3 ;m/m
    #<fpr> = #4
    #<peck> = #6
    #<retract> = #7
    #10 = [#<surfaceSpeed> / [3.1415 * #<diam>]]	; rpm from m/m request

    (debug, Drilling Diameter:    %d#<diam> mm)
    (debug, Peck Distance:    %.2f#<peck> mm)
    (debug, Start Z:    %.2f#<_z> mm)
    (debug, Finish depth:    %.2f#<zDepth> mm)
    (debug, Feed:    %.2f#<fpr> mm/rev)
    (debug, Tool:    %d#5)
    (debug, Speed    %d#<surfaceSpeed> m/min @ #10 rpm)
O100  ENDIF

M73 ; save state, restore on exit

G7 ; diameter mode
G17 ; XY Plane
G21 ; Metric Units don't change units!
G90 ; Absolute Distance

#<zStart> = #<_z> (starting Z)

(MSG, Unpause To start Drilling Macro)
m0

M6 T#5 G43

G97 S#10 ; Constant RPM mode
M3 ;Start Spindle
G95 F#<fpr> ; Feed-Per-Rev Mode

g4p1 ; Wait to reach speed

G0 X0 ; must drill from the centerline.  Tool should be zeroed here
G00 Z #<zStart>
G98
G83 Z#<zDepth> R#<zStart> Q#<peck>
G80

G0 Z #<zStart>
M5
G7

(MSG,Done Drilling Macro)
O<drill> endsub

M2
