loon-python-sdk 0.0.3__py3-none-any.whl → 0.0.5__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.
- loon_python_sdk/__init__.py +4 -1
- loon_python_sdk/client.py +45 -7
- loon_python_sdk/comms.py +5 -3
- loon_python_sdk/enums.py +29 -2
- {loon_python_sdk-0.0.3.dist-info → loon_python_sdk-0.0.5.dist-info}/METADATA +1 -1
- loon_python_sdk-0.0.5.dist-info/RECORD +10 -0
- loon_python_sdk-0.0.3.dist-info/RECORD +0 -10
- {loon_python_sdk-0.0.3.dist-info → loon_python_sdk-0.0.5.dist-info}/WHEEL +0 -0
- {loon_python_sdk-0.0.3.dist-info → loon_python_sdk-0.0.5.dist-info}/licenses/LICENSE +0 -0
- {loon_python_sdk-0.0.3.dist-info → loon_python_sdk-0.0.5.dist-info}/top_level.txt +0 -0
loon_python_sdk/__init__.py
CHANGED
|
@@ -4,13 +4,16 @@ Official Python SDK for communicating with and controlling the Loon ESP32 robot
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from .client import Loon5Arm
|
|
7
|
-
from .enums import TransportType, MsgType
|
|
7
|
+
from .enums import TransportType, MsgType, MotorIds, MotorConnectionStatus, MotorTelemetry
|
|
8
8
|
from .transport import SerialTransport, Transport, WebSocketTransport
|
|
9
9
|
|
|
10
10
|
__all__ = [
|
|
11
11
|
"Loon5Arm",
|
|
12
12
|
"TransportType",
|
|
13
13
|
"MsgType",
|
|
14
|
+
"MotorIds",
|
|
15
|
+
"MotorConnectionStatus",
|
|
16
|
+
"MotorTelemetry",
|
|
14
17
|
"Transport",
|
|
15
18
|
"WebSocketTransport",
|
|
16
19
|
"SerialTransport",
|
loon_python_sdk/client.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import threading
|
|
1
2
|
from typing import Callable, Dict, List, Optional
|
|
2
3
|
|
|
3
4
|
from loon_python_sdk.comms import (
|
|
@@ -13,7 +14,7 @@ from loon_python_sdk.comms import (
|
|
|
13
14
|
pack_pose,
|
|
14
15
|
parse_motor_telemetry,
|
|
15
16
|
)
|
|
16
|
-
from loon_python_sdk.enums import MsgType
|
|
17
|
+
from loon_python_sdk.enums import MotorTelemetry, MsgType
|
|
17
18
|
from loon_python_sdk.transport import SerialTransport, Transport, WebSocketTransport
|
|
18
19
|
|
|
19
20
|
|
|
@@ -45,16 +46,22 @@ class Loon5Arm:
|
|
|
45
46
|
self.transport = transport
|
|
46
47
|
self.on_telemetry = on_telemetry
|
|
47
48
|
self.telemetry: Dict[int, dict] = {}
|
|
49
|
+
self._telemetry_lock = threading.Lock()
|
|
48
50
|
self.started: bool = False
|
|
49
51
|
|
|
50
52
|
# Telemetry (~5/sec) and heartbeat (~1/3sec) are broadcast continuously by the
|
|
51
53
|
# ESP32 and quickly bury any other output, so they're hidden by default.
|
|
52
54
|
self.show_telemetry: bool = False
|
|
53
55
|
self.show_heartbeat: bool = False
|
|
56
|
+
def __enter__(self):
|
|
57
|
+
self.start();
|
|
58
|
+
return self;
|
|
59
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
60
|
+
self.close();
|
|
54
61
|
|
|
55
62
|
@classmethod
|
|
56
63
|
def from_websocket(
|
|
57
|
-
|
|
64
|
+
self,
|
|
58
65
|
url: str,
|
|
59
66
|
on_telemetry: Optional[Callable[[List[dict]], None]] = None,
|
|
60
67
|
) -> "Loon5Arm":
|
|
@@ -71,12 +78,12 @@ class Loon5Arm:
|
|
|
71
78
|
transport = WebSocketTransport(
|
|
72
79
|
url, on_frame_received=lambda frame: instance._handle_frame(frame)
|
|
73
80
|
)
|
|
74
|
-
instance =
|
|
81
|
+
instance = self(transport, on_telemetry=on_telemetry)
|
|
75
82
|
return instance
|
|
76
83
|
|
|
77
84
|
@classmethod
|
|
78
85
|
def from_serial(
|
|
79
|
-
|
|
86
|
+
self,
|
|
80
87
|
port: str,
|
|
81
88
|
baudrate: int = 115200,
|
|
82
89
|
on_telemetry: Optional[Callable[[List[dict]], None]] = None,
|
|
@@ -97,7 +104,7 @@ class Loon5Arm:
|
|
|
97
104
|
baudrate=baudrate,
|
|
98
105
|
on_frame_received=lambda frame: instance._handle_frame(frame),
|
|
99
106
|
)
|
|
100
|
-
instance =
|
|
107
|
+
instance = self(transport, on_telemetry=on_telemetry)
|
|
101
108
|
return instance
|
|
102
109
|
|
|
103
110
|
def start(self, timeout: float = 10.0) -> None:
|
|
@@ -115,6 +122,9 @@ class Loon5Arm:
|
|
|
115
122
|
self.transport.start(timeout=timeout)
|
|
116
123
|
self.started = True
|
|
117
124
|
|
|
125
|
+
def is_connected(self) -> bool:
|
|
126
|
+
return self.started;
|
|
127
|
+
|
|
118
128
|
def _handle_frame(self, message: bytes) -> None:
|
|
119
129
|
"""Internal callback invoked when raw binary frames are received from transport.
|
|
120
130
|
|
|
@@ -133,8 +143,9 @@ class Loon5Arm:
|
|
|
133
143
|
# Handle motor telemetry broadcasts (CAN status, angles, speeds, PID gains)
|
|
134
144
|
if msg_type == MsgType.MOTOR_TELEMETRY:
|
|
135
145
|
parsed = parse_motor_telemetry(payload)
|
|
136
|
-
|
|
137
|
-
|
|
146
|
+
with self._telemetry_lock:
|
|
147
|
+
for m in parsed:
|
|
148
|
+
self.telemetry[m["can_id"]] = m
|
|
138
149
|
if self.on_telemetry is not None:
|
|
139
150
|
self.on_telemetry(parsed)
|
|
140
151
|
if not self.show_telemetry:
|
|
@@ -310,6 +321,33 @@ class Loon5Arm:
|
|
|
310
321
|
"""
|
|
311
322
|
self.send(MsgType.MOTOR_ENABLE, pack_motor_enable(can_id, enabled))
|
|
312
323
|
|
|
324
|
+
def get_motor_telemetry(self, can_id: int) -> Optional[MotorTelemetry]:
|
|
325
|
+
"""Return the latest telemetry for a specific motor, or None if not yet received.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
can_id: CAN bus identifier of the motor.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
MotorTelemetry dataclass for the requested motor, or None if no
|
|
332
|
+
telemetry frame has been received for that CAN ID yet.
|
|
333
|
+
"""
|
|
334
|
+
with self._telemetry_lock:
|
|
335
|
+
data = self.telemetry.get(can_id)
|
|
336
|
+
return MotorTelemetry(**data) if data is not None else None
|
|
337
|
+
|
|
338
|
+
def get_arm_telemetry(self) -> Dict[int, MotorTelemetry]:
|
|
339
|
+
"""Return the latest telemetry for all motors that have reported in.
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
Dictionary mapping each motor's CAN ID to its latest MotorTelemetry.
|
|
343
|
+
"""
|
|
344
|
+
with self._telemetry_lock:
|
|
345
|
+
return {
|
|
346
|
+
can_id: MotorTelemetry(**data)
|
|
347
|
+
for can_id, data in self.telemetry.items()
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
|
|
313
351
|
def ping(self) -> None:
|
|
314
352
|
"""Send a PING heartbeat request (MsgType.PING / 0x00) to verify link connectivity.
|
|
315
353
|
|
loon_python_sdk/comms.py
CHANGED
|
@@ -70,8 +70,8 @@ def pack_homing_cmd(can_id):
|
|
|
70
70
|
return struct.pack("<B", can_id)
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
MOTOR_STATE_WIRE_FMT = "<
|
|
74
|
-
MOTOR_STATE_WIRE_SIZE = struct.calcsize(MOTOR_STATE_WIRE_FMT) #
|
|
73
|
+
MOTOR_STATE_WIRE_FMT = "<BBB6fBH"
|
|
74
|
+
MOTOR_STATE_WIRE_SIZE = struct.calcsize(MOTOR_STATE_WIRE_FMT) # 30 bytes (trailing `reversed` flag)
|
|
75
75
|
ACK_FMT = "<BB32s"
|
|
76
76
|
MOTOR_STATUS_NAMES = {0: "offline", 1: "online", 2: "error"}
|
|
77
77
|
|
|
@@ -83,7 +83,7 @@ def parse_motor_telemetry(payload: bytes) -> list:
|
|
|
83
83
|
motors = []
|
|
84
84
|
for i in range(count):
|
|
85
85
|
chunk = payload[i * MOTOR_STATE_WIRE_SIZE:(i + 1) * MOTOR_STATE_WIRE_SIZE]
|
|
86
|
-
can_id, enabled, status, pos, target, speed, kp, ki, kd, reversed_ = struct.unpack(MOTOR_STATE_WIRE_FMT, chunk)
|
|
86
|
+
can_id, enabled, status, pos, target, speed, kp, ki, kd, reversed_, workingCurrentMa = struct.unpack(MOTOR_STATE_WIRE_FMT, chunk)
|
|
87
87
|
motors.append({
|
|
88
88
|
"can_id": can_id,
|
|
89
89
|
"enabled": bool(enabled),
|
|
@@ -95,6 +95,7 @@ def parse_motor_telemetry(payload: bytes) -> list:
|
|
|
95
95
|
"ki": ki,
|
|
96
96
|
"kd": kd,
|
|
97
97
|
"reversed": bool(reversed_),
|
|
98
|
+
"working_current_ma": workingCurrentMa
|
|
98
99
|
})
|
|
99
100
|
return motors
|
|
100
101
|
|
|
@@ -127,6 +128,7 @@ def describe_payload(msg_type: int, payload: bytes) -> str:
|
|
|
127
128
|
f"pos={m['position_deg']:.2f} target={m['target_position_deg']:.2f} speed={m['speed_rpm']:.2f} "
|
|
128
129
|
f"pid=({m['kp']:.3f},{m['ki']:.3f},{m['kd']:.3f})"
|
|
129
130
|
f"{' REVERSED' if m['reversed'] else ''}"
|
|
131
|
+
f"current={m['working_current_ma']}mA "
|
|
130
132
|
)
|
|
131
133
|
return "MOTOR_TELEMETRY:\n" + "\n".join(lines)
|
|
132
134
|
return f"{t.name}, {len(payload)} bytes: {payload.hex()}"
|
loon_python_sdk/enums.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
1
2
|
from enum import IntEnum
|
|
2
3
|
|
|
3
4
|
|
|
@@ -17,10 +18,36 @@ class MsgType(IntEnum):
|
|
|
17
18
|
EXECUTE_HOMING = 0x25
|
|
18
19
|
HOME_ARM = 0x26
|
|
19
20
|
MOTOR_SET_CURRENT = 0x27
|
|
20
|
-
MOTOR_SET_ACCEL = 0x28
|
|
21
|
-
MOTOR_SET_SPEED = 0x29
|
|
22
21
|
MOTOR_TELEMETRY = 0x30
|
|
23
22
|
|
|
23
|
+
|
|
24
24
|
class TransportType(IntEnum):
|
|
25
25
|
WEBSOCKET = 0
|
|
26
26
|
SERIAL = 1
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class MotorConnectionStatus(IntEnum):
|
|
30
|
+
OFFLINE = 0
|
|
31
|
+
ONLINE = 1
|
|
32
|
+
ERROR = 2
|
|
33
|
+
|
|
34
|
+
class MotorIds(IntEnum):
|
|
35
|
+
BASE = 1
|
|
36
|
+
SHOULDER = 2
|
|
37
|
+
ELBOW = 3
|
|
38
|
+
WRIST = 4
|
|
39
|
+
END = 5
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class MotorTelemetry:
|
|
43
|
+
can_id: int
|
|
44
|
+
enabled: bool
|
|
45
|
+
status: int # MotorConnectionStatus
|
|
46
|
+
position_deg: float
|
|
47
|
+
target_position_deg: float
|
|
48
|
+
speed_rpm: float
|
|
49
|
+
kp: float
|
|
50
|
+
ki: float
|
|
51
|
+
kd: float
|
|
52
|
+
reversed: bool
|
|
53
|
+
working_current_ma: int = 0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: loon-python-sdk
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.5
|
|
4
4
|
Summary: Python SDK for the Loon Industries robotic arm
|
|
5
5
|
Author-email: Jake Horner <jakehorner@loon.industries>, Ryan Erickson <ryanerickson@loon.industries>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
loon_python_sdk/__init__.py,sha256=VIN6oY8qMtNuILofqKzZrinJeJowZkESeqdgk3ua2Rc,501
|
|
2
|
+
loon_python_sdk/client.py,sha256=WfPjjBk0KdntTAz-sgf2J0JRUtHsVaJAf4ak916RbJ0,13576
|
|
3
|
+
loon_python_sdk/comms.py,sha256=r2lMEpqoFMXRgNYZvS1YY25Kzdsi1X9h3O6sGBEd0-Q,4965
|
|
4
|
+
loon_python_sdk/enums.py,sha256=0fDuokqFiyIdxKZi8Xns24zHbPDjJd_-qnl_OZcnhRY,969
|
|
5
|
+
loon_python_sdk/transport.py,sha256=OlgcqVN-b4bNRBYlTtyoq8v9CnhqYNg8iuKNxzeMk84,6695
|
|
6
|
+
loon_python_sdk-0.0.5.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
7
|
+
loon_python_sdk-0.0.5.dist-info/METADATA,sha256=6g8cJcIhWC6fEgp8md3GyOXQ7mdr1l6IMplzf0XDJpE,3571
|
|
8
|
+
loon_python_sdk-0.0.5.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
9
|
+
loon_python_sdk-0.0.5.dist-info/top_level.txt,sha256=EIbERb1juDmoqdY15q8Vg434YHz6TyUggeK1ZjsbYfM,16
|
|
10
|
+
loon_python_sdk-0.0.5.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
loon_python_sdk/__init__.py,sha256=kSGkTLfCeWd9ip6UcdsgkkEJgGO4egvqT1n1MOtLZZ4,385
|
|
2
|
-
loon_python_sdk/client.py,sha256=S_I0xTtBueXGG7JeSaRcutOu9uEpAw79tmnb_YE1LUU,12248
|
|
3
|
-
loon_python_sdk/comms.py,sha256=fuq05aZUuZD6ONPOLG8SYSMtR8lcNY7xj3VsSL0ecEk,4839
|
|
4
|
-
loon_python_sdk/enums.py,sha256=VIUItcvmwHhUmIGsClUoxJHLm8WYvPGsF1tiVjj40cs,529
|
|
5
|
-
loon_python_sdk/transport.py,sha256=OlgcqVN-b4bNRBYlTtyoq8v9CnhqYNg8iuKNxzeMk84,6695
|
|
6
|
-
loon_python_sdk-0.0.3.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
7
|
-
loon_python_sdk-0.0.3.dist-info/METADATA,sha256=RwFttlcFvtdZrlKOGbO-YRf1hAIoZ0Ups7VNOQvL3BI,3571
|
|
8
|
-
loon_python_sdk-0.0.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
9
|
-
loon_python_sdk-0.0.3.dist-info/top_level.txt,sha256=EIbERb1juDmoqdY15q8Vg434YHz6TyUggeK1ZjsbYfM,16
|
|
10
|
-
loon_python_sdk-0.0.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|