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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-10-10 19:48:35 -04:00
parent a2df01b88e
commit def524bdf4
4 changed files with 38 additions and 8 deletions

View File

@ -329,9 +329,11 @@
# of endstop switches. # of endstop switches.
#[endstop_phase stepper_z] #[endstop_phase stepper_z]
#phases: #phases:
# Set this to the number of phases of the given stepper motor driver # This specifies the number of phases of the given stepper motor
# (which is the number of micro-steps multiplied by four). This # driver (which is the number of micro-steps multiplied by four).
# parameter must be provided. # 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 #endstop_accuracy: 0.200
# Sets the expected accuracy (in mm) of the endstop. This represents # Sets the expected accuracy (in mm) of the endstop. This represents
# the maximum error distance the endstop may trigger (eg, if an # the maximum error distance the endstop may trigger (eg, if an

View File

@ -6,17 +6,30 @@
import math, logging import math, logging
import homing import homing
TRINAMIC_DRIVERS = ["tmc2130", "tmc2208"]
class EndstopPhase: class EndstopPhase:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
self.name = config.get_name().split()[1] self.name = config.get_name().split()[1]
stepper_config = config.getsection(self.name) # Determine number of stepper phases
self.step_dist = step_dist = stepper_config.getfloat('step_distance') 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) self.phases = config.getint('phases', minval=1)
# Determine endstop phase position
self.endstop_phase = config.getint('endstop_phase', None, self.endstop_phase = config.getint('endstop_phase', None,
minval=0, maxval=self.phases-1) minval=0, maxval=self.phases-1)
self.endstop_align_zero = config.getboolean('endstop_align_zero', False) self.endstop_align_zero = config.getboolean('endstop_align_zero', False)
# Determine endstop accuracy # 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.) endstop_accuracy = config.getfloat('endstop_accuracy', None, above=0.)
if endstop_accuracy is None: if endstop_accuracy is None:
self.endstop_accuracy = self.phases//2 - 1 self.endstop_accuracy = self.phases//2 - 1
@ -44,8 +57,15 @@ class EndstopPhase:
full_step = microsteps * self.step_dist full_step = microsteps * self.step_dist
return int(pos / full_step + .5) * full_step + phase_offset return int(pos / full_step + .5) * full_step + phase_offset
def get_homed_offset(self, stepper): def get_homed_offset(self, stepper):
pos = stepper.get_mcu_position() if self.get_phase is not None:
phase = pos % self.phases 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: if self.endstop_phase is None:
logging.info("Setting %s endstop phase to %d", self.name, phase) logging.info("Setting %s endstop phase to %d", self.name, phase)
self.endstop_phase = phase self.endstop_phase = phase

View File

@ -137,6 +137,10 @@ class TMC2130:
data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff,
(val >> 8) & 0xff, val & 0xff] (val >> 8) & 0xff, val & 0xff]
self.spi_send_cmd.send([self.oid, data]) 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" cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
def cmd_DUMP_TMC(self, params): def cmd_DUMP_TMC(self, params):
self.printer.lookup_object('toolhead').get_last_move_time() self.printer.lookup_object('toolhead').get_last_move_time()

View File

@ -219,6 +219,10 @@ class TMC2208:
return return
raise self.printer.config_error( raise self.printer.config_error(
"Unable to write tmc2208 '%s' register %s" % (self.name, reg_name)) "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" cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
def cmd_DUMP_TMC(self, params): def cmd_DUMP_TMC(self, params):
self.printer.lookup_object('toolhead').get_last_move_time() self.printer.lookup_object('toolhead').get_last_move_time()