PyTmcStepper 0.20.0__tar.gz → 0.21.0__tar.gz
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.20.0 → pytmcstepper-0.21.0}/PKG-INFO +1 -1
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/pyproject.toml +2 -1
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/PyTmcStepper.egg-info/PKG-INFO +1 -1
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/PyTmcStepper.egg-info/SOURCES.txt +2 -17
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/__init__.py +6 -0
- pytmcstepper-0.21.0/src/tmc_driver/motion_control/_tmc_mc_step_pio.py +465 -0
- pytmcstepper-0.20.0/tests/test_gpiozero.py +0 -118
- pytmcstepper-0.20.0/tests/test_tmc2209.py +0 -143
- pytmcstepper-0.20.0/tests/test_tmc2240.py +0 -152
- pytmcstepper-0.20.0/tests/test_tmc_com_spi.py +0 -32
- pytmcstepper-0.20.0/tests/test_tmc_com_uart.py +0 -43
- pytmcstepper-0.20.0/tests/test_tmc_ec_pin.py +0 -26
- pytmcstepper-0.20.0/tests/test_tmc_imports.py +0 -46
- pytmcstepper-0.20.0/tests/test_tmc_math.py +0 -68
- pytmcstepper-0.20.0/tests/test_tmc_modules.py +0 -235
- pytmcstepper-0.20.0/tests/test_tmc_move_intramp.py +0 -132
- pytmcstepper-0.20.0/tests/test_tmc_move_stepdir.py +0 -109
- pytmcstepper-0.20.0/tests/test_tmc_move_steppwmdir.py +0 -93
- pytmcstepper-0.20.0/tests/test_tmc_move_stepreg.py +0 -146
- pytmcstepper-0.20.0/tests/test_tmc_move_vactual.py +0 -120
- pytmcstepper-0.20.0/tests/test_tmc_set_current.py +0 -204
- pytmcstepper-0.20.0/tests/test_tmc_stallguard.py +0 -138
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/LICENSE +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/README.md +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/setup.cfg +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/PyTmcStepper.egg-info/dependency_links.txt +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/PyTmcStepper.egg-info/requires.txt +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/PyTmcStepper.egg-info/top_level.txt +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_exceptions.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_math.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_stallguard.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_stepperdriver.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_validation.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/_tmc_xxxx.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_spi.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_spi_base.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_spi_circuitpython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_spi_ftdi.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_spi_micropython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_uart.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_uart_base.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_uart_circuitpython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/com/_tmc_com_uart_micropython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/enable_control/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/enable_control/_tmc_ec.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/enable_control/_tmc_ec_pin.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/enable_control/_tmc_ec_toff.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc_step_dir.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc_step_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/motion_control/_tmc_mc_vactual.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/platform_utils.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc2209_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc220x_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc224x_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc5160_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc_reg.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/reg/_tmc_shared_regs.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_2208.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_2209.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_220x.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_2240.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_5160.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_circuitpython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_logger/__init__.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_logger/_tmc_logger_base.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_logger/_tmc_logger_cpython.py +0 -0
- {pytmcstepper-0.20.0 → pytmcstepper-0.21.0}/src/tmc_driver/tmc_logger/_tmc_logger_micropython.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PyTmcStepper
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.21.0
|
|
4
4
|
Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
|
|
5
5
|
Author-email: Christian Köhlke <christian@koehlke.de>
|
|
6
6
|
License-Expression: GPL-3.0
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "PyTmcStepper"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.21.0"
|
|
8
8
|
authors = [{ name = "Christian Köhlke", email = "christian@koehlke.de" }]
|
|
9
9
|
description = "This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi"
|
|
10
10
|
readme = "README.md"
|
|
@@ -57,6 +57,7 @@ omit = [
|
|
|
57
57
|
"*_circuitpython.py",
|
|
58
58
|
"*_ftdi.py",
|
|
59
59
|
"*_periphery.py",
|
|
60
|
+
"*_pio.py",
|
|
60
61
|
]
|
|
61
62
|
exclude_lines = [
|
|
62
63
|
"pragma: no cover",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PyTmcStepper
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.21.0
|
|
4
4
|
Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
|
|
5
5
|
Author-email: Christian Köhlke <christian@koehlke.de>
|
|
6
6
|
License-Expression: GPL-3.0
|
|
@@ -38,6 +38,7 @@ src/tmc_driver/motion_control/__init__.py
|
|
|
38
38
|
src/tmc_driver/motion_control/_tmc_mc.py
|
|
39
39
|
src/tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py
|
|
40
40
|
src/tmc_driver/motion_control/_tmc_mc_step_dir.py
|
|
41
|
+
src/tmc_driver/motion_control/_tmc_mc_step_pio.py
|
|
41
42
|
src/tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py
|
|
42
43
|
src/tmc_driver/motion_control/_tmc_mc_step_reg.py
|
|
43
44
|
src/tmc_driver/motion_control/_tmc_mc_vactual.py
|
|
@@ -59,20 +60,4 @@ src/tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py
|
|
|
59
60
|
src/tmc_driver/tmc_logger/__init__.py
|
|
60
61
|
src/tmc_driver/tmc_logger/_tmc_logger_base.py
|
|
61
62
|
src/tmc_driver/tmc_logger/_tmc_logger_cpython.py
|
|
62
|
-
src/tmc_driver/tmc_logger/_tmc_logger_micropython.py
|
|
63
|
-
tests/test_gpiozero.py
|
|
64
|
-
tests/test_tmc2209.py
|
|
65
|
-
tests/test_tmc2240.py
|
|
66
|
-
tests/test_tmc_com_spi.py
|
|
67
|
-
tests/test_tmc_com_uart.py
|
|
68
|
-
tests/test_tmc_ec_pin.py
|
|
69
|
-
tests/test_tmc_imports.py
|
|
70
|
-
tests/test_tmc_math.py
|
|
71
|
-
tests/test_tmc_modules.py
|
|
72
|
-
tests/test_tmc_move_intramp.py
|
|
73
|
-
tests/test_tmc_move_stepdir.py
|
|
74
|
-
tests/test_tmc_move_steppwmdir.py
|
|
75
|
-
tests/test_tmc_move_stepreg.py
|
|
76
|
-
tests/test_tmc_move_vactual.py
|
|
77
|
-
tests/test_tmc_set_current.py
|
|
78
|
-
tests/test_tmc_stallguard.py
|
|
63
|
+
src/tmc_driver/tmc_logger/_tmc_logger_micropython.py
|
|
@@ -37,6 +37,7 @@ try:
|
|
|
37
37
|
from ._tmc_mc_step_dir import TmcMotionControlStepDir
|
|
38
38
|
from ._tmc_mc_step_reg import TmcMotionControlStepReg
|
|
39
39
|
from ._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
40
|
+
from ._tmc_mc_step_pio import TmcMotionControlStepPio
|
|
40
41
|
from ._tmc_mc_vactual import TmcMotionControlVActual
|
|
41
42
|
from ._tmc_mc_int_ramp_generator import (
|
|
42
43
|
TmcMotionControlIntRampGenerator,
|
|
@@ -81,6 +82,10 @@ def __getattr__(name):
|
|
|
81
82
|
from ._tmc_mc_step_pwm_dir import TmcMotionControlStepPwmDir
|
|
82
83
|
|
|
83
84
|
return TmcMotionControlStepPwmDir
|
|
85
|
+
if name == "TmcMotionControlStepPio":
|
|
86
|
+
from ._tmc_mc_step_pio import TmcMotionControlStepPio
|
|
87
|
+
|
|
88
|
+
return TmcMotionControlStepPio
|
|
84
89
|
if name == "TmcMotionControlVActual":
|
|
85
90
|
from ._tmc_mc_vactual import TmcMotionControlVActual
|
|
86
91
|
|
|
@@ -106,6 +111,7 @@ __all__ = [
|
|
|
106
111
|
"TmcMotionControlStepDir",
|
|
107
112
|
"TmcMotionControlStepReg",
|
|
108
113
|
"TmcMotionControlStepPwmDir",
|
|
114
|
+
"TmcMotionControlStepPio",
|
|
109
115
|
"TmcMotionControlVActual",
|
|
110
116
|
"TmcMotionControlIntRampGenerator",
|
|
111
117
|
]
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
# pylint: disable=too-many-instance-attributes
|
|
2
|
+
# pylint: disable=too-many-arguments
|
|
3
|
+
# pylint: disable=too-many-branches
|
|
4
|
+
# pylint: disable=too-many-positional-arguments
|
|
5
|
+
# pylint: skip-file
|
|
6
|
+
# pyright: reportUndefinedVariable=false
|
|
7
|
+
"""
|
|
8
|
+
STEP/DIR Motion Control module using PIO (Programmable I/O)
|
|
9
|
+
for Raspberry Pi Pico (RP2040/RP2350) under MicroPython
|
|
10
|
+
|
|
11
|
+
This module provides precise step pulse generation using the PIO hardware
|
|
12
|
+
of the RP2040/RP2350 microcontroller. The PIO generates step pulses with
|
|
13
|
+
consistent timing, independent of Python execution.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import time
|
|
17
|
+
import math
|
|
18
|
+
from machine import Pin
|
|
19
|
+
import rp2
|
|
20
|
+
|
|
21
|
+
from ._tmc_mc import (
|
|
22
|
+
TmcMotionControl,
|
|
23
|
+
MovementAbsRel,
|
|
24
|
+
MovementPhase,
|
|
25
|
+
Direction,
|
|
26
|
+
StopMode,
|
|
27
|
+
)
|
|
28
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
29
|
+
from ..tmc_gpio import GpioMode
|
|
30
|
+
from .. import tmc_gpio
|
|
31
|
+
from .._tmc_exceptions import TmcMotionControlException
|
|
32
|
+
from ..platform_utils import get_time_us
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class PioData:
|
|
36
|
+
|
|
37
|
+
def __init__(self, steps=0, delay=0):
|
|
38
|
+
"""
|
|
39
|
+
PIO data structure to hold steps and delay
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
steps (int): Number of step cycles (16 bits; max 65535)
|
|
43
|
+
delay (int): Delay between steps in ms (16 bits; max 65535)
|
|
44
|
+
"""
|
|
45
|
+
self.steps = min(steps, 0xFFFF)
|
|
46
|
+
self.delay = min(delay, 0xFFFF)
|
|
47
|
+
|
|
48
|
+
def put(self, sm: rp2.StateMachine, pio_freq=2000):
|
|
49
|
+
"""
|
|
50
|
+
Put the steps and delay into the PIO state machine FIFO
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
sm (rp2.StateMachine): The PIO state machine
|
|
54
|
+
pio_freq (int): Frequency of the PIO state machine in Hz
|
|
55
|
+
"""
|
|
56
|
+
if self.steps == 0:
|
|
57
|
+
return # Nothing to do
|
|
58
|
+
|
|
59
|
+
delay = int((self.delay / 1000) * pio_freq)
|
|
60
|
+
# PIO loop does N+1 iterations for Y=N (do-while structure with jmp y_dec)
|
|
61
|
+
# So we send steps-1 to get the correct number of steps
|
|
62
|
+
steps_adjusted = self.steps - 1
|
|
63
|
+
# print(
|
|
64
|
+
# f"Putting steps: {self.steps} (adjusted: {steps_adjusted}) | delay {self.delay} ms | delay: {delay} (PIO cycles)"
|
|
65
|
+
# )
|
|
66
|
+
data = ((steps_adjusted & 0xFFFF) << 16) | (delay & 0xFFFF)
|
|
67
|
+
sm.put(data)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
|
|
71
|
+
def step_pulse_pio():
|
|
72
|
+
wrap_target()
|
|
73
|
+
# Pull data (steps << 16 | delay) from FIFO into OSR
|
|
74
|
+
pull(block)
|
|
75
|
+
# Shift right 16 bits and move to Y (step count)
|
|
76
|
+
out(y, 16)
|
|
77
|
+
# Move remaining 16 bits to ISR (delay value)
|
|
78
|
+
out(isr, 16)
|
|
79
|
+
|
|
80
|
+
# Main loop - repeat for Y toggle cycles
|
|
81
|
+
label("step_loop")
|
|
82
|
+
|
|
83
|
+
# Set pin high for one cycle only
|
|
84
|
+
set(pins, 1) # pyright: ignore[reportCallIssue]
|
|
85
|
+
# Set pin low immediately (no delay while high)
|
|
86
|
+
set(pins, 0) # pyright: ignore[reportCallIssue]
|
|
87
|
+
|
|
88
|
+
# Trigger IRQ 0 to notify controller about step
|
|
89
|
+
irq(0)
|
|
90
|
+
|
|
91
|
+
# Delay countdown - restore delay from ISR to X, then count down
|
|
92
|
+
mov(x, isr)
|
|
93
|
+
label("low_delay")
|
|
94
|
+
jmp(x_dec, "low_delay")
|
|
95
|
+
|
|
96
|
+
# Decrement remaining steps (Y) and repeat if not zero
|
|
97
|
+
jmp(y_dec, "step_loop")
|
|
98
|
+
|
|
99
|
+
# All steps done, wrap to beginning for next data
|
|
100
|
+
wrap()
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class TmcMotionControlStepPio(TmcMotionControl):
|
|
104
|
+
"""STEP/DIR Motion Control class using PIO
|
|
105
|
+
|
|
106
|
+
Uses the Programmable I/O (PIO) hardware of the RP2040/RP2350
|
|
107
|
+
to generate precise step pulses. This provides more accurate
|
|
108
|
+
timing compared to software-based stepping.
|
|
109
|
+
|
|
110
|
+
Attributes:
|
|
111
|
+
pin_step: GPIO pin number for step signal
|
|
112
|
+
pin_dir: GPIO pin number for direction signal (optional)
|
|
113
|
+
pio_id: PIO block to use (0 or 1)
|
|
114
|
+
sm_id: State machine ID within the PIO block (0-3)
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def max_speed(self):
|
|
119
|
+
"""_max_speed property"""
|
|
120
|
+
return self._max_speed
|
|
121
|
+
|
|
122
|
+
@max_speed.setter
|
|
123
|
+
def max_speed(self, speed: int):
|
|
124
|
+
"""_max_speed setter"""
|
|
125
|
+
speed = abs(speed)
|
|
126
|
+
if self._max_speed != speed:
|
|
127
|
+
self._max_speed = speed
|
|
128
|
+
if speed == 0.0:
|
|
129
|
+
self._cmin = 0
|
|
130
|
+
else:
|
|
131
|
+
self._cmin = round(1000000.0 / speed)
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def acceleration(self):
|
|
135
|
+
"""_acceleration property"""
|
|
136
|
+
return self._acceleration
|
|
137
|
+
|
|
138
|
+
@acceleration.setter
|
|
139
|
+
def acceleration(self, acceleration: int):
|
|
140
|
+
"""_acceleration setter"""
|
|
141
|
+
acceleration = abs(acceleration)
|
|
142
|
+
if acceleration == 0:
|
|
143
|
+
return
|
|
144
|
+
self._acceleration = acceleration
|
|
145
|
+
# Calculate c0 per Equation 7, with correction per Equation 15
|
|
146
|
+
self._c0 = round(0.676 * math.sqrt(2.0 / acceleration) * 1000000.0)
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def pio_frequency(self):
|
|
150
|
+
"""Current PIO state machine frequency"""
|
|
151
|
+
return self._pio_frequency
|
|
152
|
+
|
|
153
|
+
def pio_irq_handler(self, sm):
|
|
154
|
+
"""IRQ handler called by PIO after each step"""
|
|
155
|
+
# Only increment position, don't double-count
|
|
156
|
+
if self._direction == Direction.CW:
|
|
157
|
+
self.current_pos += 1
|
|
158
|
+
else:
|
|
159
|
+
self.current_pos -= 1
|
|
160
|
+
self._steps_completed += 1
|
|
161
|
+
|
|
162
|
+
# print(f"IRQ: step {self._steps_completed}")
|
|
163
|
+
|
|
164
|
+
def __init__(
|
|
165
|
+
self,
|
|
166
|
+
pin_step: int,
|
|
167
|
+
pin_dir: int | None,
|
|
168
|
+
pio_id: int = 0,
|
|
169
|
+
sm_id: int = 0,
|
|
170
|
+
):
|
|
171
|
+
"""constructor
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
pin_step: GPIO pin number for step signal
|
|
175
|
+
pin_dir: GPIO pin number for direction signal (None if not used)
|
|
176
|
+
pio_id: PIO block to use (0 or 1), default 0
|
|
177
|
+
sm_id: State machine ID within the PIO block (0-3), default 0
|
|
178
|
+
"""
|
|
179
|
+
super().__init__()
|
|
180
|
+
self._pin_step = pin_step
|
|
181
|
+
self._pin_dir = pin_dir
|
|
182
|
+
self._pio_id = pio_id
|
|
183
|
+
self._sm_id = sm_id
|
|
184
|
+
|
|
185
|
+
self._sm: rp2.StateMachine | None = None
|
|
186
|
+
self._pio_frequency: int = 20000 # PIO frequency in Hz
|
|
187
|
+
self._steps_completed: int = 0
|
|
188
|
+
self._sqrt_twoa: float = 1.0
|
|
189
|
+
self._step_interval: int = 0
|
|
190
|
+
self._min_pulse_width: int = 1
|
|
191
|
+
self._last_step_time: int = 0
|
|
192
|
+
self._n: int = 0
|
|
193
|
+
self._c0: int = 0
|
|
194
|
+
self._cn: int = 0
|
|
195
|
+
self._cmin: int = 0
|
|
196
|
+
|
|
197
|
+
def init(self, tmc_logger: TmcLogger):
|
|
198
|
+
"""init: called by the Tmc class"""
|
|
199
|
+
super().init(tmc_logger)
|
|
200
|
+
self._tmc_logger.log(f"STEP Pin (PIO): {self._pin_step}", Loglevel.DEBUG)
|
|
201
|
+
self._tmc_logger.log(
|
|
202
|
+
f"Using PIO {self._pio_id}, SM {self._sm_id}", Loglevel.DEBUG
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
# Initialize the PIO state machine for step pulse generation
|
|
206
|
+
self._init_pio()
|
|
207
|
+
|
|
208
|
+
if self._pin_dir is not None:
|
|
209
|
+
self._tmc_logger.log(f"DIR Pin: {self._pin_dir}", Loglevel.DEBUG)
|
|
210
|
+
tmc_gpio.tmc_gpio.gpio_setup(
|
|
211
|
+
self._pin_dir, GpioMode.OUT, initial=int(self._direction)
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
def _init_pio(self):
|
|
215
|
+
"""Initialize or reinitialize the PIO state machine"""
|
|
216
|
+
if self._sm is not None:
|
|
217
|
+
self._sm.active(0)
|
|
218
|
+
|
|
219
|
+
# Fixed high frequency - speed control via delay values
|
|
220
|
+
self._sm = rp2.StateMachine(
|
|
221
|
+
self._pio_id * 4 + self._sm_id,
|
|
222
|
+
step_pulse_pio,
|
|
223
|
+
freq=self._pio_frequency,
|
|
224
|
+
set_base=Pin(self._pin_step),
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
# Register IRQ handler once for step counting
|
|
228
|
+
self._sm.irq(handler=self.pio_irq_handler)
|
|
229
|
+
|
|
230
|
+
self._sm.active(1)
|
|
231
|
+
|
|
232
|
+
def deinit(self):
|
|
233
|
+
"""destructor"""
|
|
234
|
+
if self._sm is not None:
|
|
235
|
+
self._sm.active(0)
|
|
236
|
+
self._sm = None
|
|
237
|
+
|
|
238
|
+
sm_key = (self._pio_id, self._sm_id)
|
|
239
|
+
if sm_key in TmcMotionControlStepPio._used_state_machines:
|
|
240
|
+
del TmcMotionControlStepPio._used_state_machines[sm_key]
|
|
241
|
+
|
|
242
|
+
if hasattr(self, "_pin_dir") and self._pin_dir is not None:
|
|
243
|
+
tmc_gpio.tmc_gpio.gpio_cleanup(self._pin_dir)
|
|
244
|
+
del self._pin_dir
|
|
245
|
+
|
|
246
|
+
def _start_pio_movement(self, steps: int):
|
|
247
|
+
"""Start the PIO with the given number of steps
|
|
248
|
+
|
|
249
|
+
Args:
|
|
250
|
+
steps: Number of steps to execute (must be positive)
|
|
251
|
+
"""
|
|
252
|
+
if self._sm is None:
|
|
253
|
+
return
|
|
254
|
+
|
|
255
|
+
# Clear TX FIFO to remove any old data from previous movements
|
|
256
|
+
# Restart the state machine to reset its state
|
|
257
|
+
self._sm.active(0)
|
|
258
|
+
# Brief delay to ensure clean stop
|
|
259
|
+
time.sleep_ms(1)
|
|
260
|
+
self._sm.restart()
|
|
261
|
+
self._sm.active(1)
|
|
262
|
+
self._steps_completed = 0
|
|
263
|
+
|
|
264
|
+
def _drain_rx_fifo(self):
|
|
265
|
+
"""Drain the RX FIFO to clear old data"""
|
|
266
|
+
if self._sm is None:
|
|
267
|
+
return
|
|
268
|
+
while self._sm.rx_fifo() > 0:
|
|
269
|
+
self._sm.get()
|
|
270
|
+
|
|
271
|
+
def set_direction(self, direction: Direction):
|
|
272
|
+
"""sets the motor shaft direction to the given value: 0 = CCW; 1 = CW
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
direction (bool): motor shaft direction: False = CCW; True = CW
|
|
276
|
+
"""
|
|
277
|
+
if self._pin_dir is None:
|
|
278
|
+
return
|
|
279
|
+
super().set_direction(direction)
|
|
280
|
+
tmc_gpio.tmc_gpio.gpio_output(self._pin_dir, int(direction))
|
|
281
|
+
|
|
282
|
+
def run_to_position_steps(
|
|
283
|
+
self, steps, movement_abs_rel: MovementAbsRel | None = None
|
|
284
|
+
) -> StopMode:
|
|
285
|
+
"""runs the motor to the given position using PIO.
|
|
286
|
+
Sends total step count to PIO and dynamically adjusts frequency
|
|
287
|
+
for acceleration and deceleration.
|
|
288
|
+
Blocks the code until finished or stopped from a different thread!
|
|
289
|
+
|
|
290
|
+
Args:
|
|
291
|
+
steps (int): amount of steps; can be negative
|
|
292
|
+
movement_abs_rel (enum): whether the movement should be absolute or relative
|
|
293
|
+
|
|
294
|
+
Returns:
|
|
295
|
+
stop (enum): how the movement was finished
|
|
296
|
+
"""
|
|
297
|
+
if self._sm is None:
|
|
298
|
+
return StopMode.HARDSTOP
|
|
299
|
+
|
|
300
|
+
print(f"cur: {self.current_pos} target: {steps} abs_rel: {movement_abs_rel}")
|
|
301
|
+
|
|
302
|
+
if movement_abs_rel is None:
|
|
303
|
+
movement_abs_rel = self.movement_abs_rel
|
|
304
|
+
|
|
305
|
+
if movement_abs_rel == MovementAbsRel.ABSOLUTE:
|
|
306
|
+
steps = steps - self.current_pos
|
|
307
|
+
|
|
308
|
+
if steps == 0:
|
|
309
|
+
return StopMode.NO
|
|
310
|
+
|
|
311
|
+
# Set direction based on steps
|
|
312
|
+
self.set_direction(Direction.CW if steps > 0 else Direction.CCW)
|
|
313
|
+
steps = abs(steps)
|
|
314
|
+
|
|
315
|
+
# Initialize movement
|
|
316
|
+
self._movement_phase = MovementPhase.ACCELERATING
|
|
317
|
+
self._start_pio_movement(steps)
|
|
318
|
+
self._n = 0 # Reset step counter for acceleration calculations
|
|
319
|
+
self._cn = self._c0 # Start with initial step interval
|
|
320
|
+
|
|
321
|
+
# Calculate acceleration/deceleration profile
|
|
322
|
+
# Steps needed to accelerate to max speed
|
|
323
|
+
steps_to_max = round(
|
|
324
|
+
(self._max_speed * self._max_speed) / (2.0 * self._acceleration)
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
# Determine if we can reach max speed
|
|
328
|
+
if steps >= 2 * steps_to_max:
|
|
329
|
+
# Full trapezoidal profile: accel -> cruise -> decel
|
|
330
|
+
accel_steps = steps_to_max
|
|
331
|
+
decel_steps = steps_to_max
|
|
332
|
+
cruise_steps = steps - accel_steps - decel_steps
|
|
333
|
+
else:
|
|
334
|
+
# Triangular profile: accel -> decel (no cruise)
|
|
335
|
+
accel_steps = steps // 2
|
|
336
|
+
decel_steps = steps - accel_steps
|
|
337
|
+
cruise_steps = 0
|
|
338
|
+
|
|
339
|
+
self._tmc_logger.log(
|
|
340
|
+
f"Total steps: {steps} | Accel: {accel_steps} | Cruise: {cruise_steps} | Decel: {decel_steps}",
|
|
341
|
+
Loglevel.MOVEMENT,
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
steps_sent = 0
|
|
345
|
+
soft_stop_initiated = False
|
|
346
|
+
|
|
347
|
+
# Main loop: send data to PIO FIFO
|
|
348
|
+
while steps_sent < steps:
|
|
349
|
+
|
|
350
|
+
# Wait for FIFO space (max 4 entries)
|
|
351
|
+
while self._sm.tx_fifo() >= 4:
|
|
352
|
+
time.sleep_us(100)
|
|
353
|
+
if self._stop != StopMode.NO:
|
|
354
|
+
break
|
|
355
|
+
|
|
356
|
+
if self._stop == StopMode.HARDSTOP:
|
|
357
|
+
self._sm.active(0) # Immediately stop PIO
|
|
358
|
+
break
|
|
359
|
+
elif self._stop == StopMode.SOFTSTOP and not soft_stop_initiated:
|
|
360
|
+
steps_sent = accel_steps + cruise_steps # Jump to deceleration phase
|
|
361
|
+
soft_stop_initiated = True
|
|
362
|
+
|
|
363
|
+
# Determine current phase and calculate delay
|
|
364
|
+
if steps_sent < accel_steps:
|
|
365
|
+
# ACCELERATION phase - send ONE step at a time for accurate ramping
|
|
366
|
+
self._movement_phase = MovementPhase.ACCELERATING
|
|
367
|
+
block_steps = 1 # Single step for acceleration
|
|
368
|
+
|
|
369
|
+
# Update step interval for next step (Equation 13)
|
|
370
|
+
# Subtract from current cn to get faster
|
|
371
|
+
self._n += 1
|
|
372
|
+
self._cn = round(self._cn - ((2.0 * self._cn) / ((4.0 * self._n) + 1)))
|
|
373
|
+
if self._cn < self._cmin:
|
|
374
|
+
self._cn = self._cmin
|
|
375
|
+
|
|
376
|
+
elif steps_sent < accel_steps + cruise_steps:
|
|
377
|
+
# CRUISE phase (constant speed) - can send larger blocks
|
|
378
|
+
self._movement_phase = MovementPhase.MAXSPEED
|
|
379
|
+
remaining_cruise = accel_steps + cruise_steps - steps_sent
|
|
380
|
+
block_steps = min(
|
|
381
|
+
remaining_cruise, 100
|
|
382
|
+
) # Larger blocks at constant speed
|
|
383
|
+
self._cn = self._cmin
|
|
384
|
+
|
|
385
|
+
else:
|
|
386
|
+
# DECELERATION phase - send ONE step at a time for accurate ramping
|
|
387
|
+
self._movement_phase = MovementPhase.DECELERATING
|
|
388
|
+
block_steps = 1 # Single step for deceleration
|
|
389
|
+
|
|
390
|
+
# Decrement n and calculate new (slower) delay
|
|
391
|
+
if self._n > 0:
|
|
392
|
+
self._n -= 1
|
|
393
|
+
if self._n > 0:
|
|
394
|
+
self._cn = round(
|
|
395
|
+
self._cn + ((2.0 * self._cn) / ((4.0 * self._n) + 1))
|
|
396
|
+
)
|
|
397
|
+
else:
|
|
398
|
+
self._cn = self._c0
|
|
399
|
+
else:
|
|
400
|
+
self._cn = self._c0
|
|
401
|
+
|
|
402
|
+
if self._cn > self._c0:
|
|
403
|
+
self._cn = self._c0
|
|
404
|
+
self._cn = min(self._cn, self._c0)
|
|
405
|
+
|
|
406
|
+
# Convert microseconds to milliseconds for PIO
|
|
407
|
+
delay_ms = max(1, self._cn // 1000)
|
|
408
|
+
|
|
409
|
+
# Send to PIO using PioData
|
|
410
|
+
self._tmc_logger.log(
|
|
411
|
+
f"Block: {block_steps} steps, delay: {delay_ms}ms, total sent: {steps_sent + block_steps}/{steps}",
|
|
412
|
+
Loglevel.MOVEMENT,
|
|
413
|
+
)
|
|
414
|
+
pio_data = PioData(block_steps, delay_ms)
|
|
415
|
+
pio_data.put(self._sm, self._pio_frequency)
|
|
416
|
+
|
|
417
|
+
steps_sent += block_steps
|
|
418
|
+
|
|
419
|
+
# Wait for all steps to complete
|
|
420
|
+
self._tmc_logger.log(
|
|
421
|
+
f"Waiting for completion: sent={steps_sent}, target={steps}, stopmode={self._stop}",
|
|
422
|
+
Loglevel.MOVEMENT,
|
|
423
|
+
)
|
|
424
|
+
timeout_counter = 0
|
|
425
|
+
while self._steps_completed < steps and self._stop == StopMode.NO:
|
|
426
|
+
time.sleep_us(100)
|
|
427
|
+
timeout_counter += 1
|
|
428
|
+
if timeout_counter % 1000 == 0: # Every second
|
|
429
|
+
self._tmc_logger.log(
|
|
430
|
+
f"Still waiting: completed={self._steps_completed}/{steps}, sent={steps_sent}",
|
|
431
|
+
Loglevel.MOVEMENT,
|
|
432
|
+
)
|
|
433
|
+
if timeout_counter > 10000: # 10 second timeout
|
|
434
|
+
self._tmc_logger.log(
|
|
435
|
+
f"TIMEOUT! completed={self._steps_completed}/{steps}",
|
|
436
|
+
Loglevel.ERROR,
|
|
437
|
+
)
|
|
438
|
+
break
|
|
439
|
+
|
|
440
|
+
self._tmc_logger.log(
|
|
441
|
+
f"Movement complete: completed={self._steps_completed}, target={steps}",
|
|
442
|
+
Loglevel.MOVEMENT,
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
# Don't unregister IRQ handler - it stays registered from _init_pio()
|
|
446
|
+
# self._sm.irq(handler=None)
|
|
447
|
+
self._movement_phase = MovementPhase.STANDSTILL
|
|
448
|
+
|
|
449
|
+
if self._stop != StopMode.NO:
|
|
450
|
+
stop_mode = self._stop
|
|
451
|
+
self._stop = StopMode.NO
|
|
452
|
+
return stop_mode
|
|
453
|
+
|
|
454
|
+
return StopMode.NO
|
|
455
|
+
|
|
456
|
+
def stop(self, stop_mode: StopMode = StopMode.HARDSTOP):
|
|
457
|
+
"""stop the current movement
|
|
458
|
+
SOFTSTOP in PIO is delayed until the current queued blocks are done, this
|
|
459
|
+
can be up to several hundred steps depending on the FIFO state.
|
|
460
|
+
HARDSTOP immediately stops the PIO state machine.
|
|
461
|
+
|
|
462
|
+
Args:
|
|
463
|
+
stop_mode (enum): whether the movement should be stopped immediately or softly
|
|
464
|
+
"""
|
|
465
|
+
super().stop(stop_mode)
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
test for Tmc2209 with gpiozero
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import time
|
|
6
|
-
import unittest
|
|
7
|
-
from gpiozero.pins.mock import MockFactory, MockPWMPin
|
|
8
|
-
from gpiozero import Device
|
|
9
|
-
|
|
10
|
-
# Konfiguriere MockFactory mit PWM-Unterstützung
|
|
11
|
-
Device.pin_factory = MockFactory(pin_class=MockPWMPin)
|
|
12
|
-
|
|
13
|
-
import src.tmc_driver.tmc_gpio as tmc_driver_gpio
|
|
14
|
-
from src.tmc_driver.tmc_gpio._tmc_gpio_board_gpiozero import GpiozeroWrapper
|
|
15
|
-
|
|
16
|
-
from src.tmc_driver import (
|
|
17
|
-
Tmc2209,
|
|
18
|
-
TmcEnableControlPin,
|
|
19
|
-
TmcMotionControlStepPwmDir,
|
|
20
|
-
MovementAbsRel,
|
|
21
|
-
)
|
|
22
|
-
from src.tmc_driver import tmc_2209
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class _FakeTmcCom:
|
|
26
|
-
"""_FakeTmcCom class for testing purposes"""
|
|
27
|
-
|
|
28
|
-
def read_int(self, addr: int, tries: int = 3): # pylint: disable=unused-argument
|
|
29
|
-
"""fake read_int method for testing purposes"""
|
|
30
|
-
value = 0
|
|
31
|
-
return value, None
|
|
32
|
-
|
|
33
|
-
def write_reg(self, addr: int, data: int):
|
|
34
|
-
"""fake write_reg method for testing purposes"""
|
|
35
|
-
# if addr == tmc_5160.XActual.ADDR:
|
|
36
|
-
# current_pos = data
|
|
37
|
-
# if addr == tmc_5160.XTarget.ADDR:
|
|
38
|
-
# current_pos = data
|
|
39
|
-
|
|
40
|
-
def write_reg_check(self, addr: int, data: int):
|
|
41
|
-
"""fake write_reg_check method for testing purposes"""
|
|
42
|
-
self.write_reg(addr, data)
|
|
43
|
-
return True
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
fake_com = _FakeTmcCom()
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
class TestTMCMove(unittest.TestCase):
|
|
50
|
-
"""TestTMCMove with gpiozero"""
|
|
51
|
-
|
|
52
|
-
@classmethod
|
|
53
|
-
def setUpClass(cls):
|
|
54
|
-
"""setUpClass"""
|
|
55
|
-
|
|
56
|
-
cls.original_tmc_gpio = tmc_driver_gpio.tmc_gpio
|
|
57
|
-
tmc_driver_gpio.tmc_gpio = GpiozeroWrapper()
|
|
58
|
-
|
|
59
|
-
@classmethod
|
|
60
|
-
def tearDownClass(cls):
|
|
61
|
-
"""tearDownClass"""
|
|
62
|
-
# Setze tmc_gpio auf die ursprüngliche Implementierung zurück
|
|
63
|
-
if cls.original_tmc_gpio is not None:
|
|
64
|
-
tmc_driver_gpio.tmc_gpio = cls.original_tmc_gpio
|
|
65
|
-
|
|
66
|
-
def setUp(self):
|
|
67
|
-
"""setUp"""
|
|
68
|
-
|
|
69
|
-
self.MovementAbsRel = MovementAbsRel
|
|
70
|
-
self.tmc = Tmc2209(TmcEnableControlPin(1), TmcMotionControlStepPwmDir(2, 3))
|
|
71
|
-
self.tmc.gconf = tmc_2209.GConf(fake_com)
|
|
72
|
-
self.tmc.sgthrs = tmc_2209.SgThrs(fake_com)
|
|
73
|
-
self.tmc.tcoolthrs = tmc_2209.TCoolThrs(fake_com)
|
|
74
|
-
|
|
75
|
-
# these values are normally set by reading the driver
|
|
76
|
-
self.tmc.mres = 2
|
|
77
|
-
|
|
78
|
-
self.tmc.acceleration_fullstep = 100000
|
|
79
|
-
self.tmc.max_speed_fullstep = 10000
|
|
80
|
-
self.tmc.movement_abs_rel = MovementAbsRel.ABSOLUTE
|
|
81
|
-
|
|
82
|
-
def tearDown(self):
|
|
83
|
-
"""tearDown"""
|
|
84
|
-
del self.tmc
|
|
85
|
-
|
|
86
|
-
def test_run_to_position_steps(self):
|
|
87
|
-
"""test_run_to_position_steps"""
|
|
88
|
-
|
|
89
|
-
self.tmc.run_to_position_steps(400, self.MovementAbsRel.RELATIVE)
|
|
90
|
-
pos = self.tmc.tmc_mc.current_pos
|
|
91
|
-
self.assertEqual(pos, 400, f"actual position: {pos}, expected position: 400")
|
|
92
|
-
|
|
93
|
-
self.tmc.run_to_position_steps(-200, self.MovementAbsRel.RELATIVE)
|
|
94
|
-
pos = self.tmc.tmc_mc.current_pos
|
|
95
|
-
self.assertEqual(pos, 200, f"actual position: {pos}, expected position: 200")
|
|
96
|
-
|
|
97
|
-
self.tmc.run_to_position_steps(400)
|
|
98
|
-
pos = self.tmc.tmc_mc.current_pos
|
|
99
|
-
self.assertEqual(pos, 400, f"actual position: {pos}, expected position: 400")
|
|
100
|
-
|
|
101
|
-
def test_stallguard_interrupt(self):
|
|
102
|
-
"""test_stallguard_interrupt"""
|
|
103
|
-
self.tmc.set_stallguard_callback(4, 100, lambda: print("Stallguard interrupt"))
|
|
104
|
-
|
|
105
|
-
def test_run_pwm(self):
|
|
106
|
-
"""test_run_to_position_steps_threaded"""
|
|
107
|
-
# continous movement using pwm
|
|
108
|
-
self.tmc.tmc_mc.run_speed_pwm_fullstep(250)
|
|
109
|
-
time.sleep(0.1)
|
|
110
|
-
self.tmc.tmc_mc.run_speed_pwm_fullstep(0)
|
|
111
|
-
time.sleep(0.1)
|
|
112
|
-
self.tmc.tmc_mc.run_speed_pwm_fullstep(-250)
|
|
113
|
-
time.sleep(0.1)
|
|
114
|
-
self.tmc.tmc_mc.run_speed_pwm_fullstep(0)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if __name__ == "__main__":
|
|
118
|
-
unittest.main()
|