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
tmc_driver/_tmc_xxxx.py
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
# pylint: disable=wildcard-import
|
|
2
|
+
# pylint: disable=unused-wildcard-import
|
|
3
|
+
# pylint: disable=unused-import
|
|
4
|
+
# pylint: disable=too-many-arguments
|
|
5
|
+
# pylint: disable=too-many-positional-arguments
|
|
6
|
+
# pylint: disable=too-many-public-methods
|
|
7
|
+
"""TmcXXXX driver module
|
|
8
|
+
|
|
9
|
+
this module has two different functions:
|
|
10
|
+
1. access register via tmc_com (UART, SPI)
|
|
11
|
+
2. Enable motor control via tmc_ec (TOFF, PIN)
|
|
12
|
+
3. move the motor via tmc_mc (STEP/DIR, STEP/REG, VACTUAL)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import time
|
|
16
|
+
from abc import abstractmethod
|
|
17
|
+
from .tmc_gpio import Gpio
|
|
18
|
+
from ._tmc_stepperdriver import *
|
|
19
|
+
from .tmc_logger import Loglevel
|
|
20
|
+
from .enable_control._tmc_ec import TmcEnableControl
|
|
21
|
+
from .motion_control._tmc_mc import TmcMotionControl
|
|
22
|
+
from .com._tmc_com import TmcCom
|
|
23
|
+
from .reg._tmc_reg import TmcReg
|
|
24
|
+
from .reg import _tmc_shared_regs as tmc_shared_regs
|
|
25
|
+
from ._tmc_validation import validate_submodule
|
|
26
|
+
from ._tmc_exceptions import (
|
|
27
|
+
TmcException,
|
|
28
|
+
TmcComException,
|
|
29
|
+
TmcMotionControlException,
|
|
30
|
+
TmcEnableControlException,
|
|
31
|
+
TmcDriverException,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TmcXXXX(TmcStepperDriver):
|
|
36
|
+
"""TmcXXXX"""
|
|
37
|
+
|
|
38
|
+
SUPPORTED_COM_TYPES = ()
|
|
39
|
+
SUPPORTED_EC_TYPES = ()
|
|
40
|
+
SUPPORTED_MC_TYPES = ()
|
|
41
|
+
DRIVER_FAMILY = "TMCXXXX"
|
|
42
|
+
|
|
43
|
+
gstat: tmc_shared_regs.GStat
|
|
44
|
+
ioin: tmc_shared_regs.Ioin
|
|
45
|
+
gconf: tmc_shared_regs.GConf
|
|
46
|
+
chopconf: tmc_shared_regs.ChopConf
|
|
47
|
+
mscnt: tmc_shared_regs.MsCnt
|
|
48
|
+
tpwmthrs: tmc_shared_regs.TPwmThrs
|
|
49
|
+
|
|
50
|
+
# Constructor/Destructor
|
|
51
|
+
# ----------------------------
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
tmc_ec: TmcEnableControl,
|
|
55
|
+
tmc_mc: TmcMotionControl,
|
|
56
|
+
tmc_com: TmcCom | None = None,
|
|
57
|
+
driver_address: int = 0,
|
|
58
|
+
gpio_mode=None,
|
|
59
|
+
loglevel: Loglevel = Loglevel.INFO,
|
|
60
|
+
logprefix: str | None = None,
|
|
61
|
+
log_handlers: list | None = None,
|
|
62
|
+
log_formatter: logging.Formatter | None = None,
|
|
63
|
+
):
|
|
64
|
+
"""constructor
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
tmc_ec (TmcEnableControl): enable control object
|
|
68
|
+
tmc_mc (TmcMotionControl): motion control object
|
|
69
|
+
tmc_com (TmcCom, optional): communication object. Defaults to None.
|
|
70
|
+
driver_address (int, optional): driver address [0-3]. Defaults to 0.
|
|
71
|
+
gpio_mode (enum, optional): gpio mode. Defaults to None.
|
|
72
|
+
loglevel (enum, optional): loglevel. Defaults to None.
|
|
73
|
+
logprefix (str, optional): log prefix (name of the logger).
|
|
74
|
+
Defaults to None (standard TMC prefix).
|
|
75
|
+
log_handlers (list, optional): list of logging handlers.
|
|
76
|
+
Defaults to None (log to console).
|
|
77
|
+
log_formatter (logging.Formatter, optional): formatter for the log messages.
|
|
78
|
+
Defaults to None (messages are logged in the format
|
|
79
|
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
|
|
80
|
+
"""
|
|
81
|
+
self.tmc_com = tmc_com
|
|
82
|
+
|
|
83
|
+
if logprefix is None:
|
|
84
|
+
logprefix = f"{self.DRIVER_FAMILY} {driver_address}"
|
|
85
|
+
|
|
86
|
+
super().__init__(
|
|
87
|
+
tmc_ec, tmc_mc, gpio_mode, loglevel, logprefix, log_handlers, log_formatter
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
validate_submodule(
|
|
91
|
+
tmc_com, self.SUPPORTED_COM_TYPES, self.__class__.__name__, "tmc_com"
|
|
92
|
+
)
|
|
93
|
+
validate_submodule(
|
|
94
|
+
tmc_ec, self.SUPPORTED_EC_TYPES, self.__class__.__name__, "tmc_ec"
|
|
95
|
+
)
|
|
96
|
+
validate_submodule(
|
|
97
|
+
tmc_mc, self.SUPPORTED_MC_TYPES, self.__class__.__name__, "tmc_mc"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if self.tmc_com is not None:
|
|
101
|
+
self.tmc_com.tmc_logger = self.tmc_logger
|
|
102
|
+
self.tmc_com.driver_address = driver_address
|
|
103
|
+
self.tmc_com.init()
|
|
104
|
+
|
|
105
|
+
# Register callback for submodules to access registers
|
|
106
|
+
self.tmc_com.set_get_register_callback(self._get_register)
|
|
107
|
+
if self.tmc_mc is not None:
|
|
108
|
+
self.tmc_mc.set_get_register_callback(self._get_register)
|
|
109
|
+
if self.tmc_ec is not None:
|
|
110
|
+
self.tmc_ec.set_get_register_callback(self._get_register)
|
|
111
|
+
|
|
112
|
+
if hasattr(self, "tmc_mc") and self.tmc_mc is not None:
|
|
113
|
+
self.max_speed_fullstep = 100
|
|
114
|
+
self.acceleration_fullstep = 100
|
|
115
|
+
|
|
116
|
+
def _init(self):
|
|
117
|
+
"""initialization after registers are created"""
|
|
118
|
+
if self.tmc_com is not None:
|
|
119
|
+
self.clear_gstat_verify()
|
|
120
|
+
if self.tmc_mc is not None:
|
|
121
|
+
self.read_steps_per_rev()
|
|
122
|
+
self.tmc_com.flush_com_buffer()
|
|
123
|
+
|
|
124
|
+
def __del__(self):
|
|
125
|
+
self.deinit()
|
|
126
|
+
|
|
127
|
+
def deinit(self):
|
|
128
|
+
"""destructor"""
|
|
129
|
+
super().deinit()
|
|
130
|
+
if self.tmc_com is not None:
|
|
131
|
+
self.tmc_com.deinit()
|
|
132
|
+
self.tmc_com = None
|
|
133
|
+
|
|
134
|
+
# Register Access
|
|
135
|
+
# ----------------------------
|
|
136
|
+
def clear_gstat_verify(self):
|
|
137
|
+
"""clears the GSTAT register and verifies that it was cleared"""
|
|
138
|
+
tries = 5
|
|
139
|
+
while True:
|
|
140
|
+
try:
|
|
141
|
+
self.gstat.clear_verify()
|
|
142
|
+
break
|
|
143
|
+
except TmcDriverException:
|
|
144
|
+
time.sleep(0.1)
|
|
145
|
+
tries -= 1
|
|
146
|
+
if tries <= 0:
|
|
147
|
+
raise TmcDriverException("Could not clear GSTAT register")
|
|
148
|
+
|
|
149
|
+
def read_steps_per_rev(self) -> int:
|
|
150
|
+
"""returns how many steps are needed for one revolution.
|
|
151
|
+
this reads the value from the tmc driver.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
int: Steps per revolution
|
|
155
|
+
"""
|
|
156
|
+
if self.tmc_mc is None:
|
|
157
|
+
raise TmcDriverException("tmc_mc is None; cannot read steps per revolution")
|
|
158
|
+
self.read_microstepping_resolution()
|
|
159
|
+
return self.tmc_mc.steps_per_rev
|
|
160
|
+
|
|
161
|
+
def _get_register(self, name: str) -> TmcReg:
|
|
162
|
+
"""Get register by name - callback for submodules
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
name: Register name (e.g. 'gconf', 'chopconf')
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
Register object or None if not found
|
|
169
|
+
"""
|
|
170
|
+
name = name.lower()
|
|
171
|
+
reg = getattr(self, name, None)
|
|
172
|
+
if reg is None:
|
|
173
|
+
raise TmcDriverException(
|
|
174
|
+
f"Register {name} not found in driver {self.DRIVER_FAMILY}"
|
|
175
|
+
)
|
|
176
|
+
return reg
|
|
177
|
+
|
|
178
|
+
@abstractmethod
|
|
179
|
+
def get_spreadcycle(self) -> bool:
|
|
180
|
+
"""reads spreadcycle
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
bool: True = spreadcycle; False = stealthchop
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
@abstractmethod
|
|
187
|
+
def set_spreadcycle(self, en: bool):
|
|
188
|
+
"""enables spreadcycle (1) or stealthchop (0)
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
en (bool): true to enable spreadcycle; false to enable stealthchop
|
|
192
|
+
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
def get_direction_reg(self) -> bool:
|
|
196
|
+
"""returns the motor shaft direction: False = CCW; True = CW
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
bool: motor shaft direction: False = CCW; True = CW
|
|
200
|
+
"""
|
|
201
|
+
self.gconf.read()
|
|
202
|
+
return self.gconf.shaft
|
|
203
|
+
|
|
204
|
+
def set_direction_reg(self, direction: bool):
|
|
205
|
+
"""sets the motor shaft direction to the given value: False = CCW; True = CW
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
direction (bool): direction of the motor False = CCW; True = CW
|
|
209
|
+
"""
|
|
210
|
+
self.gconf.modify("shaft", direction)
|
|
211
|
+
|
|
212
|
+
def get_interpolation(self) -> bool:
|
|
213
|
+
"""return whether the tmc inbuilt interpolation is active
|
|
214
|
+
|
|
215
|
+
Returns:
|
|
216
|
+
en (bool): true if internal µstep interpolation is enabled
|
|
217
|
+
"""
|
|
218
|
+
self.chopconf.read()
|
|
219
|
+
return self.chopconf.intpol
|
|
220
|
+
|
|
221
|
+
def set_interpolation(self, en: bool):
|
|
222
|
+
"""enables the tmc inbuilt interpolation of the steps to 256 µsteps
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
en (bool): true to enable internal µstep interpolation
|
|
226
|
+
"""
|
|
227
|
+
self.chopconf.modify("intpol", en)
|
|
228
|
+
|
|
229
|
+
def get_toff(self) -> int:
|
|
230
|
+
"""returns the TOFF register value
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
int: TOFF register value
|
|
234
|
+
"""
|
|
235
|
+
self.chopconf.read()
|
|
236
|
+
return self.chopconf.toff
|
|
237
|
+
|
|
238
|
+
def set_toff(self, toff: int):
|
|
239
|
+
"""Sets TOFF register to value
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
toff (uint8_t): value of toff (must be a four-bit value)
|
|
243
|
+
"""
|
|
244
|
+
self.chopconf.modify("toff", toff)
|
|
245
|
+
|
|
246
|
+
def read_microstepping_resolution(self) -> int:
|
|
247
|
+
"""returns the current native microstep resolution (1-256)
|
|
248
|
+
this reads the value from the driver register
|
|
249
|
+
|
|
250
|
+
Returns:
|
|
251
|
+
int: µstep resolution
|
|
252
|
+
"""
|
|
253
|
+
self.chopconf.read()
|
|
254
|
+
|
|
255
|
+
mres = self.chopconf.mres_ms
|
|
256
|
+
if self.tmc_mc is not None:
|
|
257
|
+
self.tmc_mc.mres = mres
|
|
258
|
+
|
|
259
|
+
return mres
|
|
260
|
+
|
|
261
|
+
def get_microstepping_resolution(self) -> int:
|
|
262
|
+
"""returns the current native microstep resolution (1-256)
|
|
263
|
+
this returns the cached value from the tmc_mc module
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
int: µstep resolution
|
|
267
|
+
"""
|
|
268
|
+
if self.tmc_mc is None:
|
|
269
|
+
raise TmcDriverException(
|
|
270
|
+
"tmc_mc is None; cannot get microstepping resolution"
|
|
271
|
+
)
|
|
272
|
+
return self.tmc_mc.mres
|
|
273
|
+
|
|
274
|
+
def set_microstepping_resolution(self, mres: int):
|
|
275
|
+
"""sets the current native microstep resolution (1,2,4,8,16,32,64,128,256)
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
mres (int): µstep resolution; has to be a power of 2 or 1 for fullstep
|
|
279
|
+
"""
|
|
280
|
+
if self.tmc_mc is not None:
|
|
281
|
+
self.tmc_mc.mres = mres
|
|
282
|
+
|
|
283
|
+
self.chopconf.read()
|
|
284
|
+
self.chopconf.mres_ms = mres
|
|
285
|
+
self.chopconf.write_check()
|
|
286
|
+
|
|
287
|
+
@abstractmethod
|
|
288
|
+
def set_current_peak(
|
|
289
|
+
self,
|
|
290
|
+
run_current: int,
|
|
291
|
+
hold_current_multiplier: float = 0.5,
|
|
292
|
+
hold_current_delay: int = 10,
|
|
293
|
+
) -> int:
|
|
294
|
+
"""sets the Peak current for the motor.
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
run_current (int): current during movement in mA
|
|
298
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
299
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
300
|
+
Returns:
|
|
301
|
+
int: theoretical final current in mA
|
|
302
|
+
"""
|
|
303
|
+
|
|
304
|
+
def set_current_rms(
|
|
305
|
+
self,
|
|
306
|
+
run_current: int,
|
|
307
|
+
hold_current_multiplier: float = 0.5,
|
|
308
|
+
hold_current_delay: int = 10,
|
|
309
|
+
) -> int:
|
|
310
|
+
"""sets the RMS current for the motor.
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
run_current (int): current during movement in mA
|
|
314
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
315
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
316
|
+
|
|
317
|
+
Returns:
|
|
318
|
+
int: theoretical final current in mA
|
|
319
|
+
"""
|
|
320
|
+
peak_current = self.set_current_peak(
|
|
321
|
+
round(run_current * 1.41421), hold_current_multiplier, hold_current_delay
|
|
322
|
+
)
|
|
323
|
+
return round(peak_current / 1.41421)
|
|
324
|
+
|
|
325
|
+
def set_current(
|
|
326
|
+
self,
|
|
327
|
+
run_current: int,
|
|
328
|
+
hold_current_multiplier: float = 0.5,
|
|
329
|
+
hold_current_delay: int = 10,
|
|
330
|
+
) -> int:
|
|
331
|
+
"""sets the Peak current for the motor.
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
run_current (int): current during movement in mA
|
|
335
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
336
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
int: theoretical final current in mA
|
|
340
|
+
"""
|
|
341
|
+
return self.set_current_rms(
|
|
342
|
+
run_current, hold_current_multiplier, hold_current_delay
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
def get_microstep_counter(self) -> int:
|
|
346
|
+
"""returns the current Microstep counter.
|
|
347
|
+
Indicates actual position in the microstep table for CUR_A
|
|
348
|
+
|
|
349
|
+
Returns:
|
|
350
|
+
int: current Microstep counter
|
|
351
|
+
"""
|
|
352
|
+
self.mscnt.read()
|
|
353
|
+
return self.mscnt.mscnt
|
|
354
|
+
|
|
355
|
+
def get_microstep_counter_in_steps(self, offset: int = 0) -> int:
|
|
356
|
+
"""returns the current Microstep counter.
|
|
357
|
+
Indicates actual position in the microstep table for CUR_A
|
|
358
|
+
|
|
359
|
+
Args:
|
|
360
|
+
offset (int): offset in steps (Default value = 0)
|
|
361
|
+
|
|
362
|
+
Returns:
|
|
363
|
+
step (int): current Microstep counter convertet to steps
|
|
364
|
+
"""
|
|
365
|
+
if self.tmc_mc is None:
|
|
366
|
+
raise TmcDriverException(
|
|
367
|
+
"tmc_mc is None; cannot get microstep counter in steps"
|
|
368
|
+
)
|
|
369
|
+
step = (self.get_microstep_counter() - 64) * (self.tmc_mc.mres * 4) / 1024
|
|
370
|
+
step = (4 * self.tmc_mc.mres) - step - 1
|
|
371
|
+
step = round(step)
|
|
372
|
+
return step + offset
|
|
373
|
+
|
|
374
|
+
def read_register(
|
|
375
|
+
self, name: str, log: bool = True
|
|
376
|
+
) -> tuple[TmcReg, int, dict | None]:
|
|
377
|
+
"""reads all relevant registers of the driver"""
|
|
378
|
+
if self.tmc_com is None:
|
|
379
|
+
raise TmcComException("tmc_com is None; cannot read registers")
|
|
380
|
+
|
|
381
|
+
reg = self._get_register(name)
|
|
382
|
+
if reg is None:
|
|
383
|
+
raise TmcDriverException(f"Register {name} not found in driver")
|
|
384
|
+
data, flags = reg.read()
|
|
385
|
+
|
|
386
|
+
if log:
|
|
387
|
+
reg.log(self.tmc_logger)
|
|
388
|
+
|
|
389
|
+
return reg, data, flags
|
|
390
|
+
|
|
391
|
+
def set_hybrid_threshold_speed(self, speed: int):
|
|
392
|
+
"""sets the hybrid threshold speed
|
|
393
|
+
|
|
394
|
+
Args:
|
|
395
|
+
speed (int): speed in steps per second
|
|
396
|
+
"""
|
|
397
|
+
tstep = tmc_math.steps_to_tstep(speed, self.get_microstepping_resolution())
|
|
398
|
+
self.tpwmthrs.modify("tpwmthrs", tstep)
|
|
399
|
+
|
|
400
|
+
# Test Methods
|
|
401
|
+
# ----------------------------
|
|
402
|
+
def test_pin(self, pin, ioin_reg_field_name: str) -> bool:
|
|
403
|
+
"""tests one pin
|
|
404
|
+
|
|
405
|
+
this function checks the connection to a pin
|
|
406
|
+
by toggling it and reading the IOIN register
|
|
407
|
+
|
|
408
|
+
Args:
|
|
409
|
+
pin: pin to be tested
|
|
410
|
+
ioin_reg_field_name (str): name of the IOIN register field
|
|
411
|
+
that corresponds to the pin
|
|
412
|
+
|
|
413
|
+
Returns:
|
|
414
|
+
bool: True = pin OK; False = pin not OK
|
|
415
|
+
"""
|
|
416
|
+
if self.tmc_mc is None or self.tmc_ec is None:
|
|
417
|
+
raise TmcDriverException("tmc_mc or tmc_ec is None; cannot test pins")
|
|
418
|
+
if not isinstance(self.tmc_mc, TmcMotionControlStepDir) or not isinstance(
|
|
419
|
+
self.tmc_ec, TmcEnableControlPin
|
|
420
|
+
):
|
|
421
|
+
raise TmcDriverException(
|
|
422
|
+
"tmc_mc or tmc_ec is not of correct type; cannot test pins"
|
|
423
|
+
)
|
|
424
|
+
if self.tmc_ec.pin_en is None:
|
|
425
|
+
raise TmcDriverException("tmc_ec pin_en is None; cannot test pins")
|
|
426
|
+
if self.tmc_mc.pin_dir is None:
|
|
427
|
+
raise TmcDriverException("tmc_mc pin_dir is None; cannot test pins")
|
|
428
|
+
if self.tmc_mc.pin_step is None:
|
|
429
|
+
raise TmcDriverException("tmc_mc pin_step is None; cannot test pins")
|
|
430
|
+
|
|
431
|
+
pin_ok = True
|
|
432
|
+
|
|
433
|
+
# turn on all pins
|
|
434
|
+
tmc_gpio.tmc_gpio.gpio_output(self.tmc_mc.pin_dir, Gpio.HIGH)
|
|
435
|
+
tmc_gpio.tmc_gpio.gpio_output(self.tmc_mc.pin_step, Gpio.HIGH)
|
|
436
|
+
tmc_gpio.tmc_gpio.gpio_output(self.tmc_ec.pin_en, Gpio.HIGH)
|
|
437
|
+
|
|
438
|
+
# check that the selected pin is on
|
|
439
|
+
if not self.ioin.get(ioin_reg_field_name):
|
|
440
|
+
pin_ok = False
|
|
441
|
+
|
|
442
|
+
# turn off only the selected pin
|
|
443
|
+
tmc_gpio.tmc_gpio.gpio_output(pin, Gpio.LOW)
|
|
444
|
+
time.sleep(0.1)
|
|
445
|
+
|
|
446
|
+
# check that the selected pin is off
|
|
447
|
+
if self.ioin.get(ioin_reg_field_name):
|
|
448
|
+
pin_ok = False
|
|
449
|
+
pin_ok = False
|
|
450
|
+
|
|
451
|
+
return pin_ok
|
|
452
|
+
|
|
453
|
+
def test_dir_step_en(self):
|
|
454
|
+
"""tests the EN, DIR and STEP pin
|
|
455
|
+
|
|
456
|
+
this sets the EN, DIR and STEP pin to HIGH, LOW and HIGH
|
|
457
|
+
and checks the IOIN Register of the TMC meanwhile
|
|
458
|
+
"""
|
|
459
|
+
if self.tmc_mc is None or self.tmc_ec is None:
|
|
460
|
+
raise TmcDriverException("tmc_mc or tmc_ec is None; cannot test pins")
|
|
461
|
+
if not isinstance(self.tmc_mc, TmcMotionControlStepDir) or not isinstance(
|
|
462
|
+
self.tmc_ec, TmcEnableControlPin
|
|
463
|
+
):
|
|
464
|
+
raise TmcDriverException(
|
|
465
|
+
"tmc_mc or tmc_ec is not of correct type; cannot test pins"
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
# test each pin on their own
|
|
469
|
+
pin_dir_ok = self.test_pin(self.tmc_mc.pin_dir, "dir")
|
|
470
|
+
pin_step_ok = self.test_pin(self.tmc_mc.pin_step, "step")
|
|
471
|
+
pin_en_ok = self.test_pin(self.tmc_ec.pin_en, "enn")
|
|
472
|
+
|
|
473
|
+
self.set_motor_enabled(False)
|
|
474
|
+
|
|
475
|
+
self.tmc_logger.log("---")
|
|
476
|
+
self.tmc_logger.log(f"Pin DIR: \t{'OK' if pin_dir_ok else 'not OK'}")
|
|
477
|
+
self.tmc_logger.log(f"Pin STEP: \t{'OK' if pin_step_ok else 'not OK'}")
|
|
478
|
+
self.tmc_logger.log(f"Pin EN: \t{'OK' if pin_en_ok else 'not OK'}")
|
|
479
|
+
self.tmc_logger.log("---")
|
|
480
|
+
|
|
481
|
+
def test_com(self):
|
|
482
|
+
"""test method"""
|
|
483
|
+
if self.tmc_com is None:
|
|
484
|
+
raise TmcDriverException("tmc_com is None; cannot test communication")
|
|
485
|
+
|
|
486
|
+
self.tmc_logger.log("---")
|
|
487
|
+
self.tmc_logger.log("TEST COM")
|
|
488
|
+
|
|
489
|
+
return self.tmc_com.test_com(self.ioin)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# pylint: disable=import-outside-toplevel
|
|
2
|
+
"""Communication module for TMC stepper drivers
|
|
3
|
+
|
|
4
|
+
Provides UART and SPI communication implementations for different platforms:
|
|
5
|
+
- TmcComUart: Standard UART communication (CPython with pyserial)
|
|
6
|
+
- TmcComSpi: Standard SPI communication (CPython with spidev)
|
|
7
|
+
- TmcComUartMicroPython: UART for MicroPython
|
|
8
|
+
- TmcComSpiMicroPython: SPI for MicroPython
|
|
9
|
+
- TmcComSpiFtdi: SPI via FTDI USB devices
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
>>> from tmc_driver.com import TmcComUart
|
|
13
|
+
>>> tmc_com = TmcComUart("/dev/serial0")
|
|
14
|
+
|
|
15
|
+
Note: Base classes (TmcCom, TmcComUartBase, TmcComSpiBase) can be imported
|
|
16
|
+
directly from their respective files if needed for type hints or subclassing.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
from typing import TYPE_CHECKING
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
# Import for type checkers/IDE only - not executed at runtime
|
|
24
|
+
from ._tmc_com_uart import TmcComUart
|
|
25
|
+
from ._tmc_com_spi import TmcComSpi
|
|
26
|
+
from ._tmc_com_uart_micropython import TmcComUartMicroPython
|
|
27
|
+
from ._tmc_com_spi_micropython import TmcComSpiMicroPython
|
|
28
|
+
from ._tmc_com_spi_ftdi import TmcComSpiFtdi
|
|
29
|
+
except ImportError:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def __getattr__(name):
|
|
34
|
+
"""Lazy import of communication classes to avoid circular imports"""
|
|
35
|
+
if name == "TmcComUart":
|
|
36
|
+
from ._tmc_com_uart import TmcComUart
|
|
37
|
+
|
|
38
|
+
return TmcComUart
|
|
39
|
+
if name == "TmcComSpi":
|
|
40
|
+
from ._tmc_com_spi import TmcComSpi
|
|
41
|
+
|
|
42
|
+
return TmcComSpi
|
|
43
|
+
if name == "TmcComUartMicroPython":
|
|
44
|
+
from ._tmc_com_uart_micropython import TmcComUartMicroPython
|
|
45
|
+
|
|
46
|
+
return TmcComUartMicroPython
|
|
47
|
+
if name == "TmcComSpiMicroPython":
|
|
48
|
+
from ._tmc_com_spi_micropython import TmcComSpiMicroPython
|
|
49
|
+
|
|
50
|
+
return TmcComSpiMicroPython
|
|
51
|
+
if name == "TmcComSpiFtdi":
|
|
52
|
+
from ._tmc_com_spi_ftdi import TmcComSpiFtdi
|
|
53
|
+
|
|
54
|
+
return TmcComSpiFtdi
|
|
55
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
__all__ = [
|
|
59
|
+
"TmcComUart",
|
|
60
|
+
"TmcComSpi",
|
|
61
|
+
"TmcComUartMicroPython",
|
|
62
|
+
"TmcComSpiMicroPython",
|
|
63
|
+
"TmcComSpiFtdi",
|
|
64
|
+
]
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# pylint: disable=unused-import
|
|
2
|
+
"""
|
|
3
|
+
TmcCom stepper driver communication module
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import time
|
|
7
|
+
import struct
|
|
8
|
+
from abc import abstractmethod
|
|
9
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
10
|
+
from .._tmc_exceptions import TmcComException
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def compute_crc8_atm(datagram, initial_value=0):
|
|
14
|
+
"""this function calculates the crc8 parity bit
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
datagram (list): datagram
|
|
18
|
+
initial_value (int): initial value (Default value = 0)
|
|
19
|
+
"""
|
|
20
|
+
crc = initial_value
|
|
21
|
+
# Iterate bytes in data
|
|
22
|
+
for byte in datagram:
|
|
23
|
+
# Iterate bits in byte
|
|
24
|
+
for _ in range(0, 8):
|
|
25
|
+
if (crc >> 7) ^ (byte & 0x01):
|
|
26
|
+
crc = ((crc << 1) ^ 0x07) & 0xFF
|
|
27
|
+
else:
|
|
28
|
+
crc = (crc << 1) & 0xFF
|
|
29
|
+
# Shift to next bit
|
|
30
|
+
byte = byte >> 1
|
|
31
|
+
return crc
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class TmcCom:
|
|
35
|
+
"""TmcCom"""
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def tmc_logger(self) -> TmcLogger:
|
|
39
|
+
"""get the tmc_logger"""
|
|
40
|
+
return self._tmc_logger
|
|
41
|
+
|
|
42
|
+
@tmc_logger.setter
|
|
43
|
+
def tmc_logger(self, tmc_logger: TmcLogger):
|
|
44
|
+
"""set the tmc_logger"""
|
|
45
|
+
self._tmc_logger = tmc_logger
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def driver_address(self) -> int:
|
|
49
|
+
"""get the driver address"""
|
|
50
|
+
return self._driver_address
|
|
51
|
+
|
|
52
|
+
@driver_address.setter
|
|
53
|
+
def driver_address(self, address: int):
|
|
54
|
+
"""set the driver address"""
|
|
55
|
+
self._driver_address = address
|
|
56
|
+
|
|
57
|
+
def __init__(self):
|
|
58
|
+
"""constructor"""
|
|
59
|
+
self._tmc_logger: TmcLogger
|
|
60
|
+
self._driver_address: int = 0
|
|
61
|
+
self.communication_pause: int = 0
|
|
62
|
+
self._get_register_callback = None
|
|
63
|
+
|
|
64
|
+
@abstractmethod
|
|
65
|
+
def init(self):
|
|
66
|
+
"""init communication"""
|
|
67
|
+
|
|
68
|
+
@abstractmethod
|
|
69
|
+
def deinit(self):
|
|
70
|
+
"""deinit communication"""
|
|
71
|
+
|
|
72
|
+
def set_get_register_callback(self, callback):
|
|
73
|
+
"""Set callback to get registers from parent TMC class
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
callback: Function that takes register name (str) and returns register object
|
|
77
|
+
"""
|
|
78
|
+
self._get_register_callback = callback
|
|
79
|
+
|
|
80
|
+
def get_register(self, name: str):
|
|
81
|
+
"""Get register by name from parent TMC class
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
name: Register name (e.g. 'gconf', 'chopconf')
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Register object or None if callback not set
|
|
88
|
+
"""
|
|
89
|
+
if self._get_register_callback is None:
|
|
90
|
+
raise TmcComException("Get register callback not set in communication")
|
|
91
|
+
return self._get_register_callback(name)
|
|
92
|
+
|
|
93
|
+
@abstractmethod
|
|
94
|
+
def read_reg(self, addr: int) -> tuple[int, dict | None]:
|
|
95
|
+
"""reads the registry on the TMC with a given address.
|
|
96
|
+
returns the binary value of that register
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
addr (int): HEX, which register to read
|
|
100
|
+
Returns:
|
|
101
|
+
int: register value
|
|
102
|
+
Dict: flags
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
@abstractmethod
|
|
106
|
+
def read_int(self, addr: int, tries: int = 10) -> tuple[int, dict | None]:
|
|
107
|
+
"""this function tries to read the registry of the TMC 10 times
|
|
108
|
+
if a valid answer is returned, this function returns it as an integer
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
addr (int): HEX, which register to read
|
|
112
|
+
tries (int): how many tries, before error is raised (Default value = 10)
|
|
113
|
+
Returns:
|
|
114
|
+
int: register value
|
|
115
|
+
Dict: flags
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
@abstractmethod
|
|
119
|
+
def write_reg(self, addr: int, val: int):
|
|
120
|
+
"""this function can write a value to the register of the tmc
|
|
121
|
+
1. use read_int to get the current setting of the TMC
|
|
122
|
+
2. then modify the settings as wished
|
|
123
|
+
3. write them back to the driver with this function
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
addr (int): HEX, which register to write
|
|
127
|
+
val (int): value for that register
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
@abstractmethod
|
|
131
|
+
def write_reg_check(self, addr: int, val: int, tries: int = 10):
|
|
132
|
+
"""this function als writes a value to the register of the TMC
|
|
133
|
+
but it also checks if the writing process was successfully by checking
|
|
134
|
+
the InterfaceTransmissionCounter before and after writing
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
addr: HEX, which register to write
|
|
138
|
+
val: value for that register
|
|
139
|
+
tries: how many tries, before error is raised (Default value = 10)
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
@abstractmethod
|
|
143
|
+
def flush_com_buffer(self):
|
|
144
|
+
"""this function clear the communication buffers of the Raspberry Pi"""
|
|
145
|
+
|
|
146
|
+
@abstractmethod
|
|
147
|
+
def test_com(self, ioin) -> bool:
|
|
148
|
+
"""test com connection"""
|