gcode: Rework endstop query to use greenlets

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-12-08 12:42:12 -05:00
parent ed9d7e4fae
commit a6de1db94d
7 changed files with 23 additions and 47 deletions

View File

@ -76,8 +76,9 @@ 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, query_state): def query_endstops(self, print_time):
query_state.set_steppers(self.steppers) endstops = [(s, s.query_endstop(print_time)) for s in self.steppers]
return [(s.name, es.query_endstop_wait()) for s, es in endstops]
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

@ -111,8 +111,9 @@ 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, query_state): def query_endstops(self, print_time):
query_state.set_steppers(self.steppers) endstops = [(s, s.query_endstop(print_time)) for s in self.steppers]
return [(s.name, es.query_endstop_wait()) for s, es in endstops]
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

@ -345,10 +345,13 @@ class GCodeParser:
# Get Endstop Status # Get Endstop Status
if self.is_fileinput: if self.is_fileinput:
return return
print_time = self.toolhead.get_last_move_time() try:
query_state = homing.QueryEndstops(print_time, self.respond) res = self.toolhead.query_endstops()
self.toolhead.query_endstops(query_state) except self.printer.mcu.error, e:
self.set_busy(query_state) self.respond_error(str(e))
return
self.respond(" ".join(["%s:%s" % (name, ["open", "TRIGGERED"][not not t])
for name, t in res]))
cmd_PID_TUNE_help = "Run PID Tuning" cmd_PID_TUNE_help = "Run PID Tuning"
cmd_PID_TUNE_aliases = ["M303"] cmd_PID_TUNE_aliases = ["M303"]
def cmd_PID_TUNE(self, params): def cmd_PID_TUNE(self, params):

View File

@ -90,40 +90,6 @@ class Homing:
def do_calc_position(self, callback): def do_calc_position(self, callback):
self.toolhead.set_position(self.fill_coord(callback(self))) self.toolhead.set_position(self.fill_coord(callback(self)))
# Helper code for querying and reporting the status of the endstops
class QueryEndstops:
def __init__(self, print_time, respond_cb):
self.print_time = print_time
self.respond_cb = respond_cb
self.endstops = []
self.busy = []
def set_steppers(self, steppers):
for stepper in steppers:
es = stepper.query_endstop(self.print_time)
if es is None:
continue
self.endstops.append((stepper.name, es))
self.busy.append((stepper.name, es))
def check_busy(self, eventtime):
# Check if all endstop queries have been received
while self.busy:
try:
if self.busy[0][1].check_busy(eventtime):
return True
except mcu.error, e:
raise EndstopError("Failed to query endstop %s: %s" % (
self.busy[0][0], str(e)))
self.busy.pop(0)
# 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))
self.respond_cb(" ".join(msgs))
return False
class EndstopError(Exception): class EndstopError(Exception):
pass pass

View File

@ -188,7 +188,10 @@ class MCU_endstop:
self._homing = False self._homing = False
self._min_query_time = time.time() self._min_query_time = time.time()
self._next_query_clock = clock self._next_query_clock = clock
def get_last_triggered(self): def query_endstop_wait(self):
eventtime = time.time()
while self.check_busy(eventtime):
eventtime = self._mcu.pause(eventtime + 0.1)
return self._last_state.get('pin', self._invert) ^ self._invert return self._last_state.get('pin', self._invert) ^ self._invert
class MCU_digital_out: class MCU_digital_out:
@ -299,6 +302,7 @@ class MCU_adc:
self._callback = callback self._callback = callback
class MCU: class MCU:
error = error
COMM_TIMEOUT = 3.5 COMM_TIMEOUT = 3.5
def __init__(self, printer, config): def __init__(self, printer, config):
self._printer = printer self._printer = printer
@ -515,5 +519,7 @@ class MCU:
mcu_time = print_time + self._print_start_time mcu_time = print_time + self._print_start_time
clock = int(mcu_time * self._mcu_freq) clock = int(mcu_time * self._mcu_freq)
self.ffi_lib.steppersync_flush(self._steppersync, clock) self.ffi_lib.steppersync_flush(self._steppersync, clock)
def pause(self, waketime):
return self._printer.reactor.pause(waketime)
def __del__(self): def __del__(self):
self.disconnect() self.disconnect()

View File

@ -78,8 +78,6 @@ class PrinterStepper:
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, print_time): def query_endstop(self, print_time):
if self.mcu_endstop is None:
return None
mcu_time = self.mcu_endstop.print_to_mcu_time(print_time) mcu_time = self.mcu_endstop.print_to_mcu_time(print_time)
self.mcu_endstop.query_endstop(mcu_time) self.mcu_endstop.query_endstop(mcu_time)
return self.mcu_endstop return self.mcu_endstop

View File

@ -272,8 +272,9 @@ 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, query_state): def query_endstops(self):
return self.kin.query_endstops(query_state) last_move_time = self.get_last_move_time()
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()