endstop_phase: Obtain phases directly from stepper microsteps config

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-01-05 20:42:34 -05:00
parent 7dcc778b6c
commit 96eba9f2df
6 changed files with 62 additions and 54 deletions

View file

@ -4,9 +4,13 @@
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging
import stepper
TRINAMIC_DRIVERS = ["tmc2130", "tmc2208", "tmc2209", "tmc2660", "tmc5160"]
def convert_phase(driver_phase, driver_phases, phases):
return (int(float(driver_phase) / driver_phases * phases + .5) % phases)
class EndstopPhase:
def __init__(self, config):
self.printer = config.get_printer()
@ -17,37 +21,26 @@ class EndstopPhase:
self.printer.register_event_handler("homing:home_rails_end",
self.handle_home_rails_end)
self.printer.load_object(config, "endstop_phase")
self.printer.load_object(config, "force_move")
# Obtain step_distance and microsteps from stepper config section
sconfig = config.getsection(self.name)
self.step_dist = stepper.parse_step_distance(sconfig)
self.phases = sconfig.getint("microsteps", note_valid=False) * 4
# Read config
self.phases = config.getint('phases', None, minval=1)
self.endstop_phase = config.getint('endstop_phase', None, minval=0)
self.endstop_phase = None
trigger_phase = config.get('trigger_phase', None)
if trigger_phase is not None:
try:
p, ps = [int(v.strip()) for v in trigger_phase.split('/')]
except:
raise config.error("Unable to parse trigger_phase '%s'"
% (trigger_phase,))
if p >= ps:
raise config.error("Invalid trigger_phase '%s'"
% (trigger_phase,))
self.endstop_phase = convert_phase(p, ps, self.phases)
self.endstop_align_zero = config.getboolean('endstop_align_zero', False)
self.endstop_accuracy = config.getfloat('endstop_accuracy', None,
above=0.)
self.step_dist = self.endstop_phase_accuracy = None
def handle_connect(self):
# Determine number of stepper phases
for driver in TRINAMIC_DRIVERS:
driver_name = "%s %s" % (driver, self.name)
module = self.printer.lookup_object(driver_name, None)
if module is not None:
self.get_phase = module.get_phase
if self.phases is not None:
raise self.printer.config_error(
"endstop_phase phases set with tmc driver")
self.phases = module.get_microsteps() * 4
break
else:
self.get_phase = None
if self.phases is None:
raise self.printer.config_error(
"endstop_phase phases must be specified")
if self.endstop_phase is not None and self.endstop_phase >= self.phases:
raise self.printer.config_error(
"endstop_phase endstop_phase parameter not valid")
# Lookup stepper step_dist
force_move = self.printer.lookup_object("force_move")
self.step_dist = force_move.lookup_stepper(self.name).get_step_dist()
# Determine endstop accuracy
if self.endstop_accuracy is None:
self.endstop_phase_accuracy = self.phases//2 - 1
@ -63,6 +56,15 @@ class EndstopPhase:
if self.printer.get_start_args().get('debugoutput') is not None:
self.endstop_phase_accuracy = self.phases
self.phase_history = [0] * self.phases
self.get_phase = None
def handle_connect(self):
# Check for trinamic driver with get_phase() method
for driver in TRINAMIC_DRIVERS:
driver_name = "%s %s" % (driver, self.name)
module = self.printer.lookup_object(driver_name, None)
if module is not None:
self.get_phase = module.get_phase
break
def align_endstop(self, pos):
if not self.endstop_align_zero or self.endstop_phase is None:
return pos
@ -76,13 +78,14 @@ class EndstopPhase:
def get_homed_offset(self, stepper):
if self.get_phase is not None:
try:
phase = self.get_phase()
driver_phase, driver_phases = self.get_phase()
except Exception as e:
msg = "Unable to get stepper %s phase: %s" % (self.name, str(e))
logging.exception(msg)
raise self.printer.command_error(msg)
if stepper.is_dir_inverted():
phase = (self.phases - 1) - phase
driver_phase = (driver_phases - 1) - driver_phase
phase = convert_phase(driver_phase, driver_phases, self.phases)
else:
phase = stepper.get_mcu_position() % self.phases
self.phase_history[phase] += 1
@ -140,10 +143,11 @@ class EndstopPhases:
if get_phase is None:
return
try:
phase = get_phase()
driver_phase, driver_phases = get_phase()
except:
logging.exception("Error in EndstopPhases get_phase")
return
phase = convert_phase(driver_phase, driver_phases, len(phase_history))
phase_history[phase] += 1
def handle_home_rails_end(self, rails):
for rail in rails:
@ -163,11 +167,12 @@ class EndstopPhases:
if info is None:
raise gcmd.error("Stats not available for stepper %s"
% (stepper_name,))
endstop_phase = self.generate_stats(stepper_name, info)
endstop_phase, phases = self.generate_stats(stepper_name, info)
configfile = self.printer.lookup_object('configfile')
section = 'endstop_phase %s' % stepper_name
section = 'endstop_phase %s' % (stepper_name,)
configfile.remove_section(section)
configfile.set(section, "endstop_phase", endstop_phase)
configfile.set(section, "trigger_phase",
"%s/%s" % (endstop_phase, phases))
gcmd.respond_info(
"The SAVE_CONFIG command will update the printer config\n"
"file with these parameters and restart the printer.")
@ -188,9 +193,9 @@ class EndstopPhases:
if wph[j]]
best_phase = best % phases
lo, hi = found[0] % phases, found[-1] % phases
self.gcode.respond_info("%s: endstop_phase=%d (range %d to %d)"
% (stepper_name, best_phase, lo, hi))
return best_phase
self.gcode.respond_info("%s: trigger_phase=%d/%d (range %d to %d)"
% (stepper_name, best_phase, phases, lo, hi))
return best_phase, phases
def report_stats(self):
if not self.tracking:
self.gcode.respond_info(

View file

@ -283,7 +283,7 @@ class TMCMicrostepHelper:
field_name = "MSTEP"
reg = self.mcu_tmc.get_register(self.fields.lookup_register(field_name))
mscnt = self.fields.get_field(field_name, reg)
return (1023 - mscnt) >> self.fields.get_field("MRES")
return 1023 - mscnt, 1024
# Helper to configure "stealthchop" mode
def TMCStealthchopHelper(config, mcu_tmc, tmc_freq):