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.

tmc_driver/tmc_2240.py ADDED
@@ -0,0 +1,801 @@
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
+ """Tmc220X stepper driver module
12
+
13
+ this module has two different functions:
14
+ 1. change setting in the TMC-driver via UART
15
+ 2. move the motor via STEP/DIR pins
16
+ """
17
+
18
+ import logging
19
+ import time
20
+ import types
21
+ from ._tmc_stepperdriver import *
22
+ from .com._tmc_com import TmcCom
23
+ from .com._tmc_com_uart import TmcComUart
24
+ from .com._tmc_com_spi import TmcComSpi
25
+ from ._tmc_gpio_board import GpioPUD
26
+ from .motion_control._tmc_mc_step_reg import TmcMotionControlStepReg
27
+ from .enable_control._tmc_ec_toff import TmcEnableControlToff
28
+ from .motion_control._tmc_mc_vactual import TmcMotionControlVActual
29
+ from ._tmc_logger import TmcLogger, Loglevel
30
+ from .reg._tmc224x_reg import *
31
+ from . import _tmc_math as tmc_math
32
+
33
+
34
+
35
+
36
+
37
+ class Tmc2240(TmcStepperDriver):
38
+ """Tmc220X
39
+
40
+ this class has two different functions:
41
+ 1. change setting in the TMC-driver via UART
42
+ 2. move the motor via STEP/DIR pins
43
+ """
44
+
45
+ tmc_com:TmcComSpi = None
46
+
47
+ _pin_stallguard:int = None
48
+ _sg_callback:types.FunctionType = None
49
+ _sg_threshold:int = 100 # threshold for stallguard
50
+
51
+
52
+ # Constructor/Destructor
53
+ # ----------------------------
54
+ def __init__(self,
55
+ tmc_ec:TmcEnableControl,
56
+ tmc_mc:TmcMotionControl,
57
+ tmc_com:TmcCom = None,
58
+ driver_address:int = 0,
59
+ gpio_mode = None,
60
+ loglevel:Loglevel = Loglevel.INFO,
61
+ logprefix:str = None,
62
+ log_handlers:list = None,
63
+ log_formatter:logging.Formatter = None
64
+ ):
65
+ """constructor
66
+
67
+ Args:
68
+ tmc_ec (TmcEnableControl): enable control object
69
+ tmc_mc (TmcMotionControl): motion control object
70
+ tmc_com (TmcCom, optional): communication object. Defaults to None.
71
+ driver_address (int, optional): driver address [0-3]. Defaults to 0.
72
+ gpio_mode (enum, optional): gpio mode. Defaults to None.
73
+ loglevel (enum, optional): loglevel. Defaults to None.
74
+ logprefix (str, optional): log prefix (name of the logger).
75
+ Defaults to None (standard TMC prefix).
76
+ log_handlers (list, optional): list of logging handlers.
77
+ Defaults to None (log to console).
78
+ log_formatter (logging.Formatter, optional): formatter for the log messages.
79
+ Defaults to None (messages are logged in the format
80
+ '%(asctime)s - %(name)s - %(levelname)s - %(message)s').
81
+ """
82
+ super().__init__(tmc_ec, tmc_mc, gpio_mode, loglevel, logprefix, log_handlers, log_formatter)
83
+
84
+ self.tmc_logger.set_logprefix(f"TMC2240 {driver_address}")
85
+
86
+ if tmc_com is not None:
87
+ self.tmc_com = tmc_com
88
+ self.tmc_com.tmc_logger = self.tmc_logger
89
+ self.tmc_com.mtr_id = driver_address
90
+
91
+ self.tmc_com.init()
92
+
93
+ if hasattr(self.tmc_mc, "tmc_com"):
94
+ self.tmc_mc.tmc_com = tmc_com
95
+
96
+ if hasattr(self.tmc_ec, "tmc_com"):
97
+ self.tmc_ec.tmc_com = tmc_com
98
+
99
+ registers_classes = {
100
+ GConf,
101
+ GStat,
102
+ IfCnt,
103
+ Ioin,
104
+ DrvConf,
105
+ GlobalScaler,
106
+ IHoldIRun,
107
+ TPowerDown,
108
+ TStep,
109
+ THigh,
110
+ ADCVSupplyAIN,
111
+ ADCTemp,
112
+ ChopConf,
113
+ CoolConf,
114
+ DrvStatus,
115
+ TCoolThrs,
116
+ SgThrs,
117
+ SgResult,
118
+ SgInd
119
+ }
120
+
121
+ self.tmc_registers = {}
122
+
123
+ for register_class in registers_classes:
124
+ register = register_class(self.tmc_com)
125
+ name = register.name.lower()
126
+ self.tmc_registers[name] = register
127
+
128
+ def create_getter(name):
129
+ def getter(self):
130
+ return self.tmc_registers[name]
131
+ return getter
132
+
133
+ setattr(self.__class__, name, property(create_getter(name)))
134
+
135
+
136
+ if tmc_com is not None:
137
+ # Setup Registers
138
+ self.tmc_com.tmc_registers = self.tmc_registers
139
+
140
+ if self.tmc_mc is not None:
141
+ self.read_steps_per_rev()
142
+ self.clear_gstat()
143
+ self.tmc_com.flush_serial_buffer()
144
+
145
+
146
+ self.max_speed_fullstep = 100
147
+ self.acceleration_fullstep = 100
148
+
149
+ self.tmc_logger.log("TMC2240 Init finished", Loglevel.INFO)
150
+
151
+
152
+
153
+ def __del__(self):
154
+ """destructor"""
155
+ if self.tmc_com is not None:
156
+ del self.tmc_com
157
+ super().__del__()
158
+
159
+
160
+
161
+ def set_deinitialize_true(self):
162
+ """set deinitialize to true"""
163
+ self._deinit_finished = True
164
+
165
+
166
+ # Tmc220x methods
167
+ # ----------------------------
168
+ def read_steps_per_rev(self) -> int:
169
+ """returns how many steps are needed for one revolution.
170
+ this reads the value from the tmc driver.
171
+
172
+ Returns:
173
+ int: Steps per revolution
174
+ """
175
+ self.read_microstepping_resolution()
176
+ return self.tmc_mc.steps_per_rev
177
+
178
+
179
+
180
+ def read_drv_status(self) -> DrvStatus:
181
+ """read the register Adress "DRV_STATUS" and logs the reg valuess
182
+
183
+ Returns:
184
+ DRV_STATUS Register instance
185
+ """
186
+ self.drvstatus.read()
187
+ self.drvstatus.log(self.tmc_logger)
188
+ return self.drvstatus
189
+
190
+
191
+
192
+ def read_gconf(self) -> GConf:
193
+ """read the register Adress "GCONF" and logs the reg values
194
+
195
+ Returns:
196
+ GCONF Register instance
197
+ """
198
+ self.gconf.read()
199
+ self.gconf.log(self.tmc_logger)
200
+ return self.gconf
201
+
202
+
203
+
204
+ def read_gstat(self) -> GStat:
205
+ """read the register Adress "GSTAT" and logs the reg values
206
+
207
+ Returns:
208
+ GSTAT Register instance
209
+ """
210
+ self.gstat.read()
211
+ self.gstat.log(self.tmc_logger)
212
+ return self.gstat
213
+
214
+
215
+
216
+ def clear_gstat(self):
217
+ """clears the "GSTAT" register"""
218
+ self.tmc_logger.log("clearing GSTAT", Loglevel.INFO)
219
+ self.gstat.read()
220
+
221
+ self.gstat.reset = True
222
+ self.gstat.drv_err = True
223
+ self.gstat.uv_cp = True
224
+
225
+ self.gstat.write_check()
226
+
227
+
228
+
229
+ def read_ioin(self) -> Ioin:
230
+ """read the register Adress "IOIN" and logs the reg values
231
+
232
+ Returns:
233
+ IOIN Register instance
234
+ """
235
+ self.ioin.read()
236
+ self.ioin.log(self.tmc_logger)
237
+ return self.ioin
238
+
239
+
240
+
241
+ def read_chopconf(self) -> ChopConf:
242
+ """read the register Adress "CHOPCONF" and logs the reg values
243
+
244
+ Returns:
245
+ CHOPCONF Register instance
246
+ """
247
+ self.chopconf.read()
248
+ self.chopconf.log(self.tmc_logger)
249
+ return self.chopconf
250
+
251
+
252
+
253
+ def get_direction_reg(self) -> bool:
254
+ """returns the motor shaft direction: False = CCW; True = CW
255
+
256
+ Returns:
257
+ bool: motor shaft direction: False = CCW; True = CW
258
+ """
259
+ self.gconf.read()
260
+ return self.gconf.shaft
261
+
262
+
263
+
264
+ def set_direction_reg(self, direction:bool):
265
+ """sets the motor shaft direction to the given value: False = CCW; True = CW
266
+
267
+ Args:
268
+ direction (bool): direction of the motor False = CCW; True = CW
269
+ """
270
+ self.gconf.modify("shaft", direction)
271
+
272
+
273
+
274
+ def _set_irun_ihold(self, ihold:int, irun:int, ihold_delay:int, irun_delay:int):
275
+ """sets the current scale (CS) for Running and Holding
276
+ and the delay, when to be switched to Holding current
277
+
278
+ Args:
279
+ ihold (int): multiplicator for current while standstill [0-31]
280
+ irun (int): current while running [0-31]
281
+ ihold_delay (int): delay after standstill for switching to ihold [0-15]
282
+
283
+ """
284
+ self.ihold_irun.read()
285
+
286
+ self.ihold_irun.ihold = ihold
287
+ self.ihold_irun.irun = irun
288
+ self.ihold_irun.iholddelay = ihold_delay
289
+ self.ihold_irun.irundelay = irun_delay
290
+
291
+ self.ihold_irun.write_check()
292
+
293
+
294
+
295
+ def _set_global_scaler(self, scaler:int):
296
+ """sets the global scaler
297
+
298
+ Args:
299
+ scaler (int): global scaler value
300
+ """
301
+ self.global_scaler.global_scaler = scaler
302
+ self.global_scaler.write_check()
303
+
304
+
305
+
306
+ def _set_current_range(self, current_range:int):
307
+ """sets the current range
308
+
309
+ 0x0 = 1 A
310
+ 0x1 = 2 A
311
+ 0x2 = 3 A
312
+ 0x3 = 3 A (maximum of driver)
313
+
314
+ Args:
315
+ current_range (int): current range in A
316
+ """
317
+ if current_range > 0:
318
+ current_range -= 1
319
+ self.drv_conf.current_range = current_range
320
+ self.drv_conf.modify("current_range", current_range)
321
+
322
+
323
+
324
+ def set_current(self, run_current:int, hold_current_multiplier:float = 0.5,
325
+ hold_current_delay:int = 10, run_current_delay:int = 0):
326
+ """sets the current flow for the motor.
327
+
328
+ Args:
329
+ run_current (int): current during movement in mA
330
+ hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
331
+ hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
332
+ """
333
+ self.tmc_logger.log(F"Desired current: {run_current} mA", Loglevel.DEBUG)
334
+
335
+ # rdson = 0.23 # 230 mOhm
336
+
337
+ current_range_a = math.ceil(run_current/1000)
338
+
339
+ current_range_a = min(current_range_a, 3)
340
+ current_range_a = max(current_range_a, 0)
341
+
342
+ current_range_ma = current_range_a * 1000
343
+
344
+ self.tmc_logger.log(F"current_range: {current_range_a} A | {current_range_ma} mA", Loglevel.DEBUG)
345
+ self._set_current_range(current_range_a)
346
+
347
+ # 256 == 0 -> max current
348
+ global_scaler = round(run_current / current_range_ma * 256)
349
+
350
+ global_scaler = min(global_scaler, 256)
351
+ global_scaler = max(global_scaler, 0)
352
+
353
+ self.tmc_logger.log(F"global_scaler: {global_scaler}", Loglevel.DEBUG)
354
+ self._set_global_scaler(global_scaler)
355
+
356
+ ct_current_ma = round(current_range_ma * global_scaler / 256)
357
+ self.tmc_logger.log(F"Calculated theoretical current after gscaler: {ct_current_ma} mA", Loglevel.DEBUG)
358
+
359
+
360
+ cs_irun = round(run_current / ct_current_ma * 31)
361
+
362
+ cs_irun = min(cs_irun, 31)
363
+ cs_irun = max(cs_irun, 0)
364
+
365
+ cs_ihold = hold_current_multiplier * cs_irun
366
+
367
+ cs_irun = round(cs_irun)
368
+ cs_ihold = round(cs_ihold)
369
+ hold_current_delay = round(hold_current_delay)
370
+ run_current_delay = round(run_current_delay)
371
+
372
+ self.tmc_logger.log(f"CS_IRun: {cs_irun}", Loglevel.DEBUG)
373
+ self.tmc_logger.log(f"CS_IHold: {cs_ihold}", Loglevel.DEBUG)
374
+ self.tmc_logger.log(f"IHold_Delay: {hold_current_delay}", Loglevel.DEBUG)
375
+ self.tmc_logger.log(f"IRun_Delay: {run_current_delay}", Loglevel.DEBUG)
376
+
377
+ self._set_irun_ihold(cs_ihold, cs_irun, hold_current_delay, run_current_delay)
378
+
379
+ ct_current_ma = round(ct_current_ma * cs_irun / 31)
380
+ self.tmc_logger.log(F"Calculated theoretical final current: {ct_current_ma} mA", Loglevel.INFO)
381
+
382
+
383
+
384
+ def get_spreadcycle(self) -> bool:
385
+ """reads spreadcycle
386
+
387
+ Returns:
388
+ bool: True = spreadcycle; False = stealthchop
389
+ """
390
+ self.gconf.read()
391
+ return not self.gconf.en_pwm_mode
392
+
393
+
394
+
395
+ def set_spreadcycle(self,en:bool):
396
+ """enables spreadcycle (1) or stealthchop (0)
397
+
398
+ Args:
399
+ en (bool): true to enable spreadcycle; false to enable stealthchop
400
+
401
+ """
402
+ self.gconf.modify("en_pwm_mode", not en)
403
+
404
+
405
+
406
+ def get_interpolation(self) -> bool:
407
+ """return whether the tmc inbuilt interpolation is active
408
+
409
+ Returns:
410
+ en (bool): true if internal µstep interpolation is enabled
411
+ """
412
+ self.chopconf.read()
413
+ return self.chopconf.intpol
414
+
415
+
416
+
417
+ def set_interpolation(self, en:bool):
418
+ """enables the tmc inbuilt interpolation of the steps to 256 µsteps
419
+
420
+ Args:
421
+ en (bool): true to enable internal µstep interpolation
422
+ """
423
+ self.chopconf.modify("intpol", en)
424
+
425
+
426
+
427
+ def get_toff(self) -> int:
428
+ """returns the TOFF register value
429
+
430
+ Returns:
431
+ int: TOFF register value
432
+ """
433
+ self.chopconf.read()
434
+ return self.chopconf.toff
435
+
436
+
437
+
438
+ def set_toff(self, toff:int):
439
+ """Sets TOFF register to value
440
+
441
+ Args:
442
+ toff (uint8_t): value of toff (must be a four-bit value)
443
+ """
444
+ self.chopconf.modify("toff", toff)
445
+
446
+
447
+
448
+ def read_microstepping_resolution(self) -> int:
449
+ """returns the current native microstep resolution (1-256)
450
+ this reads the value from the driver register
451
+
452
+ Returns:
453
+ int: µstep resolution
454
+ """
455
+ self.chopconf.read()
456
+
457
+ mres = self.chopconf.mres_ms
458
+ if self.tmc_mc is not None:
459
+ self.tmc_mc.mres = mres
460
+
461
+ return mres
462
+
463
+
464
+
465
+ def get_microstepping_resolution(self) -> int:
466
+ """returns the current native microstep resolution (1-256)
467
+ this returns the cached value from this module
468
+
469
+ Returns:
470
+ int: µstep resolution
471
+ """
472
+ return self.tmc_mc.mres
473
+
474
+
475
+
476
+ def set_microstepping_resolution(self, mres:int):
477
+ """sets the current native microstep resolution (1,2,4,8,16,32,64,128,256)
478
+
479
+ Args:
480
+ mres (int): µstep resolution; has to be a power of 2 or 1 for fullstep
481
+ """
482
+ if self.tmc_mc is not None:
483
+ self.tmc_mc.mres = mres
484
+
485
+ self.chopconf.read()
486
+ self.chopconf.mres_ms = mres
487
+ self.chopconf.write_check()
488
+
489
+
490
+
491
+ def get_interface_transmission_counter(self) -> int:
492
+ """reads the interface transmission counter from the tmc register
493
+ this value is increased on every succesfull write access
494
+ can be used to verify a write access
495
+
496
+ Returns:
497
+ int: 8bit IFCNT Register
498
+ """
499
+ self.ifcnt.read()
500
+ ifcnt = self.ifcnt.ifcnt
501
+ self.tmc_logger.log(f"Interface Transmission Counter: {ifcnt}", Loglevel.INFO)
502
+ return ifcnt
503
+
504
+
505
+
506
+ def get_tstep(self) -> int:
507
+ """reads the current tstep from the driver register
508
+
509
+ Returns:
510
+ int: TStep time
511
+ """
512
+ self.tstep.read()
513
+ return self.tstep.tstep
514
+
515
+
516
+
517
+ def set_vactual(self, vactual:int):
518
+ """sets the register bit "VACTUAL" to to a given value
519
+ VACTUAL allows moving the motor by UART control.
520
+ It gives the motor velocity in +-(2^23)-1 [μsteps / t]
521
+ 0: Normal operation. Driver reacts to STEP input
522
+
523
+ Args:
524
+ vactual (int): value for VACTUAL
525
+ """
526
+ self.vactual.vactual = vactual
527
+ self.vactual.write_check()
528
+
529
+
530
+
531
+ def get_microstep_counter(self) -> int:
532
+ """returns the current Microstep counter.
533
+ Indicates actual position in the microstep table for CUR_A
534
+
535
+ Returns:
536
+ int: current Microstep counter
537
+ """
538
+ self.mscnt.read()
539
+ return self.mscnt.mscnt
540
+
541
+
542
+
543
+ def get_microstep_counter_in_steps(self, offset:int=0) -> int:
544
+ """returns the current Microstep counter.
545
+ Indicates actual position in the microstep table for CUR_A
546
+
547
+ Args:
548
+ offset (int): offset in steps (Default value = 0)
549
+
550
+ Returns:
551
+ step (int): current Microstep counter convertet to steps
552
+ """
553
+ step = (self.get_microstep_counter()-64)*(self.tmc_mc.mres*4)/1024
554
+ step = (4*self.tmc_mc.mres)-step-1
555
+ step = round(step)
556
+ return step+offset
557
+
558
+
559
+
560
+ def set_stallguard_callback(self, pin_stallguard, threshold, callback,
561
+ min_speed = 100):
562
+ """set a function to call back, when the driver detects a stall
563
+ via stallguard
564
+ high value on the diag pin can also mean a driver error
565
+
566
+ Args:
567
+ pin_stallguard (int): pin needs to be connected to DIAG
568
+ threshold (int): value for SGTHRS
569
+ callback (func): will be called on StallGuard trigger
570
+ min_speed (int): min speed [steps/s] for StallGuard (Default value = 100)
571
+ """
572
+ self.tmc_logger.log(f"setup stallguard callback on GPIO {pin_stallguard}", Loglevel.INFO)
573
+ self.tmc_logger.log(f"StallGuard Threshold: {threshold} | minimum Speed: {min_speed}", Loglevel.INFO)
574
+
575
+ self._set_stallguard_threshold(threshold)
576
+ self._set_coolstep_threshold(tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution()))
577
+ self._sg_callback = callback
578
+ self._pin_stallguard = pin_stallguard
579
+
580
+ tmc_gpio.gpio_setup(self._pin_stallguard, GpioMode.IN, pull_up_down=GpioPUD.PUD_DOWN)
581
+ # first remove existing events
582
+ tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
583
+ tmc_gpio.gpio_add_event_detect(self._pin_stallguard, self.stallguard_callback)
584
+
585
+
586
+
587
+ def stallguard_callback(self, gpio_pin):
588
+ """the callback function for StallGuard.
589
+ only checks whether the duration of the current movement is longer than
590
+ _sg_delay and then calls the actual callback
591
+
592
+ Args:
593
+ gpio_pin (int): pin number of the interrupt pin
594
+ """
595
+ del gpio_pin
596
+ if self._sg_callback is None:
597
+ self.tmc_logger.log("StallGuard callback is None", Loglevel.DEBUG)
598
+ return
599
+ self._sg_callback()
600
+
601
+
602
+
603
+ def _set_coolstep_threshold(self, threshold):
604
+ """This is the lower threshold velocity for switching
605
+ on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
606
+
607
+ Args:
608
+ threshold (int): threshold velocity for coolstep
609
+ """
610
+ self.tcoolthrs.modify("tcoolthrs", threshold)
611
+
612
+
613
+
614
+ def enable_coolstep(self, semin_sg:int = 150, semax_sg:int = 200, seup:int = 1, sedn:int = 3, min_speed:int = 100):
615
+ """enables coolstep and sets the parameters for coolstep
616
+ The values for semin etc. can be tested with the test_stallguard_threshold function
617
+
618
+ Args:
619
+ semin_sg (int): lower threshold. Current will be increased if SG_Result goes below this
620
+ semax_sg (int): upper threshold. Current will be decreased if SG_Result goes above this
621
+ seup (int): current increment step
622
+ sedn (int): number of SG_Result readings for each current decrement
623
+ """
624
+ semax_sg = semax_sg - semin_sg
625
+
626
+ self.coolconf.read()
627
+ self.coolconf.semin = round(max(0, min(semin_sg/32, 15)))
628
+ self.coolconf.semax = round(max(0, min(semax_sg/32, 15)))
629
+ self.coolconf.seimin = 1 # scale down to until 1/4 of IRun (7 - 31)
630
+ self.coolconf.seup = seup
631
+ self.coolconf.sedn = sedn
632
+ self.coolconf.write_check()
633
+
634
+ self._set_coolstep_threshold(tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution()))
635
+
636
+
637
+
638
+ def get_stallguard_result(self):
639
+ """return the current stallguard result
640
+ its will be calculated with every fullstep
641
+ higher values means a lower motor load
642
+
643
+ Returns:
644
+ sg_result (int): StallGuard Result
645
+ """
646
+ self.sg_result.read()
647
+ return self.sg_result.sg_result
648
+
649
+
650
+
651
+ def _set_stallguard_threshold(self, threshold):
652
+ """sets the register bit "SGTHRS" to to a given value
653
+ this is needed for the stallguard interrupt callback
654
+ SG_RESULT becomes compared to the double of this threshold.
655
+ SG_RESULT ≤ SGTHRS*2
656
+
657
+ Args:
658
+ threshold (int): value for SGTHRS
659
+ """
660
+ self.sg_thrs.modify("sg_thrs", threshold)
661
+
662
+
663
+
664
+ def test_stallguard_threshold(self, steps):
665
+ """test method for tuning stallguard threshold
666
+
667
+ run this function with your motor settings and your motor load
668
+ the function will determine the minimum stallguard results for each movement phase
669
+
670
+ Args:
671
+ steps (int):
672
+ """
673
+
674
+ self.tmc_logger.log("---", Loglevel.INFO)
675
+ self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO)
676
+
677
+ self.set_spreadcycle(False)
678
+
679
+ min_stallguard_result_accel = 512
680
+ min_stallguard_result_maxspeed = 512
681
+ min_stallguard_result_decel = 512
682
+
683
+ self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)
684
+
685
+
686
+ while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL:
687
+ self.drvstatus.read()
688
+ stallguard_result = self.drvstatus.sg_result
689
+ stallguard_triggered = self.drvstatus.stallguard
690
+ cs_actual = self.drvstatus.cs_actual
691
+ # stallguard_result = self.get_stallguard_result()
692
+
693
+ self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result} | {stallguard_triggered} | {cs_actual}",
694
+ Loglevel.INFO)
695
+
696
+ if (self.tmc_mc.movement_phase == MovementPhase.ACCELERATING and
697
+ stallguard_result < min_stallguard_result_accel):
698
+ min_stallguard_result_accel = stallguard_result
699
+ if (self.tmc_mc.movement_phase == MovementPhase.MAXSPEED and
700
+ stallguard_result < min_stallguard_result_maxspeed):
701
+ min_stallguard_result_maxspeed = stallguard_result
702
+ if (self.tmc_mc.movement_phase == MovementPhase.DECELERATING and
703
+ stallguard_result < min_stallguard_result_decel):
704
+ min_stallguard_result_decel = stallguard_result
705
+
706
+ self.tmc_mc.wait_for_movement_finished_threaded()
707
+
708
+ self.tmc_logger.log("---", Loglevel.INFO)
709
+ self.tmc_logger.log(f"min StallGuard result during accel: {min_stallguard_result_accel}",
710
+ Loglevel.INFO)
711
+ self.tmc_logger.log(f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}",
712
+ Loglevel.INFO)
713
+ self.tmc_logger.log(f"min StallGuard result during decel: {min_stallguard_result_decel}",
714
+ Loglevel.INFO)
715
+ self.tmc_logger.log("---", Loglevel.INFO)
716
+
717
+
718
+
719
+ def get_vsupply(self) -> int:
720
+ """reads the ADC_VSUPPLY_AIN register
721
+
722
+ Returns:
723
+ int: ADC_VSUPPLY_AIN register value
724
+ """
725
+ self.adcv_supply_ain.read()
726
+ return self.adcv_supply_ain.adc_vsupply_v
727
+
728
+
729
+
730
+ def get_temperature(self) -> float:
731
+ """reads the ADC_TEMP register and returns the temperature
732
+
733
+ Returns:
734
+ float: temperature in °C
735
+ """
736
+ self.adc_temp.read()
737
+ return self.adc_temp.adc_temp_c
738
+
739
+
740
+
741
+ def test_pin(self, pin, ioin_reg_bp):
742
+ """tests one pin
743
+
744
+ this function checks the connection to a pin
745
+ by toggling it and reading the IOIN register
746
+ """
747
+ pin_ok = True
748
+
749
+ # turn on all pins
750
+ tmc_gpio.gpio_output(self.tmc_mc.pin_dir, Gpio.HIGH)
751
+ tmc_gpio.gpio_output(self.tmc_mc.pin_step, Gpio.HIGH)
752
+ tmc_gpio.gpio_output(self.tmc_ec.pin_en, Gpio.HIGH)
753
+
754
+ # check that the selected pin is on
755
+ ioin = self.read_ioin()
756
+ if not ioin.data_int >> ioin_reg_bp & 0x1:
757
+ pin_ok = False
758
+
759
+ # turn off only the selected pin
760
+ tmc_gpio.gpio_output(pin, Gpio.LOW)
761
+ time.sleep(0.1)
762
+
763
+ # check that the selected pin is off
764
+ ioin = self.read_ioin()
765
+ if ioin.data_int >> ioin_reg_bp & 0x1:
766
+ pin_ok = False
767
+
768
+ return pin_ok
769
+
770
+
771
+
772
+ def test_dir_step_en(self):
773
+ """tests the EN, DIR and STEP pin
774
+
775
+ this sets the EN, DIR and STEP pin to HIGH, LOW and HIGH
776
+ and checks the IOIN Register of the TMC meanwhile
777
+ """
778
+ # test each pin on their own
779
+
780
+ pin_dir_ok = self.test_pin(self.tmc_mc.pin_dir, 1)
781
+ pin_step_ok = self.test_pin(self.tmc_mc.pin_step, 0)
782
+ pin_en_ok = self.test_pin(self.tmc_ec.pin_en, 4)
783
+
784
+ self.set_motor_enabled(False)
785
+
786
+ self.tmc_logger.log("---")
787
+ self.tmc_logger.log(f"Pin DIR: \t{'OK' if pin_dir_ok else 'not OK'}")
788
+ self.tmc_logger.log(f"Pin STEP: \t{'OK' if pin_step_ok else 'not OK'}")
789
+ self.tmc_logger.log(f"Pin EN: \t{'OK' if pin_en_ok else 'not OK'}")
790
+ self.tmc_logger.log("---")
791
+
792
+
793
+
794
+ def test_com(self):
795
+ """test method"""
796
+ self.tmc_logger.log("---")
797
+ self.tmc_logger.log("TEST COM")
798
+
799
+ ioin = Ioin(self.tmc_com)
800
+
801
+ return self.tmc_com.test_com(ioin.addr)