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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-17 17:24:03 -05:00
parent 7ef8c0442a
commit 9e1059afb4
8 changed files with 37 additions and 36 deletions

View File

@ -10,9 +10,9 @@ StepList = (0, 1, 2)
class CartKinematics: class CartKinematics:
def __init__(self, printer, config): def __init__(self, printer, config):
steppers = ['stepper_x', 'stepper_y', 'stepper_z'] self.steppers = [stepper.PrinterStepper(
self.steppers = [stepper.PrinterStepper(printer, config.getsection(n)) printer, config.getsection('stepper_' + n), n)
for n in steppers] for n in ['x', 'y', 'z']]
self.need_motor_enable = True self.need_motor_enable = True
self.limits = [(1.0, -1.0)] * 3 self.limits = [(1.0, -1.0)] * 3
self.stepper_pos = [0, 0, 0] self.stepper_pos = [0, 0, 0]
@ -73,8 +73,8 @@ class CartKinematics:
self.steppers[i].motor_enable(move_time, 1) self.steppers[i].motor_enable(move_time, 1)
need_motor_enable |= self.steppers[i].need_motor_enable need_motor_enable |= self.steppers[i].need_motor_enable
self.need_motor_enable = need_motor_enable self.need_motor_enable = need_motor_enable
def query_endstops(self, move_time): def query_endstops(self, query_state):
return homing.QueryEndstops(["x", "y", "z"], self.steppers) query_state.set_steppers(self.steppers)
def check_endstops(self, move): def check_endstops(self, move):
end_pos = move.end_pos end_pos = move.end_pos
for i in StepList: for i in StepList:

View File

@ -10,9 +10,9 @@ StepList = (0, 1, 2)
class DeltaKinematics: class DeltaKinematics:
def __init__(self, printer, config): def __init__(self, printer, config):
steppers = ['stepper_a', 'stepper_b', 'stepper_c'] self.steppers = [stepper.PrinterStepper(
self.steppers = [stepper.PrinterStepper(printer, config.getsection(n)) printer, config.getsection('stepper_' + n), n)
for n in steppers] for n in ['a', 'b', 'c']]
self.need_motor_enable = True self.need_motor_enable = True
radius = config.getfloat('delta_radius') radius = config.getfloat('delta_radius')
arm_length = config.getfloat('delta_arm_length') arm_length = config.getfloat('delta_arm_length')
@ -111,8 +111,8 @@ class DeltaKinematics:
for i in StepList: for i in StepList:
self.steppers[i].motor_enable(move_time, 1) self.steppers[i].motor_enable(move_time, 1)
self.need_motor_enable = False self.need_motor_enable = False
def query_endstops(self, move_time): def query_endstops(self, query_state):
return homing.QueryEndstops(["a", "b", "c"], self.steppers) query_state.set_steppers(self.steppers)
def check_move(self, move): def check_move(self, move):
end_pos = move.end_pos end_pos = move.end_pos
xy2 = end_pos[0]**2 + end_pos[1]**2 xy2 = end_pos[0]**2 + end_pos[1]**2

View File

@ -9,7 +9,7 @@ import stepper, heater, homing
class PrinterExtruder: class PrinterExtruder:
def __init__(self, printer, config): def __init__(self, printer, config):
self.heater = heater.PrinterHeater(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.pressure_advance = config.getfloat('pressure_advance', 0.)
self.need_motor_enable = True self.need_motor_enable = True
self.stepper_pos = 0 self.stepper_pos = 0

View File

@ -308,16 +308,10 @@ class GCodeParser:
# Get Endstop Status # Get Endstop Status
if self.inputfile: if self.inputfile:
return return
# Wrapper class for check_busy() that responds with final status print_time = self.toolhead.get_last_move_time()
class endstop_handler_wrapper: query_state = homing.QueryEndstops(print_time, self.respond)
gcode = self self.toolhead.query_endstops(query_state)
state = self.toolhead.query_endstops() self.set_busy(query_state)
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())
def cmd_M140(self, params): def cmd_M140(self, params):
# Set Bed Temperature # Set Bed Temperature
self.set_temp(self.heater_bed, params) self.set_temp(self.heater_bed, params)

