Skip to content

Commit

Permalink
Merge pull request #39 from Chr157i4n/dev
Browse files Browse the repository at this point in the history
version 0.3.3
  • Loading branch information
Chr157i4n authored Nov 11, 2023
2 parents 5176cd3 + 2afa25c commit 310d5e1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## version 0.3.3
- added correct StallGuard min_speed calculation

## version 0.3.2
- add pylint github action
- fixed code to pass pylint check
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ tmc.set_motor_enabled(False)


## Troubleshoot
If you have questions please check out the [Wiki](https://github.com/Chr157i4n/TMC2209_Raspberry_Pi/wiki) and the other issues.

If you encounter any problems, feel free to open an issue (ENG/GER).
Please don't send any E-Mails to me. Pls use Github, so that i don't need to answer the same question multiple times.
I reserve the right not to answer E-Mails.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = TMC_2209_Raspberry_Pi
version = 0.3.2
version = 0.3.3
author = Christian Köhlke
author_email = [email protected]
description = this is a Python libary to drive a stepper motor with a Trinamic TMC2209 stepper driver and a Raspberry Pi
Expand Down
35 changes: 23 additions & 12 deletions src/TMC_2209/TMC_2209_StepperDriver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pylint: disable=invalid-name
#pylint: disable=import-error
#pylint: disable=too-many-arguments
#pylint: disable=too-many-lines
#pylint: disable=too-many-arguments
#pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -48,7 +47,7 @@ class MovementPhase(Enum):
STANDSTILL = 0
ACCELERATING = 1
MAXSPEED = 2
DEACCELERATING = 3
DECELERATING = 3


class StopMode(Enum):
Expand Down Expand Up @@ -986,6 +985,9 @@ def get_stallguard_result(self):
return the current stallguard result
its will be calculated with every fullstep
higher values means a lower motor load
Returns:
sg_result (int): StallGuard Result
"""
sg_result = self.tmc_uart.read_int(reg.SG_RESULT)
return sg_result
Expand All @@ -998,6 +1000,9 @@ def set_stallguard_threshold(self, threshold):
this is needed for the stallguard interrupt callback
SG_RESULT becomes compared to the double of this threshold.
SG_RESULT ≤ SGTHRS*2
Parameters:
threshold (int): value for SGTHRS
"""

self.log("sgthrs", Loglevel.INFO.value)
Expand All @@ -1023,18 +1028,24 @@ def set_coolstep_threshold(self, threshold):


def set_stallguard_callback(self, pin_stallguard, threshold, callback,
min_speed = 2000, ignore_delay = 0):
min_speed = 100, ignore_delay = 0):
"""
set a function to call back, when the driver detects a stall
via stallguard
high value on the diag pin can also mean a driver error
Parameters:
pin_stallguard (int): pin needs to be connected to DIAG
threshold (int): value for SGTHRS
callback (function): will be called on StallGuard trigger
min_speed (int): min speed [steps/s] for StallGuard
"""
self.log("setup stallguard callback on GPIO"+str(pin_stallguard), Loglevel.INFO.value)
self.log("StallGuard Threshold: "+str(threshold)+"\tminimum Speed: "+str(min_speed),
Loglevel.INFO.value)

self.set_stallguard_threshold(threshold)
self.set_coolstep_threshold(min_speed)
self.set_coolstep_threshold(int(round(12000000 / (min_speed * 256 / self._msres))))
self._sg_delay = ignore_delay
self._sg_callback = callback
self._pin_stallguard = pin_stallguard
Expand Down Expand Up @@ -1293,7 +1304,7 @@ def compute_new_speed(self):
if ((steps_to_stop >= distance_to) or self._direction == Direction.CCW or
self._stop == StopMode.SOFTSTOP):
self._n = -steps_to_stop # Start deceleration
self._movement_phase = MovementPhase.DEACCELERATING
self._movement_phase = MovementPhase.DECELERATING
elif self._n < 0:
# Currently decelerating, need to accel again?
if (steps_to_stop < distance_to) and self._direction == Direction.CW:
Expand All @@ -1307,7 +1318,7 @@ def compute_new_speed(self):
if (((steps_to_stop >= -distance_to) or self._direction == Direction.CW or
self._stop == StopMode.SOFTSTOP)):
self._n = -steps_to_stop # Start deceleration
self._movement_phase = MovementPhase.DEACCELERATING
self._movement_phase = MovementPhase.DECELERATING
elif self._n < 0:
# Currently decelerating, need to accel again?
if (steps_to_stop < -distance_to) and self._direction == Direction.CCW:
Expand Down Expand Up @@ -1520,7 +1531,7 @@ def test_stallguard_threshold(self, steps):

min_stallguard_result_accel = 511
min_stallguard_result_maxspeed = 511
min_stallguard_result_deaccel = 511
min_stallguard_result_decel = 511

self.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)

Expand All @@ -1537,9 +1548,9 @@ def test_stallguard_threshold(self, steps):
if (self._movement_phase == MovementPhase.MAXSPEED and
stallguard_result < min_stallguard_result_maxspeed):
min_stallguard_result_maxspeed = stallguard_result
if (self._movement_phase == MovementPhase.DEACCELERATING and
stallguard_result < min_stallguard_result_deaccel):
min_stallguard_result_deaccel = stallguard_result
if (self._movement_phase == MovementPhase.DECELERATING and
stallguard_result < min_stallguard_result_decel):
min_stallguard_result_decel = stallguard_result

self.wait_for_movement_finished_threaded()

Expand All @@ -1548,6 +1559,6 @@ def test_stallguard_threshold(self, steps):
str(min_stallguard_result_accel), Loglevel.INFO.value)
self.log("min StallGuard result during maxspeed: " +
str(min_stallguard_result_maxspeed), Loglevel.INFO.value)
self.log("min StallGuard result during deacceleration: " +
str(min_stallguard_result_deaccel), Loglevel.INFO.value)
self.log("min StallGuard result during deceleration: " +
str(min_stallguard_result_decel), Loglevel.INFO.value)
self.log("---", Loglevel.INFO.value)

0 comments on commit 310d5e1

Please sign in to comment.