From def524bdf488e0de97f287e5711bd84226e8098f Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 10 Oct 2018 19:48:35 -0400 Subject: [PATCH] endstop_phase: Add support for detecting phase via TMC stepper drivers The Trinamic stepper motor drivers are capable of reporting the stepper phase - add support for using that capability to the enddstop_phases module. Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 8 +++++--- klippy/extras/endstop_phase.py | 30 +++++++++++++++++++++++++----- klippy/extras/tmc2130.py | 4 ++++ klippy/extras/tmc2208.py | 4 ++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 5140a1bf..aed162ee 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -329,9 +329,11 @@ # of endstop switches. #[endstop_phase stepper_z] #phases: -# Set this to the number of phases of the given stepper motor driver -# (which is the number of micro-steps multiplied by four). This -# parameter must be provided. +# This specifies the number of phases of the given stepper motor +# driver (which is the number of micro-steps multiplied by four). +# This setting is automatically determined if one uses TMC2130, +# TMC2208, or TMC2224 drivers with run-time configuration. +# Otherwise, this parameter must be provided. #endstop_accuracy: 0.200 # Sets the expected accuracy (in mm) of the endstop. This represents # the maximum error distance the endstop may trigger (eg, if an diff --git a/klippy/extras/endstop_phase.py b/klippy/extras/endstop_phase.py index d3deba7c..552bcceb 100644 --- a/klippy/extras/endstop_phase.py +++ b/klippy/extras/endstop_phase.py @@ -6,17 +6,30 @@ import math, logging import homing +TRINAMIC_DRIVERS = ["tmc2130", "tmc2208"] + class EndstopPhase: def __init__(self, config): self.printer = config.get_printer() self.name = config.get_name().split()[1] - stepper_config = config.getsection(self.name) - self.step_dist = step_dist = stepper_config.getfloat('step_distance') - self.phases = config.getint('phases', minval=1) + # Determine number of stepper phases + for driver in TRINAMIC_DRIVERS: + driver_name = "%s %s" % (driver, self.name) + if config.has_section(driver_name): + module = self.printer.try_load_module(config, driver_name) + self.get_phase = module.get_phase + self.phases = module.get_microsteps() * 4 + break + else: + self.get_phase = None + self.phases = config.getint('phases', minval=1) + # Determine endstop phase position self.endstop_phase = config.getint('endstop_phase', None, minval=0, maxval=self.phases-1) self.endstop_align_zero = config.getboolean('endstop_align_zero', False) # Determine endstop accuracy + stepper_config = config.getsection(self.name) + self.step_dist = step_dist = stepper_config.getfloat('step_distance') endstop_accuracy = config.getfloat('endstop_accuracy', None, above=0.) if endstop_accuracy is None: self.endstop_accuracy = self.phases//2 - 1 @@ -44,8 +57,15 @@ class EndstopPhase: full_step = microsteps * self.step_dist return int(pos / full_step + .5) * full_step + phase_offset def get_homed_offset(self, stepper): - pos = stepper.get_mcu_position() - phase = pos % self.phases + if self.get_phase is not None: + try: + phase = self.get_phase() + except Exception as e: + msg = "Unable to get stepper %s phase: %s" % (self.name, str(e)) + logging.exception(msg) + raise homing.EndstopError(msg) + else: + phase = stepper.get_mcu_position() % self.phases if self.endstop_phase is None: logging.info("Setting %s endstop phase to %d", self.name, phase) self.endstop_phase = phase diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 08c7c7f3..9220ba95 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -137,6 +137,10 @@ class TMC2130: data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff] self.spi_send_cmd.send([self.oid, data]) + def get_microsteps(self): + return 256 >> self.mres + def get_phase(self): + return (self.get_register("MSCNT") & 0x3ff) >> self.mres cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers" def cmd_DUMP_TMC(self, params): self.printer.lookup_object('toolhead').get_last_move_time() diff --git a/klippy/extras/tmc2208.py b/klippy/extras/tmc2208.py index d93d7d09..6faad85c 100644 --- a/klippy/extras/tmc2208.py +++ b/klippy/extras/tmc2208.py @@ -219,6 +219,10 @@ class TMC2208: return raise self.printer.config_error( "Unable to write tmc2208 '%s' register %s" % (self.name, reg_name)) + def get_microsteps(self): + return 256 >> self.mres + def get_phase(self): + return (self.get_register("MSCNT") & 0x3ff) >> self.mres cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers" def cmd_DUMP_TMC(self, params): self.printer.lookup_object('toolhead').get_last_move_time()