-
Notifications
You must be signed in to change notification settings - Fork 97
/
deltacode.py
executable file
·160 lines (130 loc) · 4.19 KB
/
deltacode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
#
# Convert G-code from slic3r to deltacode for Rostock 3D printer.
#
# Deltacode is similar to G-code but with delta geometry for X Y Z.
# Straight lines in G-code are translated to parabolic curves in
# deltacode. These curves are made from many small linear G1
# moves. This may double the size of the G-code file. In the future
# this translation will be implemented in firmware so we can send
# normal G-code to the delta bot.
__author__ = '[email protected]'
CENTER = {'X': 100, 'Y': 100}
import math
import sys
class Vector(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __abs__(self):
return math.sqrt(self.x * self.x +
self.y * self.y +
self.z * self.z)
def __add__(self, other):
return Vector(self.x + other.x,
self.y + other.y,
self.z + other.z)
def __sub__(self, other):
return Vector(self.x - other.x,
self.y - other.y,
self.z - other.z)
def __mul__(self, factor):
return Vector(self.x * factor,
self.y * factor,
self.z * factor)
def gcode(self):
return 'X%.8g Y%.8g Z%.8g' % (self.x, self.y, self.z)
SIN_60 = math.sin(math.pi / 3)
COS_60 = 0.5
RADIUS = 175 - 33 - 18 # Horizontal distance of diagonal rods when centered.
ZERO_OFFSET = -9 # Print surface is lower than bottom endstops.
TOWER_1 = Vector(-SIN_60 * RADIUS, -COS_60*RADIUS, 0)
TOWER_2 = Vector(SIN_60 * RADIUS, -COS_60*RADIUS, 0)
TOWER_3 = Vector(0, RADIUS, 0)
def delta(v):
t1 = TOWER_1 - v
t2 = TOWER_2 - v
t3 = TOWER_3 - v
return Vector(
v.z + math.sqrt(250*250 - t1.x*t1.x - t1.y*t1.y) + ZERO_OFFSET,
v.z + math.sqrt(250*250 - t2.x*t2.x - t2.y*t2.y) + ZERO_OFFSET,
v.z + math.sqrt(250*250 - t3.x*t3.x - t3.y*t3.y) + ZERO_OFFSET)
def G1(pos, dest):
"""Convert a long linear cartesian move into many small moves."""
global num_lines
for char in 'XYZEF':
if char not in dest:
dest[char] = pos[char]
start = Vector(pos['X'], pos['Y'], pos['Z'])
finish = Vector(dest['X'], dest['Y'], dest['Z'])
cartesian_mm = abs(finish - start)
steps = max(1, int(5 * cartesian_mm))
cartesian_mm /= steps
previous = delta(start)
previous_e = 'E%.8g' % pos['E']
previous_f = 'F%.5g' % pos['F']
for step in range(steps):
fraction = float(step + 1) / steps
d = delta(start + (finish - start) * fraction)
print 'G1', d.gcode(),
# Extruder steps.
e = pos['E'] + (dest['E'] - pos['E']) * fraction
e = 'E%.8g' % e
if e != previous_e:
previous_e = e
print e,
# Feedrate needs to be adjusted for delta geometry.
f = pos['F'] + (dest['F'] - pos['F']) * fraction
if abs(cartesian_mm) > 0.1:
delta_mm = abs(d - previous)
f *= delta_mm / cartesian_mm
f = 'F%.5g' % f
if f != previous_f:
previous_f = f
print f,
print
previous = d
num_lines += 1
for char in 'XYZEF':
pos[char] = dest[char]
def G28(pos, dest):
"""Home all axes."""
if 'X' in dest:
pos['X'] = -100 * SIN_60
pos['Y'] = 100 * COS_60
pos['Z'] = 0
if 'Y' in dest:
pos['X'] = 100 * SIN_60
pos['Y'] = 100 * COS_60
pos['Z'] = 0
if 'Z' in dest or not dest:
pos['X'] = 0
pos['Y'] = 100
pos['Z'] = 0
pos = {}
for char in 'XYZEF':
pos[char] = 0.0
num_lines = 0
for line in sys.stdin:
words = line.split()
if not words:
print
continue
dest = {}
for word in words[1:]:
if word.startswith(';'):
break
for char in 'XYZF':
if word.startswith(char):
dest[char] = float(word[1:]) - CENTER.get(char, 0)
if words[0] == 'G1':
print ';', line.strip()
G1(pos, dest)
if num_lines > 10000:
sys.exit(0)
elif words[0] == 'G28':
G28(pos, dest)
print line.strip()
else:
print line.rstrip()