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.
Files changed (56) hide show
  1. pytmcstepper-0.15.0.dist-info/METADATA +338 -0
  2. pytmcstepper-0.15.0.dist-info/RECORD +56 -0
  3. pytmcstepper-0.15.0.dist-info/WHEEL +5 -0
  4. pytmcstepper-0.15.0.dist-info/licenses/LICENSE +683 -0
  5. pytmcstepper-0.15.0.dist-info/top_level.txt +1 -0
  6. tmc_driver/__init__.py +96 -0
  7. tmc_driver/_tmc_exceptions.py +21 -0
  8. tmc_driver/_tmc_math.py +122 -0
  9. tmc_driver/_tmc_stallguard.py +340 -0
  10. tmc_driver/_tmc_stepperdriver.py +210 -0
  11. tmc_driver/_tmc_validation.py +29 -0
  12. tmc_driver/_tmc_xxxx.py +489 -0
  13. tmc_driver/com/__init__.py +64 -0
  14. tmc_driver/com/_tmc_com.py +148 -0
  15. tmc_driver/com/_tmc_com_spi.py +81 -0
  16. tmc_driver/com/_tmc_com_spi_base.py +159 -0
  17. tmc_driver/com/_tmc_com_spi_ftdi.py +48 -0
  18. tmc_driver/com/_tmc_com_spi_micropython.py +99 -0
  19. tmc_driver/com/_tmc_com_uart.py +113 -0
  20. tmc_driver/com/_tmc_com_uart_base.py +386 -0
  21. tmc_driver/com/_tmc_com_uart_micropython.py +115 -0
  22. tmc_driver/enable_control/__init__.py +55 -0
  23. tmc_driver/enable_control/_tmc_ec.py +59 -0
  24. tmc_driver/enable_control/_tmc_ec_pin.py +48 -0
  25. tmc_driver/enable_control/_tmc_ec_toff.py +30 -0
  26. tmc_driver/motion_control/__init__.py +111 -0
  27. tmc_driver/motion_control/_tmc_mc.py +280 -0
  28. tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +218 -0
  29. tmc_driver/motion_control/_tmc_mc_step_dir.py +415 -0
  30. tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +78 -0
  31. tmc_driver/motion_control/_tmc_mc_step_reg.py +36 -0
  32. tmc_driver/motion_control/_tmc_mc_vactual.py +221 -0
  33. tmc_driver/platform_utils.py +13 -0
  34. tmc_driver/reg/__init__.py +0 -0
  35. tmc_driver/reg/_tmc2209_reg.py +55 -0
  36. tmc_driver/reg/_tmc220x_reg.py +261 -0
  37. tmc_driver/reg/_tmc224x_reg.py +414 -0
  38. tmc_driver/reg/_tmc5160_reg.py +526 -0
  39. tmc_driver/reg/_tmc_reg.py +202 -0
  40. tmc_driver/reg/_tmc_shared_regs.py +314 -0
  41. tmc_driver/tmc_2208.py +17 -0
  42. tmc_driver/tmc_2209.py +75 -0
  43. tmc_driver/tmc_220x.py +383 -0
  44. tmc_driver/tmc_2240.py +340 -0
  45. tmc_driver/tmc_5160.py +344 -0
  46. tmc_driver/tmc_gpio/__init__.py +101 -0
  47. tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +106 -0
  48. tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +162 -0
  49. tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +135 -0
  50. tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +108 -0
  51. tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +87 -0
  52. tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +182 -0
  53. tmc_driver/tmc_logger/__init__.py +15 -0
  54. tmc_driver/tmc_logger/_tmc_logger_base.py +111 -0
  55. tmc_driver/tmc_logger/_tmc_logger_cpython.py +155 -0
  56. tmc_driver/tmc_logger/_tmc_logger_micropython.py +52 -0
