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.

@@ -0,0 +1,351 @@
1
+ #pylint: disable=wildcard-import
2
+ #pylint: disable=unused-wildcard-import
3
+ #pylint: disable=unused-import
4
+ #pylint: disable=attribute-defined-outside-init
5
+ #pylint: disable=no-member
6
+ """
7
+ Register module
8
+ """
9
+
10
+ import math
11
+ from ._tmc_reg import *
12
+
13
+
14
+ class GConf(TmcReg):
15
+ """GCONF register class"""
16
+
17
+ def __init__(self, tmc_com: TmcCom):
18
+ """constructor"""
19
+
20
+ reg_map = [
21
+ ["direct_mode", 16, 0x1, bool, None, ""],
22
+ ["stop_enable", 15, 0x1, bool, None, ""],
23
+ ["small_hysteresis", 14, 0x1, bool, None, ""],
24
+ ["diag1_pushpull", 13, 0x1, bool, None, ""],
25
+ ["diag0_pushpull", 12, 0x1, bool, None, ""],
26
+ ["diag1_onstate", 10, 0x1, bool, None, ""],
27
+ ["diag1_index", 9, 0x1, bool, None, ""],
28
+ ["diag1_stall", 8, 0x1, bool, None, ""],
29
+ ["diag0_stall", 7, 0x1, bool, None, ""],
30
+ ["diag0_otpw", 6, 0x1, bool, None, ""],
31
+ ["diag0_error", 5, 0x1, bool, None, ""],
32
+ ["shaft", 4, 0x1, bool, None, ""],
33
+ ["multistep_filt", 3, 0x1, bool, None, ""],
34
+ ["en_pwm_mode", 2, 0x1, bool, None, ""],
35
+ ["fast_standstill", 1, 0x1, bool, None, ""]
36
+ ]
37
+ super().__init__(0x0, "GCONF", tmc_com, reg_map)
38
+
39
+
40
+ class GStat(TmcReg):
41
+ """GSTAT register class"""
42
+
43
+ def __init__(self, tmc_com: TmcCom):
44
+ """constructor"""
45
+
46
+ reg_map = [
47
+ ["vm_uvlo", 4, 0x1, bool, None, ""],
48
+ ["register_reset", 3, 0x1, bool, None, ""],
49
+ ["uv_cp", 2, 0x1, bool, None, ""],
50
+ ["drv_err", 1, 0x1, bool, None, ""],
51
+ ["reset", 0, 0x1, bool, None, ""]
52
+ ]
53
+ super().__init__(0x1, "GSTAT", tmc_com, reg_map)
54
+
55
+
56
+ class IfCnt(TmcReg):
57
+ """IFCNT register class"""
58
+
59
+ def __init__(self, tmc_com: TmcCom):
60
+ """constructor"""
61
+
62
+ reg_map = [
63
+ ["ifcnt", 0, 0xFF, int, None, ""]
64
+ ]
65
+ super().__init__(0x2, "IFCNT", tmc_com, reg_map)
66
+
67
+
68
+ class Ioin(TmcReg):
69
+ """IOIN register class"""
70
+
71
+ def __init__(self, tmc_com: TmcCom):
72
+ """constructor"""
73
+
74
+ reg_map = [
75
+ ["version", 24, 0xFF, int, None, ""],
76
+ ["silicon_rv", 24, 0xFF, int, None, ""],
77
+ ["adc_err", 15, 0x1, bool, None, ""],
78
+ ["ext_clk", 14, 0x1, bool, None, ""],
79
+ ["ext_res_det", 13, 0x1, bool, None, ""],
80
+ ["output", 12, 0x1, bool, None, ""],
81
+ ["comp_b1_b2", 11, 0x1, bool, None, ""],
82
+ ["comp_a1_a2", 10, 0x1, bool, None, ""],
83
+ ["comp_b", 9, 0x1, bool, None, ""],
84
+ ["comp_a", 8, 0x1, bool, None, ""],
85
+ ["uart_en", 6, 0x1, bool, None, ""],
86
+ ["encn", 5, 0x1, bool, None, ""],
87
+ ["drv_enn", 4, 0x1, bool, None, ""],
88
+ ["enca", 3, 0x1, bool, None, ""],
89
+ ["encb", 2, 0x1, bool, None, ""],
90
+ ["dir", 1, 0x1, bool, None, ""],
91
+ ["step", 0, 0x1, bool, None, ""]
92
+ ]
93
+ super().__init__(0x4, "IOIN", tmc_com, reg_map)
94
+
95
+
96
+ class DrvConf(TmcReg):
97
+ """DRV_CONF register class"""
98
+
99
+ def __init__(self, tmc_com: TmcCom):
100
+ """constructor"""
101
+
102
+ reg_map = [
103
+ ["slope_control", 4, 0x3, int, None, ""],
104
+ ["current_range", 0, 0x3, int, None, ""],
105
+ ]
106
+ super().__init__(0x6, "DRV_CONF", tmc_com, reg_map)
107
+
108
+
109
+ class GlobalScaler(TmcReg):
110
+ """GLOBAL_SCALER register class"""
111
+
112
+ def __init__(self, tmc_com: TmcCom):
113
+ """constructor"""
114
+
115
+ reg_map = [
116
+ ["global_scaler", 0, 0xFF, int, None, ""]
117
+ ]
118
+ super().__init__(0xB, "GLOBAL_SCALER", tmc_com, reg_map)
119
+
120
+
121
+ class IHoldIRun(TmcReg):
122
+ """IHOLD_IRUN register class"""
123
+
124
+ def __init__(self, tmc_com: TmcCom):
125
+ """constructor"""
126
+
127
+ reg_map = [
128
+ ["irundelay", 24, 0xF, int, None, ""],
129
+ ["iholddelay", 16, 0xF, int, None, ""],
130
+ ["irun", 8, 0x1F, int, None, ""],
131
+ ["ihold", 0, 0x1F, int, None, ""]
132
+ ]
133
+ super().__init__(0x10, "IHOLD_IRUN", tmc_com, reg_map)
134
+
135
+
136
+ class TPowerDown(TmcReg):
137
+ """TPOWERDOWN register class"""
138
+
139
+ def __init__(self, tmc_com: TmcCom):
140
+ """constructor"""
141
+
142
+ reg_map = [
143
+ ["tpowerdown", 0, 0xFF, int, None, ""]
144
+ ]
145
+ super().__init__(0x11, "TPOWERDOWN", tmc_com, reg_map)
146
+
147
+
148
+ class TStep(TmcReg):
149
+ """TSTEP register class"""
150
+
151
+ def __init__(self, tmc_com: TmcCom):
152
+ """constructor"""
153
+
154
+ reg_map = [
155
+ ["tstep", 0, 0xFFFFF, int, None, ""]
156
+ ]
157
+ super().__init__(0x12, "TSTEP", tmc_com, reg_map)
158
+
159
+
160
+ class THigh(TmcReg):
161
+ """THIGH register class"""
162
+
163
+ def __init__(self, tmc_com: TmcCom):
164
+ """constructor"""
165
+
166
+ reg_map = [
167
+ ["thigh", 0, 0xFFFFF, int, None, ""]
168
+ ]
169
+ super().__init__(0x13, "THIGH", tmc_com, reg_map)
170
+
171
+
172
+ class ADCVSupplyAIN(TmcReg):
173
+ """ADCV_SUPPLY_AIN register class"""
174
+
175
+ def __init__(self, tmc_com: TmcCom):
176
+ """constructor"""
177
+
178
+ reg_map = [
179
+ ["adc_ain", 16, 0xFFFF, int, lambda: self.adc_ain_v, "V"],
180
+ ["adc_vsupply", 0, 0xFFFF, int, lambda: self.adc_vsupply_v, "V"]
181
+ ]
182
+ super().__init__(0x50, "ADCV_SUPPLY_AIN", tmc_com, reg_map)
183
+
184
+ @property
185
+ def adc_vsupply_v(self) -> float:
186
+ """return Supplyvoltage in V"""
187
+ if hasattr(self, "adc_vsupply"):
188
+ return round(self.adc_vsupply * 9.732/1000, 2)
189
+ return None
190
+
191
+ @property
192
+ def adc_ain_v(self) -> float:
193
+ """return voltage on AIN in V"""
194
+ if hasattr(self, "adc_ain"):
195
+ return round(self.adc_ain * 305.2/1000/1000, 2)
196
+ return None
197
+
198
+
199
+ class ADCTemp(TmcReg):
200
+ """ADC_TEMP register class"""
201
+
202
+ def __init__(self, tmc_com: TmcCom):
203
+ """constructor"""
204
+
205
+ reg_map = [
206
+ ["adc_temp", 0, 0xFFFF, int, lambda: self.adc_temp_c, "°C"]
207
+ ]
208
+ super().__init__(0x51, "ADC_TEMP", tmc_com, reg_map)
209
+
210
+ @property
211
+ def adc_temp_c(self) -> float:
212
+ """return temperature in °C"""
213
+ if hasattr(self, "adc_temp"):
214
+ return round((self.adc_temp - 2038) / 7.7, 1)
215
+ return None
216
+
217
+
218
+ class ChopConf(TmcReg):
219
+ """CHOPCONF register class"""
220
+
221
+ def __init__(self, tmc_com: TmcCom):
222
+ """constructor"""
223
+
224
+ reg_map = [
225
+ ["diss2vs", 31, 0x1, bool, None, ""],
226
+ ["diss2g", 30, 0x1, bool, None, ""],
227
+ ["dedge", 29, 0x1, bool, None, ""],
228
+ ["intpol", 28, 0x1, bool, None, ""],
229
+ ["mres", 24, 0xF, int, lambda: self.mres_ms, "µStep"],
230
+ ["tpfd", 20, 0xF, int, None, ""],
231
+ ["vhighchm", 19, 0x1, bool, None, ""],
232
+ ["vhighfs", 18, 0x1, bool, None, ""],
233
+ ["tbl", 15, 0x3, int, None, ""],
234
+ ["chm", 14, 0x3, int, None, ""],
235
+ ["disfdcc", 12, 0x1, bool, None, ""],
236
+ ["fd3", 11, 0x1, bool, None, ""],
237
+ ["hend_offset", 7, 0xF, int, None, ""],
238
+ ["hstrt_tfd210", 4, 0x7, int, None, ""],
239
+ ["toff", 0, 0xF, int, None, ""]
240
+ ]
241
+ super().__init__(0x6C, "CHOPCONF", tmc_com, reg_map)
242
+
243
+ @property
244
+ def mres_ms(self) -> int:
245
+ """return µstep resolution"""
246
+ if hasattr(self, "mres"):
247
+ return int(math.pow(2, 8 - self.mres))
248
+ return None
249
+
250
+ @mres_ms.setter
251
+ def mres_ms(self, mres: int):
252
+ """set µstep resolution"""
253
+ mres_bit = int(math.log(mres, 2))
254
+ mres_bit = 8 - mres_bit
255
+ self.mres = mres_bit
256
+
257
+
258
+ class CoolConf(TmcReg):
259
+ """COOLCONF register class"""
260
+
261
+ def __init__(self, tmc_com: TmcCom):
262
+ """constructor"""
263
+
264
+ reg_map = [
265
+ ["sfilt", 24, 0x1, bool, None, ""],
266
+ ["sgt", 16, 0x7F, int, None, ""],
267
+ ["seimin", 15, 0x1, bool, None, ""],
268
+ ["sedn", 13, 0x3, int, None, ""],
269
+ ["semax", 8, 0xF, int, None, ""],
270
+ ["seup", 5, 0x3, int, None, ""],
271
+ ["semin", 0, 0xF, int, None, ""]
272
+
273
+ ]
274
+ super().__init__(0x6D, "COOLCONF", tmc_com, reg_map)
275
+
276
+
277
+ class DrvStatus(TmcReg):
278
+ """DRVSTATUS register class"""
279
+
280
+ def __init__(self, tmc_com: TmcCom):
281
+ """constructor"""
282
+
283
+ reg_map = [
284
+ ["stst", 31, 0x1, bool, None, ""],
285
+ ["olb", 30, 0x1, bool, None, ""],
286
+ ["ola", 29, 0x1, bool, None, ""],
287
+ ["s2gb", 28, 0x1, bool, None, ""],
288
+ ["s2ga", 27, 0x1, bool, None, ""],
289
+ ["otpw", 26, 0x1, bool, None, ""],
290
+ ["ot", 25, 0x1, bool, None, ""],
291
+ ["stallguard", 24, 0x1, bool, None, ""],
292
+ ["cs_actual", 16, 0x1F, int, None, ""],
293
+ ["fsactive", 15, 0x1, bool, None, ""],
294
+ ["stealth", 14, 0x1, bool, None, ""],
295
+ ["s2vsb", 13, 0x1, bool, None, ""],
296
+ ["s2vsa", 12, 0x1, bool, None, ""],
297
+ ["sg_result", 0, 0x3FF, int, None, ""]
298
+ ]
299
+ super().__init__(0x6F, "DRVSTATUS", tmc_com, reg_map)
300
+
301
+
302
+ class TCoolThrs(TmcReg):
303
+ """TCOOLTHRS register class"""
304
+
305
+ def __init__(self, tmc_com: TmcCom):
306
+ """constructor"""
307
+
308
+ reg_map = [
309
+ ["tcoolthrs", 0, 0xFFFFF, int, None, ""]
310
+ ]
311
+ super().__init__(0x14, "TCOOLTHRS", tmc_com, reg_map)
312
+
313
+
314
+ class SgThrs(TmcReg):
315
+ """SGTHRS register class"""
316
+
317
+ def __init__(self, tmc_com: TmcCom):
318
+ """constructor"""
319
+
320
+ reg_map = [
321
+ ["sg_angle_offset", 9, 0x1, bool, None, ""],
322
+ ["sg4_filt_en", 8, 0x1, bool, None, ""],
323
+ ["sg_thrs", 0, 0xFFF, int, None, ""]
324
+ ]
325
+ super().__init__(0x74, "SG_THRS", tmc_com, reg_map)
326
+
327
+
328
+ class SgResult(TmcReg):
329
+ """SGRESULT register class"""
330
+
331
+ def __init__(self, tmc_com: TmcCom):
332
+ """constructor"""
333
+
334
+ reg_map = [
335
+ ["sg_result", 0, 0xFFFFF, int, None, ""]
336
+ ]
337
+ super().__init__(0x75, "SG_RESULT", tmc_com, reg_map)
338
+
339
+
340
+ class SgInd(TmcReg):
341
+ """SGIND register class"""
342
+
343
+ def __init__(self, tmc_com: TmcCom):
344
+ """constructor"""
345
+
346
+ reg_map = [
347
+ ["sg_ind_2", 16, 0xFF, int, None, ""],
348
+ ["sg_ind_1", 8, 0xFF, int, None, ""],
349
+ ["sg_ind_0", 0, 0xFF, int, None, ""],
350
+ ]
351
+ super().__init__(0x76, "SG_IND", tmc_com, reg_map)
@@ -0,0 +1,121 @@
1
+ #pylint: disable=too-many-instance-attributes
2
+ #pylint: disable=unused-import
3
+ """
4
+ Register module
5
+ """
6
+
7
+ import typing
8
+ from .._tmc_logger import TmcLogger, Loglevel
9
+ from ..com._tmc_com import TmcCom
10
+
11
+
12
+ class TmcReg():
13
+ """Register class"""
14
+
15
+ _addr: hex
16
+ _name: str
17
+ _tmc_com: TmcCom
18
+ _reg_map: typing.List
19
+ _data_int: int
20
+
21
+
22
+ @property
23
+ def addr(self) -> hex:
24
+ """addr property"""
25
+ return self._addr
26
+
27
+ @property
28
+ def name(self) -> str:
29
+ """name property"""
30
+ return self._name
31
+
32
+ @property
33
+ def reg_map(self) -> typing.List:
34
+ """reg_map property"""
35
+ return self._reg_map
36
+
37
+ @property
38
+ def data_int(self) -> int:
39
+ """data_int property"""
40
+ return self._data_int
41
+
42
+
43
+ def __init__(self, address:hex, name:str, tmc_com:TmcCom, reg_map:typing.List):
44
+ """Constructor"""
45
+ self._addr = address
46
+ self._name = name
47
+ self._tmc_com = tmc_com
48
+ self._reg_map = reg_map
49
+
50
+
51
+ def deserialise(self, data:int):
52
+ """Deserialises the register value
53
+
54
+ Args:
55
+ data (int): register value
56
+ """
57
+ self._data_int = data
58
+
59
+ for reg in self._reg_map:
60
+ name, pos, mask, _, _, _ = reg
61
+ value = data >> pos & mask
62
+ setattr(self, name, reg[3](value))
63
+
64
+
65
+ def serialise(self) -> int:
66
+ """Serialises the object to a register value
67
+
68
+ Returns:
69
+ int: register value
70
+ """
71
+ data = 0
72
+
73
+ for reg in self._reg_map:
74
+ name, pos, mask, _, _, _ = reg
75
+ value = getattr(self, name)
76
+ data |= (int(value) & mask) << pos
77
+
78
+ return data
79
+
80
+
81
+ def log(self, logger: TmcLogger):
82
+ """log this register"""
83
+ logger.log(f"{self._name} | {hex(self._addr)} | {bin(self._data_int)}")
84
+
85
+ for reg in self._reg_map:
86
+ name, _, _, _, conv_func, unit = reg
87
+ value = getattr(self, name)
88
+ log_string = f" {name:<20}{value:<10}"
89
+ if conv_func is not None:
90
+ log_string += f" {conv_func()} {unit}"
91
+ logger.log(log_string)
92
+
93
+
94
+ def read(self):
95
+ """read this register"""
96
+ data = self._tmc_com.read_int(self._addr)
97
+ self.deserialise(data)
98
+
99
+
100
+ def write(self):
101
+ """write this register"""
102
+ data = self.serialise()
103
+ self._tmc_com.write_reg(self._addr, data)
104
+
105
+
106
+ def write_check(self):
107
+ """write this register and checks that the write was successful"""
108
+ data = self.serialise()
109
+ self._tmc_com.write_reg_check(self._addr, data)
110
+
111
+
112
+ def modify(self, name:str, value):
113
+ """modify a register value
114
+
115
+ Args:
116
+ name (str): register name
117
+ value: new value
118
+ """
119
+ self.read()
120
+ setattr(self, name, value)
121
+ self.write_check()
tmc_driver/tmc_2208.py ADDED
@@ -0,0 +1,12 @@
1
+ #pylint: disable=no-member
2
+ #pylint: disable=wildcard-import
3
+ #pylint: disable=unused-wildcard-import
4
+ """Tmc2208 stepper driver module
5
+ """
6
+
7
+ from .tmc_220x import *
8
+
9
+
10
+
11
+ class Tmc2208(Tmc220x):
12
+ """Tmc2209"""