From a0ce69c811207a4cf6b644f04aa702d163f8cac4 Mon Sep 17 00:00:00 2001 From: Rogerio Goncalves Date: Tue, 10 Dec 2024 22:06:12 +0000 Subject: [PATCH] homing: allow homing_accel to be configurable --- docs/Config_Reference.md | 3 +++ klippy/extras/homing.py | 47 +++++++++++++++++++++++++++------------- klippy/stepper.py | 6 +++++ klippy/toolhead.py | 8 +++++++ 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index 06a6c6d99..b5a66d00d 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -296,6 +296,9 @@ position_max: #homing_speed: 5.0 # Maximum velocity (in mm/s) of the stepper when homing. The default # is 5mm/s. +#homing_accel: +# Maximum accel (in mm/s) of the stepper when homing. The default +# is to use the max accel configured in the [printer]'s object. #homing_retract_dist: 5.0 # Distance to backoff (in mm) before homing a second time during # homing. If `use_sensorless_homing` is false, this setting can be set diff --git a/klippy/extras/homing.py b/klippy/extras/homing.py index a32341929..2c9343a24 100644 --- a/klippy/extras/homing.py +++ b/klippy/extras/homing.py @@ -270,7 +270,15 @@ def _fill_coord(self, coord): def set_homed_position(self, pos): self.toolhead.set_position(self._fill_coord(pos)) - def _set_current_homing(self, homing_axes, pre_homing): + def _set_homing_accel(self, accel, pre_homing): + if accel is None: + return + if pre_homing: + self.toolhead.set_accel(accel) + else: + self.toolhead.reset_accel() + + def _set_homing_current(self, homing_axes, pre_homing): print_time = self.toolhead.get_last_move_time() affected_rails = set() for axis in homing_axes: @@ -312,10 +320,13 @@ def home_rails(self, rails, forcepos, movepos): hi = rails[0].get_homing_info() hmove = HomingMove(self.printer, endstops) - self._set_current_homing(homing_axes, pre_homing=True) - self._reset_endstop_states(endstops) - - hmove.homing_move(homepos, hi.speed) + try: + self._set_homing_accel(hi.accel, pre_homing=True) + self._set_homing_current(homing_axes, pre_homing=True) + self._reset_endstop_states(endstops) + hmove.homing_move(homepos, hi.speed) + finally: + self._set_homing_accel(hi.accel, pre_homing=False) needs_rehome = False retract_dist = hi.retract_dist @@ -337,15 +348,18 @@ def home_rails(self, rails, forcepos, movepos): ] self.toolhead.move(retractpos, hi.retract_speed) if not hi.use_sensorless_homing or needs_rehome: - # Home again - startpos = [ - rp - ad * retract_r for rp, ad in zip(retractpos, axes_d) - ] - self.toolhead.set_position(startpos) - self._reset_endstop_states(endstops) - hmove = HomingMove(self.printer, endstops) - hmove.homing_move(homepos, hi.second_homing_speed) try: + # Home again + startpos = [ + rp - ad * retract_r + for rp, ad in zip(retractpos, axes_d) + ] + self.toolhead.set_position(startpos) + self._reset_endstop_states(endstops) + + hmove = HomingMove(self.printer, endstops) + hmove.homing_move(homepos, hi.second_homing_speed) + if hmove.check_no_movement() is not None: raise self.printer.command_error( "Endstop %s still triggered after retract" @@ -362,7 +376,9 @@ def home_rails(self, rails, forcepos, movepos): "Early homing trigger on second home!" ) finally: - self._set_current_homing(homing_axes, pre_homing=False) + self._set_homing_accel(hi.accel, pre_homing=False) + self._set_homing_current(homing_axes, pre_homing=False) + if hi.retract_dist: # Retract (again) startpos = self._fill_coord(forcepos) @@ -375,7 +391,8 @@ def home_rails(self, rails, forcepos, movepos): ] self.toolhead.move(retractpos, hi.retract_speed) - self._set_current_homing(homing_axes, pre_homing=False) + self._set_homing_accel(hi.accel, pre_homing=False) + self._set_homing_current(homing_axes, pre_homing=False) # Signal home operation complete self.toolhead.flush_step_generation() self.trigger_mcu_pos = { diff --git a/klippy/stepper.py b/klippy/stepper.py index 453d81a65..65ee53490 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -461,6 +461,10 @@ def __init__( "min_home_dist", self.homing_retract_dist, minval=0.0 ) + self.homing_accel = config.get("homing_accel", None) + if self.homing_accel is not None: + self.homing_accel = config.getfloat("homing_accel", minval=0.0) + if self.homing_positive_dir is None: axis_len = self.position_max - self.position_min if self.position_endstop <= self.position_min + axis_len / 4.0: @@ -507,6 +511,7 @@ def get_homing_info(self): "second_homing_speed", "use_sensorless_homing", "min_home_dist", + "accel", ], )( self.homing_speed, @@ -517,6 +522,7 @@ def get_homing_info(self): self.second_homing_speed, self.use_sensorless_homing, self.min_home_dist, + self.homing_accel, ) return homing_info diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 59df059ef..04d6952a3 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -864,6 +864,14 @@ def cmd_M204(self, gcmd): self.max_accel = accel self._calc_junction_deviation() + def set_accel(self, accel): + self.max_accel = accel + self._calc_junction_deviation() + + def reset_accel(self): + self.max_accel = self.orig_cfg["max_accel"] + self._calc_junction_deviation() + def add_printer_objects(config): config.get_printer().add_object("toolhead", ToolHead(config))