tmc_driver/tmc_220x.py ADDED
@@ -0,0 +1,383 @@
1
+ # pylint: disable=wildcard-import
2
+ # pylint: disable=unused-wildcard-import
3
+ # pylint: disable=too-many-arguments
4
+ # pylint: disable=too-many-positional-arguments
5
+ # pylint: disable=too-many-instance-attributes
6
+ # pylint: disable=too-many-public-methods
7
+ """Tmc220X stepper 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
+ from ._tmc_xxxx import *
16
+ from .com._tmc_com import TmcCom
17
+ from .com._tmc_com_uart_base import TmcComUartBase
18
+ from .motion_control._tmc_mc_step_reg import TmcMotionControlStepReg
19
+ from .motion_control._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
20
+ from .motion_control._tmc_mc_vactual import TmcMotionControlVActual
21
+ from .enable_control._tmc_ec_toff import TmcEnableControlToff
22
+ from .enable_control._tmc_ec_pin import TmcEnableControlPin
23
+ from .tmc_logger import Loglevel
24
+ from .reg._tmc220x_reg import *
25
+
26
+
27
+ class Tmc220x(TmcXXXX):
28
+ """Tmc220X"""
29
+
30
+ SUPPORTED_COM_TYPES = (TmcComUartBase,)
31
+ SUPPORTED_EC_TYPES = (TmcEnableControlToff, TmcEnableControlPin)
32
+ SUPPORTED_MC_TYPES = (
33
+ TmcMotionControlStepDir,
34
+ TmcMotionControlStepReg,
35
+ TmcMotionControlStepPwmDir,
36
+ TmcMotionControlVActual,
37
+ )
38
+ DRIVER_FAMILY = "TMC220X"
39
+
40
+ # Constructor/Destructor
41
+ # ----------------------------
42
+ def __init__(
43
+ self,
44
+ tmc_ec: TmcEnableControl,
45
+ tmc_mc: TmcMotionControl,
46
+ tmc_com: TmcCom | None = None,
47
+ driver_address: int = 0,
48
+ gpio_mode=None,
49
+ loglevel: Loglevel = Loglevel.INFO,
50
+ logprefix: str | None = None,
51
+ log_handlers: list | None = None,
52
+ log_formatter: logging.Formatter | None = None,
53
+ ):
54
+ """constructor
55
+
56
+ Args:
57
+ tmc_ec (TmcEnableControl): enable control object
58
+ tmc_mc (TmcMotionControl): motion control object
59
+ tmc_com (TmcCom, optional): communication object. Defaults to None.
60
+ driver_address (int, optional): driver address [0-3]. Defaults to 0.
61
+ gpio_mode (enum, optional): gpio mode. Defaults to None.
62
+ loglevel (enum, optional): loglevel. Defaults to None.
63
+ logprefix (str, optional): log prefix (name of the logger).
64
+ Defaults to None (standard TMC prefix).
65
+ log_handlers (list, optional): list of logging handlers.
66
+ Defaults to None (log to console).
67
+ log_formatter (logging.Formatter, optional): formatter for the log messages.
68
+ Defaults to None (messages are logged in the format
69
+ '%(asctime)s - %(name)s - %(levelname)s - %(message)s').
70
+ """
71
+ if logprefix is None:
72
+ logprefix = f"{self.DRIVER_FAMILY} {driver_address}"
73
+
74
+ super().__init__(
75
+ tmc_ec,
76
+ tmc_mc,
77
+ tmc_com,
78
+ driver_address,
79
+ gpio_mode,
80
+ loglevel,
81
+ logprefix,
82
+ log_handlers,
83
+ log_formatter,
84
+ )
85
+
86
+ if self.tmc_com is not None:
87
+ self.gconf: GConf = GConf(self.tmc_com)
88
+ self.gstat: GStat = GStat(self.tmc_com)
89
+ self.ifcnt: IfCnt = IfCnt(self.tmc_com)
90
+ self.ioin: Ioin = Ioin(self.tmc_com)
91
+ self.ihold_irun: IHoldIRun = IHoldIRun(self.tmc_com)
92
+ self.tpowerdown: TPowerDown = TPowerDown(self.tmc_com)
93
+ self.tstep: TStep = TStep(self.tmc_com)
94
+ self.tpwmthrs: TPwmThrs = TPwmThrs(self.tmc_com)
95
+ self.vactual: VActual = VActual(self.tmc_com)
96
+ self.mscnt: MsCnt = MsCnt(self.tmc_com)
97
+ self.chopconf: ChopConf = ChopConf(self.tmc_com)
98
+ self.pwmconf: PwmConf = PwmConf(self.tmc_com)
99
+ self.drvstatus: DrvStatus = DrvStatus(self.tmc_com)
100
+
101
+ super()._init()
102
+
103
+ # Register Access
104
+ # ----------------------------
105
+ def get_iscale_analog(self) -> bool:
106
+ """return whether Vref (True) or 5V (False) is used for current scale
107
+
108
+ Returns:
109
+ en (bool): whether Vref (True) or 5V (False) is used for current scale
110
+ """
111
+ self.gconf.read()
112
+ return self.gconf.i_scale_analog
113
+
114
+ def set_iscale_analog(self, en: bool):
115
+ """sets Vref (True) or 5V (False) for current scale
116
+
117
+ Args:
118
+ en (bool): True=Vref, False=5V
119
+ """
120
+ self.gconf.modify("i_scale_analog", en)
121
+
122
+ def get_vsense(self) -> bool:
123
+ """returns which sense resistor voltage is used for current scaling
124
+ False: Low sensitivity, high sense resistor voltage
125
+ True: High sensitivity, low sense resistor voltage
126
+
127
+ Returns:
128
+ bool: whether high sensitivity should is used
129
+ """
130
+ self.chopconf.read()
131
+ return self.chopconf.vsense
132
+
133
+ def set_vsense(self, en: bool):
134
+ """sets which sense resistor voltage is used for current scaling
135
+ False: Low sensitivity, high sense resistor voltage
136
+ True: High sensitivity, low sense resistor voltage
137
+
138
+ Args:
139
+ en (bool):
140
+ """
141
+ self.chopconf.modify("vsense", en)
142
+
143
+ def get_internal_rsense(self) -> bool:
144
+ """returns which sense resistor voltage is used for current scaling
145
+ False: Operation with external sense resistors
146
+ True Internal sense resistors. Use current supplied into
147
+ VREF as reference for internal sense resistor. VREF
148
+ pin internally is driven to GND in this mode.
149
+
150
+ Returns:
151
+ bool: which sense resistor voltage is used
152
+ """
153
+ self.gconf.read()
154
+ return self.gconf.internal_rsense
155
+
156
+ def set_internal_rsense(self, en: bool):
157
+ """sets which sense resistor voltage is used for current scaling
158
+ False: Operation with external sense resistors
159
+ True: Internal sense resistors. Use current supplied into
160
+ VREF as reference for internal sense resistor. VREF
161
+ pin internally is driven to GND in this mode.
162
+
163
+ Args:
164
+ en (bool): which sense resistor voltage is used; true will propably destroy your tmc
165
+
166
+ """
167
+ if en:
168
+ self.tmc_logger.log("activated internal sense resistors.", Loglevel.INFO)
169
+ self.tmc_logger.log(
170
+ "VREF pin internally is driven to GND in this mode.", Loglevel.INFO
171
+ )
172
+ self.tmc_logger.log(
173
+ "This will most likely destroy your driver!!!", Loglevel.INFO
174
+ )
175
+ raise SystemExit
176
+
177
+ self.gconf.modify("internal_rsense", en)
178
+
179
+ def _set_irun_ihold(self, ihold: int, irun: int, iholddelay: int):
180
+ """sets the current scale (CS) for Running and Holding
181
+ and the delay, when to be switched to Holding current
182
+
183
+ Args:
184
+ ihold (int): multiplicator for current while standstill [0-31]
185
+ irun (int): current while running [0-31]
186
+ iholddelay (int): delay after standstill for switching to ihold [0-15]
187
+
188
+ """
189
+ self.ihold_irun.read()
190
+
191
+ self.ihold_irun.ihold = ihold
192
+ self.ihold_irun.irun = irun
193
+ self.ihold_irun.iholddelay = iholddelay
194
+
195
+ self.ihold_irun.write_check()
196
+
197
+ def _set_pdn_disable(self, pdn_disable: bool):
198
+ """disables PDN on the UART pin
199
+ False: PDN_UART controls standstill current reduction
200
+ True: PDN_UART input function disabled. Set this bit,
201
+ when using the UART interface!
202
+
203
+ Args:
204
+ pdn_disable (bool): whether PDN should be disabled
205
+ """
206
+ self.gconf.modify("pdn_disable", pdn_disable)
207
+
208
+ def set_current_peak(
209
+ self,
210
+ run_current: int,
211
+ hold_current_multiplier: float = 0.5,
212
+ hold_current_delay: int = 10,
213
+ pdn_disable: bool = True,
214
+ ) -> int:
215
+ """sets the Peak current for the motor.
216
+
217
+ Args:
218
+ run_current (int): current during movement in mA
219
+ hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
220
+ hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
221
+ pdn_disable (bool): disables PDN on the UART pin (Default value = True)
222
+ Returns:
223
+ int: theoretical final current in mA
224
+ """
225
+ self.tmc_logger.log(f"Desired peak current: {run_current} mA", Loglevel.DEBUG)
226
+
227
+ cs_irun = 0
228
+ rsense = 0.11
229
+ vfs = 0.325
230
+
231
+ self.set_iscale_analog(False)
232
+
233
+ def calc_cs_irun(run_current: int, rsense: float, vfs: float) -> float:
234
+ """calculates the current scale value for a given current"""
235
+ return 32.0 * run_current / 1000.0 * (rsense + 0.02) / vfs - 1
236
+
237
+ def calc_run_current(cs_irun: float, rsense: float, vfs: float) -> float:
238
+ """calculates the current for a given current scale value"""
239
+ return (cs_irun + 1) / 32.0 * vfs / (rsense + 0.02) * 1000
240
+
241
+ cs_irun = calc_cs_irun(run_current, rsense, vfs)
242
+
243
+ # If Current Scale is too low, turn on high sensitivity VSsense and calculate again
244
+ if cs_irun < 16:
245
+ self.tmc_logger.log("CS too low; switching to VSense True", Loglevel.INFO)
246
+ vfs = 0.180
247
+ cs_irun = calc_cs_irun(run_current, rsense, vfs)
248
+ self.set_vsense(True)
249
+ else: # If CS >= 16, turn off high_senser
250
+ self.tmc_logger.log("CS in range; using VSense False", Loglevel.INFO)
251
+ self.set_vsense(False)
252
+
253
+ cs_irun = min(cs_irun, 31)
254
+ cs_irun = max(cs_irun, 0)
255
+
256
+ cs_ihold = hold_current_multiplier * cs_irun
257
+
258
+ cs_irun = round(cs_irun)
259
+ cs_ihold = round(cs_ihold)
260
+ hold_current_delay = round(hold_current_delay)
261
+
262
+ self.tmc_logger.log(f"cs_irun: {cs_irun}", Loglevel.INFO)
263
+ self.tmc_logger.log(f"CS_IHold: {cs_ihold}", Loglevel.INFO)
264
+ self.tmc_logger.log(f"Delay: {hold_current_delay}", Loglevel.INFO)
265
+
266
+ # return (float)(CS+1)/32.0 * (vsense() ? 0.180 : 0.325)/(rsense+0.02) / 1.41421 * 1000;
267
+ run_current_actual = calc_run_current(cs_irun, rsense, vfs)
268
+ self.tmc_logger.log(
269
+ f"Calculated theoretical peak current after gscaler: {run_current_actual} mA",
270
+ Loglevel.DEBUG,
271
+ )
272
+
273
+ self._set_irun_ihold(cs_ihold, cs_irun, hold_current_delay)
274
+
275
+ self._set_pdn_disable(pdn_disable)
276
+
277
+ return round(run_current_actual)
278
+
279
+ def set_current_rms(
280
+ self,
281
+ run_current: int,
282
+ hold_current_multiplier: float = 0.5,
283
+ hold_current_delay: int = 10,
284
+ pdn_disable: bool = True,
285
+ ) -> int:
286
+ """sets the RMS current for the motor.
287
+
288
+ Args:
289
+ run_current (int): current during movement in mA
290
+ hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
291
+ hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
292
+ pdn_disable (bool): disables PDN on the UART pin (Default value = True)
293
+
294
+ Returns:
295
+ int: theoretical final current in mA
296
+ """
297
+ peak_current = self.set_current_peak(
298
+ round(run_current * 1.41421),
299
+ hold_current_multiplier,
300
+ hold_current_delay,
301
+ pdn_disable,
302
+ )
303
+ return round(peak_current / 1.41421)
304
+
305
+ def get_spreadcycle(self) -> bool:
306
+ """reads spreadcycle
307
+
308
+ Returns:
309
+ bool: True = spreadcycle; False = stealthchop
310
+ """
311
+ self.gconf.read()
312
+ return self.gconf.en_spreadcycle
313
+
314
+ def set_spreadcycle(self, en: bool):
315
+ """enables spreadcycle (1) or stealthchop (0)
316
+
317
+ Args:
318
+ en (bool): true to enable spreadcycle; false to enable stealthchop
319
+
320
+ """
321
+ self.gconf.modify("en_spreadcycle", en)
322
+
323
+ def set_microstepping_resolution(self, mres: int):
324
+ """sets the current native microstep resolution (1,2,4,8,16,32,64,128,256)
325
+
326
+ Args:
327
+ mres (int): µstep resolution; has to be a power of 2 or 1 for fullstep
328
+ """
329
+ super().set_microstepping_resolution(mres)
330
+ self.set_mstep_resolution_reg_select(True)
331
+
332
+ def set_mstep_resolution_reg_select(self, en: bool):
333
+ """sets the register bit "mstep_reg_select" to 1 or 0 depending to the given value.
334
+ this is needed to set the microstep resolution via UART
335
+ this method is called by "set_microstepping_resolution"
336
+
337
+ Args:
338
+ en (bool): true to set µstep resolution via UART
339
+ """
340
+ self.gconf.modify("mstep_reg_select", en)
341
+
342
+ def get_interface_transmission_counter(self) -> int:
343
+ """reads the interface transmission counter from the tmc register
344
+ this value is increased on every succesfull write access
345
+ can be used to verify a write access
346
+
347
+ Returns:
348
+ int: 8bit IFCNT Register
349
+ """
350
+ self.ifcnt.read()
351
+ ifcnt = self.ifcnt.ifcnt
352
+ self.tmc_logger.log(f"Interface Transmission Counter: {ifcnt}", Loglevel.INFO)
353
+ return ifcnt
354
+
355
+ def get_tstep(self) -> int:
356
+ """reads the current tstep from the driver register
357
+
358
+ Returns:
359
+ int: TStep time
360
+ """
361
+ self.tstep.read()
362
+ return self.tstep.tstep
363
+
364
+ def set_tpwmthrs(self, tpwmthrs: int):
365
+ """sets the current tpwmthrs
366
+
367
+ Args:
368
+ tpwmthrs (int): value for tpwmthrs
369
+ """
370
+ self.tpwmthrs.tpwmthrs = tpwmthrs
371
+ self.tpwmthrs.write_check()
372
+
373
+ def set_vactual(self, vactual: int):
374
+ """sets the register bit "VACTUAL" to to a given value
375
+ VACTUAL allows moving the motor by UART control.
376
+ It gives the motor velocity in +-(2^23)-1 [μsteps / t]
377
+ 0: Normal operation. Driver reacts to STEP input
378
+
379
+ Args:
380
+ vactual (int): value for VACTUAL
381
+ """
382
+ self.vactual.vactual = vactual
383
+ self.vactual.write_check()