From 9e1059afb46c9df439211d21590da987b32e2963 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 17 Nov 2016 17:24:03 -0500 Subject: [PATCH] homing: Create QueryEndstops class from gcode Create the QueryEndstops in the gcode handler instead of in the kinematic classes. This simplifies the gcode handler as it can directly register its response callback. Also, store the stepper name in the stepper class. Also, propagate the print_time of the query request to the mcu_endstop class. Signed-off-by: Kevin O'Connor --- klippy/cartesian.py | 10 +++++----- klippy/delta.py | 10 +++++----- klippy/extruder.py | 2 +- klippy/gcode.py | 14 ++++---------- klippy/homing.py | 19 ++++++++++++------- klippy/mcu.py | 5 +++-- klippy/stepper.py | 8 +++++--- klippy/toolhead.py | 5 ++--- 8 files changed, 37 insertions(+), 36 deletions(-) diff --git a/klippy/cartesian.py b/klippy/cartesian.py index e386704c..b7b4a9ab 100644 --- a/klippy/cartesian.py +++ b/klippy/cartesian.py @@ -10,9 +10,9 @@ StepList = (0, 1, 2) class CartKinematics: def __init__(self, printer, config): - steppers = ['stepper_x', 'stepper_y', 'stepper_z'] - self.steppers = [stepper.PrinterStepper(printer, config.getsection(n)) - for n in steppers] + self.steppers = [stepper.PrinterStepper( + printer, config.getsection('stepper_' + n), n) + for n in ['x', 'y', 'z']] self.need_motor_enable = True self.limits = [(1.0, -1.0)] * 3 self.stepper_pos = [0, 0, 0] @@ -73,8 +73,8 @@ class CartKinematics: self.steppers[i].motor_enable(move_time, 1) need_motor_enable |= self.steppers[i].need_motor_enable self.need_motor_enable = need_motor_enable - def query_endstops(self, move_time): - return homing.QueryEndstops(["x", "y", "z"], self.steppers) + def query_endstops(self, query_state): + query_state.set_steppers(self.steppers) def check_endstops(self, move): end_pos = move.end_pos for i in StepList: diff --git a/klippy/delta.py b/klippy/delta.py index 99233cf2..8f5bd409 100644 --- a/klippy/delta.py +++ b/klippy/delta.py @@ -10,9 +10,9 @@ StepList = (0, 1, 2) class DeltaKinematics: def __init__(self, printer, config): - steppers = ['stepper_a', 'stepper_b', 'stepper_c'] - self.steppers = [stepper.PrinterStepper(printer, config.getsection(n)) - for n in steppers] + self.steppers = [stepper.PrinterStepper( + printer, config.getsection('stepper_' + n), n) + for n in ['a', 'b', 'c']] self.need_motor_enable = True radius = config.getfloat('delta_radius') arm_length = config.getfloat('delta_arm_length') @@ -111,8 +111,8 @@ class DeltaKinematics: for i in StepList: self.steppers[i].motor_enable(move_time, 1) self.need_motor_enable = False - def query_endstops(self, move_time): - return homing.QueryEndstops(["a", "b", "c"], self.steppers) + def query_endstops(self, query_state): + query_state.set_steppers(self.steppers) def check_move(self, move): end_pos = move.end_pos xy2 = end_pos[0]**2 + end_pos[1]**2 diff --git a/klippy/extruder.py b/klippy/extruder.py index 8b66dad6..451aebb0 100644 --- a/klippy/extruder.py +++ b/klippy/extruder.py @@ -9,7 +9,7 @@ import stepper, heater, homing class PrinterExtruder: def __init__(self, printer, config): self.heater = heater.PrinterHeater(printer, config) - self.stepper = stepper.PrinterStepper(printer, config) + self.stepper = stepper.PrinterStepper(printer, config, 'extruder') self.pressure_advance = config.getfloat('pressure_advance', 0.) self.need_motor_enable = True self.stepper_pos = 0 diff --git a/klippy/gcode.py b/klippy/gcode.py index 9902ec4c..97f88563 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -308,16 +308,10 @@ class GCodeParser: # Get Endstop Status if self.inputfile: return - # Wrapper class for check_busy() that responds with final status - class endstop_handler_wrapper: - gcode = self - state = self.toolhead.query_endstops() - def check_busy(self, eventtime): - busy = self.state.check_busy(eventtime) - if not busy: - self.gcode.respond(self.state.get_msg()) - return busy - self.set_busy(endstop_handler_wrapper()) + print_time = self.toolhead.get_last_move_time() + query_state = homing.QueryEndstops(print_time, self.respond) + self.toolhead.query_endstops(query_state) + self.set_busy(query_state) def cmd_M140(self, params): # Set Bed Temperature self.set_temp(self.heater_bed, params) diff --git a/klippy/homing.py b/klippy/homing.py index 9b405299..117080f1 100644 --- a/klippy/homing.py +++ b/klippy/homing.py @@ -65,31 +65,36 @@ class Homing: del self.endstops[:] return False +# Helper code for querying and reporting the status of the endstops class QueryEndstops: - def __init__(self, names, steppers): + def __init__(self, print_time, respond_cb): + self.print_time = print_time + self.respond_cb = respond_cb self.endstops = [] self.busy = [] - for name, stepper in zip(names, steppers): - es = stepper.query_endstop() + def set_steppers(self, steppers): + for stepper in steppers: + es = stepper.query_endstop(self.print_time) if es is None: continue - self.endstops.append((name, es)) + self.endstops.append((stepper.name, es)) self.busy.append(es) def check_busy(self, eventtime): + # Check if all endstop queries have been received while self.busy: busy = self.busy[0].check_busy(eventtime) if busy: return True self.busy.pop(0) - return False - def get_msg(self): + # All responses received - report status msgs = [] for name, es in self.endstops: msg = "open" if es.get_last_triggered(): msg = "TRIGGERED" msgs.append("%s:%s" % (name, msg)) - return " ".join(msgs) + self.respond_cb(" ".join(msgs)) + return False class EndstopError(Exception): def __init__(self, pos, msg="Move out of range"): diff --git a/klippy/mcu.py b/klippy/mcu.py index de9518ac..df1bb3dc 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -140,10 +140,11 @@ class MCU_endstop: if self._stepper.get_invert_dir(): return -pos return pos - def query_endstop(self): + def query_endstop(self, mcu_time): + clock = int(mcu_time * self._mcu_freq) self._homing = False self._min_query_time = time.time() - self._next_query_clock = 0 + self._next_query_clock = clock def get_last_triggered(self): return self._last_state.get('pin', self._invert) ^ self._invert diff --git a/klippy/stepper.py b/klippy/stepper.py index 51abd093..61a9d26a 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -6,9 +6,10 @@ import math, logging class PrinterStepper: - def __init__(self, printer, config): + def __init__(self, printer, config, name): self.printer = printer self.config = config + self.name = name self.mcu_stepper = self.mcu_enable = self.mcu_endstop = None self.step_dist = config.getfloat('step_distance') @@ -76,10 +77,11 @@ class PrinterStepper: mcu_time = self.mcu_endstop.print_to_mcu_time(move_time) self.mcu_endstop.home(mcu_time, step_time) return self.mcu_endstop - def query_endstop(self): + def query_endstop(self, print_time): if self.mcu_endstop is None: return None - self.mcu_endstop.query_endstop() + mcu_time = self.mcu_endstop.print_to_mcu_time(print_time) + self.mcu_endstop.query_endstop(mcu_time) return self.mcu_endstop def get_homed_offset(self): if not self.homing_stepper_phases or self.need_motor_enable: diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 91843805..91bcd87f 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -279,9 +279,8 @@ class ToolHead: self.extruder.motor_off(last_move_time) self.dwell(STALL_TIME) logging.debug('; Max time of %f' % (last_move_time,)) - def query_endstops(self): - last_move_time = self.get_last_move_time() - return self.kin.query_endstops(last_move_time) + def query_endstops(self, query_state): + return self.kin.query_endstops(query_state) def force_shutdown(self): self.printer.mcu.force_shutdown() self.move_queue.reset()