View File

@ -65,31 +65,36 @@ class Homing:
del self.endstops[:] del self.endstops[:]
return False return False
# Helper code for querying and reporting the status of the endstops
class QueryEndstops: 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.endstops = []
self.busy = [] self.busy = []
for name, stepper in zip(names, steppers): def set_steppers(self, steppers):
es = stepper.query_endstop() for stepper in steppers:
es = stepper.query_endstop(self.print_time)
if es is None: if es is None:
continue continue
self.endstops.append((name, es)) self.endstops.append((stepper.name, es))
self.busy.append(es) self.busy.append(es)
def check_busy(self, eventtime): def check_busy(self, eventtime):
# Check if all endstop queries have been received
while self.busy: while self.busy:
busy = self.busy[0].check_busy(eventtime) busy = self.busy[0].check_busy(eventtime)
if busy: if busy:
return True return True
self.busy.pop(0) self.busy.pop(0)
return False # All responses received - report status
def get_msg(self):
msgs = [] msgs = []
for name, es in self.endstops: for name, es in self.endstops:
msg = "open" msg = "open"
if es.get_last_triggered(): if es.get_last_triggered():
msg = "TRIGGERED" msg = "TRIGGERED"
msgs.append("%s:%s" % (name, msg)) msgs.append("%s:%s" % (name, msg))
return " ".join(msgs) self.respond_cb(" ".join(msgs))
return False
class EndstopError(Exception): class EndstopError(Exception):
def __init__(self, pos, msg="Move out of range"): def __init__(self, pos, msg="Move out of range"):

View File

@ -140,10 +140,11 @@ class MCU_endstop:
if self._stepper.get_invert_dir(): if self._stepper.get_invert_dir():
return -pos return -pos
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._homing = False
self._min_query_time = time.time() self._min_query_time = time.time()
self._next_query_clock = 0 self._next_query_clock = clock
def get_last_triggered(self): def get_last_triggered(self):
return self._last_state.get('pin', self._invert) ^ self._invert return self._last_state.get('pin', self._invert) ^ self._invert

View File

@ -6,9 +6,10 @@
import math, logging import math, logging
class PrinterStepper: class PrinterStepper:
def __init__(self, printer, config): def __init__(self, printer, config, name):
self.printer = printer self.printer = printer
self.config = config self.config = config
self.name = name
self.mcu_stepper = self.mcu_enable = self.mcu_endstop = None self.mcu_stepper = self.mcu_enable = self.mcu_endstop = None
self.step_dist = config.getfloat('step_distance') self.step_dist = config.getfloat('step_distance')
@ -76,10 +77,11 @@ class PrinterStepper:
mcu_time = self.mcu_endstop.print_to_mcu_time(move_time) mcu_time = self.mcu_endstop.print_to_mcu_time(move_time)
self.mcu_endstop.home(mcu_time, step_time) self.mcu_endstop.home(mcu_time, step_time)
return self.mcu_endstop return self.mcu_endstop
def query_endstop(self): def query_endstop(self, print_time):
if self.mcu_endstop is None: if self.mcu_endstop is None:
return 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 return self.mcu_endstop
def get_homed_offset(self): def get_homed_offset(self):
if not self.homing_stepper_phases or self.need_motor_enable: if not self.homing_stepper_phases or self.need_motor_enable:

View File

@ -279,9 +279,8 @@ class ToolHead:
self.extruder.motor_off(last_move_time) self.extruder.motor_off(last_move_time)
self.dwell(STALL_TIME) self.dwell(STALL_TIME)
logging.debug('; Max time of %f' % (last_move_time,)) logging.debug('; Max time of %f' % (last_move_time,))
def query_endstops(self): def query_endstops(self, query_state):
last_move_time = self.get_last_move_time() return self.kin.query_endstops(query_state)
return self.kin.query_endstops(last_move_time)
def force_shutdown(self): def force_shutdown(self):
self.printer.mcu.force_shutdown() self.printer.mcu.force_shutdown()
self.move_queue.reset() self.move_queue.reset()