gcode: Rework toolhead stalling to use greenlets

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-12-09 09:41:46 -05:00
parent 89f5452ddb
commit edd7dfe1c8
3 changed files with 23 additions and 35 deletions

View File

@ -22,7 +22,6 @@ class GCodeParser:
self.input_commands = [""] self.input_commands = [""]
self.bytes_read = 0 self.bytes_read = 0
self.input_log = collections.deque([], 50) self.input_log = collections.deque([], 50)
self.busy_state = None
# Command handling # Command handling
self.gcode_handlers = {} self.gcode_handlers = {}
self.is_printer_ready = False self.is_printer_ready = False
@ -114,12 +113,6 @@ class GCodeParser:
logging.exception("Exception in command handler") logging.exception("Exception in command handler")
self.toolhead.force_shutdown() self.toolhead.force_shutdown()
self.respond_error('Internal error on command:"%s"' % (cmd,)) self.respond_error('Internal error on command:"%s"' % (cmd,))
# Check if machine can process next command or must stall input
if self.busy_state is not None:
self.busy_handler(eventtime)
if self.is_printer_ready and self.toolhead.check_busy(eventtime):
self.set_busy(self.toolhead)
self.busy_handler(eventtime)
self.ack() self.ack()
del self.input_commands[:i+1] del self.input_commands[:i+1]
def process_data(self, eventtime): def process_data(self, eventtime):
@ -160,26 +153,6 @@ class GCodeParser:
for line in lines[:-1]: for line in lines[:-1]:
self.respond('// %s' % (line.strip(),)) self.respond('// %s' % (line.strip(),))
self.respond('!! %s' % (lines[-1].strip(),)) self.respond('!! %s' % (lines[-1].strip(),))
# Busy handling
def set_busy(self, busy_handler):
self.busy_state = busy_handler
def busy_handler(self, eventtime):
while 1:
try:
busy = self.busy_state.check_busy(eventtime)
except homing.EndstopError, e:
self.respond_error(str(e))
busy = False
except:
logging.exception("Exception in busy handler")
self.toolhead.force_shutdown()
self.respond_error('Internal error in busy handler')
busy = False
if not busy:
break
self.toolhead.reset_motor_off_time(eventtime)
eventtime = self.reactor.pause(eventtime + self.RETRY_TIME)
self.busy_state = None
# Temperature wrappers # Temperature wrappers
def get_temp(self): def get_temp(self):
if not self.is_printer_ready: if not self.is_printer_ready:

View File

@ -506,6 +506,8 @@ class MCU:
est_mcu_time = self.serial.get_clock(eventtime) / self._mcu_freq est_mcu_time = self.serial.get_clock(eventtime) / self._mcu_freq
self._print_start_time = est_mcu_time self._print_start_time = est_mcu_time
def get_print_buffer_time(self, eventtime, print_time): def get_print_buffer_time(self, eventtime, print_time):
if self.is_shutdown:
return 0.
mcu_time = print_time + self._print_start_time mcu_time = print_time + self._print_start_time
est_mcu_time = self.serial.get_clock(eventtime) / self._mcu_freq est_mcu_time = self.serial.get_clock(eventtime) / self._mcu_freq
return mcu_time - est_mcu_time return mcu_time - est_mcu_time

View File

@ -173,9 +173,10 @@ class ToolHead:
self.move_flush_time = config.getfloat('move_flush_time', 0.050) self.move_flush_time = config.getfloat('move_flush_time', 0.050)
self.motor_off_delay = config.getfloat('motor_off_time', 60.000) self.motor_off_delay = config.getfloat('motor_off_time', 60.000)
self.print_time = 0. self.print_time = 0.
self.need_check_stall = -1.
self.print_time_stall = 0 self.print_time_stall = 0
self.motor_off_time = self.reactor.NEVER self.motor_off_time = self.reactor.NEVER
self.flush_timer = self.reactor.register_timer(self.flush_handler) self.flush_timer = self.reactor.register_timer(self._flush_handler)
def build_config(self): def build_config(self):
self.kin.set_max_jerk(0.005 * self.max_accel, self.max_accel) # XXX self.kin.set_max_jerk(0.005 * self.max_accel, self.max_accel) # XXX
self.kin.build_config() self.kin.build_config()
@ -200,18 +201,27 @@ class ToolHead:
self.move_queue.flush() self.move_queue.flush()
self.printer.mcu.flush_moves(self.print_time) self.printer.mcu.flush_moves(self.print_time)
self.print_time = 0. self.print_time = 0.
self.need_check_stall = -1.
self.reset_motor_off_time(time.time()) self.reset_motor_off_time(time.time())
self.reactor.update_timer(self.flush_timer, self.motor_off_time) self.reactor.update_timer(self.flush_timer, self.motor_off_time)
def check_busy(self, eventtime): def _check_stall(self):
if not self.print_time: if not self.print_time:
# XXX - find better way to flush initial move_queue items # XXX - find better way to flush initial move_queue items
if self.move_queue.queue: if self.move_queue.queue:
self.reactor.update_timer(self.flush_timer, eventtime + 0.100) self.reactor.update_timer(self.flush_timer, time.time() + 0.100)
return False return
eventtime = time.time()
while 1:
buffer_time = self.printer.mcu.get_print_buffer_time( buffer_time = self.printer.mcu.get_print_buffer_time(
eventtime, self.print_time) eventtime, self.print_time)
return buffer_time > self.buffer_time_high stall_time = buffer_time - self.buffer_time_high
def flush_handler(self, eventtime): if stall_time <= 0.:
break
eventtime = self.reactor.pause(eventtime + stall_time)
if not self.print_time:
return
self.need_check_stall = self.print_time - stall_time + 0.100
def _flush_handler(self, eventtime):
try: try:
if not self.print_time: if not self.print_time:
self.move_queue.flush() self.move_queue.flush()
@ -260,11 +270,14 @@ class ToolHead:
self.extruder.check_move(move) self.extruder.check_move(move)
self.commanded_pos[:] = newpos self.commanded_pos[:] = newpos
self.move_queue.add_move(move) self.move_queue.add_move(move)
if self.print_time > self.need_check_stall:
self._check_stall()
def home(self, homing_state): def home(self, homing_state):
self.kin.home(homing_state) self.kin.home(homing_state)
def dwell(self, delay): def dwell(self, delay):
self.get_last_move_time() self.get_last_move_time()
self.update_move_time(delay) self.update_move_time(delay)
self._check_stall()
def motor_off(self): def motor_off(self):
self.dwell(STALL_TIME) self.dwell(STALL_TIME)
last_move_time = self.get_last_move_time() last_move_time = self.get_last_move_time()