loon-python-sdk 0.0.3__py3-none-any.whl → 0.0.4__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.
@@ -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,21 @@ 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
+ def __exit__(self, exc_type, exc_val, exc_tb):
59
+ self.close();
54
60
 
55
61
  @classmethod
56
62
  def from_websocket(
57
- cls,
63
+ self,
58
64
  url: str,
59
65
  on_telemetry: Optional[Callable[[List[dict]], None]] = None,
60
66
  ) -> "Loon5Arm":
@@ -71,12 +77,12 @@ class Loon5Arm:
71
77
  transport = WebSocketTransport(
72
78
  url, on_frame_received=lambda frame: instance._handle_frame(frame)
73
79
  )
74
- instance = cls(transport, on_telemetry=on_telemetry)
80
+ instance = self(transport, on_telemetry=on_telemetry)
75
81
  return instance
76
82
 
77
83
  @classmethod
78
84
  def from_serial(
79
- cls,
85
+ self,
80
86
  port: str,
81
87
  baudrate: int = 115200,
82
88
  on_telemetry: Optional[Callable[[List[dict]], None]] = None,
@@ -97,7 +103,7 @@ class Loon5Arm:
97
103
  baudrate=baudrate,
98
104
  on_frame_received=lambda frame: instance._handle_frame(frame),
99
105
  )
100
- instance = cls(transport, on_telemetry=on_telemetry)
106
+ instance = self(transport, on_telemetry=on_telemetry)
101
107
  return instance
102
108
 
103
109
  def start(self, timeout: float = 10.0) -> None:
@@ -115,6 +121,9 @@ class Loon5Arm:
115
121
  self.transport.start(timeout=timeout)
116
122
  self.started = True
117
123
 
124
+ def is_connected(self) -> bool:
125
+ return self.started;
126
+
118
127
  def _handle_frame(self, message: bytes) -> None:
119
128
  """Internal callback invoked when raw binary frames are received from transport.
120
129
 
@@ -133,8 +142,9 @@ class Loon5Arm:
133
142
  # Handle motor telemetry broadcasts (CAN status, angles, speeds, PID gains)
134
143
  if msg_type == MsgType.MOTOR_TELEMETRY:
135
144
  parsed = parse_motor_telemetry(payload)
136
- for m in parsed:
137
- self.telemetry[m["can_id"]] = m
145
+ with self._telemetry_lock:
146
+ for m in parsed:
147
+ self.telemetry[m["can_id"]] = m
138
148
  if self.on_telemetry is not None:
139
149
  self.on_telemetry(parsed)
140
150
  if not self.show_telemetry:
@@ -310,6 +320,33 @@ class Loon5Arm:
310
320
  """
311
321
  self.send(MsgType.MOTOR_ENABLE, pack_motor_enable(can_id, enabled))
312
322
 
323
+ def get_motor_telemetry(self, can_id: int) -> Optional[MotorTelemetry]:
324
+ """Return the latest telemetry for a specific motor, or None if not yet received.
325
+
326
+ Args:
327
+ can_id: CAN bus identifier of the motor.
328
+
329
+ Returns:
330
+ MotorTelemetry dataclass for the requested motor, or None if no
331
+ telemetry frame has been received for that CAN ID yet.
332
+ """
333
+ with self._telemetry_lock:
334
+ data = self.telemetry.get(can_id)
335
+ return MotorTelemetry(**data) if data is not None else None
336
+
337
+ def get_arm_telemetry(self) -> Dict[int, MotorTelemetry]:
338
+ """Return the latest telemetry for all motors that have reported in.
339
+
340
+ Returns:
341
+ Dictionary mapping each motor's CAN ID to its latest MotorTelemetry.
342
+ """
343
+ with self._telemetry_lock:
344
+ return {
345
+ can_id: MotorTelemetry(**data)
346
+ for can_id, data in self.telemetry.items()
347
+ }
348
+
349
+
313
350
  def ping(self) -> None:
314
351
  """Send a PING heartbeat request (MsgType.PING / 0x00) to verify link connectivity.
315
352
 
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
3
+ Version: 0.0.4
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=r9_Jz0HQdugJKJMdHcY7u6wy9achrqwtyD4gVh6-9sA,13555
3
+ loon_python_sdk/comms.py,sha256=fuq05aZUuZD6ONPOLG8SYSMtR8lcNY7xj3VsSL0ecEk,4839
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.4.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
7
+ loon_python_sdk-0.0.4.dist-info/METADATA,sha256=tadFa312ahvZmehXS5fm-2amEYxiZ81_rIecpYf1cGY,3571
8
+ loon_python_sdk-0.0.4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
9
+ loon_python_sdk-0.0.4.dist-info/top_level.txt,sha256=EIbERb1juDmoqdY15q8Vg434YHz6TyUggeK1ZjsbYfM,16
10
+ loon_python_sdk-0.0.4.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,,