PyTmcStepper 0.9.3__tar.gz → 0.10.0__tar.gz
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.9.3 → pytmcstepper-0.10.0}/PKG-INFO +1 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/pyproject.toml +1 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/PKG-INFO +1 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/SOURCES.txt +4 -1
- pytmcstepper-0.10.0/src/tmc_driver/_tmc_stallguard.py +248 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_stepperdriver.py +13 -24
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com.py +13 -7
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi.py +1 -3
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_base.py +13 -9
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_ftdi.py +2 -3
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_micropython.py +1 -3
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart.py +10 -10
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart_base.py +23 -11
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart_micropython.py +2 -5
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/_tmc_ec.py +5 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/_tmc_ec_pin.py +1 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc.py +6 -3
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_dir.py +0 -3
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_reg.py +0 -3
- pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc2209_reg.py +55 -0
- pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc220x_reg.py +259 -0
- pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc224x_reg.py +403 -0
- pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc_reg.py +164 -0
- pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc_shared_regs.py +54 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_2208.py +2 -1
- pytmcstepper-0.10.0/src/tmc_driver/tmc_2209.py +139 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_220x.py +31 -155
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_2240.py +28 -164
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +3 -0
- pytmcstepper-0.10.0/src/tmc_driver/tmc_xxxx.py +230 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc2209.py +0 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc2240.py +0 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_com_spi.py +1 -1
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_com_uart.py +1 -1
- pytmcstepper-0.10.0/tests/test_tmc_modules.py +175 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_move.py +0 -1
- pytmcstepper-0.10.0/tests/test_tmc_stallguard.py +112 -0
- pytmcstepper-0.9.3/src/tmc_driver/_tmc_stallguard.py +0 -157
- pytmcstepper-0.9.3/src/tmc_driver/reg/_tmc2209_reg.py +0 -66
- pytmcstepper-0.9.3/src/tmc_driver/reg/_tmc220x_reg.py +0 -287
- pytmcstepper-0.9.3/src/tmc_driver/reg/_tmc224x_reg.py +0 -464
- pytmcstepper-0.9.3/src/tmc_driver/reg/_tmc_reg.py +0 -135
- pytmcstepper-0.9.3/src/tmc_driver/tmc_2209.py +0 -276
- pytmcstepper-0.9.3/tests/test_tmc_modules.py +0 -64
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/LICENSE +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/README.md +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/setup.cfg +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/dependency_links.txt +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/requires.txt +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/top_level.txt +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_exceptions.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_logger.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_math.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_validation.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/com/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/_tmc_ec_toff.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_vactual.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/reg/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/__init__.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_ec_pin.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_math.py +0 -0
- {pytmcstepper-0.9.3 → pytmcstepper-0.10.0}/tests/test_tmc_set_current.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PyTmcStepper
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.10.0
|
|
4
4
|
Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
|
|
5
5
|
Author-email: Christian Köhlke <christian@koehlke.de>
|
|
6
6
|
License-Expression: GPL-3.0
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "PyTmcStepper"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.10.0"
|
|
8
8
|
authors = [{ name = "Christian Köhlke", email = "christian@koehlke.de" }]
|
|
9
9
|
description = "This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi"
|
|
10
10
|
readme = "README.md"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PyTmcStepper
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.10.0
|
|
4
4
|
Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
|
|
5
5
|
Author-email: Christian Köhlke <christian@koehlke.de>
|
|
6
6
|
License-Expression: GPL-3.0
|
|
@@ -17,6 +17,7 @@ src/tmc_driver/tmc_2208.py
|
|
|
17
17
|
src/tmc_driver/tmc_2209.py
|
|
18
18
|
src/tmc_driver/tmc_220x.py
|
|
19
19
|
src/tmc_driver/tmc_2240.py
|
|
20
|
+
src/tmc_driver/tmc_xxxx.py
|
|
20
21
|
src/tmc_driver/com/__init__.py
|
|
21
22
|
src/tmc_driver/com/_tmc_com.py
|
|
22
23
|
src/tmc_driver/com/_tmc_com_spi.py
|
|
@@ -41,6 +42,7 @@ src/tmc_driver/reg/_tmc2209_reg.py
|
|
|
41
42
|
src/tmc_driver/reg/_tmc220x_reg.py
|
|
42
43
|
src/tmc_driver/reg/_tmc224x_reg.py
|
|
43
44
|
src/tmc_driver/reg/_tmc_reg.py
|
|
45
|
+
src/tmc_driver/reg/_tmc_shared_regs.py
|
|
44
46
|
src/tmc_driver/tmc_gpio/__init__.py
|
|
45
47
|
src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py
|
|
46
48
|
src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py
|
|
@@ -56,4 +58,5 @@ tests/test_tmc_ec_pin.py
|
|
|
56
58
|
tests/test_tmc_math.py
|
|
57
59
|
tests/test_tmc_modules.py
|
|
58
60
|
tests/test_tmc_move.py
|
|
59
|
-
tests/test_tmc_set_current.py
|
|
61
|
+
tests/test_tmc_set_current.py
|
|
62
|
+
tests/test_tmc_stallguard.py
|
|
@@ -0,0 +1,248 @@
|
|
|
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
|
+
|
|
46
|
+
@property
|
|
47
|
+
def sg_callback(self):
|
|
48
|
+
"""stallguard callback function"""
|
|
49
|
+
return self._sg_callback
|
|
50
|
+
|
|
51
|
+
@sg_callback.setter
|
|
52
|
+
def sg_callback(self, callback):
|
|
53
|
+
if self._pin_stallguard is None:
|
|
54
|
+
raise TmcDriverException(
|
|
55
|
+
"StallGuard pin not set. Cannot set callback function."
|
|
56
|
+
)
|
|
57
|
+
self._sg_callback = callback
|
|
58
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
59
|
+
if callback is not None:
|
|
60
|
+
tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
|
|
61
|
+
|
|
62
|
+
def __init__(self):
|
|
63
|
+
"""initialize StallGuard instance variables"""
|
|
64
|
+
self._pin_stallguard: int | None = None
|
|
65
|
+
self._sg_threshold: int = 100 # threshold for stallguard
|
|
66
|
+
|
|
67
|
+
def __del__(self):
|
|
68
|
+
self.deinit()
|
|
69
|
+
|
|
70
|
+
def deinit(self):
|
|
71
|
+
"""destructor"""
|
|
72
|
+
if (
|
|
73
|
+
getattr(self, "_pin_stallguard", None) is not None
|
|
74
|
+
and self._pin_stallguard is not None
|
|
75
|
+
):
|
|
76
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
77
|
+
tmc_gpio.tmc_gpio.gpio_cleanup(self._pin_stallguard)
|
|
78
|
+
self._pin_stallguard = None
|
|
79
|
+
|
|
80
|
+
def set_stallguard_callback(
|
|
81
|
+
self, pin_stallguard, threshold, callback, min_speed=100
|
|
82
|
+
):
|
|
83
|
+
"""set a function to call back, when the driver detects a stall
|
|
84
|
+
via stallguard
|
|
85
|
+
high value on the diag pin can also mean a driver error
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
pin_stallguard (int): pin needs to be connected to DIAG
|
|
89
|
+
threshold (int): value for SGTHRS
|
|
90
|
+
callback (func): will be called on StallGuard trigger
|
|
91
|
+
min_speed (int): min speed [steps/s] for StallGuard (Default value = 100)
|
|
92
|
+
"""
|
|
93
|
+
self.tmc_logger.log(
|
|
94
|
+
f"setup stallguard callback on GPIO {pin_stallguard}", Loglevel.INFO
|
|
95
|
+
)
|
|
96
|
+
self.tmc_logger.log(
|
|
97
|
+
f"StallGuard Threshold: {threshold} | minimum Speed: {min_speed}",
|
|
98
|
+
Loglevel.INFO,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
self._set_stallguard_threshold(threshold)
|
|
102
|
+
self._set_coolstep_threshold(
|
|
103
|
+
tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution())
|
|
104
|
+
)
|
|
105
|
+
self._sg_callback = callback
|
|
106
|
+
self._pin_stallguard = pin_stallguard
|
|
107
|
+
|
|
108
|
+
if self._pin_stallguard is not None:
|
|
109
|
+
tmc_gpio.tmc_gpio.gpio_setup(
|
|
110
|
+
self._pin_stallguard, GpioMode.IN, pull_up_down=GpioPUD.PUD_DOWN
|
|
111
|
+
)
|
|
112
|
+
# first remove existing events
|
|
113
|
+
tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
114
|
+
if callback is not None:
|
|
115
|
+
tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
|
|
116
|
+
|
|
117
|
+
def enable_coolstep(
|
|
118
|
+
self,
|
|
119
|
+
semin_sg: int = 150,
|
|
120
|
+
semax_sg: int = 200,
|
|
121
|
+
seup: int = 1,
|
|
122
|
+
sedn: int = 3,
|
|
123
|
+
min_speed: int = 100,
|
|
124
|
+
):
|
|
125
|
+
"""enables coolstep and sets the parameters for coolstep
|
|
126
|
+
The values for semin etc. can be tested with the test_stallguard_threshold function
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
semin_sg (int): lower threshold. Current will be increased if SG_Result goes below this
|
|
130
|
+
semax_sg (int): upper threshold. Current will be decreased if SG_Result goes above this
|
|
131
|
+
seup (int): current increment step
|
|
132
|
+
sedn (int): number of SG_Result readings for each current decrement
|
|
133
|
+
"""
|
|
134
|
+
semax_sg = semax_sg - semin_sg
|
|
135
|
+
|
|
136
|
+
self.coolconf.read()
|
|
137
|
+
self.coolconf.semin = round(max(0, min(semin_sg / 32, 15)))
|
|
138
|
+
self.coolconf.semax = round(max(0, min(semax_sg / 32, 15)))
|
|
139
|
+
self.coolconf.seimin = True # scale down to until 1/4 of IRun (7 - 31)
|
|
140
|
+
self.coolconf.seup = int(seup)
|
|
141
|
+
self.coolconf.sedn = int(sedn)
|
|
142
|
+
self.coolconf.write_check()
|
|
143
|
+
|
|
144
|
+
self._set_coolstep_threshold(
|
|
145
|
+
tmc_math.steps_to_tstep(int(min_speed), self.get_microstepping_resolution())
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def get_stallguard_result(self):
|
|
149
|
+
"""return the current stallguard result
|
|
150
|
+
its will be calculated with every fullstep
|
|
151
|
+
higher values means a lower motor load
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
sgresult (int): StallGuard Result
|
|
155
|
+
"""
|
|
156
|
+
self.sgresult.read()
|
|
157
|
+
return self.sgresult.sgresult
|
|
158
|
+
|
|
159
|
+
def _set_stallguard_threshold(self, threshold):
|
|
160
|
+
"""sets the register bit "SGTHRS" to to a given value
|
|
161
|
+
this is needed for the stallguard interrupt callback
|
|
162
|
+
SGRESULT becomes compared to the double of this threshold.
|
|
163
|
+
SGRESULT ≤ SGTHRS*2
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
threshold (int): value for SGTHRS
|
|
167
|
+
"""
|
|
168
|
+
self.sgthrs.modify("sgthrs", int(threshold))
|
|
169
|
+
|
|
170
|
+
def _set_coolstep_threshold(self, threshold):
|
|
171
|
+
"""This is the lower threshold velocity for switching
|
|
172
|
+
on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
threshold (int): threshold velocity for coolstep
|
|
176
|
+
"""
|
|
177
|
+
self.tcoolthrs.modify("tcoolthrs", int(threshold))
|
|
178
|
+
|
|
179
|
+
def _reset_current_pos(self):
|
|
180
|
+
"""resets the current position of the motor to 0"""
|
|
181
|
+
if self.tmc_mc is None:
|
|
182
|
+
raise TmcMotionControlException("tmc_mc is None; cannot reset current pos")
|
|
183
|
+
self.tmc_mc.current_pos = 0
|
|
184
|
+
|
|
185
|
+
def do_homing(
|
|
186
|
+
self,
|
|
187
|
+
diag_pin: int,
|
|
188
|
+
revolutions=10,
|
|
189
|
+
threshold=100,
|
|
190
|
+
max_speed: int | None = None,
|
|
191
|
+
cb_success: types.FunctionType | object | None = _CB_SENTINEL,
|
|
192
|
+
cb_failure: types.FunctionType | object | None = None,
|
|
193
|
+
) -> bool:
|
|
194
|
+
"""homes the motor in the given direction using stallguard.
|
|
195
|
+
this method is using vactual to move the motor and an interrupt on the DIAG pin
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
diag_pin (int): DIAG pin number
|
|
199
|
+
revolutions (int): max number of revolutions. Can be negative for inverse direction
|
|
200
|
+
(Default value = 10)
|
|
201
|
+
threshold (int): StallGuard detection threshold (Default value = 100)
|
|
202
|
+
max_speed (int): max speed for homing in steps/s (Default value = None)
|
|
203
|
+
cb_success (func|None): callback function on successful homing
|
|
204
|
+
cb_failure (func|None): callback function on failed homing
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
not homing_failed (bool): true when homing was successful
|
|
208
|
+
"""
|
|
209
|
+
if self.tmc_com is None:
|
|
210
|
+
raise TmcComException("tmc_com is None; cannot do homing")
|
|
211
|
+
if self.tmc_mc is None:
|
|
212
|
+
raise TmcMotionControlException("tmc_mc is None; cannot do homing")
|
|
213
|
+
|
|
214
|
+
self.tmc_logger.log(f"Stallguard threshold: {threshold}", Loglevel.DEBUG)
|
|
215
|
+
|
|
216
|
+
if max_speed is None:
|
|
217
|
+
max_speed = self.tmc_mc.max_speed_homing
|
|
218
|
+
if cb_success is _CB_SENTINEL:
|
|
219
|
+
cb_success = self._reset_current_pos
|
|
220
|
+
|
|
221
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
222
|
+
self.tmc_logger.log("homing", Loglevel.INFO)
|
|
223
|
+
|
|
224
|
+
self.set_spreadcycle(0)
|
|
225
|
+
|
|
226
|
+
self.set_stallguard_callback(
|
|
227
|
+
diag_pin,
|
|
228
|
+
threshold,
|
|
229
|
+
lambda _: self.tmc_mc.stop(),
|
|
230
|
+
round(0.5 * max_speed),
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
stop_mode = self.tmc_mc.run_to_position_steps(
|
|
234
|
+
revolutions * self.tmc_mc.steps_per_rev, MovementAbsRel.RELATIVE
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
homing_succeeded = stop_mode is StopMode.HARDSTOP
|
|
238
|
+
|
|
239
|
+
if homing_succeeded:
|
|
240
|
+
self.tmc_logger.log("homing successful", Loglevel.INFO)
|
|
241
|
+
if cb_success is not None:
|
|
242
|
+
cb_success()
|
|
243
|
+
else:
|
|
244
|
+
self.tmc_logger.log("homing failed", Loglevel.INFO)
|
|
245
|
+
if cb_failure is not None:
|
|
246
|
+
cb_failure()
|
|
247
|
+
|
|
248
|
+
return homing_succeeded
|
|
@@ -68,12 +68,10 @@ class TmcStepperDriver:
|
|
|
68
68
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
|
|
69
69
|
"""
|
|
70
70
|
self.BOARD: Board = tmc_gpio.BOARD
|
|
71
|
-
self.
|
|
72
|
-
self.
|
|
71
|
+
self.tmc_ec = tmc_ec
|
|
72
|
+
self.tmc_mc = tmc_mc
|
|
73
73
|
self.tmc_logger: TmcLogger
|
|
74
74
|
|
|
75
|
-
self._deinit_finished: bool = False
|
|
76
|
-
|
|
77
75
|
if logprefix is None:
|
|
78
76
|
logprefix = "StepperDriver"
|
|
79
77
|
self.tmc_logger = TmcLogger(loglevel, logprefix, log_handlers, log_formatter)
|
|
@@ -82,38 +80,29 @@ class TmcStepperDriver:
|
|
|
82
80
|
|
|
83
81
|
tmc_gpio.tmc_gpio.init(gpio_mode)
|
|
84
82
|
|
|
85
|
-
if tmc_mc is not None:
|
|
86
|
-
self.tmc_mc = tmc_mc
|
|
83
|
+
if self.tmc_mc is not None:
|
|
87
84
|
self.tmc_mc.init(self.tmc_logger)
|
|
88
85
|
|
|
89
|
-
if tmc_ec is not None:
|
|
90
|
-
self.tmc_ec = tmc_ec
|
|
86
|
+
if self.tmc_ec is not None:
|
|
91
87
|
self.tmc_ec.init(self.tmc_logger)
|
|
92
88
|
|
|
93
|
-
self.tmc_logger.log("GPIO Init finished", Loglevel.INFO)
|
|
94
|
-
|
|
95
|
-
self.tmc_logger.log("Init finished", Loglevel.INFO)
|
|
96
|
-
|
|
97
89
|
def __del__(self):
|
|
98
90
|
self.deinit()
|
|
99
91
|
|
|
100
92
|
def deinit(self):
|
|
101
93
|
"""destructor"""
|
|
102
|
-
if self.
|
|
103
|
-
self.tmc_logger.log("Deinit", Loglevel.INFO)
|
|
104
|
-
|
|
105
|
-
self.set_motor_enabled(False)
|
|
106
|
-
|
|
107
|
-
self.tmc_logger.log("Deinit finished", Loglevel.INFO)
|
|
108
|
-
self._deinit_finished = True
|
|
109
|
-
else:
|
|
110
|
-
self.tmc_logger.log("Deinit already finished", Loglevel.INFO)
|
|
111
|
-
if self.tmc_ec is not None:
|
|
94
|
+
if getattr(self, "tmc_ec", None) is not None and self.tmc_ec is not None:
|
|
112
95
|
self.tmc_ec.deinit()
|
|
113
|
-
|
|
96
|
+
self.tmc_ec = None
|
|
97
|
+
if getattr(self, "tmc_mc", None) is not None and self.tmc_mc is not None:
|
|
114
98
|
self.tmc_mc.deinit()
|
|
115
|
-
|
|
99
|
+
self.tmc_mc = None
|
|
100
|
+
if (
|
|
101
|
+
getattr(self, "tmc_logger", None) is not None
|
|
102
|
+
and self.tmc_logger is not None
|
|
103
|
+
):
|
|
116
104
|
self.tmc_logger.deinit()
|
|
105
|
+
self.tmc_logger = None
|
|
117
106
|
|
|
118
107
|
# TmcEnableControl Wrapper
|
|
119
108
|
# ----------------------------
|
|
@@ -45,14 +45,20 @@ class TmcCom:
|
|
|
45
45
|
"""set the tmc_logger"""
|
|
46
46
|
self._tmc_logger = tmc_logger
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
@property
|
|
49
|
+
def driver_address(self) -> int:
|
|
50
|
+
"""get the driver address"""
|
|
51
|
+
return self._driver_address
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"""
|
|
53
|
+
@driver_address.setter
|
|
54
|
+
def driver_address(self, address: int):
|
|
55
|
+
"""set the driver address"""
|
|
56
|
+
self._driver_address = address
|
|
57
|
+
|
|
58
|
+
def __init__(self):
|
|
59
|
+
"""constructor"""
|
|
54
60
|
self._tmc_logger: TmcLogger
|
|
55
|
-
self.
|
|
61
|
+
self._driver_address: int = 0
|
|
56
62
|
self.communication_pause: int = 0
|
|
57
63
|
self._get_register_callback = None
|
|
58
64
|
|
|
@@ -135,7 +141,7 @@ class TmcCom:
|
|
|
135
141
|
"""
|
|
136
142
|
|
|
137
143
|
@abstractmethod
|
|
138
|
-
def
|
|
144
|
+
def flush_com_buffer(self):
|
|
139
145
|
"""this function clear the communication buffers of the Raspberry Pi"""
|
|
140
146
|
|
|
141
147
|
@abstractmethod
|
|
@@ -25,7 +25,6 @@ class TmcComSpi(TmcComSpiBase):
|
|
|
25
25
|
spi_bus: int,
|
|
26
26
|
spi_dev: int,
|
|
27
27
|
spi_speed: int = 8000000,
|
|
28
|
-
driver_address: int = 0,
|
|
29
28
|
):
|
|
30
29
|
"""constructor
|
|
31
30
|
|
|
@@ -33,9 +32,8 @@ class TmcComSpi(TmcComSpiBase):
|
|
|
33
32
|
spi_bus (int): SPI bus number
|
|
34
33
|
spi_dev (int): SPI device number
|
|
35
34
|
spi_speed (int, optional): SPI speed in Hz. Defaults to 8000000.
|
|
36
|
-
driver_address (int, optional): driver address. Defaults to 0.
|
|
37
35
|
"""
|
|
38
|
-
super().__init__(
|
|
36
|
+
super().__init__()
|
|
39
37
|
|
|
40
38
|
self.spi = spidev.SpiDev()
|
|
41
39
|
|
|
@@ -11,6 +11,14 @@ from ._tmc_com import *
|
|
|
11
11
|
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
class IoinStub(TmcReg):
|
|
15
|
+
"""IOIN Register Stub"""
|
|
16
|
+
|
|
17
|
+
# pylint: disable=too-few-public-methods
|
|
18
|
+
|
|
19
|
+
version: int
|
|
20
|
+
|
|
21
|
+
|
|
14
22
|
class TmcComSpiBase(TmcCom):
|
|
15
23
|
"""TmcComSpiBase
|
|
16
24
|
|
|
@@ -19,13 +27,9 @@ class TmcComSpiBase(TmcCom):
|
|
|
19
27
|
Subclasses must implement the actual SPI transfer methods.
|
|
20
28
|
"""
|
|
21
29
|
|
|
22
|
-
def __init__(self
|
|
23
|
-
"""constructor
|
|
24
|
-
|
|
25
|
-
Args:
|
|
26
|
-
driver_address (int, optional): driver address. Defaults to 0.
|
|
27
|
-
"""
|
|
28
|
-
super().__init__(driver_address)
|
|
30
|
+
def __init__(self):
|
|
31
|
+
"""constructor"""
|
|
32
|
+
super().__init__()
|
|
29
33
|
|
|
30
34
|
self.spi = None # To be set by subclass
|
|
31
35
|
|
|
@@ -138,7 +142,7 @@ class TmcComSpiBase(TmcCom):
|
|
|
138
142
|
self.write_reg(addr, val)
|
|
139
143
|
return True
|
|
140
144
|
|
|
141
|
-
def
|
|
145
|
+
def flush_com_buffer(self):
|
|
142
146
|
"""this function clear the communication buffers of the Raspberry Pi"""
|
|
143
147
|
|
|
144
148
|
def test_com(self):
|
|
@@ -150,7 +154,7 @@ class TmcComSpiBase(TmcCom):
|
|
|
150
154
|
Raises:
|
|
151
155
|
TmcComException: if TMC register IOIN not available
|
|
152
156
|
"""
|
|
153
|
-
ioin = self.get_register("ioin")
|
|
157
|
+
ioin: IoinStub = self.get_register("ioin") # type: ignore
|
|
154
158
|
if ioin is None:
|
|
155
159
|
raise TmcComException("TMC register IOIN not available")
|
|
156
160
|
data, flags = ioin.read()
|
|
@@ -17,14 +17,13 @@ class TmcComSpiFtdi(TmcComSpiBase):
|
|
|
17
17
|
like the current or the microsteppingmode
|
|
18
18
|
"""
|
|
19
19
|
|
|
20
|
-
def __init__(self, spi_port: SpiPort
|
|
20
|
+
def __init__(self, spi_port: SpiPort):
|
|
21
21
|
"""constructor
|
|
22
22
|
|
|
23
23
|
Args:
|
|
24
24
|
spi_port (SpiPort): pyftdi SpiPort object
|
|
25
|
-
driver_address (int, optional): driver address. Defaults to 0.
|
|
26
25
|
"""
|
|
27
|
-
super().__init__(
|
|
26
|
+
super().__init__()
|
|
28
27
|
|
|
29
28
|
self.spi = spi_port
|
|
30
29
|
|
|
@@ -31,7 +31,6 @@ class TmcComSpiMicroPython(TmcComSpiBase):
|
|
|
31
31
|
mosi_pin=19,
|
|
32
32
|
miso_pin=16,
|
|
33
33
|
baudrate=1000000,
|
|
34
|
-
driver_address=0,
|
|
35
34
|
):
|
|
36
35
|
"""Initialize SPI communication
|
|
37
36
|
|
|
@@ -42,9 +41,8 @@ class TmcComSpiMicroPython(TmcComSpiBase):
|
|
|
42
41
|
mosi_pin: MOSI GPIO pin
|
|
43
42
|
miso_pin: MISO GPIO pin
|
|
44
43
|
baudrate: SPI clock frequency (default 1MHz)
|
|
45
|
-
driver_address: driver address (default 0)
|
|
46
44
|
"""
|
|
47
|
-
super().__init__(
|
|
45
|
+
super().__init__()
|
|
48
46
|
|
|
49
47
|
self._spi_id = spi_id
|
|
50
48
|
self._cs_pin = cs_pin
|
|
@@ -17,17 +17,14 @@ class TmcComUart(TmcComUartBase):
|
|
|
17
17
|
like the current or the microsteppingmode
|
|
18
18
|
"""
|
|
19
19
|
|
|
20
|
-
def __init__(
|
|
21
|
-
self, serialport: str, baudrate: int = 115200, driver_address: int = 0
|
|
22
|
-
):
|
|
20
|
+
def __init__(self, serialport: str, baudrate: int = 11520):
|
|
23
21
|
"""constructor
|
|
24
22
|
|
|
25
23
|
Args:
|
|
26
24
|
serialport (string): serialport path
|
|
27
25
|
baudrate (int): baudrate
|
|
28
|
-
driver_address (int, optional): driver address. Defaults to 0.
|
|
29
26
|
"""
|
|
30
|
-
super().__init__(
|
|
27
|
+
super().__init__()
|
|
31
28
|
|
|
32
29
|
self.ser = serial.Serial()
|
|
33
30
|
|
|
@@ -61,18 +58,21 @@ class TmcComUart(TmcComUartBase):
|
|
|
61
58
|
)
|
|
62
59
|
raise SystemExit from e
|
|
63
60
|
|
|
61
|
+
if self.ser.baudrate is None:
|
|
62
|
+
raise TmcComException("Baudrate is not set")
|
|
63
|
+
|
|
64
64
|
# adjust per baud and hardware. Sequential reads without some delay fail.
|
|
65
|
-
self.communication_pause = 500
|
|
65
|
+
self.communication_pause = 500 // self.ser.baudrate
|
|
66
66
|
|
|
67
67
|
if self.ser is None:
|
|
68
68
|
return
|
|
69
69
|
|
|
70
|
-
self.ser.
|
|
71
|
-
self.ser.
|
|
72
|
-
self.ser.
|
|
70
|
+
self.ser.bytesize = serial.EIGHTBITS
|
|
71
|
+
self.ser.parity = serial.PARITY_NONE
|
|
72
|
+
self.ser.stopbits = serial.STOPBITS_ONE
|
|
73
73
|
|
|
74
74
|
# adjust per baud and hardware. Sequential reads without some delay fail.
|
|
75
|
-
self.ser.timeout = 20000
|
|
75
|
+
self.ser.timeout = 20000 // self.ser.baudrate
|
|
76
76
|
|
|
77
77
|
self._uart_flush()
|
|
78
78
|
|
|
@@ -11,6 +11,22 @@ from ._tmc_com import *
|
|
|
11
11
|
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
class IfcntStub(TmcReg):
|
|
15
|
+
"""IFCNT Register Stub"""
|
|
16
|
+
|
|
17
|
+
# pylint: disable=too-few-public-methods
|
|
18
|
+
|
|
19
|
+
ifcnt: int
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class IoinStub(TmcReg):
|
|
23
|
+
"""IOIN Register Stub"""
|
|
24
|
+
|
|
25
|
+
# pylint: disable=too-few-public-methods
|
|
26
|
+
|
|
27
|
+
version: int
|
|
28
|
+
|
|
29
|
+
|
|
14
30
|
class TmcComUartBase(TmcCom):
|
|
15
31
|
"""TmcComUartBase
|
|
16
32
|
|
|
@@ -19,13 +35,9 @@ class TmcComUartBase(TmcCom):
|
|
|
19
35
|
Subclasses must implement the actual UART transfer methods.
|
|
20
36
|
"""
|
|
21
37
|
|
|
22
|
-
def __init__(self
|
|
23
|
-
"""constructor
|
|
24
|
-
|
|
25
|
-
Args:
|
|
26
|
-
driver_address (int, optional): driver address. Defaults to 0.
|
|
27
|
-
"""
|
|
28
|
-
super().__init__(driver_address)
|
|
38
|
+
def __init__(self):
|
|
39
|
+
"""constructor"""
|
|
40
|
+
super().__init__()
|
|
29
41
|
|
|
30
42
|
self.ser = None # To be set by subclass
|
|
31
43
|
|
|
@@ -212,7 +224,7 @@ class TmcComUartBase(TmcCom):
|
|
|
212
224
|
Raises:
|
|
213
225
|
TmcComException: if IFCNT register is not set or write fails after retries
|
|
214
226
|
"""
|
|
215
|
-
ifcnt = self.get_register("ifcnt")
|
|
227
|
+
ifcnt: IfcntStub = self.get_register("ifcnt") # type: ignore
|
|
216
228
|
if ifcnt is None:
|
|
217
229
|
raise TmcComException("TMC register IFCNT not available")
|
|
218
230
|
|
|
@@ -234,7 +246,7 @@ class TmcComUartBase(TmcCom):
|
|
|
234
246
|
if tries <= 0:
|
|
235
247
|
raise TmcComException("after 10 tries writing not successful")
|
|
236
248
|
|
|
237
|
-
def
|
|
249
|
+
def flush_com_buffer(self):
|
|
238
250
|
"""this function clear the communication buffers"""
|
|
239
251
|
if self.ser is None:
|
|
240
252
|
return
|
|
@@ -256,12 +268,12 @@ class TmcComUartBase(TmcCom):
|
|
|
256
268
|
if not self.ser.is_open:
|
|
257
269
|
raise TmcComException("Cannot test com, serial port is closed")
|
|
258
270
|
|
|
259
|
-
ioin = self.get_register("ioin")
|
|
271
|
+
ioin: IoinStub = self.get_register("ioin") # type: ignore
|
|
260
272
|
if ioin is None:
|
|
261
273
|
raise TmcComException("TMC register IOIN not available")
|
|
262
274
|
|
|
263
275
|
self.r_frame[1] = self.driver_address
|
|
264
|
-
self.r_frame[2] = ioin.
|
|
276
|
+
self.r_frame[2] = ioin.ADDR
|
|
265
277
|
self.r_frame[3] = compute_crc8_atm(self.r_frame[:-1])
|
|
266
278
|
|
|
267
279
|
rtn = self._uart_write(self.r_frame)
|
|
@@ -28,9 +28,7 @@ class TmcComUartMicroPython(TmcComUartBase):
|
|
|
28
28
|
For single-wire UART, connect TX to the TMC UART pin through a 1k resistor.
|
|
29
29
|
"""
|
|
30
30
|
|
|
31
|
-
def __init__(
|
|
32
|
-
self, uart_id=0, tx_pin=0, rx_pin=1, baudrate=115200, driver_address=0
|
|
33
|
-
):
|
|
31
|
+
def __init__(self, uart_id=0, tx_pin=0, rx_pin=1, baudrate=115200):
|
|
34
32
|
"""Initialize UART communication
|
|
35
33
|
|
|
36
34
|
Args:
|
|
@@ -38,9 +36,8 @@ class TmcComUartMicroPython(TmcComUartBase):
|
|
|
38
36
|
tx_pin: TX GPIO pin
|
|
39
37
|
rx_pin: RX GPIO pin
|
|
40
38
|
baudrate: UART baudrate
|
|
41
|
-
driver_address: driver address (default 0)
|
|
42
39
|
"""
|
|
43
|
-
super().__init__(
|
|
40
|
+
super().__init__()
|
|
44
41
|
|
|
45
42
|
self._uart_id = uart_id
|
|
46
43
|
self._tx_pin = tx_pin
|
|
@@ -40,8 +40,13 @@ class TmcEnableControl:
|
|
|
40
40
|
return self._get_register_callback(name)
|
|
41
41
|
return None
|
|
42
42
|
|
|
43
|
+
def __del__(self):
|
|
44
|
+
"""destructor"""
|
|
45
|
+
self.deinit()
|
|
46
|
+
|
|
43
47
|
def deinit(self):
|
|
44
48
|
"""destructor"""
|
|
49
|
+
self.set_motor_enabled(False)
|
|
45
50
|
|
|
46
51
|
@abstractmethod
|
|
47
52
|
def set_motor_enabled(self, en):
|