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:
parent
a2df01b88e
commit
def524bdf4
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
# 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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue