;threading

O<threading> sub

G7 ; Lathe Diameter Mode
G18 ; XZ Plane
G21 ; Metric Units
G90 ; Absolute Distance


M6 T#3 G43

#14 = [#<_x> * 2] (starting X)
#13 = #<_z> (starting Z)

G96 D200 S#2 ; Constant Surface Speed Mode
M3
g95 F0.25 ; Feed-Per-Rev Mode

O90 IF [#8 GT 0.5]
	M8
O90 ENDIF

g4p1 ; Wait to reach speed

;Threading
        O51 IF [#6 GT 0.5]
		#<OD> = [#1]
		#<ID> = [#1 - 1.3 * #4]
		;g1X [#<ID> - 1] ;thread truncation
		;g0 Z #13
		;g1 X #<ID>
		;g1 Z #5
		G0 X[#<ID> - 1]
		g0 Z #13
		#3 = [#4 * 1.3]
        (debug, INTERNAL Threading thread dia-#1 start-#13 finish-#5 Pitch-#4 Depth-#3)
		g1X [#<ID> - 1]
 		g76 p#4 z#5 i1 j1 k#3 h3 r1.5 q29.5 e0 l0
		
        O51 ELSE
		#<OD> = [#1 - 0.108 * #4]
		#<ID> = [#1 - 1.0825 * #4]
		(debug, EXTERNAL Threading OD = #<OD> ID = #<ID>)
		#3 = [#4 * 1.0825]
		g1X [#<OD> + 1] ;final thread truncation
		g0 z#13
		g1 X #<OD>
		g1 Z #5
		G0 X[#<OD> +1]
		G0 Z #13
 		g76 p#4 z#5 i-1 j1 k#3 h3 r1.5 q29.5 e0 l0
		
        O51 ENDIF
	G0 Z #13
	m5 M9
O<threading> endsub

M2
