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
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# pylint: disable=import-outside-toplevel
|
|
2
|
+
"""Motion control module for TMC stepper drivers
|
|
3
|
+
|
|
4
|
+
Provides various motion control strategies:
|
|
5
|
+
- TmcMotionControlStepDir: STEP/DIR pin-based control
|
|
6
|
+
- TmcMotionControlStepReg: STEP/DIR with register-based direction
|
|
7
|
+
- TmcMotionControlStepPwmDir: STEP/DIR with PWM for step pin
|
|
8
|
+
- TmcMotionControlVActual: Velocity control via VACTUAL register
|
|
9
|
+
- TmcMotionControlIntRampGenerator: Internal ramp generator (TMC5160)
|
|
10
|
+
|
|
11
|
+
Enums:
|
|
12
|
+
- Direction: Motor rotation direction (CW/CCW)
|
|
13
|
+
- MovementAbsRel: Absolute or relative positioning
|
|
14
|
+
- MovementPhase: Current movement phase
|
|
15
|
+
- StopMode: How to stop motor movement
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
>>> from tmc_driver.motion_control import TmcMotionControlStepDir, Direction
|
|
19
|
+
>>> mc = TmcMotionControlStepDir(step_pin=16, dir_pin=20)
|
|
20
|
+
>>> mc.set_direction_pin(Direction.CW)
|
|
21
|
+
|
|
22
|
+
Note: Uses lazy imports to avoid circular import issues.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
try:
|
|
26
|
+
from typing import TYPE_CHECKING
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
# Import for type checkers/IDE only - not executed at runtime
|
|
30
|
+
from ._tmc_mc import (
|
|
31
|
+
TmcMotionControl,
|
|
32
|
+
Direction,
|
|
33
|
+
MovementAbsRel,
|
|
34
|
+
MovementPhase,
|
|
35
|
+
StopMode,
|
|
36
|
+
)
|
|
37
|
+
from ._tmc_mc_step_dir import TmcMotionControlStepDir
|
|
38
|
+
from ._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
39
|
+
from ._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
40
|
+
from ._tmc_mc_vactual import TmcMotionControlVActual
|
|
41
|
+
from ._tmc_mc_int_ramp_generator import (
|
|
42
|
+
TmcMotionControlIntRampGenerator,
|
|
43
|
+
RampMode,
|
|
44
|
+
)
|
|
45
|
+
except ImportError:
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def __getattr__(name):
|
|
50
|
+
# pylint: disable=too-many-return-statements
|
|
51
|
+
"""Lazy import of motion control classes to avoid circular imports"""
|
|
52
|
+
if name == "TmcMotionControl":
|
|
53
|
+
from ._tmc_mc import TmcMotionControl
|
|
54
|
+
|
|
55
|
+
return TmcMotionControl
|
|
56
|
+
if name == "Direction":
|
|
57
|
+
from ._tmc_mc import Direction
|
|
58
|
+
|
|
59
|
+
return Direction
|
|
60
|
+
if name == "MovementAbsRel":
|
|
61
|
+
from ._tmc_mc import MovementAbsRel
|
|
62
|
+
|
|
63
|
+
return MovementAbsRel
|
|
64
|
+
if name == "MovementPhase":
|
|
65
|
+
from ._tmc_mc import MovementPhase
|
|
66
|
+
|
|
67
|
+
return MovementPhase
|
|
68
|
+
if name == "StopMode":
|
|
69
|
+
from ._tmc_mc import StopMode
|
|
70
|
+
|
|
71
|
+
return StopMode
|
|
72
|
+
if name == "TmcMotionControlStepDir":
|
|
73
|
+
from ._tmc_mc_step_dir import TmcMotionControlStepDir
|
|
74
|
+
|
|
75
|
+
return TmcMotionControlStepDir
|
|
76
|
+
if name == "TmcMotionControlStepReg":
|
|
77
|
+
from ._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
78
|
+
|
|
79
|
+
return TmcMotionControlStepReg
|
|
80
|
+
if name == "TmcMotionControlStepPwmDir":
|
|
81
|
+
from ._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
82
|
+
|
|
83
|
+
return TmcMotionControlStepPwmDir
|
|
84
|
+
if name == "TmcMotionControlVActual":
|
|
85
|
+
from ._tmc_mc_vactual import TmcMotionControlVActual
|
|
86
|
+
|
|
87
|
+
return TmcMotionControlVActual
|
|
88
|
+
if name == "TmcMotionControlIntRampGenerator":
|
|
89
|
+
from ._tmc_mc_int_ramp_generator import TmcMotionControlIntRampGenerator
|
|
90
|
+
|
|
91
|
+
return TmcMotionControlIntRampGenerator
|
|
92
|
+
if name == "RampMode":
|
|
93
|
+
from ._tmc_mc_int_ramp_generator import RampMode
|
|
94
|
+
|
|
95
|
+
return RampMode
|
|
96
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
__all__ = [
|
|
100
|
+
"TmcMotionControl",
|
|
101
|
+
"Direction",
|
|
102
|
+
"MovementAbsRel",
|
|
103
|
+
"MovementPhase",
|
|
104
|
+
"StopMode",
|
|
105
|
+
"RampMode",
|
|
106
|
+
"TmcMotionControlStepDir",
|
|
107
|
+
"TmcMotionControlStepReg",
|
|
108
|
+
"TmcMotionControlStepPwmDir",
|
|
109
|
+
"TmcMotionControlVActual",
|
|
110
|
+
"TmcMotionControlIntRampGenerator",
|
|
111
|
+
]
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# pylint: disable=too-many-instance-attributes
|
|
2
|
+
# pylint: skip-file
|
|
3
|
+
"""
|
|
4
|
+
Motion Control base module
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from enum import IntEnum
|
|
8
|
+
from abc import abstractmethod
|
|
9
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
10
|
+
from ..reg._tmc_reg import TmcReg
|
|
11
|
+
from .._tmc_exceptions import TmcMotionControlException
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Direction(IntEnum):
|
|
15
|
+
"""movement direction of the motor"""
|
|
16
|
+
|
|
17
|
+
CCW = 0
|
|
18
|
+
CW = 1
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MovementAbsRel(IntEnum):
|
|
22
|
+
"""movement absolute or relative"""
|
|
23
|
+
|
|
24
|
+
ABSOLUTE = 0
|
|
25
|
+
RELATIVE = 1
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MovementPhase(IntEnum):
|
|
29
|
+
"""movement phase"""
|
|
30
|
+
|
|
31
|
+
STANDSTILL = 0
|
|
32
|
+
ACCELERATING = 1
|
|
33
|
+
MAXSPEED = 2
|
|
34
|
+
DECELERATING = 3
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class StopMode(IntEnum):
|
|
38
|
+
"""stopmode"""
|
|
39
|
+
|
|
40
|
+
NO = 0
|
|
41
|
+
SOFTSTOP = 1
|
|
42
|
+
HARDSTOP = 2
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class TmcMotionControl:
|
|
46
|
+
"""Motion Control base class"""
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def current_pos(self):
|
|
50
|
+
"""_current_pos property"""
|
|
51
|
+
return self._current_pos
|
|
52
|
+
|
|
53
|
+
@current_pos.setter
|
|
54
|
+
def current_pos(self, current_pos: int):
|
|
55
|
+
"""_current_pos setter"""
|
|
56
|
+
self._current_pos = current_pos
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def current_pos_fullstep(self) -> int:
|
|
60
|
+
"""_current_pos as fullstep property"""
|
|
61
|
+
return self.current_pos // self.mres
|
|
62
|
+
|
|
63
|
+
@current_pos_fullstep.setter
|
|
64
|
+
def current_pos_fullstep(self, current_pos: int):
|
|
65
|
+
"""_current_pos as fullstep setter"""
|
|
66
|
+
self.current_pos = current_pos * self.mres
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def mres(self):
|
|
70
|
+
"""_mres property"""
|
|
71
|
+
return self._mres
|
|
72
|
+
|
|
73
|
+
@mres.setter
|
|
74
|
+
def mres(self, mres: int):
|
|
75
|
+
"""_mres setter"""
|
|
76
|
+
self._mres = mres
|
|
77
|
+
self._steps_per_rev = self._fullsteps_per_rev * self._mres
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def steps_per_rev(self):
|
|
81
|
+
"""_steps_per_rev property"""
|
|
82
|
+
return self._steps_per_rev
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def fullsteps_per_rev(self):
|
|
86
|
+
"""_fullsteps_per_rev property"""
|
|
87
|
+
return self._fullsteps_per_rev
|
|
88
|
+
|
|
89
|
+
@fullsteps_per_rev.setter
|
|
90
|
+
def fullsteps_per_rev(self, fullsteps_per_rev: int):
|
|
91
|
+
"""_fullsteps_per_rev setter"""
|
|
92
|
+
self._fullsteps_per_rev = fullsteps_per_rev
|
|
93
|
+
self._steps_per_rev = self._fullsteps_per_rev * self._mres
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def movement_abs_rel(self):
|
|
97
|
+
"""_movement_abs_rel property"""
|
|
98
|
+
return self._movement_abs_rel
|
|
99
|
+
|
|
100
|
+
@movement_abs_rel.setter
|
|
101
|
+
def movement_abs_rel(self, movement_abs_rel: MovementAbsRel):
|
|
102
|
+
"""_movement_abs_rel setter"""
|
|
103
|
+
self._movement_abs_rel = movement_abs_rel
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def movement_phase(self):
|
|
107
|
+
"""_movement_phase property"""
|
|
108
|
+
return self._movement_phase
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def speed(self):
|
|
112
|
+
"""_speed property"""
|
|
113
|
+
return self._speed
|
|
114
|
+
|
|
115
|
+
@speed.setter
|
|
116
|
+
def speed(self, speed: float):
|
|
117
|
+
"""_speed setter"""
|
|
118
|
+
self._speed = speed
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def speed_fullstep(self):
|
|
122
|
+
"""_speed property"""
|
|
123
|
+
return self._speed * self.mres
|
|
124
|
+
|
|
125
|
+
@speed_fullstep.setter
|
|
126
|
+
def speed_fullstep(self, speed_fullstep: int):
|
|
127
|
+
"""_speed_fullstep setter"""
|
|
128
|
+
self._speed = speed_fullstep / self.mres
|
|
129
|
+
|
|
130
|
+
@property
|
|
131
|
+
def max_speed(self):
|
|
132
|
+
"""_max_speed property"""
|
|
133
|
+
return self._max_speed
|
|
134
|
+
|
|
135
|
+
@max_speed.setter
|
|
136
|
+
def max_speed(self, speed: int):
|
|
137
|
+
"""_max_speed setter"""
|
|
138
|
+
self._max_speed = abs(speed)
|
|
139
|
+
|
|
140
|
+
@property
|
|
141
|
+
def max_speed_fullstep(self):
|
|
142
|
+
"""_max_speed_fullstep property"""
|
|
143
|
+
return self.max_speed // self.mres
|
|
144
|
+
|
|
145
|
+
@max_speed_fullstep.setter
|
|
146
|
+
def max_speed_fullstep(self, max_speed_fullstep: int):
|
|
147
|
+
"""_max_speed_fullstep setter"""
|
|
148
|
+
self.max_speed = max_speed_fullstep * self.mres
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def max_speed_homing(self):
|
|
152
|
+
"""_max_speed_homing property"""
|
|
153
|
+
return self._max_speed_homing
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def acceleration(self):
|
|
157
|
+
"""_acceleration property"""
|
|
158
|
+
return self._acceleration
|
|
159
|
+
|
|
160
|
+
@acceleration.setter
|
|
161
|
+
def acceleration(self, acceleration: int):
|
|
162
|
+
"""_acceleration setter"""
|
|
163
|
+
self._acceleration = abs(acceleration)
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def acceleration_fullstep(self):
|
|
167
|
+
"""_acceleration_fullstep property"""
|
|
168
|
+
return self._acceleration // self.mres
|
|
169
|
+
|
|
170
|
+
@acceleration_fullstep.setter
|
|
171
|
+
def acceleration_fullstep(self, acceleration_fullstep: int):
|
|
172
|
+
"""_acceleration_fullstep setter"""
|
|
173
|
+
self.acceleration = acceleration_fullstep * self.mres
|
|
174
|
+
|
|
175
|
+
def __init__(self):
|
|
176
|
+
"""constructor"""
|
|
177
|
+
self._tmc_logger: TmcLogger
|
|
178
|
+
|
|
179
|
+
self._direction: Direction = Direction.CW
|
|
180
|
+
|
|
181
|
+
self._stop: StopMode = StopMode.NO
|
|
182
|
+
|
|
183
|
+
self._current_pos: int = 0 # current position of stepper in steps
|
|
184
|
+
self._target_pos: int = 0 # the target position in steps
|
|
185
|
+
|
|
186
|
+
self._speed: float = 0.0 # the current speed in steps per second
|
|
187
|
+
|
|
188
|
+
self._max_speed: int = 1 # the maximum speed in steps per second
|
|
189
|
+
self._max_speed_homing: int = (
|
|
190
|
+
200 # the maximum speed in steps per second for homing
|
|
191
|
+
)
|
|
192
|
+
self._acceleration: int = 1 # the acceleration in steps per second per second
|
|
193
|
+
self._acceleration_homing: int = (
|
|
194
|
+
10000 # the acceleration in steps per second per second for homing
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
self._mres: int = 2 # microstepping resolution
|
|
198
|
+
self._steps_per_rev: int = 400 # microsteps per revolution
|
|
199
|
+
self._fullsteps_per_rev: int = 200 # fullsteps per revolution
|
|
200
|
+
|
|
201
|
+
self._movement_abs_rel: MovementAbsRel = MovementAbsRel.ABSOLUTE
|
|
202
|
+
self._movement_phase: MovementPhase = MovementPhase.STANDSTILL
|
|
203
|
+
|
|
204
|
+
self._get_register_callback = None
|
|
205
|
+
|
|
206
|
+
def init(self, tmc_logger: TmcLogger):
|
|
207
|
+
"""init: called by the Tmc class"""
|
|
208
|
+
self._tmc_logger = tmc_logger
|
|
209
|
+
self.max_speed_fullstep = 100
|
|
210
|
+
self.acceleration_fullstep = 100
|
|
211
|
+
|
|
212
|
+
def __del__(self):
|
|
213
|
+
self.deinit()
|
|
214
|
+
|
|
215
|
+
def deinit(self):
|
|
216
|
+
"""destructor"""
|
|
217
|
+
|
|
218
|
+
def set_get_register_callback(self, callback):
|
|
219
|
+
"""Set callback to get registers from parent TMC class
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
callback: Function that takes register name (str) and returns register object
|
|
223
|
+
"""
|
|
224
|
+
self._get_register_callback = callback
|
|
225
|
+
|
|
226
|
+
def get_register(self, name: str):
|
|
227
|
+
"""Get register by name from parent TMC class
|
|
228
|
+
|
|
229
|
+
Args:
|
|
230
|
+
name: Register name (e.g. 'gconf', 'chopconf')
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
Register object or None if callback not set
|
|
234
|
+
"""
|
|
235
|
+
if self._get_register_callback is None:
|
|
236
|
+
raise TmcMotionControlException(
|
|
237
|
+
"Get register callback not set in motion control"
|
|
238
|
+
)
|
|
239
|
+
return self._get_register_callback(name)
|
|
240
|
+
|
|
241
|
+
@abstractmethod
|
|
242
|
+
def make_a_step(self):
|
|
243
|
+
"""make a Step"""
|
|
244
|
+
|
|
245
|
+
def set_direction(self, direction: Direction):
|
|
246
|
+
"""sets the motor shaft direction to the given value: 0 = CCW; 1 = CW
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
direction (bool): motor shaft direction: False = CCW; True = CW
|
|
250
|
+
"""
|
|
251
|
+
self._direction = direction
|
|
252
|
+
self._tmc_logger.log(f"New Direction is: {direction}", Loglevel.MOVEMENT)
|
|
253
|
+
|
|
254
|
+
def stop(self, stop_mode=StopMode.HARDSTOP):
|
|
255
|
+
"""stop the current movement
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
stop_mode (enum): whether the movement should be stopped immediately or softly
|
|
259
|
+
(Default value = StopMode.HARDSTOP)
|
|
260
|
+
"""
|
|
261
|
+
self._stop = stop_mode
|
|
262
|
+
|
|
263
|
+
@abstractmethod
|
|
264
|
+
def run_to_position_steps(
|
|
265
|
+
self, steps: int, movement_abs_rel: MovementAbsRel | None = None
|
|
266
|
+
) -> StopMode:
|
|
267
|
+
"""runs the motor to the given position.
|
|
268
|
+
with acceleration and deceleration
|
|
269
|
+
blocks the code until finished or stopped from a different thread!
|
|
270
|
+
returns true when the movement if finished normally and false,
|
|
271
|
+
when the movement was stopped
|
|
272
|
+
|
|
273
|
+
Args:
|
|
274
|
+
steps (int): amount of steps; can be negative
|
|
275
|
+
movement_abs_rel (enum): whether the movement should be absolut or relative
|
|
276
|
+
(Default value = None)
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
stop (enum): how the movement was finished
|
|
280
|
+
"""
|
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
TMC internal Ramp Generator Motion Control module
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import time
|
|
11
|
+
from enum import IntEnum
|
|
12
|
+
from ._tmc_mc import TmcMotionControl, MovementAbsRel, StopMode
|
|
13
|
+
from ..tmc_logger import Loglevel
|
|
14
|
+
from ..reg import _tmc5160_reg as tmc5160_reg
|
|
15
|
+
|
|
16
|
+
# from .. import _tmc_math as tmc_math
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RampMode(IntEnum):
|
|
20
|
+
"""Ramp modes of the TMC internal ramp generator"""
|
|
21
|
+
|
|
22
|
+
POSITIONING_MODE = 0
|
|
23
|
+
VELOCITY_MODE_POS = 1
|
|
24
|
+
VELOCITY_MODE_NEG = 1
|
|
25
|
+
HOLD_MODE = 2
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class TmcMotionControlIntRampGenerator(TmcMotionControl):
|
|
29
|
+
"""TMC internal Ramp Generator Motion Control class"""
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def current_pos(self):
|
|
33
|
+
"""_current_pos property"""
|
|
34
|
+
xactual: tmc5160_reg.XActual = self.get_register("xactual")
|
|
35
|
+
xactual.read()
|
|
36
|
+
self._current_pos = xactual.xactual
|
|
37
|
+
return self._current_pos
|
|
38
|
+
|
|
39
|
+
@current_pos.setter
|
|
40
|
+
def current_pos(self, current_pos: int):
|
|
41
|
+
"""_current_pos setter"""
|
|
42
|
+
self._current_pos = current_pos
|
|
43
|
+
xactual: tmc5160_reg.XActual = self.get_register("xactual")
|
|
44
|
+
xactual.xactual = current_pos
|
|
45
|
+
xactual.write()
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def target_pos(self):
|
|
49
|
+
"""_target_pos property"""
|
|
50
|
+
xtarget: tmc5160_reg.XTarget = self.get_register("xtarget")
|
|
51
|
+
xtarget.read()
|
|
52
|
+
self._target_pos = xtarget.xtarget
|
|
53
|
+
return self._target_pos
|
|
54
|
+
|
|
55
|
+
@target_pos.setter
|
|
56
|
+
def target_pos(self, target_pos: int):
|
|
57
|
+
"""_target_pos setter"""
|
|
58
|
+
self._target_pos = target_pos
|
|
59
|
+
xtarget: tmc5160_reg.XTarget = self.get_register("xtarget")
|
|
60
|
+
xtarget.xtarget = target_pos
|
|
61
|
+
xtarget.write()
|
|
62
|
+
|
|
63
|
+
def __init__(self):
|
|
64
|
+
"""constructor"""
|
|
65
|
+
super().__init__()
|
|
66
|
+
self._starttime: int = 0
|
|
67
|
+
|
|
68
|
+
def set_ramp_mode(self, ramp_mode: RampMode):
|
|
69
|
+
"""sets the ramp mode of the internal ramp generator
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
ramp_mode (enum): the ramp mode to set
|
|
73
|
+
"""
|
|
74
|
+
rampmode: tmc5160_reg.RampMode = self.get_register("rampmode")
|
|
75
|
+
rampmode.rampmode = int(ramp_mode)
|
|
76
|
+
rampmode.write()
|
|
77
|
+
|
|
78
|
+
def set_motion_profile(self, max_speed: int, acceleration: int, deceleration: int):
|
|
79
|
+
"""sets the motion profile of the internal ramp generator
|
|
80
|
+
Args:
|
|
81
|
+
max_speed (int): maximum speed in µsteps/s
|
|
82
|
+
acceleration (int): acceleration in µsteps/s²
|
|
83
|
+
deceleration (int): deceleration in µsteps/s²
|
|
84
|
+
"""
|
|
85
|
+
vstart: tmc5160_reg.VStart = self.get_register("vstart")
|
|
86
|
+
vstart.vstart = 5 # Motor start velocity (unsigned)
|
|
87
|
+
vstart.write()
|
|
88
|
+
|
|
89
|
+
vstop: tmc5160_reg.VStop = self.get_register("vstop")
|
|
90
|
+
vstop.vstop = 10 # Motor stop velocity (unsigned)
|
|
91
|
+
vstop.write()
|
|
92
|
+
|
|
93
|
+
# Datasheet says:
|
|
94
|
+
# 0: Disables A1 and D1 phase, use AMAX, DMAX only
|
|
95
|
+
# but that did not work in my tests, so we set them to the same as VMAX/AMAX/DMAX
|
|
96
|
+
v1: tmc5160_reg.V1 = self.get_register("v1")
|
|
97
|
+
v1.v1 = max_speed
|
|
98
|
+
v1.write()
|
|
99
|
+
|
|
100
|
+
a1: tmc5160_reg.A1 = self.get_register("a1")
|
|
101
|
+
a1.a1 = acceleration
|
|
102
|
+
a1.write()
|
|
103
|
+
|
|
104
|
+
d1: tmc5160_reg.D1 = self.get_register("d1")
|
|
105
|
+
d1.d1 = deceleration
|
|
106
|
+
d1.write()
|
|
107
|
+
|
|
108
|
+
vmax: tmc5160_reg.VMax = self.get_register("vmax")
|
|
109
|
+
vmax.vmax = max_speed
|
|
110
|
+
vmax.write()
|
|
111
|
+
|
|
112
|
+
amax: tmc5160_reg.AMax = self.get_register("amax")
|
|
113
|
+
amax.amax = acceleration
|
|
114
|
+
amax.write()
|
|
115
|
+
|
|
116
|
+
dmax: tmc5160_reg.DMax = self.get_register("dmax")
|
|
117
|
+
dmax.dmax = deceleration
|
|
118
|
+
dmax.write()
|
|
119
|
+
|
|
120
|
+
def make_a_step(self):
|
|
121
|
+
"""method that makes on step"""
|
|
122
|
+
raise NotImplementedError
|
|
123
|
+
|
|
124
|
+
def stop(self, stop_mode=StopMode.HARDSTOP):
|
|
125
|
+
"""stop the current movement
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
stop_mode (enum): whether the movement should be stopped immediately or softly
|
|
129
|
+
(Default value = StopMode.HARDSTOP)
|
|
130
|
+
"""
|
|
131
|
+
super().stop(stop_mode)
|
|
132
|
+
self.set_ramp_mode(RampMode.VELOCITY_MODE_POS)
|
|
133
|
+
|
|
134
|
+
vmax: tmc5160_reg.VMax = self.get_register("vmax")
|
|
135
|
+
vmax.vmax = 0
|
|
136
|
+
vmax.write()
|
|
137
|
+
|
|
138
|
+
if stop_mode == StopMode.HARDSTOP:
|
|
139
|
+
amax: tmc5160_reg.AMax = self.get_register("amax")
|
|
140
|
+
amax.amax = 65535 # max deceleration (amax is used in velocity mode for deceleration)
|
|
141
|
+
amax.write()
|
|
142
|
+
|
|
143
|
+
def wait_until_stop(self):
|
|
144
|
+
"""blocks the code until the movement is finished or stopped from a different thread!"""
|
|
145
|
+
rampstat: tmc5160_reg.RampStat = self.get_register("rampstat")
|
|
146
|
+
while True:
|
|
147
|
+
# self._tmc_logger.log(f"current pos: {self.current_pos}", Loglevel.MOVEMENT)
|
|
148
|
+
rampstat.read()
|
|
149
|
+
if rampstat.position_reached:
|
|
150
|
+
self._tmc_logger.log("position reached", Loglevel.MOVEMENT)
|
|
151
|
+
return
|
|
152
|
+
if rampstat.event_stop_sg:
|
|
153
|
+
self._tmc_logger.log("stopped by stallguard", Loglevel.MOVEMENT)
|
|
154
|
+
self._stop = StopMode.HARDSTOP
|
|
155
|
+
return
|
|
156
|
+
if rampstat.event_stop_l:
|
|
157
|
+
self._tmc_logger.log("stopped by limit switch l", Loglevel.MOVEMENT)
|
|
158
|
+
self._stop = StopMode.HARDSTOP
|
|
159
|
+
return
|
|
160
|
+
if rampstat.event_stop_r:
|
|
161
|
+
self._tmc_logger.log("stopped by limit switch r", Loglevel.MOVEMENT)
|
|
162
|
+
self._stop = StopMode.HARDSTOP
|
|
163
|
+
return
|
|
164
|
+
time.sleep(0.01) # sleep 10ms to reduce CPU load
|
|
165
|
+
|
|
166
|
+
def run_to_position_steps(
|
|
167
|
+
self, steps, movement_abs_rel: MovementAbsRel | None = None
|
|
168
|
+
) -> StopMode:
|
|
169
|
+
"""runs the motor to the given position.
|
|
170
|
+
with acceleration and deceleration
|
|
171
|
+
blocks the code until finished or stopped from a different thread!
|
|
172
|
+
returns true when the movement if finished normally and false,
|
|
173
|
+
when the movement was stopped
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
steps (int): amount of steps; can be negative
|
|
177
|
+
movement_abs_rel (enum): whether the movement should be absolut or relative
|
|
178
|
+
(Default value = None)
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
stop (enum): how the movement was finished
|
|
182
|
+
"""
|
|
183
|
+
self._current_pos = self.current_pos # sync current pos with register
|
|
184
|
+
|
|
185
|
+
self.set_ramp_mode(RampMode.POSITIONING_MODE)
|
|
186
|
+
self.set_motion_profile(self._max_speed, self._acceleration, self._acceleration)
|
|
187
|
+
|
|
188
|
+
self._stop = StopMode.NO
|
|
189
|
+
|
|
190
|
+
if movement_abs_rel is None:
|
|
191
|
+
movement_abs_rel = self._movement_abs_rel
|
|
192
|
+
|
|
193
|
+
if movement_abs_rel == MovementAbsRel.RELATIVE:
|
|
194
|
+
self.target_pos = self._current_pos + steps
|
|
195
|
+
else:
|
|
196
|
+
self.target_pos = steps
|
|
197
|
+
|
|
198
|
+
self._tmc_logger.log(
|
|
199
|
+
f"Before movement cur: {self._current_pos} | tar: {self._target_pos}",
|
|
200
|
+
Loglevel.MOVEMENT,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
self.wait_until_stop()
|
|
204
|
+
self.target_pos = self.current_pos
|
|
205
|
+
|
|
206
|
+
loststeps: tmc5160_reg.LostSteps = self.get_register("loststeps")
|
|
207
|
+
loststeps.read()
|
|
208
|
+
if loststeps.lost_steps != 0:
|
|
209
|
+
self._tmc_logger.log(
|
|
210
|
+
f"Lost steps detected: {loststeps.lost_steps}", Loglevel.MOVEMENT
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
self._tmc_logger.log(
|
|
214
|
+
f"After movement cur: {self.current_pos} | tar: {self._target_pos}",
|
|
215
|
+
Loglevel.MOVEMENT,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
return self._stop
|