forked from spegelius/filaswitch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgcode.py
More file actions
454 lines (407 loc) · 15.6 KB
/
gcode.py
File metadata and controls
454 lines (407 loc) · 15.6 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import math
import re
import utils
E = 0
NE = E + 45
N = NE + 45
NW = N + 45
W = NW + 45
SW = W + 45
S = SW + 45
SE = S + 45
TYPE_CARTESIAN = 0
TYPE_DELTA = 1
class GCode:
EXTRUDER_MOVE_RE = re.compile(b"^G1\s+E([-]*\d+\.\d+)\s+F(\d+\.*\d*)$")
Z_MOVE_RE = re.compile(b"^G1\s+Z([-]*\d+\.\d+)\s+F(\d+\.*\d*)$")
EXTRUSION_MOVE_RE = re.compile(b"^G1\s+X([-]*\d+\.\d+)\s+Y([-]*\d+\.\d+)\s+E([-]*\d+\.\d+)$")
EXTRUSION_MOVE_SPEED_RE = re.compile(b"^G1\s+X([-]*\d+\.\d+)\s+Y([-]*\d+\.\d+)\s+E([-]*\d+\.\d+)\s+F(\d+\.*\d*)$")
MOVE_HEAD_RE = re.compile(b"^G1\s+X([-]*\d+\.\d+)\s+Y([-]*\d+\.\d+)\s+F(\d+\.*\d*)$")
SPEED_RE = re.compile(b"^G1\s+F(\d+\.*\d*)$")
EXTRUDER_POSITION_RE = re.compile(b"^G92\s+E0$")
TOOL_RE = re.compile(b"T(\d)")
RELATIVE_POSITIONING_RE = re.compile(b"^G91")
ABSOLUTE_POSITIONING_RE = re.compile(b"^G90")
TEMP_NOWAIT_RE = re.compile(b"M104\s+S(\d+)$")
TEMP_NOWAIT_TOOL_RE = re.compile(b"M104\s+S(\d+)\s+T(\d)$")
TEMP_WAIT_RE = re.compile(b"M109\s+S(\d+)$")
TEMP_WAIT_TOOL_RE = re.compile(b"M109\s+S(\d+)\s+T(\d)$")
def __init__(self):
self.last_match = None
def read_gcode_line(self, line):
"""
Read given line and return it split to g-code and comment.
:param line: g-code line
:return: g-code, comment
"""
if line.startswith(b";"):
return None, line.split(b";", 1)[1]
values = line.split(b";", 1)
l = values[0]
if len(values) == 2:
return l, values[1]
return l, None
def format_to_string(self, cmd, comment):
"""
Format given g-code command and optional comment to byte string
:param cmd: command
:param comment: comment
:return: byte string
"""
if cmd and not comment:
return cmd
elif comment is not None and not cmd:
return b";" + comment
else:
return cmd + b";" + comment
def calculate_path_length(self, prev_position, new_position):
""" Calculate path length from given coordinates"""
x_len = prev_position[0] - new_position[0]
y_len = prev_position[1] - new_position[1]
path_len = math.sqrt((x_len * x_len) + (y_len * y_len))
return path_len
def calculate_feed_rate(self, path_len, extrusion_length):
"""
Calculate the feed rate for given path
:param path_len: length of path
:param extrusion_length: length of extrusion
:return: feed rate (mm of filament/1 mm path)
"""
if not path_len or not extrusion_length:
return 0
rate = extrusion_length / path_len
return rate
def is_tool_change(self, line):
"""
Match given line against tool change regex
:param line: g-code line
:return: None or tool number
"""
self.last_match = None
m = self.TOOL_RE.match(line)
if m:
self.last_match = int(m.groups()[0])
return self.last_match
def is_extrusion_move(self, line):
"""
Match given line against extrusion move regex
:param line: g-code line
:return: None or tuple with X, Y and E positions
"""
self.last_match = None
m = self.EXTRUSION_MOVE_RE.match(line)
if m:
g = m.groups()
self.last_match = float(g[0]), float(g[1]), float(g[2])
return self.last_match
def is_extrusion_speed_move(self, line):
"""
Match given line against extrusion speed move regex
:param line: g-code line
:return: None or tuple with X, Y, E positions and speed
"""
self.last_match = None
m = self.EXTRUSION_MOVE_SPEED_RE.match(line)
if m:
g = m.groups()
self.last_match = float(g[0]), float(g[1]), float(g[2]), float(g[3])
return self.last_match
def is_z_move(self, line):
"""
Match given line against z move regex
:param line: g-code line
:return: None or z value
"""
self.last_match = None
m = self.Z_MOVE_RE.match(line)
if m:
self.last_match = float(m.groups()[0]), float(m.groups()[1])
return self.last_match
def is_extruder_move(self, line):
"""
Match given line against extruder move regex
:param line: g-code line
:return: None or extruder position and speed
"""
self.last_match = None
m = self.EXTRUDER_MOVE_RE.match(line)
if m:
self.last_match = float(m.groups()[0]), float(m.groups()[1])
return self.last_match
def is_head_move(self, line):
"""
Match given line against heade move regex
:param line: g-code line
:return: None or head position and speed
"""
self.last_match = None
m = self.MOVE_HEAD_RE.match(line)
if m:
self.last_match = float(m.groups()[0]), float(m.groups()[1]), float(m.groups()[2])
return self.last_match
def is_relative_positioning(self, line):
"""
Match given line against relative positioning regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.RELATIVE_POSITIONING_RE.match(line)
return m is not None
def is_absolute_positioning(self, line):
"""
Match given line against absolute positioning regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.ABSOLUTE_POSITIONING_RE.match(line)
return m is not None
def is_temp_nowait(self, line):
"""
Match given line against temperature change no wait regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.TEMP_NOWAIT_RE.match(line)
if m:
self.last_match = int(m.groups()[0])
return self.last_match
def is_temp_nowait_tool(self, line):
"""
Match given line against temperature change no wait tool regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.TEMP_NOWAIT_TOOL_RE.match(line)
if m:
self.last_match = int(m.groups()[0]), int(m.groups()[1])
return self.last_match
def is_temp_wait(self, line):
"""
Match given line against temperature change wait regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.TEMP_WAIT_RE.match(line)
if m:
self.last_match = int(m.groups()[0])
return self.last_match
def is_temp_wait_tool(self, line):
"""
Match given line against temperature change wait tool regex
:param line: g-code line
:return: boolean
"""
self.last_match = None
m = self.TEMP_WAIT_TOOL_RE.match(line)
if m:
self.last_match = int(m.groups()[0]), int(m.groups()[1])
return self.last_match
def gen_head_move(self, x, y, speed):
"""
Generate g-code line for head move
:param x: x coordinate
:param y: y coordinate
:param speed: movement speed
:return: byte string
"""
if utils.is_float_zero(x, 3):
return ("G1 Y%.3f F%d" % (y, speed)).encode()
elif utils.is_float_zero(y, 3):
return ("G1 X%.3f F%d" % (x, speed)).encode()
return ("G1 X%.3f Y%.3f F%d" % (x, y, speed)).encode()
def gen_extrusion_move(self, x, y, e_length):
"""
Generate g-code line for extrusion move. Relative distances
:param x: x coordinate
:param y: y coordinate
:param e_length: extrusion length
:return: byte string
"""
if utils.is_float_zero(x, 3):
return ("G1 Y%.3f E%.4f" % (y, e_length)).encode()
elif utils.is_float_zero(y, 3):
return ("G1 X%.3f E%.4f" % (x, e_length)).encode()
return ("G1 X%.3f Y%.3f E%.4f" % (x, y, e_length)).encode()
def gen_extrusion_speed_move(self, x, y, speed, e_length):
"""
Generate g-code line for extrusion move with speed. Relative distances
:param x: x coordinate
:param y: y coordinate
:param speed: movement speed
:param e_length: extrusion length
:return: byte string
"""
if utils.is_float_zero(x, 3):
return ("G1 Y%.3f E%.4f F%d" % (y, e_length, speed)).encode()
elif utils.is_float_zero(y, 3):
return ("G1 X%.3f E%.4f F%d" % (x, e_length, speed)).encode()
return ("G1 X%.3f Y%.3f E%.4f F%d" % (x, y, e_length, speed)).encode()
def gen_extruder_move(self, e_length, speed):
"""
Generate g-code line for extruder move with given length and speed.
:param e_length: extruder move length
:param speed: move speed
:return: byte string
"""
return ("G1 E%.4f F%d" % (e_length, speed)).encode()
def gen_z_move(self, z, speed):
"""
Generate g-code line for z move with given z-position and speed.
:param z: z position
:param speed: move speed
:return: byte string
"""
return ("G1 Z%.4f F%d" % (z, speed)).encode()
def gen_temperature_nowait(self, temperature):
"""
Generate g-code line for temperature change with no wait.
:param temperature: temperature to set
:return: byte string
"""
return ("M104 S%d" % temperature).encode()
def gen_temperature_nowait_tool(self, temperature, tool):
"""
Generate g-code line for temperature change with no wait and specific tool.
:param temperature: temperature to set
:param tool: tool to use
:return: byte string
"""
return ("M104 S%d T%d" % (temperature, tool)).encode()
def gen_temperature_wait(self, temperature):
"""
Generate g-code line for temperature change with wait.
:param temperature: temperature to set
:return: byte string
"""
return ("M109 S%d" % temperature).encode()
def gen_temperature_wait_tool(self, temperature, tool):
"""
Generate g-code line for temperature change with wait and specific tool.
:param temperature: temperature to set
:param tool: tool to use
:return: byte string
"""
return ("M109 S%d T%d" % (temperature, tool)).encode()
def _get_coordinates(self, direction, length):
"""
Calculate coordinates from given direction and length. If coasting, calculate those coordinates too.
Relative to 0,0
:param direction:
:param length:
:param coasting:
:return: tuple
"""
angle = math.radians(direction)
cosine = math.cos(angle)
sine = math.sin(angle)
x = cosine * length
y = sine * length
return x, y
def gen_direction_move(self, direction, length, speed, extruder=None, feed_rate=None, last_line=False):
"""
Generate g-code for head move to given direction. Relative distances
:param direction: direction to move to (DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT)
:param length: move length
:param speed: move speed
:param extruder: extruder object or None
:param feed_rate: feed rate override
:param last_line: last line before move. Used only with extrusion moves
:return:
"""
if extruder and extruder.coasting and last_line:
_length = abs(length) - extruder.coasting
x, y = self._get_coordinates(direction, _length)
c_x, c_y = self._get_coordinates(direction, extruder.coasting)
e_length = extruder.get_feed_length(_length, feed_rate=feed_rate)
yield self.gen_extrusion_speed_move(x, y, speed, e_length)
yield self.gen_head_move(c_x, c_y, speed)
else:
_length = abs(length)
x, y = self._get_coordinates(direction, _length)
if not extruder:
yield self.gen_head_move(x, y, speed)
else:
e_length = extruder.get_feed_length(_length, feed_rate=feed_rate)
yield self.gen_extrusion_speed_move(x, y, speed, e_length)
def get_coordinates_by_offsets(self, direction, start_x, start_y, offset_x, offset_y):
"""
Calculate new x-y coordinates by given direction and offsets
:param direction: direction to go
:param start_x: start position
:param start_y: start position
:param offset_x: offset x
:param offset_y: offset y
:return: tuple with new x-y coordinates
"""
length = math.sqrt(offset_x ** 2 + offset_y ** 2)
if offset_x < 0 and offset_y >= 0:
angle = math.atan(abs(offset_x) / abs(offset_y))
new_angle = math.degrees(angle) + direction + 90
elif offset_x < 0 and offset_y < 0:
angle = math.atan(abs(offset_y) / abs(offset_x))
new_angle = math.degrees(angle) + direction + 180
elif offset_x >= 0 and offset_y < 0:
angle = math.atan(abs(offset_x) / abs(offset_y))
new_angle = math.degrees(angle) + direction + 270
else:
angle = math.atan(abs(offset_y) / abs(offset_x))
new_angle = math.degrees(angle) + direction
x, y = self._get_coordinates(new_angle, length)
return start_x + x, start_y + y
if __name__ == "__main__":
# test stuff
obj = GCode()
print(obj.is_extruder_move(b"G1 E-2.5 F1500"))
print(obj.read_gcode_line(b"G1 E5 F1500 ; juu"))
print(obj.read_gcode_line(b"; juu"))
print(obj.read_gcode_line(b"; juu ; joo"))
print(obj.is_z_move(b"G1 Z5.500 F1500"))
print(obj.is_tool_change(b"T0"))
print(obj.is_tool_change(b"T1"))
print(obj.is_extrusion_move(b"G1 X80.349 Y81.849 E-2.5000"))
print(obj.is_extrusion_speed_move(b"G1 X80.349 Y81.849 E-2.5000 F3000"))
print(obj.read_gcode_line(b"G1 E-3.00000 F4800.00000"))
print(obj.is_extruder_move(b'G1 E-3.00000 F4800.00000'))
ret = obj.gen_direction_move(E, 40, 3000)
for r in ret:
print(r)
import extruder
e = extruder.Extruder(0)
e.coasting = 0.2
ret = obj.gen_direction_move(W, 40, 3000, e, feed_rate=0.05)
for r in ret:
print(r)
ret = obj.gen_direction_move(S, 40, 3000, e, feed_rate=0.05, last_line=True)
for r in ret:
print(r)
print(obj._get_coordinates(N, 10))
print(obj._get_coordinates(NE, 10))
print(obj._get_coordinates(E, 10))
print(obj._get_coordinates(SE, 10))
print(obj._get_coordinates(S, 10))
print(obj._get_coordinates(SW, 10))
print(obj._get_coordinates(W, 10))
print(obj._get_coordinates(NW, 10))
print(obj._get_coordinates(30, 10))
print(obj._get_coordinates(80, 10))
print(obj._get_coordinates(100, 10))
print(obj._get_coordinates(200, 10))
print(obj._get_coordinates(346, 10))
print("------------------------")
print(obj.get_coordinates_by_offsets(0, 10, 10, 1, 15))
print(obj.get_coordinates_by_offsets(1, 10, 10, 1, 15))
print(obj.get_coordinates_by_offsets(0, 10, 10, -3, 15))
print(obj.get_coordinates_by_offsets(10, 10, 10, -3, 15))
print(obj.get_coordinates_by_offsets(10, 10, 10, 2, 1))
print(obj.get_coordinates_by_offsets(10, 10, 10, -4, -2))
print(obj.get_coordinates_by_offsets(10, 10, 10, -2, -4))
print(obj.get_coordinates_by_offsets(10, 10, 10, 2, -4))
print(obj.is_temp_nowait(b"M104 S255"))
print(obj.is_temp_nowait_tool(b"M104 S255 T0"))
print(obj.is_temp_wait(b"M109 S255"))
print(obj.is_temp_wait_tool(b"M109 S255 T0"))