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,179 @@
1
+ #pylint: disable=too-many-instance-attributes
2
+ #pylint: disable=too-many-arguments
3
+ #pylint: disable=too-many-public-methods
4
+ #pylint: disable=too-many-branches
5
+ #pylint: disable=too-many-positional-arguments
6
+ """
7
+ VActual Motion Control module
8
+ """
9
+
10
+ import time
11
+ from ._tmc_mc import TmcMotionControl, MovementAbsRel, StopMode
12
+ from ..com._tmc_com import TmcCom
13
+ from .._tmc_logger import Loglevel
14
+ from .. import _tmc_math as tmc_math
15
+
16
+
17
+ class TmcMotionControlVActual(TmcMotionControl):
18
+ """VActual Motion Control class"""
19
+
20
+ _tmc_com:TmcCom = None
21
+
22
+ _starttime:int = 0
23
+
24
+
25
+ @property
26
+ def tmc_com(self):
27
+ """get the tmc_logger"""
28
+ return self._tmc_com
29
+
30
+ @tmc_com.setter
31
+ def tmc_com(self, tmc_com):
32
+ """set the tmc_logger"""
33
+ self._tmc_com = tmc_com
34
+
35
+
36
+
37
+ def make_a_step(self):
38
+ """method that makes on step
39
+ """
40
+ raise NotImplementedError
41
+
42
+
43
+ def run_to_position_steps(self, steps, movement_abs_rel:MovementAbsRel = None):
44
+ """runs the motor to the given position.
45
+ with acceleration and deceleration
46
+ blocks the code until finished or stopped from a different thread!
47
+ returns true when the movement if finished normally and false,
48
+ when the movement was stopped
49
+
50
+ Args:
51
+ steps (int): amount of steps; can be negative
52
+ movement_abs_rel (enum): whether the movement should be absolut or relative
53
+ (Default value = None)
54
+
55
+ Returns:
56
+ stop (enum): how the movement was finished
57
+ """
58
+ rps = tmc_math.steps_to_rps(self.max_speed_fullstep, self.steps_per_rev)
59
+ self.set_vactual_rps(rps, revolutions=steps/self.steps_per_rev)
60
+
61
+
62
+ def set_vactual(self, vactual:int):
63
+ """sets the register bit "VACTUAL" to to a given value
64
+ VACTUAL allows moving the motor by UART control.
65
+ It gives the motor velocity in +-(2^23)-1 [μsteps / t]
66
+ 0: Normal operation. Driver reacts to STEP input
67
+
68
+ Args:
69
+ vactual (int): value for VACTUAL
70
+ """
71
+ self.tmc_com.tmc_registers["vactual"].modify("vactual", vactual)
72
+
73
+
74
+ def set_vactual_dur(self, vactual, duration=0, acceleration=0,
75
+ show_stallguard_result=False, show_tstep=False):
76
+ """sets the register bit "VACTUAL" to to a given value
77
+ VACTUAL allows moving the motor by UART control.
78
+ It gives the motor velocity in +-(2^23)-1 [μsteps / t]
79
+ 0: Normal operation. Driver reacts to STEP input
80
+
81
+ Args:
82
+ vactual (int): value for VACTUAL
83
+ duration (int): after this vactual will be set to 0 (Default value = 0)
84
+ acceleration (int): use this for a velocity ramp (Default value = 0)
85
+ show_stallguard_result (bool): prints StallGuard Result during movement
86
+ (Default value = False)
87
+ show_tstep (bool): prints TStep during movement (Default value = False)
88
+
89
+ Returns:
90
+ stop (enum): how the movement was finished
91
+ """
92
+ self._stop = StopMode.NO
93
+ current_vactual = 0
94
+ sleeptime = 0.05
95
+ time_to_stop = 0
96
+ if vactual<0:
97
+ acceleration = -acceleration
98
+
99
+ if duration != 0:
100
+ self._tmc_logger.log(f"vactual: {vactual} for {duration} sec",
101
+ Loglevel.INFO)
102
+ else:
103
+ self._tmc_logger.log(f"vactual: {vactual}", Loglevel.INFO)
104
+ self._tmc_logger.log(str(bin(vactual)), Loglevel.INFO)
105
+
106
+ self._tmc_logger.log("writing vactual", Loglevel.INFO)
107
+ if acceleration == 0:
108
+ self.set_vactual(int(round(vactual)))
109
+
110
+ if duration == 0:
111
+ return -1
112
+
113
+ self._starttime = time.time()
114
+ current_time = time.time()
115
+ while current_time < self._starttime+duration:
116
+ if self._stop == StopMode.HARDSTOP:
117
+ break
118
+ if acceleration != 0:
119
+ time_to_stop = self._starttime+duration-abs(current_vactual/acceleration)
120
+ if self._stop == StopMode.SOFTSTOP:
121
+ time_to_stop = current_time-1
122
+ if acceleration != 0 and current_time > time_to_stop:
123
+ current_vactual -= acceleration*sleeptime
124
+ self.set_vactual(int(round(current_vactual)))
125
+ time.sleep(sleeptime)
126
+ elif acceleration != 0 and abs(current_vactual)<abs(vactual):
127
+ current_vactual += acceleration*sleeptime
128
+ self.set_vactual(int(round(current_vactual)))
129
+ time.sleep(sleeptime)
130
+ if show_stallguard_result:
131
+ # self._tmc_logger.log(f"StallGuard result: {self.get_stallguard_result()}",
132
+ # Loglevel.INFO)
133
+ time.sleep(0.1)
134
+ if show_tstep:
135
+ # self._tmc_logger.log(f"TStep result: {self.get_tstep()}",
136
+ # Loglevel.INFO)
137
+ time.sleep(0.1)
138
+ current_time = time.time()
139
+ self.set_vactual(0)
140
+ return self._stop
141
+
142
+
143
+ def set_vactual_rps(self, rps, duration=0, revolutions=0, acceleration=0):
144
+ """converts the rps parameter to a vactual value which represents
145
+ rotation speed in revolutions per second
146
+ With internal oscillator:
147
+ VACTUAL[2209] = v[Hz] / 0.715Hz
148
+
149
+ Args:
150
+ rps (int): value for vactual in rps
151
+ duration (int): after this vactual will be set to 0 (Default value = 0)
152
+ revolutions (int): after this vactual will be set to 0 (Default value = 0)
153
+ acceleration (int): use this for a velocity ramp (Default value = 0)
154
+
155
+ Returns:
156
+ stop (enum): how the movement was finished
157
+ """
158
+ vactual = tmc_math.rps_to_vactual(rps, self._steps_per_rev)
159
+ if revolutions!=0:
160
+ duration = abs(revolutions/rps)
161
+ if revolutions<0:
162
+ vactual = -vactual
163
+ return self.set_vactual_dur(vactual, duration, acceleration=acceleration)
164
+
165
+
166
+ def set_vactual_rpm(self, rpm, duration=0, revolutions=0, acceleration=0):
167
+ """converts the rps parameter to a vactual value which represents
168
+ rotation speed in revolutions per minute
169
+
170
+ Args:
171
+ rpm (int): value for vactual in rpm
172
+ duration (int): after this vactual will be set to 0 (Default value = 0)
173
+ revolutions (int): after this vactual will be set to 0 (Default value = 0)
174
+ acceleration (int): use this for a velocity ramp (Default value = 0)
175
+
176
+ Returns:
177
+ stop (enum): how the movement was finished
178
+ """
179
+ return self.set_vactual_rps(rpm/60, duration, revolutions, acceleration)
File without changes
@@ -0,0 +1,62 @@
1
+ #pylint: disable=wildcard-import
2
+ #pylint: disable=unused-wildcard-import
3
+ #pylint: disable=unused-import
4
+ #pylint: disable=attribute-defined-outside-init
5
+ """
6
+ Register module
7
+ """
8
+
9
+ from ._tmc220x_reg import *
10
+
11
+
12
+
13
+ class TCoolThrs(TmcReg):
14
+ """TCOOLTHRS register class"""
15
+
16
+ def __init__(self, tmc_com: TmcCom):
17
+ """constructor"""
18
+
19
+ reg_map = [
20
+ ["tcoolthrs", 0, 0xFFFFF, int, None, ""]
21
+ ]
22
+ super().__init__(0x14, "TCOOLTHRS", tmc_com, reg_map)
23
+
24
+
25
+ class SGThrs(TmcReg):
26
+ """SGTHRS register class"""
27
+
28
+ def __init__(self, tmc_com: TmcCom):
29
+ """constructor"""
30
+
31
+ reg_map = [
32
+ ["sgthrs", 0, 0xFFFFF, int, None, ""]
33
+ ]
34
+ super().__init__(0x40, "SGTHRS", tmc_com, reg_map)
35
+
36
+
37
+ class SGResult(TmcReg):
38
+ """SGRESULT register class"""
39
+
40
+ def __init__(self, tmc_com: TmcCom):
41
+ """constructor"""
42
+
43
+ reg_map = [
44
+ ["sgresult", 0, 0xFFFFF, int, None, ""]
45
+ ]
46
+ super().__init__(0x41, "SGRESULT", tmc_com, reg_map)
47
+
48
+
49
+ class CoolConf(TmcReg):
50
+ """COOLCONF register class"""
51
+
52
+ def __init__(self, tmc_com: TmcCom):
53
+ """constructor"""
54
+
55
+ reg_map = [
56
+ ["seemin", 15, 0x1, bool, None, ""],
57
+ ["sedn", 13, 0x3, int, None, ""],
58
+ ["semax", 8, 0xF, int, None, ""],
59
+ ["seup", 5, 0x3, int, None, ""],
60
+ ["semin", 0, 0xF, int, None, ""]
61
+ ]
62
+ super().__init__(0x42, "COOLCONF", tmc_com, reg_map)
@@ -0,0 +1,198 @@
1
+ #pylint: disable=wildcard-import
2
+ #pylint: disable=unused-wildcard-import
3
+ #pylint: disable=unused-import
4
+ #pylint: disable=attribute-defined-outside-init
5
+ """
6
+ Register module
7
+ """
8
+
9
+ import math
10
+ from ._tmc_reg import *
11
+
12
+
13
+ class GConf(TmcReg):
14
+ """GCONF register class"""
15
+
16
+ def __init__(self, tmc_com: TmcCom):
17
+ """constructor"""
18
+
19
+ reg_map = [
20
+ ["test_mode", 9, 0x1, bool, None, ""],
21
+ ["multistep_filt", 8, 0x1, bool, None, ""],
22
+ ["mstep_reg_select", 7, 0x1, bool, None, ""],
23
+ ["pdn_disable", 6, 0x1, bool, None, ""],
24
+ ["index_step", 5, 0x1, bool, None, ""],
25
+ ["index_otpw", 4, 0x1, bool, None, ""],
26
+ ["shaft", 3, 0x1, bool, None, ""],
27
+ ["en_spreadcycle", 2, 0x1, bool, None, ""],
28
+ ["internal_rsense", 1, 0x1, bool, None, ""],
29
+ ["i_scale_analog", 0, 0x1, bool, None, ""]
30
+ ]
31
+ super().__init__(0x0, "GCONF", tmc_com, reg_map)
32
+
33
+
34
+ class GStat(TmcReg):
35
+ """GSTAT register class"""
36
+
37
+ def __init__(self, tmc_com: TmcCom):
38
+ """constructor"""
39
+
40
+ reg_map = [
41
+ ["uv_cp", 2, 0x1, bool, None, ""],
42
+ ["drv_err", 1, 0x1, bool, None, ""],
43
+ ["reset", 0, 0x1, bool, None, ""]
44
+ ]
45
+ super().__init__(0x1, "GSTAT", tmc_com, reg_map)
46
+
47
+
48
+ class IfCnt(TmcReg):
49
+ """IFCNT register class"""
50
+
51
+ def __init__(self, tmc_com: TmcCom):
52
+ """constructor"""
53
+
54
+ reg_map = [
55
+ ["ifcnt", 0, 0xFF, int, None, ""]
56
+ ]
57
+ super().__init__(0x2, "IFCNT", tmc_com, reg_map)
58
+
59
+
60
+ class Ioin(TmcReg):
61
+ """IOIN register class"""
62
+
63
+ def __init__(self, tmc_com: TmcCom):
64
+ """constructor"""
65
+
66
+ reg_map = [
67
+ ["version", 24, 0xFF, int, None, ""],
68
+ ["dir", 9, 0x1, bool, None, ""],
69
+ ["spread", 8, 0x1, bool, None, ""],
70
+ ["step", 7, 0x1, bool, None, ""],
71
+ ["ms2", 3, 0x1, bool, None, ""],
72
+ ["ms1", 2, 0x1, bool, None, ""],
73
+ ["enn", 0, 0x1, bool, None, ""]
74
+ ]
75
+ super().__init__(0x3, "IOIN", tmc_com, reg_map)
76
+
77
+
78
+ class IHoldIRun(TmcReg):
79
+ """IHOLD_IRUN register class"""
80
+
81
+ def __init__(self, tmc_com: TmcCom):
82
+ """constructor"""
83
+
84
+ reg_map = [
85
+ ["iholddelay", 16, 0xF, int, None, ""],
86
+ ["irun", 8, 0x1F, int, None, ""],
87
+ ["ihold", 0, 0x1F, int, None, ""]
88
+ ]
89
+ super().__init__(0x10, "IHOLD_IRUN", tmc_com, reg_map)
90
+
91
+
92
+ class TPowerDown(TmcReg):
93
+ """TPowerDown register class"""
94
+
95
+ def __init__(self, tmc_com: TmcCom):
96
+ """constructor"""
97
+
98
+ reg_map = [
99
+ ["tpowerdown", 0, 0xFF, int, None, ""]
100
+ ]
101
+ super().__init__(0x11, "TPowerDown", tmc_com, reg_map)
102
+
103
+
104
+ class TStep(TmcReg):
105
+ """TSTEP register class"""
106
+
107
+ def __init__(self, tmc_com: TmcCom):
108
+ """constructor"""
109
+
110
+ reg_map = [
111
+ ["tstep", 0, 0xFFFFF, int, None, ""]
112
+ ]
113
+ super().__init__(0x12, "TSTEP", tmc_com, reg_map)
114
+
115
+
116
+ class VActual(TmcReg):
117
+ """VACTUAL register class"""
118
+
119
+ def __init__(self, tmc_com: TmcCom):
120
+ """constructor"""
121
+
122
+ reg_map = [
123
+ ["vactual", 0, 0x1FFFFF, int, None, ""]
124
+ ]
125
+ super().__init__(0x22, "VACTUAL", tmc_com, reg_map)
126
+
127
+
128
+ class MsCnt(TmcReg):
129
+ """MSCNT register class"""
130
+
131
+ def __init__(self, tmc_com: TmcCom):
132
+ """constructor"""
133
+
134
+ reg_map = [
135
+ ["mscnt", 0, 0xFF, int, None, ""]
136
+ ]
137
+ super().__init__(0x6A, "MSCNT", tmc_com, reg_map)
138
+
139
+
140
+ class ChopConf(TmcReg):
141
+ """CHOPCONF register class"""
142
+
143
+ def __init__(self, tmc_com: TmcCom):
144
+ """constructor"""
145
+
146
+ reg_map = [
147
+ ["diss2vs", 31, 0x1, bool, None, ""],
148
+ ["diss2g", 30, 0x1, bool, None, ""],
149
+ ["dedge", 29, 0x1, bool, None, ""],
150
+ ["intpol", 28, 0x1, bool, None, ""],
151
+ ["mres", 24, 0xF, int, lambda: self.mres_ms, "µStep"],
152
+ ["vsense", 17, 0x1, bool, None, ""],
153
+ ["tbl", 15, 0x3, int, None, ""],
154
+ ["hend", 7, 0xF, int, None, ""],
155
+ ["hstrt", 4, 0x7, int, None, ""],
156
+ ["toff", 0, 0xF, int, None, ""]
157
+ ]
158
+ super().__init__(0x6C, "CHOPCONF", tmc_com, reg_map)
159
+
160
+ @property
161
+ def mres_ms(self) -> int:
162
+ """return µstep resolution"""
163
+ if hasattr(self, "mres"):
164
+ return int(math.pow(2, 8 - self.mres))
165
+ return None
166
+
167
+ @mres_ms.setter
168
+ def mres_ms(self, mres: int):
169
+ """set µstep resolution"""
170
+ mres_bit = int(math.log(mres, 2))
171
+ mres_bit = 8 - mres_bit
172
+ self.mres = mres_bit
173
+
174
+
175
+ class DrvStatus(TmcReg):
176
+ """DRVSTATUS register class"""
177
+
178
+ def __init__(self, tmc_com: TmcCom):
179
+ """constructor"""
180
+
181
+ reg_map = [
182
+ ["stst", 31, 0x1, bool, None, ""],
183
+ ["stealth", 30, 0x1, bool, None, ""],
184
+ ["cs_actual", 16, 0x1F, int, None, ""],
185
+ ["t157", 11, 0x1, bool, None, ""],
186
+ ["t150", 10, 0x1, bool, None, ""],
187
+ ["t143", 9, 0x1, bool, None, ""],
188
+ ["t120", 8, 0x1, bool, None, ""],
189
+ ["olb", 7, 0x1, bool, None, ""],
190
+ ["ola", 6, 0x1, bool, None, ""],
191
+ ["s2vsb", 5, 0x1, bool, None, ""],
192
+ ["s2vsa", 4, 0x1, bool, None, ""],
193
+ ["s2gb", 3, 0x1, bool, None, ""],
194
+ ["s2ga", 2, 0x1, bool, None, ""],
195
+ ["ot", 1, 0x1, bool, None, ""],
196
+ ["otpw", 0, 0x1, bool, None, ""]
197
+ ]
198
+ super().__init__(0x6F, "DRVSTATUS", tmc_com, reg_map)