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_2240.py
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
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_spi_base import TmcComSpiBase
|
|
18
|
+
from .com._tmc_com_uart_base import TmcComUartBase
|
|
19
|
+
from .motion_control._tmc_mc_step_reg import TmcMotionControlStepDir
|
|
20
|
+
from .motion_control._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
21
|
+
from .motion_control._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
22
|
+
from .enable_control._tmc_ec_toff import TmcEnableControlToff
|
|
23
|
+
from .enable_control._tmc_ec_pin import TmcEnableControlPin
|
|
24
|
+
from ._tmc_stallguard import StallGuard
|
|
25
|
+
from .tmc_logger import *
|
|
26
|
+
from .reg._tmc224x_reg import *
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Tmc2240(TmcXXXX, StallGuard):
|
|
30
|
+
"""Tmc2240"""
|
|
31
|
+
|
|
32
|
+
SUPPORTED_COM_TYPES = (TmcComSpiBase, TmcComUartBase)
|
|
33
|
+
SUPPORTED_EC_TYPES = (TmcEnableControlToff, TmcEnableControlPin)
|
|
34
|
+
SUPPORTED_MC_TYPES = (
|
|
35
|
+
TmcMotionControlStepDir,
|
|
36
|
+
TmcMotionControlStepReg,
|
|
37
|
+
TmcMotionControlStepPwmDir,
|
|
38
|
+
)
|
|
39
|
+
DRIVER_FAMILY = "TMC2240"
|
|
40
|
+
|
|
41
|
+
# Constructor/Destructor
|
|
42
|
+
# ----------------------------
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
tmc_ec: TmcEnableControl,
|
|
46
|
+
tmc_mc: TmcMotionControl,
|
|
47
|
+
tmc_com: TmcCom | None = None,
|
|
48
|
+
driver_address: int = 0,
|
|
49
|
+
gpio_mode=None,
|
|
50
|
+
loglevel: Loglevel = Loglevel.INFO,
|
|
51
|
+
logprefix: str | None = None,
|
|
52
|
+
log_handlers: list | None = None,
|
|
53
|
+
log_formatter: logging.Formatter | None = None,
|
|
54
|
+
):
|
|
55
|
+
"""constructor
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
tmc_ec (TmcEnableControl): enable control object
|
|
59
|
+
tmc_mc (TmcMotionControl): motion control object
|
|
60
|
+
tmc_com (TmcCom, optional): communication object. Defaults to None.
|
|
61
|
+
driver_address (int, optional): driver address [0-3]. Defaults to 0.
|
|
62
|
+
gpio_mode (enum, optional): gpio mode. Defaults to None.
|
|
63
|
+
loglevel (enum, optional): loglevel. Defaults to None.
|
|
64
|
+
logprefix (str, optional): log prefix (name of the logger).
|
|
65
|
+
Defaults to None (standard TMC prefix).
|
|
66
|
+
log_handlers (list, optional): list of logging handlers.
|
|
67
|
+
Defaults to None (log to console).
|
|
68
|
+
log_formatter (logging.Formatter, optional): formatter for the log messages.
|
|
69
|
+
Defaults to None (messages are logged in the format
|
|
70
|
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
|
|
71
|
+
"""
|
|
72
|
+
super().__init__(
|
|
73
|
+
tmc_ec,
|
|
74
|
+
tmc_mc,
|
|
75
|
+
tmc_com,
|
|
76
|
+
driver_address,
|
|
77
|
+
gpio_mode,
|
|
78
|
+
loglevel,
|
|
79
|
+
logprefix,
|
|
80
|
+
log_handlers,
|
|
81
|
+
log_formatter,
|
|
82
|
+
)
|
|
83
|
+
StallGuard.__init__(self)
|
|
84
|
+
|
|
85
|
+
if self.tmc_com is not None:
|
|
86
|
+
|
|
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.drv_conf: DrvConf = DrvConf(self.tmc_com)
|
|
92
|
+
self.global_scaler: GlobalScaler = GlobalScaler(self.tmc_com)
|
|
93
|
+
self.ihold_irun: IHoldIRun = IHoldIRun(self.tmc_com)
|
|
94
|
+
self.tpowerdown: TPowerDown = TPowerDown(self.tmc_com)
|
|
95
|
+
self.tstep: TStep = TStep(self.tmc_com)
|
|
96
|
+
self.tpwmthrs: TPwmThrs = TPwmThrs(self.tmc_com)
|
|
97
|
+
self.thigh: THigh = THigh(self.tmc_com)
|
|
98
|
+
self.adcv_supply_ain: ADCVSupplyAIN = ADCVSupplyAIN(self.tmc_com)
|
|
99
|
+
self.adc_temp: ADCTemp = ADCTemp(self.tmc_com)
|
|
100
|
+
self.mscnt: MsCnt = MsCnt(self.tmc_com)
|
|
101
|
+
self.chopconf: ChopConf = ChopConf(self.tmc_com)
|
|
102
|
+
self.coolconf: CoolConf = CoolConf(self.tmc_com)
|
|
103
|
+
self.drvstatus: DrvStatus = DrvStatus(self.tmc_com)
|
|
104
|
+
self.tcoolthrs: TCoolThrs = TCoolThrs(self.tmc_com)
|
|
105
|
+
self.sgthrs: SgThrs = SgThrs(self.tmc_com)
|
|
106
|
+
self.sgresult: SgResult = SgResult(self.tmc_com)
|
|
107
|
+
self.sgind: SgInd = SgInd(self.tmc_com)
|
|
108
|
+
|
|
109
|
+
super()._init()
|
|
110
|
+
|
|
111
|
+
def deinit(self):
|
|
112
|
+
"""destructor"""
|
|
113
|
+
super().deinit()
|
|
114
|
+
StallGuard.deinit(self)
|
|
115
|
+
|
|
116
|
+
# Register Access
|
|
117
|
+
# ----------------------------
|
|
118
|
+
def _set_irun_ihold(self, ihold: int, irun: int, iholddelay: int, irundelay: int):
|
|
119
|
+
"""sets the current scale (CS) for Running and Holding
|
|
120
|
+
and the delay, when to be switched to Holding current
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
ihold (int): multiplicator for current while standstill [0-31]
|
|
124
|
+
irun (int): current while running [0-31]
|
|
125
|
+
iholddelay (int): delay after standstill for switching to ihold [0-15]
|
|
126
|
+
|
|
127
|
+
"""
|
|
128
|
+
self.ihold_irun.read()
|
|
129
|
+
|
|
130
|
+
self.ihold_irun.ihold = ihold
|
|
131
|
+
self.ihold_irun.irun = irun
|
|
132
|
+
self.ihold_irun.iholddelay = iholddelay
|
|
133
|
+
self.ihold_irun.irundelay = irundelay
|
|
134
|
+
|
|
135
|
+
self.ihold_irun.write_check()
|
|
136
|
+
|
|
137
|
+
def _set_global_scaler(self, scaler: int):
|
|
138
|
+
"""sets the global scaler
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
scaler (int): global scaler value
|
|
142
|
+
"""
|
|
143
|
+
self.global_scaler.global_scaler = scaler
|
|
144
|
+
self.global_scaler.write_check()
|
|
145
|
+
|
|
146
|
+
def _set_current_range(self, current_range: int):
|
|
147
|
+
"""sets the current range
|
|
148
|
+
|
|
149
|
+
0 = 1 A
|
|
150
|
+
1 = 2 A
|
|
151
|
+
2 = 3 A
|
|
152
|
+
3 = 3 A (maximum of driver)
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
current_range (int): current range in A
|
|
156
|
+
"""
|
|
157
|
+
self.drv_conf.current_range = current_range
|
|
158
|
+
self.drv_conf.modify("current_range", current_range)
|
|
159
|
+
|
|
160
|
+
def set_current_peak(
|
|
161
|
+
self,
|
|
162
|
+
run_current: int,
|
|
163
|
+
hold_current_multiplier: float = 0.5,
|
|
164
|
+
hold_current_delay: int = 10,
|
|
165
|
+
run_current_delay: int = 0,
|
|
166
|
+
rref: int = 12,
|
|
167
|
+
) -> int:
|
|
168
|
+
"""sets the Peak current for the motor.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
run_current (int): current during movement in mA
|
|
172
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
173
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
174
|
+
run_current_delay (int): delay after movement start after which cur rises (Default value = 0)
|
|
175
|
+
rref (int): reference resistor in kOhm (Default value = 12)
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
int: theoretical final current in mA
|
|
179
|
+
"""
|
|
180
|
+
self.tmc_logger.log(f"Desired peak current: {run_current} mA", Loglevel.DEBUG)
|
|
181
|
+
|
|
182
|
+
K_IFS_TABLE = [11.75, 24, 36, 36] # A*kOhm
|
|
183
|
+
current_fs_table = [k_ifs / rref * 1000 for k_ifs in K_IFS_TABLE]
|
|
184
|
+
|
|
185
|
+
current_range_reg_value = 3
|
|
186
|
+
for i, current_fs in enumerate(current_fs_table):
|
|
187
|
+
if run_current < current_fs:
|
|
188
|
+
current_range_reg_value = i
|
|
189
|
+
break
|
|
190
|
+
|
|
191
|
+
current_fs = current_fs_table[current_range_reg_value]
|
|
192
|
+
|
|
193
|
+
self.tmc_logger.log(
|
|
194
|
+
f"current_fs: {current_fs:.0f} mA | {current_fs/1000:.1f} A", Loglevel.DEBUG
|
|
195
|
+
)
|
|
196
|
+
self._set_current_range(current_range_reg_value)
|
|
197
|
+
|
|
198
|
+
# 256 == 0 -> max current
|
|
199
|
+
global_scaler = round(run_current / current_fs * 256)
|
|
200
|
+
|
|
201
|
+
global_scaler = min(global_scaler, 256)
|
|
202
|
+
global_scaler = max(global_scaler, 0)
|
|
203
|
+
|
|
204
|
+
self.tmc_logger.log(f"global_scaler: {global_scaler}", Loglevel.DEBUG)
|
|
205
|
+
self._set_global_scaler(global_scaler)
|
|
206
|
+
|
|
207
|
+
ct_current_ma = round(current_fs * global_scaler / 256)
|
|
208
|
+
self.tmc_logger.log(
|
|
209
|
+
f"Calculated theoretical peak current after gscaler: {ct_current_ma} mA",
|
|
210
|
+
Loglevel.DEBUG,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
cs_irun = round(run_current / ct_current_ma * 31)
|
|
214
|
+
|
|
215
|
+
cs_irun = min(cs_irun, 31)
|
|
216
|
+
cs_irun = max(cs_irun, 0)
|
|
217
|
+
|
|
218
|
+
cs_ihold = hold_current_multiplier * cs_irun
|
|
219
|
+
|
|
220
|
+
cs_irun = round(cs_irun)
|
|
221
|
+
cs_ihold = round(cs_ihold)
|
|
222
|
+
hold_current_delay = round(hold_current_delay)
|
|
223
|
+
run_current_delay = round(run_current_delay)
|
|
224
|
+
|
|
225
|
+
self.tmc_logger.log(f"CS_IRun: {cs_irun}", Loglevel.DEBUG)
|
|
226
|
+
self.tmc_logger.log(f"CS_IHold: {cs_ihold}", Loglevel.DEBUG)
|
|
227
|
+
self.tmc_logger.log(f"IHold_Delay: {hold_current_delay}", Loglevel.DEBUG)
|
|
228
|
+
self.tmc_logger.log(f"IRun_Delay: {run_current_delay}", Loglevel.DEBUG)
|
|
229
|
+
|
|
230
|
+
self._set_irun_ihold(cs_ihold, cs_irun, hold_current_delay, run_current_delay)
|
|
231
|
+
|
|
232
|
+
ct_current_ma = round(ct_current_ma * cs_irun / 31)
|
|
233
|
+
self.tmc_logger.log(
|
|
234
|
+
f"Calculated theoretical final current: {ct_current_ma} mA", Loglevel.INFO
|
|
235
|
+
)
|
|
236
|
+
return ct_current_ma
|
|
237
|
+
|
|
238
|
+
def set_current_rms(
|
|
239
|
+
self,
|
|
240
|
+
run_current: int,
|
|
241
|
+
hold_current_multiplier: float = 0.5,
|
|
242
|
+
hold_current_delay: int = 10,
|
|
243
|
+
run_current_delay: int = 0,
|
|
244
|
+
rref: int = 12,
|
|
245
|
+
) -> int:
|
|
246
|
+
"""sets the RMS current for the motor.
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
run_current (int): current during movement in mA
|
|
250
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
251
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
252
|
+
run_current_delay (int): delay after movement start after which cur rises (Default value = 0)
|
|
253
|
+
rref (int): reference resistor in kOhm (Default value = 12)
|
|
254
|
+
|
|
255
|
+
Returns:
|
|
256
|
+
int: theoretical final current in mA
|
|
257
|
+
"""
|
|
258
|
+
peak_current = self.set_current_peak(
|
|
259
|
+
round(run_current * 1.41421),
|
|
260
|
+
hold_current_multiplier,
|
|
261
|
+
hold_current_delay,
|
|
262
|
+
run_current_delay,
|
|
263
|
+
rref,
|
|
264
|
+
)
|
|
265
|
+
return round(peak_current / 1.41421)
|
|
266
|
+
|
|
267
|
+
def get_spreadcycle(self) -> bool:
|
|
268
|
+
"""reads spreadcycle
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
bool: True = spreadcycle; False = stealthchop
|
|
272
|
+
"""
|
|
273
|
+
self.gconf.read()
|
|
274
|
+
return not self.gconf.en_pwm_mode
|
|
275
|
+
|
|
276
|
+
def set_spreadcycle(self, en: bool):
|
|
277
|
+
"""enables spreadcycle (1) or stealthchop (0)
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
en (bool): true to enable spreadcycle; false to enable stealthchop
|
|
281
|
+
|
|
282
|
+
"""
|
|
283
|
+
self.gconf.modify("en_pwm_mode", not en)
|
|
284
|
+
|
|
285
|
+
def get_interface_transmission_counter(self) -> int:
|
|
286
|
+
"""reads the interface transmission counter from the tmc register
|
|
287
|
+
this value is increased on every succesfull write access
|
|
288
|
+
can be used to verify a write access
|
|
289
|
+
|
|
290
|
+
Returns:
|
|
291
|
+
int: 8bit IFCNT Register
|
|
292
|
+
"""
|
|
293
|
+
self.ifcnt.read()
|
|
294
|
+
ifcnt = self.ifcnt.ifcnt
|
|
295
|
+
self.tmc_logger.log(f"Interface Transmission Counter: {ifcnt}", Loglevel.INFO)
|
|
296
|
+
return ifcnt
|
|
297
|
+
|
|
298
|
+
def get_tstep(self) -> int:
|
|
299
|
+
"""reads the current tstep from the driver register
|
|
300
|
+
|
|
301
|
+
Returns:
|
|
302
|
+
int: TStep time
|
|
303
|
+
"""
|
|
304
|
+
self.tstep.read()
|
|
305
|
+
return self.tstep.tstep
|
|
306
|
+
|
|
307
|
+
def get_vsupply(self) -> float:
|
|
308
|
+
"""reads the ADC_VSUPPLY_AIN register
|
|
309
|
+
|
|
310
|
+
Returns:
|
|
311
|
+
int: ADC_VSUPPLY_AIN register value
|
|
312
|
+
"""
|
|
313
|
+
self.adcv_supply_ain.read()
|
|
314
|
+
return self.adcv_supply_ain.adc_vsupply_v
|
|
315
|
+
|
|
316
|
+
def get_temperature(self) -> float:
|
|
317
|
+
"""reads the ADC_TEMP register and returns the temperature
|
|
318
|
+
|
|
319
|
+
Returns:
|
|
320
|
+
float: temperature in °C
|
|
321
|
+
"""
|
|
322
|
+
self.adc_temp.read()
|
|
323
|
+
return self.adc_temp.adc_temp_c
|
|
324
|
+
|
|
325
|
+
# TMC224x methods
|
|
326
|
+
# ----------------------------
|
|
327
|
+
def stallguard_setup(
|
|
328
|
+
self,
|
|
329
|
+
threshold: int,
|
|
330
|
+
min_speed: int,
|
|
331
|
+
enable: bool = True,
|
|
332
|
+
):
|
|
333
|
+
"""internal setup for stallguard
|
|
334
|
+
Args:
|
|
335
|
+
threshold (int): value for SGTHRS
|
|
336
|
+
min_speed (int): min speed [steps/s] for StallGuard
|
|
337
|
+
"""
|
|
338
|
+
super().stallguard_setup(threshold, min_speed)
|
|
339
|
+
self.gconf.modify("diag0_stall", enable)
|
|
340
|
+
self.gconf.modify("diag0_pushpull", enable)
|
tmc_driver/tmc_5160.py
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
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
|
+
"""Tmc5160 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_spi_base import TmcComSpiBase
|
|
18
|
+
from .com._tmc_com_uart_base import TmcComUartBase
|
|
19
|
+
from .motion_control._tmc_mc_step_reg import TmcMotionControlStepDir
|
|
20
|
+
from .motion_control._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
21
|
+
from .motion_control._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
22
|
+
from .motion_control._tmc_mc_int_ramp_generator import TmcMotionControlIntRampGenerator
|
|
23
|
+
from .enable_control._tmc_ec_toff import TmcEnableControlToff
|
|
24
|
+
from .enable_control._tmc_ec_pin import TmcEnableControlPin
|
|
25
|
+
from ._tmc_stallguard import StallGuard
|
|
26
|
+
from .tmc_logger import *
|
|
27
|
+
from .reg._tmc5160_reg import *
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Tmc5160(TmcXXXX, StallGuard):
|
|
31
|
+
"""Tmc5160"""
|
|
32
|
+
|
|
33
|
+
SUPPORTED_COM_TYPES = (TmcComSpiBase, TmcComUartBase)
|
|
34
|
+
SUPPORTED_EC_TYPES = (TmcEnableControlToff, TmcEnableControlPin)
|
|
35
|
+
SUPPORTED_MC_TYPES = (
|
|
36
|
+
TmcMotionControlStepDir,
|
|
37
|
+
TmcMotionControlStepReg,
|
|
38
|
+
TmcMotionControlStepPwmDir,
|
|
39
|
+
TmcMotionControlIntRampGenerator,
|
|
40
|
+
)
|
|
41
|
+
DRIVER_FAMILY = "TMC5160"
|
|
42
|
+
|
|
43
|
+
# Constructor/Destructor
|
|
44
|
+
# ----------------------------
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
tmc_ec: TmcEnableControl,
|
|
48
|
+
tmc_mc: TmcMotionControl,
|
|
49
|
+
tmc_com: TmcCom | None = None,
|
|
50
|
+
driver_address: int = 0,
|
|
51
|
+
gpio_mode=None,
|
|
52
|
+
loglevel: Loglevel = Loglevel.INFO,
|
|
53
|
+
logprefix: str | None = None,
|
|
54
|
+
log_handlers: list | None = None,
|
|
55
|
+
log_formatter: logging.Formatter | None = None,
|
|
56
|
+
):
|
|
57
|
+
"""constructor
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
tmc_ec (TmcEnableControl): enable control object
|
|
61
|
+
tmc_mc (TmcMotionControl): motion control object
|
|
62
|
+
tmc_com (TmcCom, optional): communication object. Defaults to None.
|
|
63
|
+
driver_address (int, optional): driver address [0-3]. Defaults to 0.
|
|
64
|
+
gpio_mode (enum, optional): gpio mode. Defaults to None.
|
|
65
|
+
loglevel (enum, optional): loglevel. Defaults to None.
|
|
66
|
+
logprefix (str, optional): log prefix (name of the logger).
|
|
67
|
+
Defaults to None (standard TMC prefix).
|
|
68
|
+
log_handlers (list, optional): list of logging handlers.
|
|
69
|
+
Defaults to None (log to console).
|
|
70
|
+
log_formatter (logging.Formatter, optional): formatter for the log messages.
|
|
71
|
+
Defaults to None (messages are logged in the format
|
|
72
|
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
|
|
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
|
+
StallGuard.__init__(self)
|
|
86
|
+
|
|
87
|
+
if self.tmc_com is not None:
|
|
88
|
+
|
|
89
|
+
self.gconf: GConf = GConf(self.tmc_com)
|
|
90
|
+
self.gstat: GStat = GStat(self.tmc_com)
|
|
91
|
+
self.ifcnt: IfCnt = IfCnt(self.tmc_com)
|
|
92
|
+
self.ioin: Ioin = Ioin(self.tmc_com)
|
|
93
|
+
self.drv_conf: DrvConf = DrvConf(self.tmc_com)
|
|
94
|
+
self.global_scaler: GlobalScaler = GlobalScaler(self.tmc_com)
|
|
95
|
+
self.ihold_irun: IHoldIRun = IHoldIRun(self.tmc_com)
|
|
96
|
+
self.tpowerdown: TPowerDown = TPowerDown(self.tmc_com)
|
|
97
|
+
self.tstep: TStep = TStep(self.tmc_com)
|
|
98
|
+
self.tpwmthrs: TPwmThrs = TPwmThrs(self.tmc_com)
|
|
99
|
+
self.thigh: THigh = THigh(self.tmc_com)
|
|
100
|
+
self.rampmode: RampMode = RampMode(self.tmc_com)
|
|
101
|
+
self.xactual: XActual = XActual(self.tmc_com)
|
|
102
|
+
self.vactual: VActual = VActual(self.tmc_com)
|
|
103
|
+
self.vstart: VStart = VStart(self.tmc_com)
|
|
104
|
+
self.a1: A1 = A1(self.tmc_com)
|
|
105
|
+
self.v1: V1 = V1(self.tmc_com)
|
|
106
|
+
self.amax: AMax = AMax(self.tmc_com)
|
|
107
|
+
self.vmax: VMax = VMax(self.tmc_com)
|
|
108
|
+
self.dmax: DMax = DMax(self.tmc_com)
|
|
109
|
+
self.d1: D1 = D1(self.tmc_com)
|
|
110
|
+
self.vstop: VStop = VStop(self.tmc_com)
|
|
111
|
+
self.tzerowait: TZeroWait = TZeroWait(self.tmc_com)
|
|
112
|
+
self.xtarget: XTarget = XTarget(self.tmc_com)
|
|
113
|
+
self.vdcmin: VDcMin = VDcMin(self.tmc_com)
|
|
114
|
+
self.swmode: SWMode = SWMode(self.tmc_com)
|
|
115
|
+
self.rampstat: RampStat = RampStat(self.tmc_com)
|
|
116
|
+
self.mscnt: MsCnt = MsCnt(self.tmc_com)
|
|
117
|
+
self.chopconf: ChopConf = ChopConf(self.tmc_com)
|
|
118
|
+
self.coolconf: CoolConf = CoolConf(self.tmc_com)
|
|
119
|
+
self.drvstatus: DrvStatus = DrvStatus(self.tmc_com)
|
|
120
|
+
self.tcoolthrs: TCoolThrs = TCoolThrs(self.tmc_com)
|
|
121
|
+
self.loststeps: LostSteps = LostSteps(self.tmc_com)
|
|
122
|
+
|
|
123
|
+
super()._init()
|
|
124
|
+
|
|
125
|
+
def deinit(self):
|
|
126
|
+
"""destructor"""
|
|
127
|
+
super().deinit()
|
|
128
|
+
StallGuard.deinit(self)
|
|
129
|
+
|
|
130
|
+
# Register Access
|
|
131
|
+
# ----------------------------
|
|
132
|
+
def _set_irun_ihold(self, ihold: int, irun: int, iholddelay: int):
|
|
133
|
+
"""sets the current scale (CS) for Running and Holding
|
|
134
|
+
and the delay, when to be switched to Holding current
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
ihold (int): multiplicator for current while standstill [0-31]
|
|
138
|
+
irun (int): current while running [0-31]
|
|
139
|
+
iholddelay (int): delay after standstill for switching to ihold [0-15]
|
|
140
|
+
|
|
141
|
+
"""
|
|
142
|
+
self.ihold_irun.read()
|
|
143
|
+
|
|
144
|
+
self.ihold_irun.ihold = ihold
|
|
145
|
+
self.ihold_irun.irun = irun
|
|
146
|
+
self.ihold_irun.iholddelay = iholddelay
|
|
147
|
+
|
|
148
|
+
self.ihold_irun.write_check()
|
|
149
|
+
|
|
150
|
+
def _set_global_scaler(self, scaler: int):
|
|
151
|
+
"""sets the global scaler
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
scaler (int): global scaler value
|
|
155
|
+
"""
|
|
156
|
+
self.global_scaler.global_scaler = scaler
|
|
157
|
+
self.global_scaler.write_check()
|
|
158
|
+
|
|
159
|
+
def set_current_peak(
|
|
160
|
+
self,
|
|
161
|
+
run_current: int,
|
|
162
|
+
hold_current_multiplier: float = 0.5,
|
|
163
|
+
hold_current_delay: int = 10,
|
|
164
|
+
) -> int:
|
|
165
|
+
"""sets the Peak current for the motor.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
run_current (int): current during movement in mA
|
|
169
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
170
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
171
|
+
run_current_delay (int): delay after movement start after which cur rises (Default value = 0)
|
|
172
|
+
rref (int): reference resistor in kOhm (Default value = 12)
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
int: theoretical final current in mA
|
|
176
|
+
"""
|
|
177
|
+
self.tmc_logger.log(f"Desired peak current: {run_current} mA", Loglevel.DEBUG)
|
|
178
|
+
|
|
179
|
+
rsense = 0.075 # ohm
|
|
180
|
+
vfs = 0.325 # V
|
|
181
|
+
|
|
182
|
+
current_fs = vfs / (rsense) * 1000 # in mA
|
|
183
|
+
|
|
184
|
+
self.tmc_logger.log(
|
|
185
|
+
f"current_fs: {current_fs:.0f} mA | {current_fs/1000:.1f} A", Loglevel.DEBUG
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# 256 == 0 -> max current
|
|
189
|
+
global_scaler = round(run_current / current_fs * 256)
|
|
190
|
+
|
|
191
|
+
global_scaler = min(global_scaler, 256)
|
|
192
|
+
global_scaler = max(global_scaler, 0)
|
|
193
|
+
|
|
194
|
+
self.tmc_logger.log(f"global_scaler: {global_scaler}", Loglevel.DEBUG)
|
|
195
|
+
self._set_global_scaler(global_scaler)
|
|
196
|
+
|
|
197
|
+
ct_current_ma = round(current_fs * global_scaler / 256)
|
|
198
|
+
self.tmc_logger.log(
|
|
199
|
+
f"Calculated theoretical peak current after gscaler: {ct_current_ma} mA",
|
|
200
|
+
Loglevel.DEBUG,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
cs_irun = round(run_current / ct_current_ma * 31)
|
|
204
|
+
|
|
205
|
+
cs_irun = min(cs_irun, 31)
|
|
206
|
+
cs_irun = max(cs_irun, 0)
|
|
207
|
+
|
|
208
|
+
cs_ihold = hold_current_multiplier * cs_irun
|
|
209
|
+
|
|
210
|
+
cs_irun = round(cs_irun)
|
|
211
|
+
cs_ihold = round(cs_ihold)
|
|
212
|
+
hold_current_delay = round(hold_current_delay)
|
|
213
|
+
|
|
214
|
+
self.tmc_logger.log(f"CS_IRun: {cs_irun}", Loglevel.DEBUG)
|
|
215
|
+
self.tmc_logger.log(f"CS_IHold: {cs_ihold}", Loglevel.DEBUG)
|
|
216
|
+
self.tmc_logger.log(f"IHold_Delay: {hold_current_delay}", Loglevel.DEBUG)
|
|
217
|
+
|
|
218
|
+
self._set_irun_ihold(cs_ihold, cs_irun, hold_current_delay)
|
|
219
|
+
|
|
220
|
+
ct_current_ma = round(ct_current_ma * cs_irun / 31)
|
|
221
|
+
self.tmc_logger.log(
|
|
222
|
+
f"Calculated theoretical final current: {ct_current_ma} mA", Loglevel.INFO
|
|
223
|
+
)
|
|
224
|
+
return ct_current_ma
|
|
225
|
+
|
|
226
|
+
def set_current_rms(
|
|
227
|
+
self,
|
|
228
|
+
run_current: int,
|
|
229
|
+
hold_current_multiplier: float = 0.5,
|
|
230
|
+
hold_current_delay: int = 10,
|
|
231
|
+
) -> int:
|
|
232
|
+
"""sets the RMS current for the motor.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
run_current (int): current during movement in mA
|
|
236
|
+
hold_current_multiplier (int):current multiplier during standstill (Default value = 0.5)
|
|
237
|
+
hold_current_delay (int): delay after standstill after which cur drops (Default value = 10)
|
|
238
|
+
|
|
239
|
+
Returns:
|
|
240
|
+
int: theoretical final current in mA
|
|
241
|
+
"""
|
|
242
|
+
peak_current = self.set_current_peak(
|
|
243
|
+
round(run_current * 1.41421),
|
|
244
|
+
hold_current_multiplier,
|
|
245
|
+
hold_current_delay,
|
|
246
|
+
)
|
|
247
|
+
return round(peak_current / 1.41421)
|
|
248
|
+
|
|
249
|
+
def get_spreadcycle(self) -> bool:
|
|
250
|
+
"""reads spreadcycle
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
bool: True = spreadcycle; False = stealthchop
|
|
254
|
+
"""
|
|
255
|
+
self.gconf.read()
|
|
256
|
+
return not self.gconf.en_pwm_mode
|
|
257
|
+
|
|
258
|
+
def set_spreadcycle(self, en: bool):
|
|
259
|
+
"""enables spreadcycle (1) or stealthchop (0)
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
en (bool): true to enable spreadcycle; false to enable stealthchop
|
|
263
|
+
|
|
264
|
+
"""
|
|
265
|
+
self.gconf.modify("en_pwm_mode", not en)
|
|
266
|
+
|
|
267
|
+
def get_interface_transmission_counter(self) -> int:
|
|
268
|
+
"""reads the interface transmission counter from the tmc register
|
|
269
|
+
this value is increased on every succesfull write access
|
|
270
|
+
can be used to verify a write access
|
|
271
|
+
|
|
272
|
+
Returns:
|
|
273
|
+
int: 8bit IFCNT Register
|
|
274
|
+
"""
|
|
275
|
+
self.ifcnt.read()
|
|
276
|
+
ifcnt = self.ifcnt.ifcnt
|
|
277
|
+
self.tmc_logger.log(f"Interface Transmission Counter: {ifcnt}", Loglevel.INFO)
|
|
278
|
+
return ifcnt
|
|
279
|
+
|
|
280
|
+
def get_tstep(self) -> int:
|
|
281
|
+
"""reads the current tstep from the driver register
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
int: TStep time
|
|
285
|
+
"""
|
|
286
|
+
self.tstep.read()
|
|
287
|
+
return self.tstep.tstep
|
|
288
|
+
|
|
289
|
+
# TMC5160 methods
|
|
290
|
+
# ----------------------------
|
|
291
|
+
def stallguard_setup(
|
|
292
|
+
self,
|
|
293
|
+
threshold: int,
|
|
294
|
+
min_speed: int,
|
|
295
|
+
enable: bool = True,
|
|
296
|
+
):
|
|
297
|
+
"""internal setup for stallguard
|
|
298
|
+
|
|
299
|
+
TMC5160 has only StallGuard2 which only works with Spreadcycle enabled
|
|
300
|
+
If you want to use StallGuard afterwards call this function again to disable Spreadcycle
|
|
301
|
+
and reset coolstep threshold
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
threshold (int): value for SGT [-64 to 63] higher = less sensitive
|
|
305
|
+
min_speed (int): min speed [steps/s] for StallGuard
|
|
306
|
+
enable (bool): enable stallguard (True) or disable (False)
|
|
307
|
+
"""
|
|
308
|
+
# self.set_spreadcycle(enable)
|
|
309
|
+
|
|
310
|
+
if not enable:
|
|
311
|
+
min_speed = 0
|
|
312
|
+
|
|
313
|
+
self._set_coolstep_threshold(
|
|
314
|
+
tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution())
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
self.coolconf.read()
|
|
318
|
+
self.coolconf.sgt = threshold
|
|
319
|
+
self.coolconf.write_check()
|
|
320
|
+
|
|
321
|
+
self.swmode.read()
|
|
322
|
+
self.swmode.sg_stop = enable
|
|
323
|
+
self.swmode.write_check()
|
|
324
|
+
|
|
325
|
+
self.gconf.modify("diag0_stall", enable)
|
|
326
|
+
self.gconf.modify("diag0_pushpull", enable)
|
|
327
|
+
|
|
328
|
+
def clear_rampstat(self):
|
|
329
|
+
"""clears the rampstat register
|
|
330
|
+
use after a stallguard stop to clear the flag
|
|
331
|
+
If the flag is not cleared, further movements are not possible
|
|
332
|
+
"""
|
|
333
|
+
time.sleep(0.1)
|
|
334
|
+
self.rampstat.clear()
|
|
335
|
+
time.sleep(0.1)
|
|
336
|
+
|
|
337
|
+
def reset_position(self):
|
|
338
|
+
"""resets the current position to 0
|
|
339
|
+
additionally resets the xtarget register to 0
|
|
340
|
+
"""
|
|
341
|
+
super().reset_position()
|
|
342
|
+
|
|
343
|
+
self.xtarget.xtarget = 0
|
|
344
|
+
self.xtarget.write_check()
|