-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redeem.py
608 lines (499 loc) · 26.1 KB
/
Redeem.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
#!/usr/bin/python
"""
Redeem main program. This should run on the BeagleBone.
Author: Elias Bakken
email: elias(dot)bakken(at)gmail(dot)com
Website: http://www.thing-printer.com
License: GNU GPL v3: http://www.gnu.org/copyleft/gpl.html
Redeem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redeem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redeem. If not, see <http://www.gnu.org/licenses/>.
Minor version tag is Arnold Schwarzenegger movies chronologically.
"""
import glob
import shutil
import logging
from logging.handlers import SysLogHandler
import os
import os.path
import signal
from threading import Thread
from multiprocessing import JoinableQueue
import Queue
import numpy as np
import sys
import time
from Mosfet import Mosfet
from Stepper import Stepper, Stepper_00A3, Stepper_00A4, Stepper_00B1, Stepper_00B2
from Thermistor import Thermistor
from Fan import Fan
from Servo import Servo
from EndStop import EndStop
from USB import USB
from Pipe import Pipe
from Ethernet import Ethernet
from Extruder import Extruder, HBP
from Cooler import Cooler
from Path import Path
from PathPlanner import PathPlanner
from ColdEnd import ColdEnd
from PruFirmware import PruFirmware
from CascadingConfigParser import CascadingConfigParser
from Printer import Printer
from GCodeProcessor import GCodeProcessor
from PluginsController import PluginsController
from Delta import Delta
from Scara import Scara
from Enable import Enable
from PWM import PWM
version = "1.0.4~Commando"
# Default logging level is set to debug
#logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
# datefmt='%m-%d %H:%M')
#
# need to create a object type logger and use logger.info("msg") to stream to the syslog,
#
#syslog = logging.handlers.SysLogHandler(address = '/dev/log')
#logging.addHandler(syslog)
#
# first step toward an log file mechnism
#
def epoc_time(record, datefmt=None, dateArg3=None):
return ("[ %d ]"%(time.time()*1000))
logging.Formatter.formatTime = epoc_time
logging.basicConfig(filename="/var/log/redeem/redeem.log",
level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
class Redeem:
def __init__(self):
""" Init """
logging.info("Redeem initializing " + version)
printer = Printer()
self.printer = printer
# check for config files
if not os.path.exists("/etc/redeem/default.cfg"):
logging.error("/etc/redeem/default.cfg does not exist, this file is required for operation")
sys.exit() # maybe use something more graceful?
# Parse the config files.
printer.config = CascadingConfigParser(
['/etc/redeem/default.cfg', '/etc/redeem/printer.cfg',
'/etc/redeem/local.cfg'])
# Find out which capes are connected
self.printer.config.parse_capes()
self.revision = self.printer.config.replicape_revision
if self.revision:
logging.info("Found Replicape rev. " + self.revision)
Path.set_axes(5)
else:
logging.warning("Oh no! No Replicape present!")
self.revision = "0A4A"
# We set it to 5 axis by default
Path.set_axes(5)
if self.printer.config.reach_revision:
Path.set_axes(8)
logging.info("Found Reach rev. "+self.printer.config.reach_revision)
# Get the revision and loglevel from the Config file
level = self.printer.config.getint('System', 'loglevel')
if level > 0:
logging.getLogger().setLevel(level)
if self.revision in ["00A4", "0A4A", "00A3"]:
PWM.set_frequency(100)
elif self.revision in ["00B1", "00B2"]:
PWM.set_frequency(1000)
# Init the Paths
Path.axis_config = printer.config.getint('Geometry', 'axis_config')
# Init the end stops
EndStop.callback = self.end_stop_hit
EndStop.inputdev = self.printer.config.get("Endstops", "inputdev")
for es in ["X1", "X2", "Y1", "Y2", "Z1", "Z2"]:
pin = self.printer.config.get("Endstops", "pin_"+es)
keycode = self.printer.config.getint("Endstops", "keycode_"+es)
invert = self.printer.config.getboolean("Endstops", "invert_"+es)
self.printer.end_stops[es] = EndStop(pin, keycode, es, invert)
# Backwards compatibility with A3
if self.revision == "00A3":
# Init the 5 Stepper motors (step, dir, fault, DAC channel, name)
printer.steppers["X"] = Stepper_00A3("GPIO0_27", "GPIO1_29", "GPIO2_4" , 0, "X", 0, 0)
printer.steppers["Y"] = Stepper_00A3("GPIO1_12", "GPIO0_22", "GPIO2_5" , 1, "Y", 1, 1)
printer.steppers["Z"] = Stepper_00A3("GPIO0_23", "GPIO0_26", "GPIO0_15", 2, "Z", 2, 2)
printer.steppers["E"] = Stepper_00A3("GPIO1_28", "GPIO1_15", "GPIO2_1" , 3, "E", 3, 3)
printer.steppers["H"] = Stepper_00A3("GPIO1_13", "GPIO1_14", "GPIO2_3" , 4, "H", 4, 4)
elif self.revision == "00B1":
# Init the 5 Stepper motors (step, dir, fault, DAC channel, name)
printer.steppers["X"] = Stepper_00B1("GPIO0_27", "GPIO1_29", "GPIO2_4" , 11, 0, "X", 0, 0)
printer.steppers["Y"] = Stepper_00B1("GPIO1_12", "GPIO0_22", "GPIO2_5" , 12, 1, "Y", 1, 1)
printer.steppers["Z"] = Stepper_00B1("GPIO0_23", "GPIO0_26", "GPIO0_15", 13, 2, "Z", 2, 2)
printer.steppers["E"] = Stepper_00B1("GPIO1_28", "GPIO1_15", "GPIO2_1" , 14, 3, "E", 3, 3)
printer.steppers["H"] = Stepper_00B1("GPIO1_13", "GPIO1_14", "GPIO2_3" , 15, 4, "H", 4, 4)
elif self.revision == "00B2":
# Init the 5 Stepper motors (step, dir, fault, DAC channel, name)
printer.steppers["X"] = Stepper_00B2("GPIO0_27", "GPIO1_29", "GPIO2_4" , 11, 0, "X", 0, 0)
printer.steppers["Y"] = Stepper_00B2("GPIO1_12", "GPIO0_22", "GPIO2_5" , 12, 1, "Y", 1, 1)
printer.steppers["Z"] = Stepper_00B2("GPIO0_23", "GPIO0_26", "GPIO0_15", 13, 2, "Z", 2, 2)
printer.steppers["E"] = Stepper_00B2("GPIO1_28", "GPIO1_15", "GPIO2_1" , 14, 3, "E", 3, 3)
printer.steppers["H"] = Stepper_00B2("GPIO1_13", "GPIO1_14", "GPIO2_3" , 15, 4, "H", 4, 4)
else:
# Init the 5 Stepper motors (step, dir, fault, DAC channel, name)
printer.steppers["X"] = Stepper_00A4("GPIO0_27", "GPIO1_29", "GPIO2_4" , 0, 0, "X", 0, 0)
printer.steppers["Y"] = Stepper_00A4("GPIO1_12", "GPIO0_22", "GPIO2_5" , 1, 1, "Y", 1, 1)
printer.steppers["Z"] = Stepper_00A4("GPIO0_23", "GPIO0_26", "GPIO0_15", 2, 2, "Z", 2, 2)
printer.steppers["E"] = Stepper_00A4("GPIO1_28", "GPIO1_15", "GPIO2_1" , 3, 3, "E", 3, 3)
printer.steppers["H"] = Stepper_00A4("GPIO1_13", "GPIO1_14", "GPIO2_3" , 4, 4, "H", 4, 4)
if printer.config.reach_revision:
printer.steppers["A"] = Stepper_00A4("GPIO2_2" , "GPIO1_18", "GPIO0_14", 5, 5, "A", 5, 5)
printer.steppers["B"] = Stepper_00A4("GPIO1_14", "GPIO0_5" , "GPIO0_14", 6, 6, "B", 6, 6)
printer.steppers["C"] = Stepper_00A4("GPIO0_3" , "GPIO3_19", "GPIO0_14", 7, 7, "C", 7, 7)
# Enable the steppers and set the current, steps pr mm and
# microstepping
for name, stepper in self.printer.steppers.iteritems():
stepper.in_use = printer.config.getboolean('Steppers', 'in_use_' + name)
stepper.direction = printer.config.getint('Steppers', 'direction_' + name)
stepper.has_endstop = printer.config.getboolean('Endstops', 'has_' + name)
stepper.set_current_value(printer.config.getfloat('Steppers', 'current_' + name))
stepper.set_steps_pr_mm(printer.config.getfloat('Steppers', 'steps_pr_mm_' + name))
stepper.set_microstepping(printer.config.getint('Steppers', 'microstepping_' + name))
stepper.set_decay(printer.config.getboolean("Steppers", "slow_decay_" + name))
# Add soft end stops
Path.soft_min[Path.axis_to_index(name)] = printer.config.getfloat('Endstops', 'soft_end_stop_min_' + name)
Path.soft_max[Path.axis_to_index(name)] = printer.config.getfloat('Endstops', 'soft_end_stop_max_' + name)
# Commit changes for the Steppers
#Stepper.commit()
Stepper.printer = printer
# Delta printer setup
if Path.axis_config == Path.AXIS_CONFIG_DELTA:
opts = ["Hez", "L", "r", "Ae", "Be", "Ce", "A_radial", "B_radial", "C_radial", "A_tangential", "B_tangential", "C_tangential" ]
for opt in opts:
Delta.__dict__[opt] = printer.config.getfloat('Delta', opt)
Delta.recalculate()
# Discover and add all DS18B20 cold ends.
import glob
paths = glob.glob("/sys/bus/w1/devices/28-*/w1_slave")
logging.debug("Found cold ends: "+str(paths))
for i, path in enumerate(paths):
self.printer.cold_ends.append(ColdEnd(path, "ds18b20-"+str(i)))
logging.info("Found Cold end "+str(i)+" on " + path)
# Make Mosfets, thermistors and extruders
heaters = ["E", "H", "HBP"]
if self.printer.config.reach_revision:
heaters.extend(["A", "B", "C"])
for e in heaters:
# Mosfets
channel = self.printer.config.getint("Heaters", "mosfet_"+e)
self.printer.mosfets[e] = Mosfet(channel)
# Thermistors
adc = self.printer.config.get("Heaters", "path_adc_"+e)
chart = self.printer.config.get("Heaters", "temp_chart_"+e)
resistance = self.printer.config.getfloat("Heaters", "resistance_"+e)
self.printer.thermistors[e] = Thermistor(adc, "MOSFET "+e, chart, resistance)
self.printer.thermistors[e].printer = printer
# Extruders
onoff = self.printer.config.getboolean('Heaters', 'onoff_'+e)
prefix = self.printer.config.get('Heaters', 'prefix_'+e)
if e != "HBP":
self.printer.heaters[e] = Extruder(
self.printer.steppers[e],
self.printer.thermistors[e],
self.printer.mosfets[e], e, onoff)
else:
self.printer.heaters[e] = HBP(
self.printer.thermistors[e],
self.printer.mosfets[e], onoff)
self.printer.heaters[e].prefix = prefix
self.printer.heaters[e].P = self.printer.config.getfloat('Heaters', 'pid_p_'+e)
self.printer.heaters[e].I = self.printer.config.getfloat('Heaters', 'pid_i_'+e)
self.printer.heaters[e].D = self.printer.config.getfloat('Heaters', 'pid_d_'+e)
# Init the three fans. Argument is PWM channel number
self.printer.fans = []
if self.revision == "00A3":
self.printer.fans.append(Fan(0))
self.printer.fans.append(Fan(1))
self.printer.fans.append(Fan(2))
elif self.revision == "0A4A":
self.printer.fans.append(Fan(8))
self.printer.fans.append(Fan(9))
self.printer.fans.append(Fan(10))
elif self.revision in ["00B1", "00B2"]:
self.printer.fans.append(Fan(7))
self.printer.fans.append(Fan(8))
self.printer.fans.append(Fan(9))
self.printer.fans.append(Fan(10))
# Disable all fans
for f in self.printer.fans:
f.set_value(0)
# Init the servos
printer.servos = []
servo_nr = 0
while(printer.config.has_option("Servos", "servo_"+str(servo_nr)+"_enable")):
if printer.config.getboolean("Servos", "servo_"+str(servo_nr)+"_enable"):
channel = printer.config.get("Servos", "servo_"+str(servo_nr)+"_channel")
angle_init = printer.config.getint("Servos", "servo_"+str(servo_nr)+"_angle_init")
s = Servo(channel, 0.1, 0.2, angle_init)
printer.servos.append(s)
logging.info("Added servo "+str(servo_nr))
servo_nr += 1
# Connect thermitors to fans
for t, therm in self.printer.heaters.iteritems():
for f, fan in enumerate(self.printer.fans):
if self.printer.config.getboolean('Cold-ends', "connect-therm-{}-fan-{}".format(t, f)):
c = Cooler(therm, fan, "Cooler-{}-{}".format(t, f), False)
c.ok_range = 4
c.set_target_temperature(60)
c.enable()
self.printer.coolers.append(c)
logging.info("Cooler connects therm {} with fan {}".format(t, f))
# Connect fans to M106
printer.controlled_fans = []
for i, fan in enumerate(self.printer.fans):
if self.printer.config.getboolean('Cold-ends', "add-fan-{}-to-M106".format(i)):
printer.controlled_fans.append(self.printer.fans[i])
logging.info("Added fan {} to M106/M107".format(i))
# Connect the colds to fans
for ce, cold_end in enumerate(self.printer.cold_ends):
for f, fan in enumerate(self.printer.fans):
option = "connect-ds18b20-{}-fan-{}".format(ce, f)
if self.printer.config.has_option('Cold-ends', option):
if self.printer.config.getboolean('Cold-ends', option):
c = Cooler(cold_end, fan, "Cooler-ds18b20-{}-{}".format(ce, f), False)
c.ok_range = 4
c.set_target_temperature(60)
c.enable()
self.printer.coolers.append(c)
logging.info("Cooler connects temp sensor ds18b20 {} with fan {}".format(ce, f))
# Make a queue of commands
self.printer.commands = JoinableQueue(10)
# Make a queue of commands that should not be buffered
self.printer.sync_commands = JoinableQueue()
self.printer.unbuffered_commands = JoinableQueue(10)
# Bed compensation matrix
Path.matrix_bed_comp = printer.load_bed_compensation_matrix()
Path.matrix_bed_comp_inv = np.linalg.inv(Path.matrix_bed_comp)
logging.debug("Loaded bed compensation matrix: \n"+str(Path.matrix_bed_comp))
for axis in printer.steppers.keys():
i = Path.axis_to_index(axis)
Path.max_speeds[i] = printer.config.getfloat('Planner', 'max_speed_'+axis.lower())
Path.home_speed[i] = printer.config.getfloat('Homing', 'home_speed_'+axis.lower())
Path.home_backoff_speed[i] = printer.config.getfloat('Homing', 'home_backoff_speed_'+axis.lower())
Path.home_backoff_offset[i] = printer.config.getfloat('Homing', 'home_backoff_offset_'+axis.lower())
Path.steps_pr_meter[i] = printer.steppers[axis].get_steps_pr_meter()
Path.backlash_compensation[i] = printer.config.getfloat('Steppers', 'backlash_'+axis.lower())
dirname = os.path.dirname(os.path.realpath(__file__))
# Create the firmware compiler
pru_firmware = PruFirmware(
dirname + "/firmware/firmware_runtime.p",
dirname + "/firmware/firmware_runtime.bin",
dirname + "/firmware/firmware_endstops.p",
dirname + "/firmware/firmware_endstops.bin",
self.revision, self.printer.config, "/usr/bin/pasm")
printer.maxJerkXY = printer.config.getfloat('Planner', 'maxJerk_xy')
printer.maxJerkZ = printer.config.getfloat('Planner', 'maxJerk_z')
printer.maxJerkEH = printer.config.getfloat('Planner', 'maxJerk_eh')
printer.move_cache_size = printer.config.getfloat('Planner', 'move_cache_size')
printer.print_move_buffer_wait = printer.config.getfloat('Planner', 'print_move_buffer_wait')
printer.min_buffered_move_time = printer.config.getfloat('Planner', 'min_buffered_move_time')
printer.max_buffered_move_time = printer.config.getfloat('Planner', 'max_buffered_move_time')
self.printer.processor = GCodeProcessor(self.printer)
self.printer.plugins = PluginsController(self.printer)
# Path planner
travel_default = False
center_default = False
home_default = False
# Setting acceleration before PathPlanner init
for axis in printer.steppers.keys():
printer.acceleration[Path.axis_to_index(axis)] = printer.config.getfloat(
'Planner', 'acceleration_' + axis.lower())
self.printer.path_planner = PathPlanner(self.printer, pru_firmware)
for axis in printer.steppers.keys():
i = Path.axis_to_index(axis)
# Sometimes soft_end_stop aren't defined to be at the exact hardware boundary.
# Adding 100mm for searching buffer.
if printer.config.has_option('Geometry', 'travel_' + axis.lower()):
printer.path_planner.travel_length[axis] = printer.config.getfloat('Geometry', 'travel_' + axis.lower())
else:
printer.path_planner.travel_length[axis] = (Path.soft_max[i] - Path.soft_min[i]) + .1
if axis in ['X','Y','Z']:
travel_default = True
if printer.config.has_option('Geometry', 'offset_' + axis.lower()):
printer.path_planner.center_offset[axis] = printer.config.getfloat('Geometry', 'offset_' + axis.lower())
else:
printer.path_planner.center_offset[axis] =(Path.soft_min[i] if Path.home_speed[i] > 0 else Path.soft_max[i])
if axis in ['X','Y','Z']:
center_default = True
if printer.config.has_option('Homing', 'home_' + axis.lower()):
printer.path_planner.home_pos[axis] = printer.config.getfloat('Homing', 'home_' + axis.lower())
else:
printer.path_planner.home_pos[axis] = printer.path_planner.center_offset[axis]
if axis in ['X','Y','Z']:
home_default = True
if Path.axis_config == Path.AXIS_CONFIG_DELTA:
if travel_default:
logging.warning("Axis travel (travel_*) set by soft limits, manual setup is recommended for a delta")
if center_default:
logging.warning("Axis offsets (offset_*) set by soft limits, manual setup is recommended for a delta")
if home_default:
logging.warning("Home position (home_*) set by soft limits or offset_*")
logging.info("Home position will be recalculated...")
# convert home_pos to effector space
Az = printer.path_planner.home_pos['X']
Bz = printer.path_planner.home_pos['Y']
Cz = printer.path_planner.home_pos['Z']
z_offset = Delta.vertical_offset(Az,Bz,Cz) # vertical offset
xyz = Delta.forward_kinematics2(Az, Bz, Cz) # effector position
# The default home_pos, provided above, is based on effector space
# coordinates for carriage positions. We need to transform these to
# get where the effector actually is.
xyz[2] += z_offset
for i, a in enumerate(['X','Y','Z']):
printer.path_planner.home_pos[a] = xyz[i]
logging.info("Home position = %s"%str(printer.path_planner.home_pos))
if Path.axis_config == Path.AXIS_CONFIG_SCARA:
if travel_default:
logging.warning("Axis travel (travel_*) set by soft limits, manual setup is recommended for a scara")
if center_default:
logging.warning("Axis offsets (offset_*) set by soft limits, manual setup is recommended for a scara")
if home_default:
logging.warning("Home position (home_*) set by soft limits or offset_*")
logging.info("Home position will be recalculated...")
# convert home_pos to effector space
#A = printer.path_planner.home_pos['X']
#B = printer.path_planner.home_pos['Y']
#C = printer.path_planner.home_pos['Z']
#z_offset = Delta.vertical_offset(Az,Bz,Cz) # vertical offset
#xyz = Scara.forward_kinematics(A, B, C) # effector position
# don't cpnvert home_pos to effector spac
# home offset is defined in cartesian space
xyz = np.array([printer.path_planner.home_pos['X'],printer.path_planner.home_pos['Y'],printer.path_planner.home_pos['Z']])
# The default home_pos, provided above, is based on effector space
# coordinates for carriage positions. We need to transform these to
# get where the effector actually is.
#xyz[2] += z_offset
for i, a in enumerate(['X','Y','Z']):
printer.path_planner.home_pos[a] = xyz[i]
logging.info("Home position = %s"%str(printer.path_planner.home_pos))
# Enable PWM and steppers
printer.enable = Enable("P9_41")
printer.enable.set_enabled()
# Set up communication channels
printer.comms["USB"] = USB(self.printer)
printer.comms["Eth"] = Ethernet(self.printer)
if Pipe.check_tty0tty() or Pipe.check_socat():
printer.comms["octoprint"] = Pipe(printer, "octoprint")
printer.comms["toggle"] = Pipe(printer, "toggle")
printer.comms["testing"] = Pipe(printer, "testing")
printer.comms["testing_noret"] = Pipe(printer, "testing_noret")
# Does not send "ok"
printer.comms["testing_noret"].send_response = False
else:
logging.warning("Neither tty0tty or socat is installed! No virtual tty pipes enabled")
def start(self):
""" Start the processes """
self.running = True
# Start the two processes
p0 = Thread(target=self.loop,
args=(self.printer.commands, "buffered"))
p1 = Thread(target=self.loop,
args=(self.printer.unbuffered_commands, "unbuffered"))
p2 = Thread(target=self.eventloop,
args=(self.printer.sync_commands, "sync"))
p0.daemon = True
p1.daemon = True
p2.daemon = True
p0.start()
p1.start()
p2.start()
# Signal everything ready
logging.info("Redeem ready")
def loop(self, queue, name):
""" When a new gcode comes in, execute it """
try:
while self.running:
try:
gcode = queue.get(block=True, timeout=1)
except Queue.Empty:
continue
logging.debug("Executing "+gcode.code()+" from "+name + " " + gcode.message)
self._execute(gcode)
self.printer.reply(gcode)
queue.task_done()
except Exception:
logging.exception("Exception in {} loop: ".format(name))
def eventloop(self, queue, name):
""" When a new event comes in, execute the pending gcode """
try:
while self.running:
# Returns False on timeout, else True
if self.printer.path_planner.wait_until_sync_event():
try:
gcode = queue.get(block=True, timeout=1)
except Queue.Empty:
continue
self._synchronize(gcode)
logging.info("Event handled for " + gcode.code() + " from " + name + " " + gcode.message)
queue.task_done()
except Exception:
logging.exception("Exception in {} eventloop: ".format(name))
def exit(self):
logging.info("Redeem starting exit")
self.running = False
self.printer.path_planner.wait_until_done()
self.printer.path_planner.force_exit()
# Stops plugins
self.printer.plugins.exit()
for name, stepper in self.printer.steppers.iteritems():
stepper.set_disabled()
Stepper.commit()
for name, heater in self.printer.heaters.iteritems():
logging.debug("closing "+name)
heater.disable()
for name, comm in self.printer.comms.iteritems():
logging.debug("closing "+name)
comm.close()
self.printer.enable.set_disabled()
logging.info("Redeem exited")
def _execute(self, g):
""" Execute a G-code """
if g.message == "ok" or g.code() == "ok" or g.code() == "No-Gcode":
g.set_answer(None)
return
if g.is_info_command():
desc = self.printer.processor.get_long_description(g)
self.printer.send_message(g.prot, desc)
else:
self.printer.processor.execute(g)
def _synchronize(self, g):
""" Syncrhonized execution of a G-code """
self.printer.processor.synchronize(g)
def end_stop_hit(self, endstop):
""" An endStop has been hit """
logging.info("End Stop " + endstop.name + " hit!")
if "toggle" in self.printer.comms:
self.printer.comms["toggle"].send_message("End stop hit!")
def main():
# Create Redeem
r = Redeem()
def signal_handler(signal, frame):
r.exit()
# Register signal handler to allow interrupt with CTRL-C
signal.signal(signal.SIGINT, signal_handler)
# Launch Redeem
r.start()
# Wait for end of process signal
signal.pause()
def profile():
import yappi
yappi.start()
main()
yappi.get_func_stats().print_all()
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "profile":
profile()
else:
main()