Repository navigation
Expand file tree
/
Copy pathGDecoderLine.py
More file actions
662 lines (633 loc) · 29 KB
/
Copy pathGDecoderLine.py
File metadata and controls
662 lines (633 loc) · 29 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# Decode a single line of gcode
#
# Limitation:
# The code expects a single command in each line.
# It will not work with multiple gcode commands in a single line.
#
# Hint: Many gcode details can be found at:
# https://reprap.org/wiki/G-code
# https://marlinfw.org/meta/gcode/
class GDecoderLine:
stop_on_undecoded = False
def error_undecoded_gcode(self, line):
message = "Unknown gcode: " + str(line)
if self.stop_on_undecoded:
raise Exception(message)
else:
return message
def error_undecoded_gcode_subtoken(self, splitted, token):
message = "Unknown subtoken: " + token + " in: " + str(splitted)
if self.stop_on_undecoded:
raise Exception(message)
else:
return ", " + message
def error_firmware_dependent_but_unknown_generator(self, meta_infos, splitted):
message = "Unexpected generator " + meta_infos.generator + " for firmware dependent: " + str(splitted)
if self.stop_on_undecoded:
raise Exception(message)
else:
return message
def error_value_out_of_range(self, line, value):
message = "Value " + value + " out of range in: " + line
if self.stop_on_undecoded:
raise Exception(message)
else:
return ", " + message
def decode_gcode_line(self, meta_infos, line, printer):
decoded = ""
if line == "":
return ""
# avoid a strange gcode line (bug in Slic3r 1.37.2.1-prusa3d-win64?)
if line == "Filament-specific end gcode":
return ""
# ignore comments and split into tokens
uncommented_part = line.split(";")[0]
splitted = uncommented_part.split()
match splitted[0]:
# G0: Rapid Move
# https://marlinfw.org/docs/gcode/G000-G001.html
case "G0":
x = ""
y = ""
z = ""
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "F":
decoded += ", Feedrate: " + value + " " + printer.unit + "/min"
printer.set_feedrate(value)
case "G":
decoded = "Rapid Move (no print)"
case "X":
decoded += ", X: " + value + " " + printer.unit
x = value
case "Y":
decoded += ", Y: " + value + " " + printer.unit
y = value
case "Z":
decoded += ", Z: " + value + " " + printer.unit
z = value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
# call move() after the (optional) feedrate was set
printer.move(x, y, z)
return decoded
# G1: Linear Move
# https://marlinfw.org/docs/gcode/G000-G001.html
case "G1":
x = ""
y = ""
z = ""
e = ""
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E: " + value + " " + printer.unit
e = value
case "F":
decoded += ", Feedrate: " + value + " " + printer.unit + "/min"
printer.set_feedrate(value)
case "G":
decoded = "Linear Move (print)"
case "X":
decoded += ", X: " + value + " " + printer.unit
x = value
case "Y":
decoded += ", Y: " + value + " " + printer.unit
y = value
case "Z":
decoded += ", Z: " + value + " " + printer.unit
z = value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
# call printLinear() after the feedrate was set
printer.print_linear(x, y, z, e)
return decoded
# G2: Controlled Clockwise Arc Move
case "G2":
x = ""
y = ""
i = ""
j = ""
e = ""
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E: " + value + " " + printer.unit
e = value
case "F":
decoded += ", Feedrate: " + value + " " + printer.unit + "/min"
printer.set_feedrate(value)
case "G":
decoded = "Clockwise Arc Move (print)"
case "I":
decoded += ", I (distant X): " + value
i = value
case "J":
decoded += ", J (distant Y): " + value
j = value
case "X":
decoded += ", X:" + value
x = value
case "Y":
decoded += ", Y:" + value
y = value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
printer.print_cw(x, y, i, j, e)
return decoded
# G3: Controlled Counter-Clockwise Arc Move
case "G3":
x = ""
y = ""
i = ""
j = ""
e = ""
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E: " + value + " " + printer.unit
e = value
case "F":
decoded += ", Feedrate: " + value + " " + printer.unit + "/min"
printer.set_feedrate(value)
case "G":
decoded = "Counter-Clockwise Arc Move (print)"
case "I":
decoded += ", I (distant X): " + value
i = value
case "J":
decoded += ", J (distant Y): " + value
j = value
case "X":
decoded += ", X:" + value
x = value
case "Y":
decoded += ", Y:" + value
y = value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
printer.print_ccw(x, y, i, j, e)
return decoded
# G4: Dwell (aka Pause)
case "G4":
for token in splitted:
key = token[0]
match key:
case "G":
decoded = "Dwell (aka: Pause)"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# G21: Set Units to Millimeters
case "G21":
for token in splitted:
key = token[0]
match key:
case "G":
decoded = "Set Units to Millimeters"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# G28: Move to Origin (Home)
# https://all3dp.com/2/g28-g-code-homing/
# https://marlinfw.org/docs/gcode/G028.html
# https://cncphilosophy.com/g28-g-code-demystified/
case "G28":
new_x = ""
new_y = ""
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "F":
decoded += ", unknown parameter (maybe feedrate?): " + token
# TODO: What is this doing exactly?
case "G":
decoded = "Move to Origin (Home, often: X=0, Y=0; Z=0)"
case "W":
decoded += ", Suppress mesh bed leveling (Prusa only)"
case "X":
decoded += ", X axis origin: " + value
new_x = value
case "Y":
decoded += ", Y axis origin: " + value
new_y = value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
printer.home(new_x, new_y)
return decoded
# G80: Mesh-based Z probe
# https://marlinfw.org/docs/gcode/G080.html G80 - Cancel Current Motion Mode
case "G80":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "G":
# TODO: What is this doing exactly?
decoded = "Mesh-based Z probe"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# G90: Set to Absolute Positioning
# https://all3dp.com/2/g91-g90-g-code/
# https://marlinfw.org/docs/gcode/G090.html
case "G90":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "G":
decoded = "Set to Absolute Positioning"
printer.set_positioning_mode("absolute")
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# G91: Set to Relative Positioning
# https://all3dp.com/2/g91-g90-g-code/
# https://marlinfw.org/docs/gcode/G091.html
case "G91":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "G":
decoded = "Set to Relative Positioning"
printer.set_positioning_mode("relative")
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# G92: Set Position
case "G92":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", new extruder position: " + value + " " + printer.unit
printer.set_extruder_position(value)
case "G":
decoded = "Set Position"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M73: Set/Get build percentage
case "M73":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set/Get build percentage"
case "P":
decoded += ", Normal mode: " + value + " %"
case "Q":
decoded += ", Silent mode: " + value + " %"
case "R":
decoded += ", Remaining in normal mode: " + value + " min."
case "S":
decoded += ", Remaining in silent mode: " + value + " min."
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M82: Set extruder to absolute mode
case "M82":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set extruder to absolute mode"
printer.set_extruder_move_mode("absolute")
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M83: Set extruder to relative mode
case "M83":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set extruder to relative mode"
printer.set_extruder_move_mode("relative")
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M84: Stop idle hold
case "M84":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Stop idle hold (disable motors)"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M104: Set Extruder Temperature
case "M104":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set Extruder Temperature"
case "S":
decoded += ", Target: " + value + " °C"
printer.set_extruder_temperature(value)
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M105: Get Extruder Temperature
case "M105":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Get Extruder Temperature"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M106: Fan On
case "M106":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Fan On"
case "S":
decoded += ", Fan Speed: " + value + " (0-255)"
printer.set_fan(value)
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M107: Fan Off
case "M107":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Fan Off"
printer.set_fan("0")
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M109: Set Extruder Temperature and Wait
case "M109":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set Extruder Temperature and Wait"
case "S":
decoded += ", Target: " + value + " °C"
printer.set_extruder_temperature(value)
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M115: Get Firmware Version and Capabilities
case "M115":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Get Firmware Version and Capabilities"
case "U":
decoded += ", Check the firmware version: " + value
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M117: Display Message
case "M117":
decoded = "Display Message: \"" + line[5:] + "\""
return decoded
# M140: Set Bed Temperature (Fast)
case "M140":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Set Bed Temperature (Fast)"
case "S":
decoded += ", Target: " + value + " °C"
printer.set_bed_temperature(value)
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M190: Wait for bed temperature to reach target temp
case "M190":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded += "Wait for bed temperature to reach target temp"
case "S":
decoded += ", Target: " + value + " °C"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M201: Set max acceleration
case "M201":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E: " + value + " " + printer.unit + "/s²"
case "M":
decoded = "Set max acceleration"
case "X":
decoded += ", X: " + value + " " + printer.unit + "/s²"
case "Y":
decoded += ", Y: " + value + " " + printer.unit + "/s²"
case "Z":
decoded += ", Z: " + value + " " + printer.unit + "/s²"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M203: Firmware dependent
case "M203":
decoded = ""
if meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
# M203: Set maximum feedrate
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E: " + value + " " + printer.unit + "/s²"
case "M":
decoded += "Set maximum feedrate"
case "X":
decoded += ", X: " + value + " " + printer.unit + "/s²"
case "Y":
decoded += ", Y: " + value + " " + printer.unit + "/s²"
case "Z":
decoded += ", Z: " + value + " " + printer.unit + "/s²"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M204: Firmware dependent
# https://reprap.org/wiki/G-code#M204:_Firmware_dependent
case "M204":
decoded = ""
if meta_infos.generator != "PrusaSlicer" and \
meta_infos.generator != "Cura" and \
meta_infos.generator != "Slic3r":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
if meta_infos.generator == "Cura" and meta_infos.generator_flavor != "Marlin":
decoded += self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
# M204: Set default acceleration
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded += "Set default acceleration"
case "P":
decoded += ", printing: " + value + " " + printer.unit + "/s²"
case "R":
decoded += ", retract: " + value + " " + printer.unit + "/s²"
case "S":
decoded += ", normal: " + value + " " + printer.unit + "/s²"
case "T":
decoded += ", travel: " + value + " " + printer.unit + "/s²"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M205: Firmware dependent
# https://reprap.org/wiki/G-code#M205:_Firmware_dependent
case "M205":
decoded = ""
if meta_infos.generator != "PrusaSlicer" and meta_infos.generator != "Cura":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
if meta_infos.generator == "Cura" and meta_infos.generator_flavor != "Marlin":
decoded += self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
# M205: Advanced Settings
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
decoded += ", E jerk: " + value + " " + printer.unit + "/s"
case "M":
decoded += "Advanced settings"
case "S":
decoded += ", min. print speed: " + value + " " + printer.unit + "/s"
case "T":
decoded += ", min. travel speed: " + value + " " + printer.unit + "/s"
case "X":
decoded += ", X Jerk: " + value + " " + printer.unit + "/s"
case "Y":
decoded += ", Y Jerk: " + value + " " + printer.unit + "/s"
case "Z":
decoded += ", Z Jerk: " + value + " " + printer.unit + "/s"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M221: Set extrude factor override percentage
case "M221":
decoded = ""
if meta_infos.generator != "Slic3r" and meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded += "Set extrude factor override percentage"
case "S":
decoded += ", Extrude factor override percentage: " + value + " %"
# TODO: What is this doing exactly?
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M300: Play beep sound
case "M300":
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded = "Play beep sound"
case "P":
decoded += ", duration: " + value + " ms"
case "S":
decoded += ", frequency: " + value + " Hz"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M862.1: Check nozzle diameter (prusa only)
case "M862.1":
decoded = ""
if meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
return decoded + "Check nozzle diameter (prusa only, undecoded): " + str(splitted)
# M862.3: Model name (prusa only)
case "M862.3":
decoded = ""
if meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
return decoded + "Model name (prusa only, undecoded): " + str(splitted)
# M900 Set Linear Advance Scaling Factors
# https://marlinfw.org/docs/features/lin_advance.html
case "M900":
decoded = ""
if meta_infos.generator != "Slic3r" and meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "M":
decoded += "Set Linear Advance Scaling Factors"
case "K":
decoded += ", Advance K factor: " + value
# TODO: What is this doing exactly?
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
# M907: Set digital trimpot motor current
case "M907":
decoded = ""
if meta_infos.generator != "PrusaSlicer":
decoded = self.error_firmware_dependent_but_unknown_generator(meta_infos, splitted) + " "
for token in splitted:
key = token[0]
value = token[1:]
match key:
case "E":
if float(value) <= 2.50:
unit = "A"
elif float(value) <= 200:
unit = "%"
elif float(value) <= 2500:
unit = "mA"
else:
decoded += self.error_value_out_of_range(line, value)
unit = "?"
decoded += ", Set E stepper current: " + value + " " + unit
case "M":
decoded += "Set digital trimpot motor current"
case _:
decoded += self.error_undecoded_gcode_subtoken(splitted, token)
return decoded
case _:
return self.error_undecoded_gcode(line)