uc2canopen 0.2.1__tar.gz → 0.2.2__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.
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/PKG-INFO +2 -2
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/examples/can_sniffer.py +1 -1
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/pyproject.toml +1 -1
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/motor_demo.py +2 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/client.py +1 -0
- uc2canopen-0.2.2/tests/test_aio.py +192 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/.github/workflows/publish.yml +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/.gitignore +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/CHANGELOG.md +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/README.md +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/examples/waveshare_bus.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/__init__.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/aio.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/cli.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/od.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/sdo.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/src/uc2canopen/waveshare_bus.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/tests/test_new_features.py +0 -0
- {uc2canopen-0.2.1 → uc2canopen-0.2.2}/uv.lock +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: uc2canopen
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Python CANopen master for openUC2 microscope hardware — control motors, lasers, LEDs, galvos directly over CAN bus (SocketCAN MCP2515 HAT or Waveshare USB-CAN-A adapter)
|
|
5
5
|
Project-URL: Homepage, https://github.com/openUC2/uc2-esp32
|
|
6
6
|
Project-URL: Documentation, https://openuc2.github.io
|
|
@@ -265,7 +265,7 @@ def sniff(serial_port: str, can_bitrate: int, serial_baudrate: int = 2_000_000):
|
|
|
265
265
|
|
|
266
266
|
if __name__ == "__main__":
|
|
267
267
|
#SERIAL_PORT = "/dev/cu.wchusbserial110" # Waveshare USB-CAN-A port
|
|
268
|
-
SERIAL_PORT = "/dev/cu.usbserial-
|
|
268
|
+
SERIAL_PORT = "/dev/cu.usbserial-10"
|
|
269
269
|
CAN_BITRATE = 500_000 # Must match the CAN network
|
|
270
270
|
SERIAL_BAUDRATE = 2_000_000 # Waveshare default
|
|
271
271
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "uc2canopen"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.2"
|
|
4
4
|
description = "Python CANopen master for openUC2 microscope hardware — control motors, lasers, LEDs, galvos directly over CAN bus (SocketCAN MCP2515 HAT or Waveshare USB-CAN-A adapter)"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = { text = "MIT" }
|
|
@@ -19,6 +19,8 @@ Usage:
|
|
|
19
19
|
|
|
20
20
|
# Waveshare USB-CAN-A adapter instead:
|
|
21
21
|
python src/motor_demo.py --interface waveshare --port /dev/ttyUSB0 --motor-node 11
|
|
22
|
+
python src/motor_demo.py --interface waveshare --port /dev/cu.usbserial-10 --motor-node 11
|
|
23
|
+
python src/motor_demo.py --interface waveshare --port /dev/cu.wchusbserial10 --motor-node 11
|
|
22
24
|
"""
|
|
23
25
|
|
|
24
26
|
from __future__ import annotations
|
|
@@ -951,6 +951,7 @@ class UC2Client:
|
|
|
951
951
|
interface: Optional[str] = None,
|
|
952
952
|
channel: Optional[str] = None,
|
|
953
953
|
log_level: int = logging.WARNING,
|
|
954
|
+
debug: bool = False
|
|
954
955
|
):
|
|
955
956
|
# Configure the package-wide logger so all sub-modules (sdo, waveshare_bus)
|
|
956
957
|
# honour the same level. The caller controls verbosity; we never install
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Tests for uc2canopen.aio — asyncio facade + TpdoListener event bridging.
|
|
2
|
+
|
|
3
|
+
Hardware-free: synthetic `can.Message` frames are pushed straight into a
|
|
4
|
+
`TpdoListener`, and a stub client stands in for the SDO side, so we can
|
|
5
|
+
assert the full event pipeline (TPDO/heartbeat -> typed async events) and
|
|
6
|
+
`move_and_wait`'s done-edge / cancellation semantics.
|
|
7
|
+
|
|
8
|
+
Run:
|
|
9
|
+
pytest tests/test_aio.py -v
|
|
10
|
+
# or, without pytest installed:
|
|
11
|
+
python tests/test_aio.py
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import asyncio
|
|
17
|
+
import struct
|
|
18
|
+
import sys
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))
|
|
22
|
+
|
|
23
|
+
import can # noqa: E402
|
|
24
|
+
|
|
25
|
+
from uc2canopen.aio import ( # noqa: E402
|
|
26
|
+
AsyncUC2Client,
|
|
27
|
+
HeartbeatEvent,
|
|
28
|
+
HomingChangedEvent,
|
|
29
|
+
MotorDoneEvent,
|
|
30
|
+
MotorUpdateEvent,
|
|
31
|
+
)
|
|
32
|
+
from uc2canopen.client import TpdoListener # noqa: E402
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def tpdo1_frame(node_id: int, position: int, running: bool, homing: int = 0) -> can.Message:
|
|
36
|
+
"""Build a synthetic TPDO1 frame (i32 position + u8 status + u8 homing)."""
|
|
37
|
+
data = struct.pack("<i", position) + bytes([1 if running else 0, homing])
|
|
38
|
+
return can.Message(arbitration_id=0x180 + node_id, data=data, is_extended_id=False)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def heartbeat_frame(node_id: int, nmt_state: int = 0x05) -> can.Message:
|
|
42
|
+
"""Build a synthetic heartbeat frame."""
|
|
43
|
+
return can.Message(arbitration_id=0x700 + node_id, data=bytes([nmt_state]), is_extended_id=False)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class StubMotor:
|
|
47
|
+
"""Records move/stop calls; no I/O."""
|
|
48
|
+
|
|
49
|
+
default_node = 11
|
|
50
|
+
|
|
51
|
+
def __init__(self):
|
|
52
|
+
self.moves = []
|
|
53
|
+
self.stops = []
|
|
54
|
+
|
|
55
|
+
def move(self, **kwargs):
|
|
56
|
+
self.moves.append(kwargs)
|
|
57
|
+
|
|
58
|
+
def stop(self, axis=0, node_id=None):
|
|
59
|
+
self.stops.append((axis, node_id))
|
|
60
|
+
|
|
61
|
+
def wait_for_idle(self, axis=0, node_id=None, timeout=5.0):
|
|
62
|
+
return True
|
|
63
|
+
|
|
64
|
+
def get_position(self, axis=0, node_id=None):
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class StubClient:
|
|
69
|
+
"""Stands in for UC2Client: a real TpdoListener + recording motor."""
|
|
70
|
+
|
|
71
|
+
def __init__(self):
|
|
72
|
+
self._listener = TpdoListener(bus=None)
|
|
73
|
+
self.motor = StubMotor()
|
|
74
|
+
|
|
75
|
+
def close(self):
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_events_bridge_tpdo_and_heartbeat():
|
|
80
|
+
"""TPDO1/heartbeat frames come out of events() as typed async events."""
|
|
81
|
+
|
|
82
|
+
async def scenario():
|
|
83
|
+
stub = StubClient()
|
|
84
|
+
client = AsyncUC2Client(stub) # type: ignore[arg-type]
|
|
85
|
+
|
|
86
|
+
received = []
|
|
87
|
+
|
|
88
|
+
async def collect():
|
|
89
|
+
# frame1 -> update; frame2 -> update + homing-change + done-edge;
|
|
90
|
+
# heartbeat -> 1. Five events total.
|
|
91
|
+
async for event in client.events():
|
|
92
|
+
received.append(event)
|
|
93
|
+
if len(received) >= 5:
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
collector = asyncio.create_task(collect())
|
|
97
|
+
await asyncio.sleep(0) # let the collector subscribe
|
|
98
|
+
|
|
99
|
+
listener = stub._listener
|
|
100
|
+
listener.on_message_received(tpdo1_frame(11, 100, running=True))
|
|
101
|
+
listener.on_message_received(tpdo1_frame(11, 500, running=False, homing=2))
|
|
102
|
+
listener.on_message_received(heartbeat_frame(12))
|
|
103
|
+
await asyncio.wait_for(collector, timeout=2.0)
|
|
104
|
+
return received
|
|
105
|
+
|
|
106
|
+
received = asyncio.run(scenario())
|
|
107
|
+
|
|
108
|
+
updates = [e for e in received if isinstance(e, MotorUpdateEvent)]
|
|
109
|
+
assert len(updates) == 2 and updates[0].position == 100 and updates[0].running
|
|
110
|
+
dones = [e for e in received if isinstance(e, MotorDoneEvent)]
|
|
111
|
+
assert len(dones) == 1 and dones[0].position == 500 and dones[0].node_id == 11
|
|
112
|
+
homings = [e for e in received if isinstance(e, HomingChangedEvent)]
|
|
113
|
+
assert len(homings) == 1 and homings[0].status == 2
|
|
114
|
+
beats = [e for e in received if isinstance(e, HeartbeatEvent)]
|
|
115
|
+
assert len(beats) == 1 and beats[0].node_id == 12
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_move_and_wait_resolves_on_done_edge():
|
|
119
|
+
"""move_and_wait returns the final position when the running->stopped edge arrives."""
|
|
120
|
+
|
|
121
|
+
async def scenario():
|
|
122
|
+
stub = StubClient()
|
|
123
|
+
client = AsyncUC2Client(stub) # type: ignore[arg-type]
|
|
124
|
+
|
|
125
|
+
async def inject_done():
|
|
126
|
+
await asyncio.sleep(0.05)
|
|
127
|
+
stub._listener.on_message_received(tpdo1_frame(11, 0, running=True))
|
|
128
|
+
stub._listener.on_message_received(tpdo1_frame(11, 1234, running=False))
|
|
129
|
+
|
|
130
|
+
injector = asyncio.create_task(inject_done())
|
|
131
|
+
final = await client.move_and_wait(axis=0, position=1234, node_id=11, timeout=2.0)
|
|
132
|
+
await injector
|
|
133
|
+
return final, stub.motor.moves
|
|
134
|
+
|
|
135
|
+
final, moves = asyncio.run(scenario())
|
|
136
|
+
assert final == 1234
|
|
137
|
+
assert len(moves) == 1 and moves[0]["position"] == 1234
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_move_and_wait_stops_motor_on_cancellation():
|
|
141
|
+
"""Cancelling a pending move sends a stop to the hardware."""
|
|
142
|
+
|
|
143
|
+
async def scenario():
|
|
144
|
+
stub = StubClient()
|
|
145
|
+
client = AsyncUC2Client(stub) # type: ignore[arg-type]
|
|
146
|
+
|
|
147
|
+
task = asyncio.create_task(
|
|
148
|
+
client.move_and_wait(axis=0, position=99, node_id=11, timeout=10.0)
|
|
149
|
+
)
|
|
150
|
+
await asyncio.sleep(0.05)
|
|
151
|
+
task.cancel()
|
|
152
|
+
try:
|
|
153
|
+
await task
|
|
154
|
+
except asyncio.CancelledError:
|
|
155
|
+
pass
|
|
156
|
+
return stub.motor.stops
|
|
157
|
+
|
|
158
|
+
stops = asyncio.run(scenario())
|
|
159
|
+
assert stops == [(0, 11)]
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_callbacks_fire_outside_listener_lock():
|
|
163
|
+
"""A callback may safely read back cached state (regression: lock deadlock)."""
|
|
164
|
+
listener = TpdoListener(bus=None)
|
|
165
|
+
seen = []
|
|
166
|
+
|
|
167
|
+
def callback(node_id, axis, state):
|
|
168
|
+
# This get_motor() call would deadlock if callbacks fired under the lock.
|
|
169
|
+
seen.append(listener.get_motor(node_id, axis))
|
|
170
|
+
|
|
171
|
+
listener.on_motor_update(callback)
|
|
172
|
+
listener.on_message_received(tpdo1_frame(11, 42, running=False))
|
|
173
|
+
assert len(seen) == 1 and seen[0].position == 42
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _main() -> int:
|
|
177
|
+
"""Self-runner (no pytest needed)."""
|
|
178
|
+
failures = 0
|
|
179
|
+
for name, fn in sorted(globals().items()):
|
|
180
|
+
if name.startswith("test_") and callable(fn):
|
|
181
|
+
try:
|
|
182
|
+
fn()
|
|
183
|
+
print(f"PASS {name}")
|
|
184
|
+
except Exception as exc: # noqa: BLE001
|
|
185
|
+
failures += 1
|
|
186
|
+
print(f"FAIL {name}: {exc!r}")
|
|
187
|
+
print(f"{'OK' if not failures else 'FAILED'}")
|
|
188
|
+
return failures
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
if __name__ == "__main__":
|
|
192
|
+
raise SystemExit(_main())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|