Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
The very basic spiral toolpath generator
from mecode import GMatrix
import numpy as np
e = 0.00001
inch = 25.4
unit = 19.05
feedrate = 200
tool_diameter = 1
overlap = 0.5
spindle = 3000
def prefix():
'''G40 (disable tool radius compensation)
G49 (disable tool length compensation)
G80 (cancel modal motion)
G54 (select coordinate system 1)
G90 (disable incremental moves)
G21 (metric)
G61 (exact path mode)
F {feedrate}
S {spindle}'''.format(**globals()))
def postfix():
'''M5 (stop spindle)
G04 P3 (wait for 3 seconds)
G0 Z25.0000
M2 (end program)'''.format(**globals()))
def mirror(g, x, y):
mirror_matrix = np.matrix([[x**2-y**2, 2*x*y],
[2*x*y, y**2-x**2]]) / (x**2+y**2)
g.matrix_stack[-1] = mirror_matrix * g.matrix_stack[-1]
def helical_meander(g=None, diameter=10, direction='CCW'):
assert(g != None)
diameter -= tool_diameter
assert(diameter > 0)
radius = diameter / 2
steps, rem = divmod(radius, tool_diameter*overlap)
steps += rem > 0
verts = np.arange(0, radius+e, radius/steps)
verts = [i for p in zip(verts, -verts) for i in p][1:]
for a, b in zip(verts, verts[1:]):
g.arc(x=b-a, y=0, radius=abs((b-a)/2)+e, direction=direction)
def helical_move(g=None, x=0, y=100, width=tool_diameter*2,
direction='CCW', no_init=False, clean_corner=True):
width -= tool_diameter
assert(width > 0)
assert(isinstance(g, GMatrix))
radius = width/2
length = np.hypot(x, y)
steps, rem = divmod(length, tool_diameter*overlap)
steps += rem > 0
# direction = {'CCW':1, 'CW':0}[direction]
def init():
helical_meander(g, diameter=width+tool_diameter, direction=direction)
def step():
g.move(0, length/steps)
g.arc(x=-width, y=0, radius=radius+e, direction=direction)
g.arc(x=width, y=0, radius=radius+e, direction=direction)
def corner():
outset = (np.sqrt(2)-1) * radius
steps, rem = divmod(outset, tool_diameter*overlap)
steps += rem > 0
rs = np.arange(radius+outset/steps, radius+outset+e, outset/steps)
ys = np.sqrt((rs**2-radius**2).clip(0, 987654321))
ps = radius
py = 0
for y, r in zip(ys, rs):
ps -= y-py
ps = max(0, ps)
g.move(0, y-py)
g.arc(x=-ps, y=ps, radius=r+e, direction=direction)
g.move(ps, -ps)
py = y
g.move(-radius, 0)
g.rotate(np.arctan2(y, x))
if not no_init:
for _ in range(int(steps)):
if clean_corner:
if __name__ == "__main__":
g = GMatrix()
helical_move(g, x=10, y=10, width=2*tool_diameter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment