PyTmcStepper 0.7__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.
Potentially problematic release.
This version of PyTmcStepper might be problematic. Click here for more details.
- pytmcstepper-0.7.dist-info/LICENSE +682 -0
- pytmcstepper-0.7.dist-info/METADATA +302 -0
- pytmcstepper-0.7.dist-info/RECORD +32 -0
- pytmcstepper-0.7.dist-info/WHEEL +5 -0
- pytmcstepper-0.7.dist-info/top_level.txt +1 -0
- tmc_driver/__init__.py +0 -0
- tmc_driver/_tmc_gpio_board.py +321 -0
- tmc_driver/_tmc_logger.py +170 -0
- tmc_driver/_tmc_math.py +102 -0
- tmc_driver/_tmc_stepperdriver.py +278 -0
- tmc_driver/com/__init__.py +0 -0
- tmc_driver/com/_tmc_com.py +155 -0
- tmc_driver/com/_tmc_com_spi.py +177 -0
- tmc_driver/com/_tmc_com_uart.py +336 -0
- tmc_driver/enable_control/__init__.py +0 -0
- tmc_driver/enable_control/_tmc_ec.py +26 -0
- tmc_driver/enable_control/_tmc_ec_pin.py +48 -0
- tmc_driver/enable_control/_tmc_ec_toff.py +39 -0
- tmc_driver/motion_control/__init__.py +0 -0
- tmc_driver/motion_control/_tmc_mc.py +221 -0
- tmc_driver/motion_control/_tmc_mc_step_dir.py +382 -0
- tmc_driver/motion_control/_tmc_mc_step_reg.py +61 -0
- tmc_driver/motion_control/_tmc_mc_vactual.py +179 -0
- tmc_driver/reg/__init__.py +0 -0
- tmc_driver/reg/_tmc2209_reg.py +62 -0
- tmc_driver/reg/_tmc220x_reg.py +198 -0
- tmc_driver/reg/_tmc224x_reg.py +351 -0
- tmc_driver/reg/_tmc_reg.py +121 -0
- tmc_driver/tmc_2208.py +12 -0
- tmc_driver/tmc_2209.py +369 -0
- tmc_driver/tmc_220x.py +684 -0
- tmc_driver/tmc_2240.py +801 -0
tmc_driver/tmc_2209.py
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
#pylint: disable=too-many-arguments
|
|
2
|
+
#pylint: disable=too-many-instance-attributes
|
|
3
|
+
#pylint: disable=too-many-positional-arguments
|
|
4
|
+
#pylint: disable=no-member
|
|
5
|
+
#pylint: disable=wildcard-import
|
|
6
|
+
#pylint: disable=unused-wildcard-import
|
|
7
|
+
"""Tmc2209 stepper driver module
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import statistics
|
|
11
|
+
import types
|
|
12
|
+
from .tmc_220x import *
|
|
13
|
+
from ._tmc_gpio_board import GpioPUD
|
|
14
|
+
from .reg._tmc2209_reg import *
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Tmc2209(Tmc220x):
|
|
18
|
+
"""Tmc2209"""
|
|
19
|
+
|
|
20
|
+
_pin_stallguard:int = None
|
|
21
|
+
_sg_callback:types.FunctionType = None
|
|
22
|
+
_sg_threshold:int = 100 # threshold for stallguard
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Constructor/Destructor
|
|
27
|
+
# ----------------------------
|
|
28
|
+
def __init__(self,
|
|
29
|
+
tmc_ec:TmcEnableControl,
|
|
30
|
+
tmc_mc:TmcMotionControl,
|
|
31
|
+
tmc_com:TmcCom = None,
|
|
32
|
+
driver_address:int = 0,
|
|
33
|
+
gpio_mode = None,
|
|
34
|
+
loglevel:Loglevel = Loglevel.INFO,
|
|
35
|
+
logprefix:str = None,
|
|
36
|
+
log_handlers:list = None,
|
|
37
|
+
log_formatter:logging.Formatter = None
|
|
38
|
+
):
|
|
39
|
+
"""constructor
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
tmc_ec (TmcEnableControl): enable control object
|
|
43
|
+
tmc_mc (TmcMotionControl): motion control object
|
|
44
|
+
tmc_com (TmcCom, optional): communication object. Defaults to None.
|
|
45
|
+
driver_address (int, optional): driver address [0-3]. Defaults to 0.
|
|
46
|
+
gpio_mode (enum, optional): gpio mode. Defaults to None.
|
|
47
|
+
loglevel (enum, optional): loglevel. Defaults to None.
|
|
48
|
+
logprefix (str, optional): log prefix (name of the logger).
|
|
49
|
+
Defaults to None (standard TMC prefix).
|
|
50
|
+
log_handlers (list, optional): list of logging handlers.
|
|
51
|
+
Defaults to None (log to console).
|
|
52
|
+
log_formatter (logging.Formatter, optional): formatter for the log messages.
|
|
53
|
+
Defaults to None (messages are logged in the format
|
|
54
|
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
|
|
55
|
+
"""
|
|
56
|
+
super().__init__(tmc_ec, tmc_mc, tmc_com, driver_address, gpio_mode, loglevel, logprefix, log_handlers, log_formatter)
|
|
57
|
+
|
|
58
|
+
if tmc_com is not None:
|
|
59
|
+
|
|
60
|
+
registers_classes = {
|
|
61
|
+
TCoolThrs,
|
|
62
|
+
SGThrs,
|
|
63
|
+
SGResult,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for register_class in registers_classes:
|
|
67
|
+
register = register_class(self.tmc_com)
|
|
68
|
+
name = register.name.lower()
|
|
69
|
+
self.tmc_registers[name] = register
|
|
70
|
+
|
|
71
|
+
def create_getter(name):
|
|
72
|
+
def getter(self):
|
|
73
|
+
return self.tmc_registers[name]
|
|
74
|
+
return getter
|
|
75
|
+
|
|
76
|
+
setattr(self.__class__, name, property(create_getter(name)))
|
|
77
|
+
|
|
78
|
+
self.tmc_logger.log("TMC2209 Init finished", Loglevel.INFO)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def __del__(self):
|
|
82
|
+
"""destructor"""
|
|
83
|
+
if self._deinit_finished is False:
|
|
84
|
+
if self._pin_stallguard is not None:
|
|
85
|
+
tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
86
|
+
tmc_gpio.gpio_cleanup(self._pin_stallguard)
|
|
87
|
+
|
|
88
|
+
super().__del__()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def set_stallguard_callback(self, pin_stallguard, threshold, callback,
|
|
93
|
+
min_speed = 100):
|
|
94
|
+
"""set a function to call back, when the driver detects a stall
|
|
95
|
+
via stallguard
|
|
96
|
+
high value on the diag pin can also mean a driver error
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
pin_stallguard (int): pin needs to be connected to DIAG
|
|
100
|
+
threshold (int): value for SGTHRS
|
|
101
|
+
callback (func): will be called on StallGuard trigger
|
|
102
|
+
min_speed (int): min speed [steps/s] for StallGuard (Default value = 100)
|
|
103
|
+
"""
|
|
104
|
+
self.tmc_logger.log(f"setup stallguard callback on GPIO {pin_stallguard}", Loglevel.INFO)
|
|
105
|
+
self.tmc_logger.log(f"StallGuard Threshold: {threshold} | minimum Speed: {min_speed}", Loglevel.INFO)
|
|
106
|
+
|
|
107
|
+
self._set_stallguard_threshold(threshold)
|
|
108
|
+
self._set_coolstep_threshold(tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution()))
|
|
109
|
+
self._sg_callback = callback
|
|
110
|
+
self._pin_stallguard = pin_stallguard
|
|
111
|
+
|
|
112
|
+
tmc_gpio.gpio_setup(self._pin_stallguard, GpioMode.IN, pull_up_down=GpioPUD.PUD_DOWN)
|
|
113
|
+
# first remove existing events
|
|
114
|
+
tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
|
|
115
|
+
tmc_gpio.gpio_add_event_detect(self._pin_stallguard, self.stallguard_callback)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def stallguard_callback(self, gpio_pin):
|
|
120
|
+
"""the callback function for StallGuard.
|
|
121
|
+
only checks whether the duration of the current movement is longer than
|
|
122
|
+
_sg_delay and then calls the actual callback
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
gpio_pin (int): pin number of the interrupt pin
|
|
126
|
+
"""
|
|
127
|
+
del gpio_pin
|
|
128
|
+
if self._sg_callback is None:
|
|
129
|
+
self.tmc_logger.log("StallGuard callback is None", Loglevel.DEBUG)
|
|
130
|
+
return
|
|
131
|
+
self._sg_callback()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def do_homing(self, diag_pin, revolutions = 10, threshold = None, speed_rpm = None) -> bool:
|
|
136
|
+
"""homes the motor in the given direction using stallguard.
|
|
137
|
+
this method is using vactual to move the motor and an interrupt on the DIAG pin
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
diag_pin (int): DIAG pin number
|
|
141
|
+
revolutions (int): max number of revolutions. Can be negative for inverse direction
|
|
142
|
+
(Default value = 10)
|
|
143
|
+
threshold (int): StallGuard detection threshold (Default value = None)
|
|
144
|
+
speed_rpm (float):speed in revolutions per minute (Default value = None)
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
not homing_failed (bool): true when homing was successful
|
|
148
|
+
"""
|
|
149
|
+
if self.tmc_com is None:
|
|
150
|
+
self.tmc_logger.log("do_homing only works with VActual register control via COM", Loglevel.ERROR)
|
|
151
|
+
return False
|
|
152
|
+
|
|
153
|
+
if threshold is not None:
|
|
154
|
+
self._sg_threshold = threshold
|
|
155
|
+
|
|
156
|
+
self.tmc_logger.log(f"Stallguard threshold: {self._sg_threshold}", Loglevel.DEBUG)
|
|
157
|
+
|
|
158
|
+
if speed_rpm is None:
|
|
159
|
+
speed_rpm = tmc_math.steps_to_rps(self.tmc_mc.max_speed_homing, self.tmc_mc.steps_per_rev)*60
|
|
160
|
+
|
|
161
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
162
|
+
self.tmc_logger.log("homing", Loglevel.INFO)
|
|
163
|
+
|
|
164
|
+
# StallGuard only works with StealthChop
|
|
165
|
+
self.set_spreadcycle(0)
|
|
166
|
+
|
|
167
|
+
mc_homing = TmcMotionControlVActual()
|
|
168
|
+
mc_homing.tmc_com = self.tmc_com
|
|
169
|
+
mc_homing.tmc_logger = self.tmc_logger
|
|
170
|
+
|
|
171
|
+
self.set_stallguard_callback(diag_pin, self._sg_threshold, mc_homing.stop,
|
|
172
|
+
0.5*tmc_math.rps_to_steps(speed_rpm/60, self.tmc_mc.steps_per_rev))
|
|
173
|
+
|
|
174
|
+
homing_failed = mc_homing.set_vactual_rpm(speed_rpm, revolutions=revolutions)
|
|
175
|
+
|
|
176
|
+
if homing_failed:
|
|
177
|
+
self.tmc_logger.log("homing failed", Loglevel.INFO)
|
|
178
|
+
else:
|
|
179
|
+
self.tmc_logger.log("homing successful",Loglevel.INFO)
|
|
180
|
+
|
|
181
|
+
self.tmc_mc.current_pos = 0
|
|
182
|
+
|
|
183
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
184
|
+
return not homing_failed
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def do_homing2(self, revolutions, threshold=None):
|
|
189
|
+
"""homes the motor in the given direction using stallguard
|
|
190
|
+
old function, uses STEP/DIR to move the motor and pulls the StallGuard result
|
|
191
|
+
from the interface
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
revolutions (int): max number of revolutions. Can be negative for inverse direction
|
|
195
|
+
threshold (int, optional): StallGuard detection threshold (Default value = None)
|
|
196
|
+
"""
|
|
197
|
+
if not isinstance(self.tmc_mc, TmcMotionControlStepDir):
|
|
198
|
+
self.tmc_logger.log("do_homing2 only works with STEP/DIR Control", Loglevel.ERROR)
|
|
199
|
+
return
|
|
200
|
+
sg_results = []
|
|
201
|
+
|
|
202
|
+
if threshold is not None:
|
|
203
|
+
self._sg_threshold = threshold
|
|
204
|
+
|
|
205
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
206
|
+
self.tmc_logger.log("homing", Loglevel.INFO)
|
|
207
|
+
|
|
208
|
+
self.tmc_logger.log(f"Stallguard threshold: {self._sg_threshold}", Loglevel.DEBUG)
|
|
209
|
+
|
|
210
|
+
self.tmc_mc.set_direction_pin(revolutions > 0)
|
|
211
|
+
|
|
212
|
+
# StallGuard only works with StealthChop
|
|
213
|
+
self.set_spreadcycle(0)
|
|
214
|
+
|
|
215
|
+
self.tmc_mc.target_pos = self.tmc_mc.steps_per_rev * revolutions
|
|
216
|
+
# self._step_interval = 0
|
|
217
|
+
# self._speed = 0.0
|
|
218
|
+
# self._n = 0
|
|
219
|
+
self.tmc_mc.acceleration = 10000
|
|
220
|
+
self.tmc_mc.max_speed = self.tmc_mc.max_speed_homing
|
|
221
|
+
|
|
222
|
+
coolstep_thres = tmc_math.steps_to_tstep(self.tmc_mc.max_speed_homing*0.5,
|
|
223
|
+
self.tmc_mc.mres)
|
|
224
|
+
self._set_coolstep_threshold(coolstep_thres)
|
|
225
|
+
self.tmc_mc.compute_new_speed()
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
step_counter=0
|
|
229
|
+
#self.tmc_logger.log("Steps per Revolution: {self._steps_per_rev}"")
|
|
230
|
+
while step_counter<self.tmc_mc.target_pos:
|
|
231
|
+
if self.tmc_mc.run_speed(): #returns true, when a step is made
|
|
232
|
+
step_counter += 1
|
|
233
|
+
self.tmc_mc.compute_new_speed()
|
|
234
|
+
sg_result = self.get_stallguard_result()
|
|
235
|
+
sg_results.append(sg_result)
|
|
236
|
+
if len(sg_results)>20:
|
|
237
|
+
sg_result_average = statistics.mean(sg_results[-6:])
|
|
238
|
+
if sg_result_average < self._sg_threshold:
|
|
239
|
+
break
|
|
240
|
+
|
|
241
|
+
if step_counter<self.tmc_mc.steps_per_rev:
|
|
242
|
+
self.tmc_logger.log("homing successful",Loglevel.INFO)
|
|
243
|
+
self.tmc_logger.log(f"Stepcounter: {step_counter}",Loglevel.DEBUG)
|
|
244
|
+
self.tmc_logger.log(str(sg_results),Loglevel.DEBUG)
|
|
245
|
+
self.tmc_mc.current_pos = 0
|
|
246
|
+
else:
|
|
247
|
+
self.tmc_logger.log("homing failed", Loglevel.INFO)
|
|
248
|
+
self.tmc_logger.log(f"Stepcounter: {step_counter}", Loglevel.DEBUG)
|
|
249
|
+
self.tmc_logger.log(str(sg_results),Loglevel.DEBUG)
|
|
250
|
+
|
|
251
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def get_stallguard_result(self):
|
|
256
|
+
"""return the current stallguard result
|
|
257
|
+
its will be calculated with every fullstep
|
|
258
|
+
higher values means a lower motor load
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
sg_result (int): StallGuard Result
|
|
262
|
+
"""
|
|
263
|
+
self.sgresult.read()
|
|
264
|
+
return self.sgresult.sgresult
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _set_stallguard_threshold(self, threshold):
|
|
269
|
+
"""sets the register bit "SGTHRS" to to a given value
|
|
270
|
+
this is needed for the stallguard interrupt callback
|
|
271
|
+
SG_RESULT becomes compared to the double of this threshold.
|
|
272
|
+
SG_RESULT ≤ SGTHRS*2
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
threshold (int): value for SGTHRS
|
|
276
|
+
"""
|
|
277
|
+
self.sgthrs.modify("sgthrs", threshold)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _set_coolstep_threshold(self, threshold):
|
|
282
|
+
"""This is the lower threshold velocity for switching
|
|
283
|
+
on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
threshold (int): threshold velocity for coolstep
|
|
287
|
+
"""
|
|
288
|
+
self.tcoolthrs.modify("tcoolthrs", threshold)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def enable_coolstep(self, semin_sg:int = 150, semax_sg:int = 200, seup:int = 1, sedn:int = 3, min_speed:int = 100):
|
|
293
|
+
"""enables coolstep and sets the parameters for coolstep
|
|
294
|
+
The values for semin etc. can be tested with the test_stallguard_threshold function
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
semin_sg (int): lower threshold. Current will be increased if SG_Result goes below this
|
|
298
|
+
semax_sg (int): upper threshold. Current will be decreased if SG_Result goes above this
|
|
299
|
+
seup (int): current increment step
|
|
300
|
+
sedn (int): number of SG_Result readings for each current decrement
|
|
301
|
+
"""
|
|
302
|
+
semax_sg = semax_sg - semin_sg
|
|
303
|
+
|
|
304
|
+
self.coolconf.read()
|
|
305
|
+
self.coolconf.semin = round(max(0, min(semin_sg/32, 15)))
|
|
306
|
+
self.coolconf.semax = round(max(0, min(semax_sg/32, 15)))
|
|
307
|
+
self.coolconf.seimin = 1 # scale down to until 1/4 of IRun (7 - 31)
|
|
308
|
+
self.coolconf.seup = seup
|
|
309
|
+
self.coolconf.sedn = sedn
|
|
310
|
+
self.coolconf.write_check()
|
|
311
|
+
|
|
312
|
+
self._set_coolstep_threshold(tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution()))
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_stallguard_threshold(self, steps):
|
|
317
|
+
"""test method for tuning stallguard threshold
|
|
318
|
+
|
|
319
|
+
run this function with your motor settings and your motor load
|
|
320
|
+
the function will determine the minimum stallguard results for each movement phase
|
|
321
|
+
|
|
322
|
+
Args:
|
|
323
|
+
steps (int):
|
|
324
|
+
"""
|
|
325
|
+
|
|
326
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
327
|
+
self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO)
|
|
328
|
+
|
|
329
|
+
self.set_spreadcycle(0)
|
|
330
|
+
|
|
331
|
+
min_stallguard_result_accel = 512
|
|
332
|
+
min_stallguard_result_maxspeed = 512
|
|
333
|
+
min_stallguard_result_decel = 512
|
|
334
|
+
|
|
335
|
+
self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL:
|
|
339
|
+
stallguard_result = self.get_stallguard_result()
|
|
340
|
+
self.drvstatus.read()
|
|
341
|
+
cs_actual = self.drvstatus.cs_actual
|
|
342
|
+
# stallguard_result = self.get_stallguard_result()
|
|
343
|
+
|
|
344
|
+
self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result} | {cs_actual}",
|
|
345
|
+
Loglevel.INFO)
|
|
346
|
+
|
|
347
|
+
self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result}",
|
|
348
|
+
Loglevel.INFO)
|
|
349
|
+
|
|
350
|
+
if (self.tmc_mc.movement_phase == MovementPhase.ACCELERATING and
|
|
351
|
+
stallguard_result < min_stallguard_result_accel):
|
|
352
|
+
min_stallguard_result_accel = stallguard_result
|
|
353
|
+
if (self.tmc_mc.movement_phase == MovementPhase.MAXSPEED and
|
|
354
|
+
stallguard_result < min_stallguard_result_maxspeed):
|
|
355
|
+
min_stallguard_result_maxspeed = stallguard_result
|
|
356
|
+
if (self.tmc_mc.movement_phase == MovementPhase.DECELERATING and
|
|
357
|
+
stallguard_result < min_stallguard_result_decel):
|
|
358
|
+
min_stallguard_result_decel = stallguard_result
|
|
359
|
+
|
|
360
|
+
self.tmc_mc.wait_for_movement_finished_threaded()
|
|
361
|
+
|
|
362
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|
|
363
|
+
self.tmc_logger.log(f"min StallGuard result during accel: {min_stallguard_result_accel}",
|
|
364
|
+
Loglevel.INFO)
|
|
365
|
+
self.tmc_logger.log(f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}",
|
|
366
|
+
Loglevel.INFO)
|
|
367
|
+
self.tmc_logger.log(f"min StallGuard result during decel: {min_stallguard_result_decel}",
|
|
368
|
+
Loglevel.INFO)
|
|
369
|
+
self.tmc_logger.log("---", Loglevel.INFO)
|