Skip to content

Commit

Permalink
tmc: Refactor TMCtstepHelper()
Browse files Browse the repository at this point in the history
Update TMCtstepHelper() to obtain the step_distance, tmc_frequency,
and mres fields directly.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
  • Loading branch information
KevinOConnor authored and rogerlz committed May 28, 2024
1 parent 3edaf29 commit 1930d28
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions klippy/extras/tmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,16 @@ def cmd_SET_TMC_FIELD(self, gcmd):
raise gcmd.error("Unknown field name '%s'" % (field_name,))
value = gcmd.get_int("VALUE", None)
velocity = gcmd.get_float("VELOCITY", None, minval=0.0)
tmc_frequency = self.mcu_tmc.get_tmc_frequency()
if tmc_frequency is None and velocity is not None:
raise gcmd.error("VELOCITY parameter not supported by this driver")
if (value is None) == (velocity is None):
raise gcmd.error("Specify either VALUE or VELOCITY")
if velocity is not None:
step_dist = self.stepper.get_step_dist()
mres = self.fields.get_field("mres")
value = TMCtstepHelper(step_dist, mres, tmc_frequency, velocity)
if self.mcu_tmc.get_tmc_frequency() is None:
raise gcmd.error(
"VELOCITY parameter not supported by this driver"
)
value = TMCtstepHelper(
self.mcu_tmc, velocity, pstepper=self.stepper
)
reg_val = self.fields.set_field(field_name, value)
print_time = self.printer.lookup_object("toolhead").get_last_move_time()
self.mcu_tmc.set_register(reg_name, reg_val, print_time)
Expand Down Expand Up @@ -725,13 +726,21 @@ def TMCMicrostepHelper(config, mcu_tmc):


# Helper for calculating TSTEP based values from velocity
def TMCtstepHelper(step_dist, mres, tmc_freq, velocity):
if velocity > 0.0:
step_dist_256 = step_dist / (1 << mres)
threshold = int(tmc_freq * step_dist_256 / velocity + 0.5)
return max(0, min(0xFFFFF, threshold))
else:
def TMCtstepHelper(mcu_tmc, velocity, pstepper=None, config=None):
if velocity <= 0.0:
return 0xFFFFF
if pstepper is not None:
step_dist = pstepper.get_step_dist()
else:
stepper_name = " ".join(config.get_name().split()[1:])
sconfig = config.getsection(stepper_name)
rotation_dist, steps_per_rotation = stepper.parse_step_distance(sconfig)
step_dist = rotation_dist / steps_per_rotation
mres = mcu_tmc.get_fields().get_field("mres")
step_dist_256 = step_dist / (1 << mres)
tmc_freq = mcu_tmc.get_tmc_frequency()
threshold = int(tmc_freq * step_dist_256 / velocity + 0.5)
return max(0, min(0xFFFFF, threshold))


# Helper to configure stealthChop-spreadCycle transition velocity
Expand All @@ -743,13 +752,7 @@ def TMCStealthchopHelper(config, mcu_tmc, tmc_freq):

if velocity is not None:
en_pwm_mode = True

stepper_name = " ".join(config.get_name().split()[1:])
sconfig = config.getsection(stepper_name)
rotation_dist, steps_per_rotation = stepper.parse_step_distance(sconfig)
step_dist = rotation_dist / steps_per_rotation
mres = fields.get_field("mres")
tpwmthrs = TMCtstepHelper(step_dist, mres, tmc_freq, velocity)
tpwmthrs = TMCtstepHelper(mcu_tmc, velocity, config=config)
fields.set_field("tpwmthrs", tpwmthrs)

reg = fields.lookup_register("en_pwm_mode", None)
Expand Down Expand Up @@ -858,3 +861,24 @@ def set_current(self, new_current, hold_current, print_time, force=False):

self.set_actual_current(new_current)
self.apply_current(print_time)


# Helper to configure StallGuard and CoolStep minimum velocity
def TMCVcoolthrsHelper(config, mcu_tmc):
fields = mcu_tmc.get_fields()
velocity = config.getfloat("coolstep_threshold", None, minval=0.0)
tcoolthrs = 0
if velocity is not None:
tcoolthrs = TMCtstepHelper(mcu_tmc, velocity, config=config)
fields.set_field("tcoolthrs", tcoolthrs)


# Helper to configure StallGuard and CoolStep maximum velocity and
# SpreadCycle-FullStepping (High velocity) mode threshold.
def TMCVhighHelper(config, mcu_tmc):
fields = mcu_tmc.get_fields()
velocity = config.getfloat("high_velocity_threshold", None, minval=0.0)
thigh = 0
if velocity is not None:
thigh = TMCtstepHelper(mcu_tmc, velocity, config=config)
fields.set_field("thigh", thigh)

0 comments on commit 1930d28

Please sign in to comment.