PyTmcStepper 0.15.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytmcstepper-0.15.0.dist-info/METADATA +338 -0
- pytmcstepper-0.15.0.dist-info/RECORD +56 -0
- pytmcstepper-0.15.0.dist-info/WHEEL +5 -0
- pytmcstepper-0.15.0.dist-info/licenses/LICENSE +683 -0
- pytmcstepper-0.15.0.dist-info/top_level.txt +1 -0
- tmc_driver/__init__.py +96 -0
- tmc_driver/_tmc_exceptions.py +21 -0
- tmc_driver/_tmc_math.py +122 -0
- tmc_driver/_tmc_stallguard.py +340 -0
- tmc_driver/_tmc_stepperdriver.py +210 -0
- tmc_driver/_tmc_validation.py +29 -0
- tmc_driver/_tmc_xxxx.py +489 -0
- tmc_driver/com/__init__.py +64 -0
- tmc_driver/com/_tmc_com.py +148 -0
- tmc_driver/com/_tmc_com_spi.py +81 -0
- tmc_driver/com/_tmc_com_spi_base.py +159 -0
- tmc_driver/com/_tmc_com_spi_ftdi.py +48 -0
- tmc_driver/com/_tmc_com_spi_micropython.py +99 -0
- tmc_driver/com/_tmc_com_uart.py +113 -0
- tmc_driver/com/_tmc_com_uart_base.py +386 -0
- tmc_driver/com/_tmc_com_uart_micropython.py +115 -0
- tmc_driver/enable_control/__init__.py +55 -0
- tmc_driver/enable_control/_tmc_ec.py +59 -0
- tmc_driver/enable_control/_tmc_ec_pin.py +48 -0
- tmc_driver/enable_control/_tmc_ec_toff.py +30 -0
- tmc_driver/motion_control/__init__.py +111 -0
- tmc_driver/motion_control/_tmc_mc.py +280 -0
- tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +218 -0
- tmc_driver/motion_control/_tmc_mc_step_dir.py +415 -0
- tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +78 -0
- tmc_driver/motion_control/_tmc_mc_step_reg.py +36 -0
- tmc_driver/motion_control/_tmc_mc_vactual.py +221 -0
- tmc_driver/platform_utils.py +13 -0
- tmc_driver/reg/__init__.py +0 -0
- tmc_driver/reg/_tmc2209_reg.py +55 -0
- tmc_driver/reg/_tmc220x_reg.py +261 -0
- tmc_driver/reg/_tmc224x_reg.py +414 -0
- tmc_driver/reg/_tmc5160_reg.py +526 -0
- tmc_driver/reg/_tmc_reg.py +202 -0
- tmc_driver/reg/_tmc_shared_regs.py +314 -0
- tmc_driver/tmc_2208.py +17 -0
- tmc_driver/tmc_2209.py +75 -0
- tmc_driver/tmc_220x.py +383 -0
- tmc_driver/tmc_2240.py +340 -0
- tmc_driver/tmc_5160.py +344 -0
- tmc_driver/tmc_gpio/__init__.py +101 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +106 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +162 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +135 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +108 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +87 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +182 -0
- tmc_driver/tmc_logger/__init__.py +15 -0
- tmc_driver/tmc_logger/_tmc_logger_base.py +111 -0
- tmc_driver/tmc_logger/_tmc_logger_cpython.py +155 -0
- tmc_driver/tmc_logger/_tmc_logger_micropython.py +52 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tmc_driver
|
tmc_driver/__init__.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PyTmcStepper - Python library for Trinamic TMC stepper motor drivers
|
|
3
|
+
|
|
4
|
+
This package provides a high-level interface for controlling TMC stepper motor drivers
|
|
5
|
+
via UART or SPI communication on various platforms including Raspberry Pi, Jetson, and MicroPython.
|
|
6
|
+
|
|
7
|
+
Main Components:
|
|
8
|
+
- Driver Classes: Tmc2208, Tmc2209, Tmc2240, Tmc5160
|
|
9
|
+
- Communication: TmcCom and subclasses for UART/SPI
|
|
10
|
+
- Motion Control: Various strategies for motor movement
|
|
11
|
+
- Enable Control: Motor enable/disable functionality
|
|
12
|
+
- GPIO Abstraction: Platform-independent GPIO access
|
|
13
|
+
- Logging: Configurable logging support
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
>>> from tmc_driver import Tmc2209, Loglevel
|
|
17
|
+
>>> from tmc_driver.com import TmcComUart
|
|
18
|
+
>>> from tmc_driver.motion_control import TmcMotionControlStepDir
|
|
19
|
+
>>> from tmc_driver.enable_control import TmcEnableControlPin
|
|
20
|
+
>>>
|
|
21
|
+
>>> tmc = Tmc2209(
|
|
22
|
+
... TmcEnableControlPin(21),
|
|
23
|
+
... TmcMotionControlStepDir(16, 20),
|
|
24
|
+
... TmcComUart("/dev/serial0"),
|
|
25
|
+
... loglevel=Loglevel.INFO
|
|
26
|
+
... )
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# Driver classes
|
|
30
|
+
from .tmc_2208 import Tmc2208
|
|
31
|
+
from .tmc_2209 import Tmc2209
|
|
32
|
+
from .tmc_2240 import Tmc2240
|
|
33
|
+
from .tmc_5160 import Tmc5160
|
|
34
|
+
|
|
35
|
+
# Exceptions
|
|
36
|
+
from ._tmc_exceptions import (
|
|
37
|
+
TmcException,
|
|
38
|
+
TmcDriverException,
|
|
39
|
+
TmcComException,
|
|
40
|
+
TmcMotionControlException,
|
|
41
|
+
TmcEnableControlException,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
# Logger
|
|
45
|
+
from .tmc_logger import TmcLogger, Loglevel
|
|
46
|
+
|
|
47
|
+
# GPIO
|
|
48
|
+
from . import tmc_gpio
|
|
49
|
+
from .tmc_gpio import Board
|
|
50
|
+
|
|
51
|
+
# Enable Control
|
|
52
|
+
from .enable_control._tmc_ec_pin import TmcEnableControlPin
|
|
53
|
+
from .enable_control._tmc_ec_toff import TmcEnableControlToff
|
|
54
|
+
|
|
55
|
+
# Motion Control
|
|
56
|
+
from .motion_control._tmc_mc import StopMode, MovementAbsRel, MovementPhase, Direction
|
|
57
|
+
from .motion_control._tmc_mc_step_dir import TmcMotionControlStepDir
|
|
58
|
+
from .motion_control._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
59
|
+
from .motion_control._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
60
|
+
from .motion_control._tmc_mc_vactual import TmcMotionControlVActual
|
|
61
|
+
from .motion_control._tmc_mc_int_ramp_generator import TmcMotionControlIntRampGenerator
|
|
62
|
+
|
|
63
|
+
__all__ = [
|
|
64
|
+
# Driver classes
|
|
65
|
+
"Tmc2208",
|
|
66
|
+
"Tmc2209",
|
|
67
|
+
"Tmc2240",
|
|
68
|
+
"Tmc5160",
|
|
69
|
+
# Exceptions
|
|
70
|
+
"TmcException",
|
|
71
|
+
"TmcDriverException",
|
|
72
|
+
"TmcComException",
|
|
73
|
+
"TmcMotionControlException",
|
|
74
|
+
"TmcEnableControlException",
|
|
75
|
+
# Logger
|
|
76
|
+
"TmcLogger",
|
|
77
|
+
"Loglevel",
|
|
78
|
+
# GPIO
|
|
79
|
+
"tmc_gpio",
|
|
80
|
+
"Board",
|
|
81
|
+
# Enable Control
|
|
82
|
+
"TmcEnableControlPin",
|
|
83
|
+
"TmcEnableControlToff",
|
|
84
|
+
# Motion Control
|
|
85
|
+
"StopMode",
|
|
86
|
+
"MovementAbsRel",
|
|
87
|
+
"MovementPhase",
|
|
88
|
+
"Direction",
|
|
89
|
+
"TmcMotionControlStepDir",
|
|
90
|
+
"TmcMotionControlStepPwmDir",
|
|
91
|
+
"TmcMotionControlStepReg",
|
|
92
|
+
"TmcMotionControlVActual",
|
|
93
|
+
"TmcMotionControlIntRampGenerator",
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
__version__ = "0.5.0"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Exceptions module"""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TmcException(Exception):
|
|
5
|
+
"""Base class for all TMC exceptions"""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TmcDriverException(TmcException):
|
|
9
|
+
"""Base class for all TMC driver exceptions"""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TmcComException(TmcException):
|
|
13
|
+
"""Base class for all TMC communication exceptions"""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TmcMotionControlException(TmcException):
|
|
17
|
+
"""Base class for all TMC motion control exceptions"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class TmcEnableControlException(TmcException):
|
|
21
|
+
"""Base class for all TMC enable control exceptions"""
|
tmc_driver/_tmc_math.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TMC2209_Raspberry_Pi math library
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def rps_to_vactual(rps, steps_per_rev: int, fclk: int = 12000000) -> int:
|
|
7
|
+
"""converts rps -> vactual
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
rps (float): revolutions per second
|
|
11
|
+
steps_per_rev (int): steps per revolution
|
|
12
|
+
fclk (int): clock speed of the tmc (Default value = 12000000)
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
vactual (int): value for vactual
|
|
16
|
+
"""
|
|
17
|
+
return int(round(rps / (fclk / 16777216) * steps_per_rev))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def vactual_to_rps(vactual: int, steps_per_rev: int, fclk: int = 12000000) -> float:
|
|
21
|
+
"""converts vactual -> rps
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
vactual (int): value for VACTUAL
|
|
25
|
+
steps_per_rev (int): steps per revolution
|
|
26
|
+
fclk (int): clock speed of the tmc (Default value = 12000000)
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
rps (float): revolutions per second
|
|
30
|
+
"""
|
|
31
|
+
return vactual * (fclk / 16777216) / steps_per_rev
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def rps_to_steps(rps: float, steps_per_rev: int) -> int:
|
|
35
|
+
"""converts rps -> steps/second
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
rps (float): revolutions per second
|
|
39
|
+
steps_per_rev (int): steps per revolution
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
steps (int): steps per second
|
|
43
|
+
"""
|
|
44
|
+
return int(round(rps * steps_per_rev))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def steps_to_rps(steps: int, steps_per_rev: int) -> float:
|
|
48
|
+
"""converts steps/second -> rps
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
steps (int): speed in steps per second
|
|
52
|
+
steps_per_rev (int): steps per revolution
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
rps (float): revolutions per second
|
|
56
|
+
"""
|
|
57
|
+
return steps / steps_per_rev
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def rps_to_tstep(rps: float, steps_per_rev: int, mres: int) -> int:
|
|
61
|
+
"""converts rps -> tstep
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
rps (float): revolutions per second
|
|
65
|
+
steps_per_rev (int): steps per revolution
|
|
66
|
+
mres (int): µstep resolution
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
tstep (int): time per step
|
|
70
|
+
"""
|
|
71
|
+
return int(round(12000000 / (rps_to_steps(rps, steps_per_rev) * 256 / mres)))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def steps_to_tstep(steps: int, mres: int) -> int:
|
|
75
|
+
"""converts steps/second -> tstep
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
steps (int): speed in steps per second
|
|
79
|
+
mres (int): µstep resolution
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
tstep (int): time per step
|
|
83
|
+
"""
|
|
84
|
+
if steps == 0:
|
|
85
|
+
return 0
|
|
86
|
+
return int(round(12000000 / (steps * 256 / mres)))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def constrain(val: int, min_val: int, max_val: int) -> int:
|
|
90
|
+
"""constrains a value between a min and a max
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
val (int): value that should be constrained
|
|
94
|
+
min_val (int): minimum value
|
|
95
|
+
max_val (int): maximum value
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
int: constrained value
|
|
99
|
+
"""
|
|
100
|
+
if val < min_val:
|
|
101
|
+
return min_val
|
|
102
|
+
if val > max_val:
|
|
103
|
+
return max_val
|
|
104
|
+
return val
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def mean(data) -> int:
|
|
108
|
+
"""Return the arithmetic mean
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
data (list): data to calculate the mean from
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
int: mean of the data
|
|
115
|
+
"""
|
|
116
|
+
count = len(data)
|
|
117
|
+
if count == 0:
|
|
118
|
+
return 0
|
|
119
|
+
|
|
120
|
+
total = sum(data)
|
|
121
|
+
|
|
122
|
+
return total // count
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
# pylint: disable=too-many-arguments
|
|
2
|
+
# pylint: disable=too-many-public-methods
|
|
3
|
+
# pylint: disable=too-many-branches
|
|
4
|
+
# pylint: disable=too-many-instance-attributes
|
|
5
|
+
# pylint: disable=too-many-positional-arguments
|
|
6
|
+
# pylint: disable=bare-except
|
|
7
|
+
# pylint: disable=no-member
|
|
8
|
+
# pylint: disable=unused-import
|
|
9
|
+
# pylint: disable=wildcard-import
|
|
10
|
+
# pylint: disable=unused-wildcard-import
|
|
11
|
+
"""StallGuard mixin"""
|
|
12
|
+
|
|
13
|
+
import types
|
|
14
|
+
from ._tmc_stepperdriver import *
|
|
15
|
+
from .com._tmc_com import TmcCom
|
|
16
|
+
from .tmc_gpio import GpioPUD, GpioMode
|
|
17
|
+
from . import tmc_gpio
|
|
18
|
+
from .tmc_logger import Loglevel
|
|
19
|
+
from .reg import _tmc_shared_regs as tmc_shared_regs
|
|
20
|
+
from . import _tmc_math as tmc_math
|
|
21
|
+
from ._tmc_exceptions import (
|
|
22
|
+
TmcComException,
|
|
23
|
+
TmcMotionControlException,
|
|
24
|
+
TmcDriverException,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
_CB_SENTINEL = object()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class StallGuard:
|
|
31
|
+
"""StallGuard
|
|
32
|
+
|
|
33
|
+
This class is used to control the stallguard feature of the TMC stepper driver.
|
|
34
|
+
The drivers class needs to inherit from this class to use the stallguard feature (mixin).
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
tmc_logger: TmcLogger
|
|
38
|
+
tmc_com: TmcCom | None
|
|
39
|
+
tmc_mc: TmcMotionControl
|
|
40
|
+
|
|
41
|
+
coolconf: tmc_shared_regs.CoolConf
|
|
42
|
+
sgthrs: tmc_shared_regs.SgThrs
|
|
43
|
+
sgresult: tmc_shared_regs.SgResult
|
|
44
|
+
tcoolthrs: tmc_shared_regs.TCoolThrs
|
|
45
|
+
drvstatus: tmc_shared_regs.DrvStatus
|
|
46
|
+
|
|
47
|
+
# Stub methods - implemented by the driver class (Tmc2209, Tmc2240, Tmc5160)
|
|
48
|
+
def set_spreadcycle(self, en: bool) -> None:
|
|
49
|
+
"""Set spreadcycle mode. Implemented by driver class."""
|
|
50
|
+
raise NotImplementedError("set_spreadcycle must be implemented by driver class")
|
|
51
|
+
|
|
52
|
+
def get_microstepping_resolution(self) -> int:
|
|
53
|
+
"""Get microstepping resolution. Implemented by driver class."""
|
|
54
|
+
raise NotImplementedError(
|
|
55
|
+
"get_microstepping_resolution must be implemented by driver class"
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def sg_callback(self):
|
|
60
|
+
"""stallguard callback function"""
|
|
61
|
+
return self._sg_callback
|
|
62
|
+
|
|
63
|
+
@sg_callback.setter
|
|
64
|
+
def sg_callback(self, callback):
|
|
65
|
+
if self._pin_stallguard is None:
|
|
66
|
+
raise TmcDriverException(
|
|
67
|
+
"StallGuard pin not set. Cannot set callback function."
|
|
68
|
+
)
|
|
69
|
+
self._sg_callback = callback
|
|
70
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
71
|
+
if callback is not None:
|
|
72
|
+
tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
|
|
73
|
+
|
|
74
|
+
def __init__(self):
|
|
75
|
+
"""initialize StallGuard instance variables"""
|
|
76
|
+
self._pin_stallguard: int | None = None
|
|
77
|
+
self._sg_threshold: int = 100 # threshold for stallguard
|
|
78
|
+
|
|
79
|
+
def __del__(self):
|
|
80
|
+
self.deinit()
|
|
81
|
+
|
|
82
|
+
def deinit(self):
|
|
83
|
+
"""destructor"""
|
|
84
|
+
if hasattr(self, "_pin_stallguard") and self._pin_stallguard is not None:
|
|
85
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
86
|
+
tmc_gpio.tmc_gpio.gpio_cleanup(self._pin_stallguard)
|
|
87
|
+
self._pin_stallguard = None
|
|
88
|
+
|
|
89
|
+
def set_stallguard_callback(
|
|
90
|
+
self, pin_stallguard, threshold, callback, min_speed=100
|
|
91
|
+
):
|
|
92
|
+
"""set a function to call back, when the driver detects a stall
|
|
93
|
+
via stallguard
|
|
94
|
+
high value on the diag pin can also mean a driver error
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
pin_stallguard (int): pin needs to be connected to DIAG
|
|
98
|
+
threshold (int): value for SGTHRS
|
|
99
|
+
callback (func): will be called on StallGuard trigger
|
|
100
|
+
min_speed (int): min speed [steps/s] for StallGuard (Default value = 100)
|
|
101
|
+
"""
|
|
102
|
+
self.tmc_logger.log(
|
|
103
|
+
f"setup stallguard callback on GPIO {pin_stallguard}", Loglevel.INFO
|
|
104
|
+
)
|
|
105
|
+
self.tmc_logger.log(
|
|
106
|
+
f"StallGuard Threshold: {threshold} | minimum Speed: {min_speed}",
|
|
107
|
+
Loglevel.INFO,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
self.stallguard_setup(threshold, min_speed)
|
|
111
|
+
|
|
112
|
+
self._sg_callback = callback
|
|
113
|
+
self._pin_stallguard = pin_stallguard
|
|
114
|
+
|
|
115
|
+
if self._pin_stallguard is not None:
|
|
116
|
+
tmc_gpio.tmc_gpio.gpio_setup(
|
|
117
|
+
self._pin_stallguard, GpioMode.IN, pull_up_down=GpioPUD.PUD_DOWN
|
|
118
|
+
)
|
|
119
|
+
# first remove existing events
|
|
120
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
121
|
+
if callback is not None:
|
|
122
|
+
tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
|
|
123
|
+
|
|
124
|
+
def enable_coolstep(
|
|
125
|
+
self,
|
|
126
|
+
semin_sg: int = 150,
|
|
127
|
+
semax_sg: int = 200,
|
|
128
|
+
seup: int = 1,
|
|
129
|
+
sedn: int = 3,
|
|
130
|
+
min_speed: int = 100,
|
|
131
|
+
):
|
|
132
|
+
"""enables coolstep and sets the parameters for coolstep
|
|
133
|
+
The values for semin etc. can be tested with the test_stallguard_threshold function
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
semin_sg (int): lower threshold. Current will be increased if SG_Result goes below this
|
|
137
|
+
semax_sg (int): upper threshold. Current will be decreased if SG_Result goes above this
|
|
138
|
+
seup (int): current increment step
|
|
139
|
+
sedn (int): number of SG_Result readings for each current decrement
|
|
140
|
+
"""
|
|
141
|
+
semax_sg = semax_sg - semin_sg
|
|
142
|
+
|
|
143
|
+
self.coolconf.read()
|
|
144
|
+
self.coolconf.semin = round(max(0, min(semin_sg / 32, 15)))
|
|
145
|
+
self.coolconf.semax = round(max(0, min(semax_sg / 32, 15)))
|
|
146
|
+
self.coolconf.seimin = True # scale down to until 1/4 of IRun (7 - 31)
|
|
147
|
+
self.coolconf.seup = int(seup)
|
|
148
|
+
self.coolconf.sedn = int(sedn)
|
|
149
|
+
self.coolconf.write_check()
|
|
150
|
+
|
|
151
|
+
self._set_coolstep_threshold(
|
|
152
|
+
tmc_math.steps_to_tstep(int(min_speed), self.get_microstepping_resolution())
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
def get_stallguard_result(self):
|
|
156
|
+
"""return the current stallguard result
|
|
157
|
+
its will be calculated with every fullstep
|
|
158
|
+
higher values means a lower motor load
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
sgresult (int): StallGuard Result
|
|
162
|
+
"""
|
|
163
|
+
self.sgresult.read()
|
|
164
|
+
return self.sgresult.sgresult
|
|
165
|
+
|
|
166
|
+
def _set_stallguard_threshold(self, threshold):
|
|
167
|
+
"""sets the register bit "SGTHRS" to to a given value
|
|
168
|
+
this is needed for the stallguard interrupt callback
|
|
169
|
+
SGRESULT becomes compared to the double of this threshold.
|
|
170
|
+
SGRESULT ≤ SGTHRS*2
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
threshold (int): value for SGTHRS
|
|
174
|
+
"""
|
|
175
|
+
self.sgthrs.modify("sgthrs", int(threshold))
|
|
176
|
+
|
|
177
|
+
def _set_coolstep_threshold(self, threshold):
|
|
178
|
+
"""This is the lower threshold velocity for switching
|
|
179
|
+
on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
threshold (int): threshold velocity for coolstep
|
|
183
|
+
"""
|
|
184
|
+
self.tcoolthrs.modify("tcoolthrs", int(threshold))
|
|
185
|
+
|
|
186
|
+
def _reset_current_pos(self):
|
|
187
|
+
"""resets the current position of the motor to 0"""
|
|
188
|
+
if self.tmc_mc is None:
|
|
189
|
+
raise TmcMotionControlException("tmc_mc is None; cannot reset current pos")
|
|
190
|
+
self.tmc_mc.current_pos = 0
|
|
191
|
+
|
|
192
|
+
def stallguard_setup(
|
|
193
|
+
self,
|
|
194
|
+
threshold: int,
|
|
195
|
+
min_speed: int,
|
|
196
|
+
):
|
|
197
|
+
"""internal setup for stallguard
|
|
198
|
+
Args:
|
|
199
|
+
threshold (int): value for SGTHRS
|
|
200
|
+
min_speed (int): min speed [steps/s] for StallGuard
|
|
201
|
+
"""
|
|
202
|
+
self.tmc_logger.log(f"Stallguard threshold: {threshold}", Loglevel.DEBUG)
|
|
203
|
+
|
|
204
|
+
self.set_spreadcycle(False)
|
|
205
|
+
self._set_stallguard_threshold(threshold)
|
|
206
|
+
self._set_coolstep_threshold(
|
|
207
|
+
tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution())
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
def do_homing(
|
|
211
|
+
self,
|
|
212
|
+
diag_pin: int,
|
|
213
|
+
revolutions=10,
|
|
214
|
+
threshold=100,
|
|
215
|
+
max_speed: int | None = None,
|
|
216
|
+
cb_success: types.FunctionType | object | None = _CB_SENTINEL,
|
|
217
|
+
cb_failure: types.FunctionType | object | None = None,
|
|
218
|
+
) -> bool:
|
|
219
|
+
"""homes the motor in the given direction using stallguard.
|
|
220
|
+
this method is using vactual to move the motor and an interrupt on the DIAG pin
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
diag_pin (int): DIAG pin number
|
|
224
|
+
revolutions (int): max number of revolutions. Can be negative for inverse direction
|
|
225
|
+
(Default value = 10)
|
|
226
|
+
threshold (int): StallGuard detection threshold (Default value = 100)
|
|
227
|
+
max_speed (int): max speed for homing in steps/s (Default value = None)
|
|
228
|
+
cb_success (func|None): callback function on successful homing
|
|
229
|
+
cb_failure (func|None): callback function on failed homing
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
not homing_failed (bool): true when homing was successful
|
|
233
|
+
"""
|
|
234
|
+
if self.tmc_com is None:
|
|
235
|
+
raise TmcComException("tmc_com is None; cannot do homing")
|
|
236
|
+
if self.tmc_mc is None:
|
|
237
|
+
raise TmcMotionControlException("tmc_mc is None; cannot do homing")
|
|
238
|
+
|
|
239
|
+
if max_speed is None:
|
|
240
|
+
max_speed = self.tmc_mc.max_speed_homing
|
|
241
|
+
if cb_success is _CB_SENTINEL:
|
|
242
|
+
cb_success = self._reset_current_pos
|
|
243
|
+
|
|
244
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
245
|
+
self.tmc_logger.log("homing", Loglevel.INFO)
|
|
246
|
+
|
|
247
|
+
self.set_stallguard_callback(
|
|
248
|
+
diag_pin,
|
|
249
|
+
threshold,
|
|
250
|
+
lambda _: self.tmc_mc.stop(),
|
|
251
|
+
round(0.5 * max_speed),
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
stop_mode = self.tmc_mc.run_to_position_steps(
|
|
255
|
+
revolutions * self.tmc_mc.steps_per_rev, MovementAbsRel.RELATIVE
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
homing_succeeded = stop_mode is StopMode.HARDSTOP
|
|
259
|
+
|
|
260
|
+
if homing_succeeded:
|
|
261
|
+
self.tmc_logger.log("homing successful", Loglevel.INFO)
|
|
262
|
+
if cb_success is not None and callable(cb_success):
|
|
263
|
+
cb_success()
|
|
264
|
+
else:
|
|
265
|
+
self.tmc_logger.log("homing failed", Loglevel.INFO)
|
|
266
|
+
if cb_failure is not None and callable(cb_failure):
|
|
267
|
+
cb_failure()
|
|
268
|
+
|
|
269
|
+
return homing_succeeded
|
|
270
|
+
|
|
271
|
+
def test_stallguard_threshold(self, steps):
|
|
272
|
+
"""test method for tuning stallguard threshold
|
|
273
|
+
|
|
274
|
+
run this function with your motor settings and your motor load
|
|
275
|
+
the function will determine the minimum stallguard results for each movement phase
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
steps (int):
|
|
279
|
+
"""
|
|
280
|
+
if not isinstance(self.tmc_mc, TmcMotionControlStepDir):
|
|
281
|
+
raise TmcMotionControlException(
|
|
282
|
+
"tmc_mc is not of type TmcMotionControlStepDir; cannot test stallguard threshold"
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
286
|
+
self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO)
|
|
287
|
+
|
|
288
|
+
self.set_spreadcycle(False)
|
|
289
|
+
|
|
290
|
+
min_stallguard_result_accel = 512
|
|
291
|
+
min_stallguard_result_maxspeed = 512
|
|
292
|
+
min_stallguard_result_decel = 512
|
|
293
|
+
|
|
294
|
+
self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)
|
|
295
|
+
|
|
296
|
+
while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL:
|
|
297
|
+
self.drvstatus.read()
|
|
298
|
+
stallguard_result = getattr(self.drvstatus, "sgresult", None)
|
|
299
|
+
if stallguard_result is None:
|
|
300
|
+
stallguard_result = self.get_stallguard_result()
|
|
301
|
+
stallguard_triggered = getattr(self.drvstatus, "stallguard", None)
|
|
302
|
+
cs_actual = getattr(self.drvstatus, "cs_actual", None)
|
|
303
|
+
|
|
304
|
+
self.tmc_logger.log(
|
|
305
|
+
f"{self.tmc_mc.movement_phase} | {stallguard_result} | {stallguard_triggered} | {cs_actual}",
|
|
306
|
+
Loglevel.INFO,
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
if (
|
|
310
|
+
self.tmc_mc.movement_phase == MovementPhase.ACCELERATING
|
|
311
|
+
and stallguard_result < min_stallguard_result_accel
|
|
312
|
+
):
|
|
313
|
+
min_stallguard_result_accel = stallguard_result
|
|
314
|
+
if (
|
|
315
|
+
self.tmc_mc.movement_phase == MovementPhase.MAXSPEED
|
|
316
|
+
and stallguard_result < min_stallguard_result_maxspeed
|
|
317
|
+
):
|
|
318
|
+
min_stallguard_result_maxspeed = stallguard_result
|
|
319
|
+
if (
|
|
320
|
+
self.tmc_mc.movement_phase == MovementPhase.DECELERATING
|
|
321
|
+
and stallguard_result < min_stallguard_result_decel
|
|
322
|
+
):
|
|
323
|
+
min_stallguard_result_decel = stallguard_result
|
|
324
|
+
|
|
325
|
+
self.tmc_mc.wait_for_movement_finished_threaded()
|
|
326
|
+
|
|
327
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
328
|
+
self.tmc_logger.log(
|
|
329
|
+
f"min StallGuard result during accel: {min_stallguard_result_accel}",
|
|
330
|
+
Loglevel.INFO,
|
|
331
|
+
)
|
|
332
|
+
self.tmc_logger.log(
|
|
333
|
+
f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}",
|
|
334
|
+
Loglevel.INFO,
|
|
335
|
+
)
|
|
336
|
+
self.tmc_logger.log(
|
|
337
|
+
f"min StallGuard result during decel: {min_stallguard_result_decel}",
|
|
338
|
+
Loglevel.INFO,
|
|
339
|
+
)
|
|
340
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|