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:
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:

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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"):

View File

@ -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

View File

@ -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:

View File

@ -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()