; Rectangular pocket - clears using spiral then corner-clearing helper sub ; Params: #1=x1, #2=y1, #3=x2, #4=y2, #5=zstart, #6=zend, #7=fincut, #8=mode ; Mode: 0=CCW/ramp, 1=CW/ramp, 2=CCW/plunge, 3=CW/plunge ; pre-declare named params used across sub/helper scope # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 # = 0 o sub ; x1, y1, x2, y2, zstart, zend, fincut, mode ; #1 #2 #3 #4 #5 #6 #7 #8 ; modes: ; 0: CCW, ramped plunge ; 1: CW, ramped plunge ; 2: CCW, straight plunge ; 3: CW, straight plunge G90 ; absolute x,y,z G90.1 ; absolute i,j,k M101 ; enable Z-axis (M102 disables) G17 ; select XY plane for arcs # = #<_td> o1 if [EXISTS[#<_z_clearance>]] # = #<_z_clearance> o1 else # = #5 o1 endif o2 if [EXISTS[#<_rampang>]] # = #<_rampang> o2 else # = 5 ; degrees o2 endif o3 if [EXISTS[#<_stepover>]] # = #<_stepover> o3 else # = [#*0.4] o3 endif # = #6 # = [#6+#*0.1] ; slight lift for repositioning moves # = #1 # = #3 o11 if [#3 LT #1] # = #3 # = #1 o11 endif # = #2 # = #4 o12 if [#4 LT #2] # = #4 # = #2 o12 endif # = [[#+#]/2] ; pocket center X # = [[#+#]/2] ; pocket center Y # = [[[#-#]-#]/2] ; half-width minus tool radius # = [[[#-#]-#]/2] ; half-height minus tool radius G0 X# Y# G1 X[#+#] Y[#-#] G1 X[#-#] Y[#-#] G1 X[#-#] Y[#+#] G1 X[#+#] Y[#+#] o100 if [[#8 EQ 0] OR [#8 EQ 1]] ; helical plunge # = [#*0.3] # = [#*2*3.14*TAN[#]] G0 Z# G0 X[#+#] Y[#] G0 Z#5 ; helical plunge # = [#5-#] o101 while [# GT #6] o102 if [#8 EQ 0] G3 X[#+#] Y[#] I[#] J[#] Z[#] P1 o102 else G2 X[#+#] Y[#] I[#] J[#] Z[#] P1 o102 endif # = [# - #] o101 endwhile ; finish the helical plunge # = [[#-#6]/#5*360] o103 if [#8 EQ 0] G3 X[#+COS[#]*#] Y[#+SIN[#]*#] I[#] J[#] Z[#6] G3 X[#+COS[#]*#] Y[#+SIN[#]*#] I[#] J[#] Z[#6] o103 else G2 X[#+COS[#]*#] Y[#-SIN[#]*#] I[#] J[#] Z[#6] G2 X[#+COS[#]*#] Y[#-SIN[#]*#] I[#] J[#] Z[#6] o103 endif o100 elseif [[#8 EQ 2] OR [#8 EQ 3]] ; straight plunge # = 0 G0 Z#5 G0 X[#] Y[#] G1 Z[#6] o100 endif ; spiral outward from center until hitting pocket boundary # = # # = # o105 while [1] o106 if [[#8 MOD 2] EQ 0] G1 X[#+COS[#]*#] Y[#+SIN[#]*#] o106 else G1 X[#+COS[#]*#] Y[#-SIN[#]*#] o106 endif # = [#+1] ; next angle # = [#+[#-#]/360*#] ; compute the radius accordingly o110 if [ABS[COS[#]*#] GE [#-#7]] ; hit the x limit, y is still 'unbounded' o105 BREAK o110 elseif [ABS[SIN[#]*#] GE [#-#7]] ; hit the y limit, x is still 'unbounded' o105 BREAK o110 endif o105 endwhile ; okay, let's run until we hit a limit ; then based on which limit was hit, run in the perp. direction ; then go into the negative of that direction ; subroutines might be the best way to do this?? ; o200 while [# LE 3] ; # = [#+1] ; next angle ; ; ; o201 if [[[COS[#]*# GE -#] AND [COS[#]*# LE #]] AND [[SIN[#]*# GE -#] AND [SIN[#]*# LE #]]] ; G1 X[#+COS[#]*#] Y[#+SIN[#]*#] ; o201 endif ; ; # = [#+0.5] ; next angle ; # = [#+[#-#]/360*#] ; compute the radius accordingly ; o200 endwhile o call ; clear corners that the spiral couldn't reach G0 Z# o endsub ; if you can come up with a closed-form solution for x*sin(x) = c, you could drastically streamline this code. Until then... Lots of g1s. ; idea: bias the main spiral. then you're left with a section to hog out ; idea 2: make angle arbitrary (blowing up the code so it's not x-y specific). That'll be fun. ; Helper sub: clears rectangular corner material left by circular spiral ; Walks along the X-axis edge, tracing arcs that match the spiral boundary o sub # = 0 # = 0 # = 1 o202 while [#] G0 Z# G0 X# Y[# - #] G1 Z# # = FUP[sqrt[[#]**2 + [#]**2]/#] ; spiral revolution number at this corner point # = ATAN[-#]/[#] # = [#*# + #+[#-#]/360*#] # = [#] o200 while [# LT #] G1 X[#+#] Y[#-#] # = [# + 0.01] # = ATAN[-#]/[#] # = sqrt[[#]**2 + [#]**2] # = [#*# + #+[#-#]/360*#] ;# = [FUP[sqrt[[#]**2 + [#]**2]/#]*#] o204 if [# GT #] # = 0 o200 BREAK o204 endif o200 endwhile # = [COS[#]*#] o201 while [SIN[#]*# LT #] # = [COS[#]*#] o205 if [# GT #] # = [#] o205 endif G1 X[#+#] Y[#+SIN[#]*#] # = [# + 1] # = [#*# + #+[#-#]/360*#] o201 endwhile G1 X[#+#] Y[#+#] G0 Z# G0 X[#+#] Y[#-#] # = # # = [COS[#]*#] o202 endwhile o endsub M2