forked from joakimtoe/KlipperLCD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.py
671 lines (587 loc) · 19.4 KB
/
printer.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
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
663
664
665
666
667
668
669
670
671
from asyncio.tasks import sleep
import threading
import errno
import select
import socket
import json
import requests
from requests.exceptions import ConnectionError
import atexit
import time
import asyncio
class xyze_t:
x = 0.0
y = 0.0
z = 0.0
e = 0.0
home_x = False
home_y = False
home_z = False
updated = False
class AxisEnum:
X_AXIS = 0
A_AXIS = 0
Y_AXIS = 1
B_AXIS = 1
Z_AXIS = 2
C_AXIS = 2
E_AXIS = 3
X_HEAD = 4
Y_HEAD = 5
Z_HEAD = 6
E0_AXIS = 3
E1_AXIS = 4
E2_AXIS = 5
E3_AXIS = 6
E4_AXIS = 7
E5_AXIS = 8
E6_AXIS = 9
E7_AXIS = 10
ALL_AXES = 0xFE
NO_AXIS = 0xFF
class HMI_value_t:
E_Temp = 0
Bed_Temp = 0
Fan_speed = 0
print_speed = 100
Max_Feedspeed = 0.0
Max_Acceleration = 0.0
Max_Jerk = 0.0
Max_Step = 0.0
Move_X_scale = 0.0
Move_Y_scale = 0.0
Move_Z_scale = 0.0
Move_E_scale = 0.0
offset_value = 0.0
show_mode = 0 # -1: Temperature control 0: Printing temperature
class HMI_Flag_t:
language = 0
pause_flag = False
pause_action = False
print_finish = False
done_confirm_flag = False
select_flag = False
home_flag = False
heat_flag = False # 0: heating done 1: during heating
ETempTooLow_flag = False
leveling_offset_flag = False
feedspeed_axis = AxisEnum()
acc_axis = AxisEnum()
jerk_axis = AxisEnum()
step_axis = AxisEnum()
class buzz_t:
def tone(self, t, n):
pass
class material_preset_t:
def __init__(self, name, hotend_temp, bed_temp, fan_speed=100):
self.name = name
self.hotend_temp = hotend_temp
self.bed_temp = bed_temp
self.fan_speed = fan_speed
class KlippySocket:
def __init__(self, uds_filename, callback=None):
self.connected = False
self.webhook_socket_create(uds_filename)
self.lock = threading.Lock()
self.poll = select.poll()
self.stop_threads = False
self.poll.register(self.webhook_socket, select.POLLIN | select.POLLHUP)
self.socket_data = ""
self.t = threading.Thread(target=self.polling)
self.callback = callback
self.lines = []
self.t.start()
atexit.register(self.klippyExit)
def klippyExit(self):
print("Shuting down Klippy Socket")
self.stop_threads = True
self.t.join()
def webhook_socket_create(self, uds_filename):
self.webhook_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.webhook_socket.setblocking(0)
print("Waiting for connect to %s\n" % (uds_filename,))
while 1:
try:
self.webhook_socket.connect(uds_filename)
except socket.error as e:
if e.errno == errno.ECONNREFUSED:
time.sleep(0.1)
continue
print(
"Unable to connect socket %s [%d,%s]\n" % (
uds_filename, e.errno,
errno.errorcode[e.errno]
))
exit(-1)
break
print("Connection.\n")
self.connected = True
def process_socket(self):
data = None
try:
data = self.webhook_socket.recv(4096).decode()
except:
pass
if not data:
self.connected = False
print("Socket closed\n")
exit(-1)
parts = data.split('\x03')
parts[0] = self.socket_data + parts[0]
self.socket_data = parts.pop()
for line in parts:
if self.callback:
self.callback(line)
def queue_line(self, line):
with self.lock:
self.lines.append(line)
def send_line(self):
if len(self.lines) == 0:
return
line = self.lines.pop(0).strip()
if not line or line.startswith('#'):
return
try:
m = json.loads(line)
except JSONDecodeError:
print("ERROR: Unable to parse line\n")
return
cm = json.dumps(m, separators=(',', ':'))
wdm = '{}\x03'.format(cm)
self.webhook_socket.send(wdm.encode())
def polling(self):
while True:
if self.stop_threads:
break
res = self.poll.poll(1000.)
for fd, event in res:
self.process_socket()
with self.lock:
self.send_line()
class MoonrakerSocket:
def __init__(self, address, port, api_key):
self.s = requests.Session()
self.s.headers.update({
'X-Api-Key': api_key,
'Content-Type': 'application/json'
})
self.base_address = 'http://' + address + ':' + str(port)
class PrinterData:
event_loop = None
HAS_HOTEND = True
HOTENDS = 1
HAS_HEATED_BED = True
HAS_FAN = False
HAS_ZOFFSET_ITEM = True
HAS_ONESTEP_LEVELING = False
HAS_PREHEAT = True
HAS_BED_PROBE = False
PREVENT_COLD_EXTRUSION = True
EXTRUDE_MINTEMP = 170
EXTRUDE_MAXLENGTH = 200
HEATER_0_MAXTEMP = 275
HEATER_0_MINTEMP = 5
HOTEND_OVERSHOOT = 15
MAX_E_TEMP = (HEATER_0_MAXTEMP - (HOTEND_OVERSHOOT))
MIN_E_TEMP = HEATER_0_MINTEMP
BED_OVERSHOOT = 10
BED_MAXTEMP = 150
BED_MINTEMP = 5
BED_MAX_TARGET = (BED_MAXTEMP - (BED_OVERSHOOT))
MIN_BED_TEMP = BED_MINTEMP
X_MIN_POS = 0.0
Y_MIN_POS = 0.0
Z_MIN_POS = 0.0
Z_MAX_POS = 200
Z_PROBE_OFFSET_RANGE_MIN = -20
Z_PROBE_OFFSET_RANGE_MAX = 20
buzzer = buzz_t()
material_preset = [
material_preset_t('PLA', 200, 60),
material_preset_t('ABS', 210, 100)
]
files = None
MACHINE_SIZE = "220x220x250"
SHORT_BUILD_VERSION = "1.00"
CORP_WEBSITE_E = "https://www.klipper3d.org/"
def __init__(self, API_Key, URL='127.0.0.1', klippy_sock='/home/pi/printer_data/comms/klippy.sock', callback=None):
self.response_callback = callback
self.klippy_sock = klippy_sock
self.BABY_Z_VAR = 0
self.print_speed = 100
self.flow_percentage = 100
self.led_percentage = 0
self.temphot = 0
self.tempbed = 0
self.HMI_ValueStruct = HMI_value_t()
self.HMI_flag = HMI_Flag_t()
self.current_position = xyze_t()
self.gcm = None
self.z_offset = 0
self.thermalManager = {
'temp_bed': {'celsius': 20, 'target': 120},
'temp_hotend': [{'celsius': 20, 'target': 120}],
'fan_speed': [100]
}
self.job_Info = None
self.file_path = None
self.file_name = None
self.status = None
self.max_velocity = None
self.max_accel = None
self.max_accel_to_decel = None
self.square_corner_velocity = None
self.op = MoonrakerSocket(URL, 80, API_Key)
print(self.op.base_address)
self.klippy_start()
self.event_loop = asyncio.new_event_loop()
threading.Thread(target=self.event_loop.run_forever, daemon=True).start()
# ------------- Klipper Function ----------
def klippy_start(self):
self.ks = KlippySocket(self.klippy_sock, callback=self.klippy_callback)
subscribe = {
"id": 4001,
"method": "objects/subscribe",
"params": {
"objects": {
"toolhead": [
"position"
]
},
"response_template": {}
}
}
self.klippy_z_offset = '{"id": 4002, "method": "objects/query", "params": {"objects": {"configfile": ["config"]}}}'
self.klippy_home = '{"id": 4003, "method": "objects/query", "params": {"objects": {"toolhead": ["homed_axes"]}}}'
self.gcode = '{"id": 4004, "method": "gcode/subscribe_output", "params": {"response_template":{}}}'
self.ks.queue_line(json.dumps(subscribe))
self.ks.queue_line(self.klippy_z_offset)
self.ks.queue_line(self.klippy_home)
self.ks.queue_line(self.gcode)
def klippy_callback(self, line):
klippyData = json.loads(line)
#print("klippy_callback:")
#print(json.dumps(klippyData, indent=2))
status = None
if 'result' in klippyData:
if 'status' in klippyData['result']:
status = klippyData['result']['status']
if 'params' in klippyData:
if 'status' in klippyData['params']:
status = klippyData['params']['status']
if 'response' in klippyData['params']:
if self.response_callback:
resp = klippyData['params']['response']
if 'B:' in resp and 'T0:' in resp:
pass ## Filter out temperature responses
else:
self.response_callback(resp, 'response')
if status:
if 'toolhead' in status:
if 'position' in status['toolhead']:
if self.current_position.x != status['toolhead']['position'][0]:
self.current_position.x = status['toolhead']['position'][0]
self.current_position.updated = True
if self.current_position.y != status['toolhead']['position'][1]:
self.current_position.y = status['toolhead']['position'][1]
self.current_position.updated = True
if self.current_position.z != status['toolhead']['position'][2]:
self.current_position.z = status['toolhead']['position'][2]
self.current_position.updated = True
if self.current_position.e != status['toolhead']['position'][3]:
self.current_position.e = status['toolhead']['position'][3]
self.current_position.updated = True
if 'homed_axes' in status['toolhead']:
if 'x' in status['toolhead']['homed_axes']:
self.current_position.home_x = True
else:
self.current_position.home_x = False
if 'y' in status['toolhead']['homed_axes']:
self.current_position.home_y = True
else:
self.current_position.home_y = False
if 'z' in status['toolhead']['homed_axes']:
self.current_position.home_z = True
else:
self.current_position.home_z = False
if 'max_velocity' in status['toolhead']:
if self.max_velocity != status['toolhead']['max_velocity']:
self.max_velocity = status['toolhead']['max_velocity']
if 'max_accel' in status['toolhead']:
if self.max_accel != status['toolhead']['max_accel']:
self.max_accel = status['toolhead']['max_accel']
if 'max_accel_to_decel' in status['toolhead']:
if self.max_accel_to_decel != status['toolhead']['max_accel_to_decel']:
self.max_accel_to_decel = status['toolhead']['max_accel_to_decel']
if 'square_corner_velocity' in status['toolhead']:
if self.square_corner_velocity != status['toolhead']['square_corner_velocity']:
self.square_corner_velocity = status['toolhead']['square_corner_velocity']
if 'configfile' in status:
if 'config' in status['configfile']:
if 'bltouch' in status['configfile']['config']:
if 'z_offset' in status['configfile']['config']['bltouch']:
if status['configfile']['config']['bltouch']['z_offset']:
self.BABY_Z_VAR = float(status['configfile']['config']['bltouch']['z_offset'])
if 'virtual_sdcard' in status['configfile']['config']:
if 'path' in status['configfile']['config']['virtual_sdcard']:
self.file_path = status['configfile']['config']['virtual_sdcard']['path']
def ishomed(self):
if self.current_position.home_x and self.current_position.home_y and self.current_position.home_z:
return True
else:
self.ks.queue_line(self.klippy_home)
return False
def offset_z(self, new_offset):
self.BABY_Z_VAR = new_offset
self.sendGCode('ACCEPT')
def add_mm(self, axs, new_offset):
gc = 'TESTZ Z={}'.format(new_offset)
print(axs, gc)
self.sendGCode(gc)
def probe_adjust(self, change):
gc = 'TESTZ Z={}'.format(change)
print(gc)
self.sendGCode(gc)
def probe_calibrate(self):
if self.ishomed() == False:
self.sendGCode('G28')
self.sendGCode('PROBE_CALIBRATE')
self.sendGCode('G1 Z0.0')
# ------------- OctoPrint Function ----------
def getREST(self, path):
r = self.op.s.get(self.op.base_address + path)
d = r.content.decode('utf-8')
try:
return json.loads(d)
except JSONDecodeError:
print('Decoding JSON has failed')
return None
async def _postREST(self, path, json):
self.op.s.post(self.op.base_address + path, json=json)
def postREST(self, path, json):
self.event_loop.call_soon_threadsafe(asyncio.create_task,self._postREST(path,json))
def init_Webservices(self):
try:
requests.get(self.op.base_address)
except ConnectionError:
print('Web site does not exist')
return
else:
print('Web site exists')
if self.getREST('/api/printer') is None:
return
self.update_variable()
#alternative approach
#full_version = self.getREST('/printer/info')['result']['software_version']
#self.SHORT_BUILD_VERSION = '-'.join(full_version.split('-',2)[:2])
self.SHORT_BUILD_VERSION = self.getREST('/machine/update/status?refresh=false')['result']['version_info']['klipper']['version']
data = self.getREST('/printer/objects/query?toolhead')['result']['status']
#print(json.dumps(data, indent=2))
toolhead = data['toolhead']
volume = toolhead['axis_maximum'] #[x,y,z,w]
self.MACHINE_SIZE = "{}x{}x{}".format(
int(volume[0]),
int(volume[1]),
int(volume[2])
)
self.X_MAX_POS = int(volume[0])
self.Y_MAX_POS = int(volume[1])
self.max_velocity = toolhead['max_velocity']
self.max_accel = toolhead['max_accel']
self.max_accel_to_decel = toolhead['max_accel_to_decel']
self.square_corner_velocity = toolhead['square_corner_velocity']
def get_gcode_store(self, count=100):
gcode_store = None
try:
gcode_store = self.getREST('/server/gcode_store?count=%d' % count)['result']['gcode_store']
except:
print("GCode store read failed!")
return gcode_store
def get_macros(self, filter_internal = True):
macros = []
try:
objects = self.getREST('/printer/objects/list')['result']['objects']
except:
print("Could not read macro objects!")
for obj in objects:
if 'gcode_macro' in obj:
macro = obj.split(' ')[1]
if filter_internal:
if macro[0] != '_':
macros.append(macro)
else:
macros.append(macro)
return macros
def GetFiles(self, refresh=False):
if not self.files or refresh:
try:
self.files = self.getREST('/server/files/list')["result"]
except:
print("Exception 418")
names = []
for fl in self.files:
names.append(fl["path"])
return names
def update_variable(self):
if self.ks.connected == False:
self.ks.klippyExit()
self.klippy_start()
return False
query = '/printer/objects/query?extruder&heater_bed&gcode_move&fan&print_stats&motion_report&toolhead'
try:
data = self.getREST(query)['result']['status']
except:
print("Exception 431")
return False
#print("update_variable:")
#print(json.dumps(data, indent=2))
self.gcm = data['gcode_move']
self.z_offset = self.gcm['homing_origin'][2] #z offset
self.flow_percentage = self.gcm['extrude_factor'] * 100 #flow rate percent
self.absolute_moves = self.gcm['absolute_coordinates'] #absolute or relative
self.absolute_extrude = self.gcm['absolute_extrude'] #absolute or relative
self.speed = self.gcm['speed'] #current speed in mm/s
self.print_speed = self.gcm['speed_factor'] * 100 #print speed percent
self.bed = data['heater_bed'] #temperature, target
self.extruder = data['extruder'] #temperature, target
self.fan = data['fan']
self.toolhead = data['toolhead']
Update = False
try:
if self.thermalManager['temp_bed']['celsius'] != int(self.bed['temperature']):
self.thermalManager['temp_bed']['celsius'] = int(self.bed['temperature'])
Update = True
if self.thermalManager['temp_bed']['target'] != int(self.bed['target']):
self.thermalManager['temp_bed']['target'] = int(self.bed['target'])
Update = True
if self.thermalManager['temp_hotend'][0]['celsius'] != int(self.extruder['temperature']):
self.thermalManager['temp_hotend'][0]['celsius'] = int(self.extruder['temperature'])
Update = True
if self.thermalManager['temp_hotend'][0]['target'] != int(self.extruder['target']):
self.thermalManager['temp_hotend'][0]['target'] = int(self.extruder['target'])
Update = True
if self.thermalManager['fan_speed'][0] != int((self.fan['speed'] * 100) + 0.5):
self.thermalManager['fan_speed'][0] = int((self.fan['speed'] * 100) + 0.5)
Update = True
if self.BABY_Z_VAR != self.z_offset:
self.BABY_Z_VAR = self.z_offset
self.HMI_ValueStruct.offset_value = self.z_offset * 100
Update = True
if self.max_velocity != self.toolhead['max_velocity']:
self.max_velocity = self.toolhead['max_velocity']
Update = True
if self.max_accel != self.toolhead['max_accel']:
self.max_accel = self.toolhead['max_accel']
Update = True
if self.max_accel_to_decel != self.toolhead['max_accel_to_decel']:
self.max_accel_to_decel = self.toolhead['max_accel_to_decel']
Update = True
if self.square_corner_velocity != self.toolhead['square_corner_velocity']:
self.square_corner_velocity = self.toolhead['square_corner_velocity']
Update = True
except:
pass #missing key, shouldn't happen, fixes misses on conditionals ¯\_(ツ)_/¯
try:
self.job_Info = self.getREST('/printer/objects/query?virtual_sdcard&print_stats')['result']['status']
except:
print("Exception 470")
return False
if self.job_Info:
self.file_name = self.job_Info['print_stats']['filename']
self.status = self.job_Info['print_stats']['state']
self.HMI_flag.print_finish = self.getPercent() == 100.0
return Update
def getState(self):
if self.job_Info:
return self.job_Info['print_stats']['state']
else:
return None
def printingIsPaused(self):
if self.job_Info:
return self.job_Info['print_stats']['state'] == "paused" or self.job_Info['print_stats']['state'] == "pausing"
else:
return None
def getPercent(self):
if self.job_Info:
if self.job_Info['virtual_sdcard']['is_active']:
return self.job_Info['virtual_sdcard']['progress'] * 100
return 0
def duration(self):
if self.job_Info:
if self.job_Info['virtual_sdcard']['is_active']:
return self.job_Info['print_stats']['print_duration']
return 0
def remain(self):
percent = self.getPercent()
duration = self.duration()
if percent:
total = duration / (percent / 100)
return total - duration
return 0
def openAndPrintFile(self, filenum):
self.file_name = self.files[filenum]['path']
self.postREST('/printer/print/start', json={'filename': self.file_name})
def cancel_job(self): #fixed
print('Canceling job:')
self.postREST('/printer/print/cancel', json=None)
def pause_job(self): #fixed
print('Pausing job:')
self.postREST('/printer/print/pause', json=None)
def resume_job(self): #fixed
print('Resuming job:')
self.postREST('/printer/print/resume', json=None)
def set_print_speed(self, fr):
self.print_speed = fr
self.sendGCode('M220 S%d' % fr)
def set_flow(self, fl):
self.flow_percentage = fl
self.sendGCode('M221 S%d' % fl)
def set_led(self, led):
self.led_percentage = led
if(led > 0):
self.sendGCode('SET_LED LED=top_LEDs WHITE=0.5 SYNC=0 TRANSMIT=1')
else:
self.sendGCode('SET_LED LED=top_LEDs WHITE=0 SYNC=0 TRANSMIT=1')
def set_fan(self, fan):
self.fan_percentage = fan
self.sendGCode('M106 S%s' % (int)(fan*255/100))
def home(self, axis): #fixed using gcode
GCode = 'G28 '
if axis == 'X' or axis == 'Y' or axis == 'Z' or axis == 'X Y Z':
GCode += axis
else:
print("home: parameter not recognised" + axis)
return
self.sendGCode(GCode)
def moveRelative(self, axis, distance, speed):
self.sendGCode('%s \n%s %s%s F%s%s' % ('G91', 'G1', axis, distance, speed,
'\nG90' if self.absolute_moves else ''))
def moveAbsolute(self, axis, position, speed):
self.sendGCode('%s \n%s %s%s F%s%s' % ('G90', 'G1', axis, position, speed,
'\nG91' if not self.absolute_moves else ''))
def sendGCode(self, gcode):
self.postREST('/printer/gcode/script', json={'script': gcode})
if self.response_callback:
self.response_callback(gcode, 'command')
def disable_all_heaters(self):
self.setExtTemp(0)
self.setBedTemp(0)
def zero_fan_speeds(self):
pass
def preheat(self, profile):
if profile == "PLA":
self.preHeat(self.material_preset[0].bed_temp, self.material_preset[0].hotend_temp)
elif profile == "ABS":
self.preHeat(self.material_preset[1].bed_temp, self.material_preset[1].hotend_temp)
def save_settings(self):
print('saving settings')
return True
def setExtTemp(self, target, toolnum=0):
self.sendGCode('M104 T%s S%s' % (toolnum, target))
def setBedTemp(self, target):
self.sendGCode('M140 S%s' % target)
def preHeat(self, bedtemp, exttemp, toolnum=0):
self.setBedTemp(bedtemp)
self.setExtTemp(exttemp)
def setZOffset(self, offset):
self.sendGCode('SET_GCODE_OFFSET Z=%s MOVE=1' % offset)