From b340fdcc4a3285cb7568f86cbae268aa4a21fb30 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 5 Dec 2017 23:33:23 -0500 Subject: [PATCH] homing: Make sure to clean up homing state even if homing fails Make sure to always call MCU_endstop.home_wait() if MCU_endstop.home_start() is invoked. Rename MCU_stepper.note_homing_triggered() to note_homing_end() and make sure it is always called if MCU_stepper.note_homing_start() is invoked. With these changes, MCU_endstop.home_finalize() is no longer needed. Signed-off-by: Kevin O'Connor --- klippy/homing.py | 66 +++++++++++++++++++++++++++--------------------- klippy/mcu.py | 35 ++++++++++++------------- 2 files changed, 55 insertions(+), 46 deletions(-) diff --git a/klippy/homing.py b/klippy/homing.py index 9fde0000..db99fea9 100644 --- a/klippy/homing.py +++ b/klippy/homing.py @@ -29,61 +29,69 @@ class Homing: return thcoord def retract(self, newpos, speed): self.toolhead.move(self._fill_coord(newpos), speed) - def home(self, forcepos, movepos, endstops, speed, second_home=False): - # Alter kinematics class to think printer is at forcepos - self.toolhead.set_position(self._fill_coord(forcepos)) - # Start homing and issue move - if not second_home: - est_move_d = sum([abs(forcepos[i]-movepos[i]) - for i in range(3) if movepos[i] is not None]) - est_steps = sum([est_move_d / s.get_step_dist() - for es, n in endstops for s in es.get_steppers()]) - self.toolhead.dwell(est_steps * HOMING_STEP_DELAY, check_stall=False) + def set_homed_position(self, pos): + self.toolhead.set_position(self._fill_coord(pos)) + def homing_move(self, movepos, endstops, speed): + # Start endstop checking print_time = self.toolhead.get_last_move_time() - start_mcu_pos = [(s, name, s.get_mcu_position()) - for es, name in endstops for s in es.get_steppers()] for mcu_endstop, name in endstops: min_step_dist = min([s.get_step_dist() for s in mcu_endstop.get_steppers()]) mcu_endstop.home_start( print_time, ENDSTOP_SAMPLE_TIME, ENDSTOP_SAMPLE_COUNT, min_step_dist / speed) - self.toolhead.move(self._fill_coord(movepos), speed) + # Issue move + error = None + try: + self.toolhead.move(self._fill_coord(movepos), speed) + except EndstopError as e: + error = "Error during homing move: %s" % (str(e),) + # Wait for endstops to trigger move_end_print_time = self.toolhead.get_last_move_time() self.toolhead.reset_print_time(print_time) - for mcu_endstop, name in endstops: - mcu_endstop.home_finalize(move_end_print_time) - # Wait for endstops to trigger for mcu_endstop, name in endstops: try: - mcu_endstop.home_wait() - except mcu_endstop.error as e: - raise EndstopError("Failed to home stepper %s: %s" % ( - name, str(e))) + mcu_endstop.home_wait(move_end_print_time) + except mcu_endstop.TimeoutError as e: + if error is None: + error = "Failed to home %s: %s" % (name, str(e)) + if error is not None: + raise EndstopError(error) + def home(self, forcepos, movepos, endstops, speed, second_home=False): + # Alter kinematics class to think printer is at forcepos + self.toolhead.set_position(self._fill_coord(forcepos)) + # Add a CPU delay when homing a large axis + if not second_home: + est_move_d = sum([abs(forcepos[i]-movepos[i]) + for i in range(3) if movepos[i] is not None]) + est_steps = sum([est_move_d / s.get_step_dist() + for es, n in endstops for s in es.get_steppers()]) + self.toolhead.dwell(est_steps * HOMING_STEP_DELAY, check_stall=False) + # Setup for retract verification + self.toolhead.get_last_move_time() + start_mcu_pos = [(s, name, s.get_mcu_position()) + for es, name in endstops for s in es.get_steppers()] + # Issue homing move + self.homing_move(movepos, endstops, speed) # Verify retract led to some movement on second home if second_home and self.verify_retract: for s, name, pos in start_mcu_pos: if s.get_mcu_position() == pos: raise EndstopError( "Endstop %s still triggered after retract" % (name,)) - def set_homed_position(self, pos): - self.toolhead.set_position(self._fill_coord(pos)) def query_endstops(print_time, query_flags, steppers): if query_flags == "get_mcu_position": # Only the commanded position is requested return [(s.name.upper(), s.mcu_stepper.get_mcu_position()) for s in steppers] - for s in steppers: - for mcu_endstop, name in s.get_endstops(): - mcu_endstop.query_endstop(print_time) out = [] for s in steppers: for mcu_endstop, name in s.get_endstops(): - try: - out.append((name, mcu_endstop.query_endstop_wait())) - except mcu_endstop.error as e: - raise EndstopError(str(e)) + mcu_endstop.query_endstop(print_time) + for s in steppers: + for mcu_endstop, name in s.get_endstops(): + out.append((name, mcu_endstop.query_endstop_wait())) return out class EndstopError(Exception): diff --git a/klippy/mcu.py b/klippy/mcu.py index aa4f6297..606c22fb 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -79,14 +79,17 @@ class MCU_stepper: self._stepqueue, homing_clock) if ret: raise error("Internal error in stepcompress") - def note_homing_finalized(self): + def note_homing_end(self, did_trigger=False): ret = self._ffi_lib.stepcompress_set_homing(self._stepqueue, 0) if ret: raise error("Internal error in stepcompress") + if not did_trigger: + return ret = self._ffi_lib.stepcompress_reset(self._stepqueue, 0) if ret: raise error("Internal error in stepcompress") - def note_homing_triggered(self): + if self._mcu.is_fileoutput(): + return cmd = self._get_position_cmd.encode(self._oid) params = self._mcu.send_with_response(cmd, 'stepper_position', self._oid) pos = params['pos'] @@ -131,7 +134,8 @@ class MCU_stepper: self._commanded_pos += count class MCU_endstop: - error = error + class TimeoutError(Exception): + pass RETRY_QUERY = 1.000 def __init__(self, mcu, pin_params): self._mcu = mcu @@ -142,7 +146,7 @@ class MCU_endstop: self._cmd_queue = mcu.alloc_command_queue() self._oid = self._home_cmd = self._query_cmd = None self._homing = False - self._min_query_time = self._next_query_time = self._home_timeout = 0. + self._min_query_time = self._next_query_time = 0. self._last_state = {} def get_mcu(self): return self._mcu @@ -182,36 +186,33 @@ class MCU_endstop: self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) for s in self._steppers: s.note_homing_start(clock) - def home_finalize(self, print_time): - for s in self._steppers: - s.note_homing_finalized() - self._home_timeout = print_time - def home_wait(self): + def home_wait(self, home_end_time): eventtime = self._mcu.monotonic() - while self._check_busy(eventtime): + while self._check_busy(eventtime, home_end_time): eventtime = self._mcu.pause(eventtime + 0.1) def _handle_end_stop_state(self, params): logging.debug("end_stop_state %s", params) self._last_state = params - def _check_busy(self, eventtime): + def _check_busy(self, eventtime, home_end_time=0.): # Check if need to send an end_stop_query command - if self._mcu.is_fileoutput(): - return False print_time = self._mcu.estimated_print_time(eventtime) last_sent_time = self._last_state.get('#sent_time', -1.) - if last_sent_time >= self._min_query_time: + if last_sent_time >= self._min_query_time or self._mcu.is_fileoutput(): if not self._homing: return False if not self._last_state.get('homing', 0): for s in self._steppers: - s.note_homing_triggered() + s.note_homing_end(did_trigger=True) self._homing = False return False - if print_time > self._home_timeout: + if print_time > home_end_time: # Timeout - disable endstop checking + for s in self._steppers: + s.note_homing_end() + self._homing = False msg = self._home_cmd.encode(self._oid, 0, 0, 0, 0, 0) self._mcu.send(msg, reqclock=0, cq=self._cmd_queue) - raise error("Timeout during endstop homing") + raise self.TimeoutError("Timeout during endstop homing") if self._mcu.is_shutdown(): raise error("MCU is shutdown") if print_time >= self._next_query_time: