rfabric-control-wire 0.1.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.
- rfabric_control_wire-0.1.0/.gitignore +21 -0
- rfabric_control_wire-0.1.0/PKG-INFO +61 -0
- rfabric_control_wire-0.1.0/README.md +34 -0
- rfabric_control_wire-0.1.0/pyproject.toml +63 -0
- rfabric_control_wire-0.1.0/src/rfabric_control_wire/__init__.py +83 -0
- rfabric_control_wire-0.1.0/src/rfabric_control_wire/payloads.py +192 -0
- rfabric_control_wire-0.1.0/src/rfabric_control_wire/py.typed +0 -0
- rfabric_control_wire-0.1.0/src/rfabric_control_wire/wire.py +132 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Build & distribution
|
|
2
|
+
dist/
|
|
3
|
+
build/
|
|
4
|
+
*.egg-info/
|
|
5
|
+
|
|
6
|
+
# Virtual environments
|
|
7
|
+
.venv/
|
|
8
|
+
venv/
|
|
9
|
+
|
|
10
|
+
# Python cache
|
|
11
|
+
__pycache__/
|
|
12
|
+
*.py[cod]
|
|
13
|
+
|
|
14
|
+
# Test & coverage
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.coverage
|
|
17
|
+
htmlcov/
|
|
18
|
+
|
|
19
|
+
# Tooling
|
|
20
|
+
.mypy_cache/
|
|
21
|
+
.ruff_cache/
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rfabric-control-wire
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Wire format primitives for the rFabric realtime control bridge (UDS/CBOR Frame envelope + payload constants)
|
|
5
|
+
Project-URL: Homepage, https://rfabric.io
|
|
6
|
+
Project-URL: Repository, https://github.com/rfabric/rfabric
|
|
7
|
+
Author-email: rFabric <engineering@rfabric.io>
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Keywords: cbor,rfabric,robotics,teleoperation,uds
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: cbor2<6.0.0,>=5.6.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# rfabric-control-wire
|
|
29
|
+
|
|
30
|
+
Wire-format primitives shared between every Python consumer of the rFabric
|
|
31
|
+
realtime control bridge.
|
|
32
|
+
|
|
33
|
+
The Rust agent (`rfabric-agent`) terminates the operator-side WebRTC data
|
|
34
|
+
channel and forwards length-prefixed CBOR `Frame`s onto a Unix-domain socket.
|
|
35
|
+
This package owns the Python side of that contract:
|
|
36
|
+
|
|
37
|
+
- `Frame`, `read_frame`, `write_frame` — length-prefixed CBOR framing.
|
|
38
|
+
- `CONTROL_KIND` / `STATE_KIND` / `MODE_KIND` / `ACK_KIND` — frame envelope
|
|
39
|
+
kinds.
|
|
40
|
+
- `PAYLOAD_*` constants and lightweight payload dataclasses
|
|
41
|
+
(`JointTargetsPayload`, `JointDeltasPayload`, `JointVelocityPayload`,
|
|
42
|
+
`EndEffectorPayload`, `StopPayload`, `HomePayload`) — the discriminated set
|
|
43
|
+
of `payload.kind` values an operator may send.
|
|
44
|
+
|
|
45
|
+
Consumers today: the lerobot teleoperator (`rfabric_remote`) and the
|
|
46
|
+
standalone YAM bridge (`rfabric-yam-bridge`). Anything else that wants to
|
|
47
|
+
implement a follower binds the same UDS, accepts the same frames, and uses
|
|
48
|
+
this package to decode them.
|
|
49
|
+
|
|
50
|
+
## Wire envelope
|
|
51
|
+
|
|
52
|
+
Each frame on the wire is
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
[ u32 little-endian length ][ CBOR-encoded Frame map ]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
where the CBOR map carries `kind`, `seq`, `tsNs`, and `payload`. The same
|
|
59
|
+
shape is emitted by `agent-src/src/media/envelope.rs` and
|
|
60
|
+
`app/src/frontend/components/intervention/envelope.ts` — so this package is
|
|
61
|
+
the canonical Python mirror of that single contract.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# rfabric-control-wire
|
|
2
|
+
|
|
3
|
+
Wire-format primitives shared between every Python consumer of the rFabric
|
|
4
|
+
realtime control bridge.
|
|
5
|
+
|
|
6
|
+
The Rust agent (`rfabric-agent`) terminates the operator-side WebRTC data
|
|
7
|
+
channel and forwards length-prefixed CBOR `Frame`s onto a Unix-domain socket.
|
|
8
|
+
This package owns the Python side of that contract:
|
|
9
|
+
|
|
10
|
+
- `Frame`, `read_frame`, `write_frame` — length-prefixed CBOR framing.
|
|
11
|
+
- `CONTROL_KIND` / `STATE_KIND` / `MODE_KIND` / `ACK_KIND` — frame envelope
|
|
12
|
+
kinds.
|
|
13
|
+
- `PAYLOAD_*` constants and lightweight payload dataclasses
|
|
14
|
+
(`JointTargetsPayload`, `JointDeltasPayload`, `JointVelocityPayload`,
|
|
15
|
+
`EndEffectorPayload`, `StopPayload`, `HomePayload`) — the discriminated set
|
|
16
|
+
of `payload.kind` values an operator may send.
|
|
17
|
+
|
|
18
|
+
Consumers today: the lerobot teleoperator (`rfabric_remote`) and the
|
|
19
|
+
standalone YAM bridge (`rfabric-yam-bridge`). Anything else that wants to
|
|
20
|
+
implement a follower binds the same UDS, accepts the same frames, and uses
|
|
21
|
+
this package to decode them.
|
|
22
|
+
|
|
23
|
+
## Wire envelope
|
|
24
|
+
|
|
25
|
+
Each frame on the wire is
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
[ u32 little-endian length ][ CBOR-encoded Frame map ]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
where the CBOR map carries `kind`, `seq`, `tsNs`, and `payload`. The same
|
|
32
|
+
shape is emitted by `agent-src/src/media/envelope.rs` and
|
|
33
|
+
`app/src/frontend/components/intervention/envelope.ts` — so this package is
|
|
34
|
+
the canonical Python mirror of that single contract.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rfabric-control-wire"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Wire format primitives for the rFabric realtime control bridge (UDS/CBOR Frame envelope + payload constants)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [{ name = "rFabric", email = "engineering@rfabric.io" }]
|
|
13
|
+
keywords = ["rfabric", "robotics", "teleoperation", "cbor", "uds"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: Apache Software License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"cbor2>=5.6.0,<6.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=8.0",
|
|
33
|
+
"mypy>=1.10",
|
|
34
|
+
"ruff>=0.5",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://rfabric.io"
|
|
39
|
+
Repository = "https://github.com/rfabric/rfabric"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/rfabric_control_wire"]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.sdist]
|
|
45
|
+
include = ["src/rfabric_control_wire", "README.md", "pyproject.toml"]
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
line-length = 100
|
|
49
|
+
target-version = "py310"
|
|
50
|
+
|
|
51
|
+
[tool.ruff.lint]
|
|
52
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF"]
|
|
53
|
+
ignore = ["E501"]
|
|
54
|
+
|
|
55
|
+
[tool.mypy]
|
|
56
|
+
python_version = "3.10"
|
|
57
|
+
strict = true
|
|
58
|
+
warn_unused_ignores = true
|
|
59
|
+
warn_redundant_casts = true
|
|
60
|
+
|
|
61
|
+
[tool.pytest.ini_options]
|
|
62
|
+
testpaths = ["tests"]
|
|
63
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Public surface of the rFabric realtime-control wire package."""
|
|
2
|
+
|
|
3
|
+
from rfabric_control_wire.payloads import (
|
|
4
|
+
GRIPPER_CLOSE,
|
|
5
|
+
GRIPPER_HOLD,
|
|
6
|
+
GRIPPER_OPEN,
|
|
7
|
+
PAYLOAD_END_EFFECTOR,
|
|
8
|
+
PAYLOAD_HEARTBEAT,
|
|
9
|
+
PAYLOAD_HOME,
|
|
10
|
+
PAYLOAD_JOINT_DELTAS,
|
|
11
|
+
PAYLOAD_JOINT_TARGETS,
|
|
12
|
+
PAYLOAD_JOINT_VELOCITY,
|
|
13
|
+
PAYLOAD_RERECORD,
|
|
14
|
+
PAYLOAD_STOP,
|
|
15
|
+
PAYLOAD_SUCCESS,
|
|
16
|
+
PAYLOAD_TERMINATE,
|
|
17
|
+
PAYLOAD_TWIST,
|
|
18
|
+
ControlPayload,
|
|
19
|
+
EndEffectorPayload,
|
|
20
|
+
HeartbeatPayload,
|
|
21
|
+
HomePayload,
|
|
22
|
+
JointDeltasPayload,
|
|
23
|
+
JointTargetsPayload,
|
|
24
|
+
JointVelocityPayload,
|
|
25
|
+
PayloadError,
|
|
26
|
+
StopPayload,
|
|
27
|
+
parse_control_payload,
|
|
28
|
+
)
|
|
29
|
+
from rfabric_control_wire.wire import (
|
|
30
|
+
ACK_KIND,
|
|
31
|
+
CONTROL_KIND,
|
|
32
|
+
LENGTH_PREFIX_FORMAT,
|
|
33
|
+
LENGTH_PREFIX_SIZE,
|
|
34
|
+
MAX_FRAME_BYTES,
|
|
35
|
+
MODE_KIND,
|
|
36
|
+
STATE_KIND,
|
|
37
|
+
VALID_KINDS,
|
|
38
|
+
Frame,
|
|
39
|
+
FrameError,
|
|
40
|
+
now_ts_ns,
|
|
41
|
+
read_frame,
|
|
42
|
+
write_frame,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"ACK_KIND",
|
|
47
|
+
"CONTROL_KIND",
|
|
48
|
+
"ControlPayload",
|
|
49
|
+
"EndEffectorPayload",
|
|
50
|
+
"Frame",
|
|
51
|
+
"FrameError",
|
|
52
|
+
"GRIPPER_CLOSE",
|
|
53
|
+
"GRIPPER_HOLD",
|
|
54
|
+
"GRIPPER_OPEN",
|
|
55
|
+
"HeartbeatPayload",
|
|
56
|
+
"HomePayload",
|
|
57
|
+
"JointDeltasPayload",
|
|
58
|
+
"JointTargetsPayload",
|
|
59
|
+
"JointVelocityPayload",
|
|
60
|
+
"LENGTH_PREFIX_FORMAT",
|
|
61
|
+
"LENGTH_PREFIX_SIZE",
|
|
62
|
+
"MAX_FRAME_BYTES",
|
|
63
|
+
"MODE_KIND",
|
|
64
|
+
"PAYLOAD_END_EFFECTOR",
|
|
65
|
+
"PAYLOAD_HEARTBEAT",
|
|
66
|
+
"PAYLOAD_HOME",
|
|
67
|
+
"PAYLOAD_JOINT_DELTAS",
|
|
68
|
+
"PAYLOAD_JOINT_TARGETS",
|
|
69
|
+
"PAYLOAD_JOINT_VELOCITY",
|
|
70
|
+
"PAYLOAD_RERECORD",
|
|
71
|
+
"PAYLOAD_STOP",
|
|
72
|
+
"PAYLOAD_SUCCESS",
|
|
73
|
+
"PAYLOAD_TERMINATE",
|
|
74
|
+
"PAYLOAD_TWIST",
|
|
75
|
+
"PayloadError",
|
|
76
|
+
"STATE_KIND",
|
|
77
|
+
"StopPayload",
|
|
78
|
+
"VALID_KINDS",
|
|
79
|
+
"now_ts_ns",
|
|
80
|
+
"parse_control_payload",
|
|
81
|
+
"read_frame",
|
|
82
|
+
"write_frame",
|
|
83
|
+
]
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Discriminated set of operator-side control payloads carried by a ``Frame``.
|
|
2
|
+
|
|
3
|
+
The Rust agent and the operator UI both speak this same vocabulary:
|
|
4
|
+
``payload.kind`` selects one of the variants below, and the remaining
|
|
5
|
+
fields carry the value. Every consumer of the UDS bridge should accept
|
|
6
|
+
the unknown / malformed case by holding position — never by extrapolating
|
|
7
|
+
or ignoring the watchdog.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
PAYLOAD_TWIST = "twist"
|
|
16
|
+
PAYLOAD_STOP = "stop"
|
|
17
|
+
PAYLOAD_HOME = "home"
|
|
18
|
+
PAYLOAD_JOINT_TARGETS = "joint_targets"
|
|
19
|
+
PAYLOAD_JOINT_DELTAS = "joint_deltas"
|
|
20
|
+
PAYLOAD_JOINT_VELOCITY = "joint_velocity"
|
|
21
|
+
PAYLOAD_END_EFFECTOR = "end_effector"
|
|
22
|
+
PAYLOAD_HEARTBEAT = "heartbeat"
|
|
23
|
+
|
|
24
|
+
# Operator-driven session edges. The agent also forwards these as `mode`
|
|
25
|
+
# frames, but they share the same envelope as `control` frames so we
|
|
26
|
+
# keep the constants here for cross-consumer reuse.
|
|
27
|
+
PAYLOAD_TERMINATE = "terminate"
|
|
28
|
+
PAYLOAD_SUCCESS = "success"
|
|
29
|
+
PAYLOAD_RERECORD = "rerecord"
|
|
30
|
+
|
|
31
|
+
# Discrete gripper state from the operator UI: 0 = open, 1 = hold, 2 = close.
|
|
32
|
+
GRIPPER_OPEN = 0
|
|
33
|
+
GRIPPER_HOLD = 1
|
|
34
|
+
GRIPPER_CLOSE = 2
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class JointTargetsPayload:
|
|
39
|
+
"""Absolute joint targets in radians, keyed by joint name."""
|
|
40
|
+
|
|
41
|
+
joints: dict[str, float]
|
|
42
|
+
arm: str = ""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True)
|
|
46
|
+
class JointDeltasPayload:
|
|
47
|
+
"""Per-joint relative offsets in radians, keyed by joint name."""
|
|
48
|
+
|
|
49
|
+
joints: dict[str, float]
|
|
50
|
+
arm: str = ""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class JointVelocityPayload:
|
|
55
|
+
"""Per-joint normalised velocities in ``[-1, 1]``, keyed by joint name.
|
|
56
|
+
|
|
57
|
+
Followers integrate the value into a position target each control tick.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
joints: dict[str, float]
|
|
61
|
+
arm: str = ""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class EndEffectorPayload:
|
|
66
|
+
"""Cartesian end-effector delta + discrete gripper state.
|
|
67
|
+
|
|
68
|
+
``delta_x`` / ``delta_y`` / ``delta_z`` are added to the current EE
|
|
69
|
+
pose in the world frame; ``gripper`` is one of the ``GRIPPER_*``
|
|
70
|
+
discrete states.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
delta_x: float
|
|
74
|
+
delta_y: float
|
|
75
|
+
delta_z: float
|
|
76
|
+
gripper: int = GRIPPER_HOLD
|
|
77
|
+
arm: str = ""
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True)
|
|
81
|
+
class StopPayload:
|
|
82
|
+
"""Hold the most recently commanded pose; cancel any active velocity."""
|
|
83
|
+
|
|
84
|
+
arm: str = ""
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(frozen=True)
|
|
88
|
+
class HomePayload:
|
|
89
|
+
"""Move the arm to its operator-configured home pose."""
|
|
90
|
+
|
|
91
|
+
arm: str = ""
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class HeartbeatPayload:
|
|
96
|
+
"""Wire-level liveness ping sent by the agent to the follower.
|
|
97
|
+
|
|
98
|
+
Carries no operator intent and never updates a control slot.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
arm: str = ""
|
|
102
|
+
extras: dict[str, Any] = field(default_factory=dict)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
ControlPayload = (
|
|
106
|
+
JointTargetsPayload
|
|
107
|
+
| JointDeltasPayload
|
|
108
|
+
| JointVelocityPayload
|
|
109
|
+
| EndEffectorPayload
|
|
110
|
+
| StopPayload
|
|
111
|
+
| HomePayload
|
|
112
|
+
| HeartbeatPayload
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class PayloadError(ValueError):
|
|
117
|
+
"""Raised when a payload's ``kind`` is missing or its fields are malformed."""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def parse_control_payload(payload: Any) -> ControlPayload:
|
|
121
|
+
"""Decode a raw CBOR-decoded payload dict into a typed ``ControlPayload``.
|
|
122
|
+
|
|
123
|
+
Raises ``PayloadError`` for unknown kinds or shape mismatches; callers
|
|
124
|
+
are expected to translate that into a hold-position cue rather than
|
|
125
|
+
propagating an exception into the control loop.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
if not isinstance(payload, dict):
|
|
129
|
+
raise PayloadError(f"payload must be a dict; got {type(payload).__name__}")
|
|
130
|
+
kind = payload.get("kind")
|
|
131
|
+
if not isinstance(kind, str):
|
|
132
|
+
raise PayloadError(f"payload.kind must be a string; got {type(kind).__name__}")
|
|
133
|
+
arm = _coerce_arm_tag(payload.get("arm"))
|
|
134
|
+
|
|
135
|
+
if kind == PAYLOAD_JOINT_TARGETS:
|
|
136
|
+
return JointTargetsPayload(joints=_coerce_joint_dict(payload.get("joints")), arm=arm)
|
|
137
|
+
if kind == PAYLOAD_JOINT_DELTAS:
|
|
138
|
+
return JointDeltasPayload(joints=_coerce_joint_dict(payload.get("joints")), arm=arm)
|
|
139
|
+
if kind == PAYLOAD_JOINT_VELOCITY:
|
|
140
|
+
return JointVelocityPayload(joints=_coerce_joint_dict(payload.get("joints")), arm=arm)
|
|
141
|
+
if kind == PAYLOAD_END_EFFECTOR:
|
|
142
|
+
return EndEffectorPayload(
|
|
143
|
+
delta_x=_coerce_float(payload.get("deltaX")),
|
|
144
|
+
delta_y=_coerce_float(payload.get("deltaY")),
|
|
145
|
+
delta_z=_coerce_float(payload.get("deltaZ")),
|
|
146
|
+
gripper=_coerce_gripper_state(payload.get("gripper")),
|
|
147
|
+
arm=arm,
|
|
148
|
+
)
|
|
149
|
+
if kind == PAYLOAD_STOP:
|
|
150
|
+
return StopPayload(arm=arm)
|
|
151
|
+
if kind == PAYLOAD_HOME:
|
|
152
|
+
return HomePayload(arm=arm)
|
|
153
|
+
if kind == PAYLOAD_HEARTBEAT:
|
|
154
|
+
extras = {key: value for key, value in payload.items() if key not in {"kind", "arm"}}
|
|
155
|
+
return HeartbeatPayload(arm=arm, extras=extras)
|
|
156
|
+
raise PayloadError(f"unsupported payload kind {kind!r}")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _coerce_arm_tag(value: Any) -> str:
|
|
160
|
+
if isinstance(value, str):
|
|
161
|
+
return value
|
|
162
|
+
return ""
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _coerce_joint_dict(value: Any) -> dict[str, float]:
|
|
166
|
+
if not isinstance(value, dict):
|
|
167
|
+
raise PayloadError(f"`joints` must be a dict; got {type(value).__name__}")
|
|
168
|
+
coerced: dict[str, float] = {}
|
|
169
|
+
for joint, magnitude in value.items():
|
|
170
|
+
if not isinstance(joint, str):
|
|
171
|
+
raise PayloadError(f"joint key must be a string; got {type(joint).__name__}")
|
|
172
|
+
coerced[joint] = _coerce_float(magnitude)
|
|
173
|
+
return coerced
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _coerce_float(value: Any, default: float = 0.0) -> float:
|
|
177
|
+
if isinstance(value, bool):
|
|
178
|
+
return default
|
|
179
|
+
if isinstance(value, (int, float)):
|
|
180
|
+
return float(value)
|
|
181
|
+
return default
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _coerce_gripper_state(value: Any) -> int:
|
|
185
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
186
|
+
return GRIPPER_HOLD
|
|
187
|
+
numeric = int(round(float(value)))
|
|
188
|
+
if numeric <= GRIPPER_OPEN:
|
|
189
|
+
return GRIPPER_OPEN
|
|
190
|
+
if numeric >= GRIPPER_CLOSE:
|
|
191
|
+
return GRIPPER_CLOSE
|
|
192
|
+
return GRIPPER_HOLD
|
|
File without changes
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Length-prefixed CBOR framing primitives for the rFabric control bridge.
|
|
2
|
+
|
|
3
|
+
The frame envelope mirrors ``agent-src/src/media/envelope.rs`` and
|
|
4
|
+
``app/src/frontend/components/intervention/envelope.ts``: a CBOR map
|
|
5
|
+
keyed ``kind`` / ``seq`` / ``tsNs`` / ``payload``. On the wire each
|
|
6
|
+
frame is preceded by a u32 little-endian length prefix:
|
|
7
|
+
|
|
8
|
+
::
|
|
9
|
+
|
|
10
|
+
[ u32 LE length ][ CBOR-encoded Frame ]
|
|
11
|
+
|
|
12
|
+
This module deliberately keeps the framing primitives separate from the
|
|
13
|
+
payload semantics so the same helpers are usable by tests, ad-hoc
|
|
14
|
+
tooling, and every follower implementation.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import socket
|
|
20
|
+
import struct
|
|
21
|
+
import time
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
import cbor2
|
|
26
|
+
|
|
27
|
+
CONTROL_KIND = "control"
|
|
28
|
+
STATE_KIND = "state"
|
|
29
|
+
ACK_KIND = "ack"
|
|
30
|
+
MODE_KIND = "mode"
|
|
31
|
+
|
|
32
|
+
VALID_KINDS = frozenset({CONTROL_KIND, STATE_KIND, ACK_KIND, MODE_KIND})
|
|
33
|
+
|
|
34
|
+
LENGTH_PREFIX_FORMAT = "<I"
|
|
35
|
+
LENGTH_PREFIX_SIZE = 4
|
|
36
|
+
MAX_FRAME_BYTES = 64 * 1024
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class FrameError(ValueError):
|
|
40
|
+
"""Raised when a frame cannot be parsed or violates contract bounds."""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class Frame:
|
|
45
|
+
"""In-memory representation of a wire frame."""
|
|
46
|
+
|
|
47
|
+
kind: str
|
|
48
|
+
seq: int
|
|
49
|
+
ts_ns: int
|
|
50
|
+
payload: Any
|
|
51
|
+
|
|
52
|
+
def encode(self) -> bytes:
|
|
53
|
+
if self.kind not in VALID_KINDS:
|
|
54
|
+
raise FrameError(f"unsupported frame kind {self.kind!r}")
|
|
55
|
+
return cbor2.dumps(
|
|
56
|
+
{
|
|
57
|
+
"kind": self.kind,
|
|
58
|
+
"seq": self.seq,
|
|
59
|
+
"tsNs": self.ts_ns,
|
|
60
|
+
"payload": self.payload,
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def decode(cls, raw: bytes) -> Frame:
|
|
66
|
+
if len(raw) > MAX_FRAME_BYTES:
|
|
67
|
+
raise FrameError(f"frame size {len(raw)} exceeds maximum {MAX_FRAME_BYTES}")
|
|
68
|
+
try:
|
|
69
|
+
decoded = cbor2.loads(raw)
|
|
70
|
+
except Exception as err:
|
|
71
|
+
raise FrameError(f"CBOR decode failed: {err}") from err
|
|
72
|
+
if not isinstance(decoded, dict):
|
|
73
|
+
raise FrameError(f"frame must decode to a CBOR map; got {type(decoded).__name__}")
|
|
74
|
+
kind = decoded.get("kind")
|
|
75
|
+
if kind not in VALID_KINDS:
|
|
76
|
+
raise FrameError(f"unsupported frame kind {kind!r}")
|
|
77
|
+
seq = decoded.get("seq", 0)
|
|
78
|
+
# Both the TypeScript operator and the Rust agent use camelCase
|
|
79
|
+
# `tsNs` on the wire; older payloads sometimes ship snake_case.
|
|
80
|
+
ts_ns = decoded.get("tsNs", decoded.get("ts_ns", 0))
|
|
81
|
+
return cls(kind=kind, seq=int(seq), ts_ns=int(ts_ns), payload=decoded.get("payload"))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def now_ts_ns() -> int:
|
|
85
|
+
"""Wall-clock nanoseconds since the Unix epoch.
|
|
86
|
+
|
|
87
|
+
The bridge tags every emitted frame with this so the receiver can
|
|
88
|
+
measure end-to-end latency.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
return time.time_ns()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def write_frame(sock: socket.socket, frame: Frame) -> None:
|
|
95
|
+
"""Write a length-prefixed CBOR frame onto a connected UDS."""
|
|
96
|
+
|
|
97
|
+
raw = frame.encode()
|
|
98
|
+
if len(raw) == 0 or len(raw) > MAX_FRAME_BYTES:
|
|
99
|
+
raise FrameError(f"refusing to write frame of size {len(raw)}")
|
|
100
|
+
sock.sendall(struct.pack(LENGTH_PREFIX_FORMAT, len(raw)) + raw)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def read_frame(sock: socket.socket) -> Frame | None:
|
|
104
|
+
"""Block until the next frame arrives. Returns ``None`` on clean EOF."""
|
|
105
|
+
|
|
106
|
+
header = _recv_exact(sock, LENGTH_PREFIX_SIZE)
|
|
107
|
+
if header is None:
|
|
108
|
+
return None
|
|
109
|
+
(length,) = struct.unpack(LENGTH_PREFIX_FORMAT, header)
|
|
110
|
+
if length == 0 or length > MAX_FRAME_BYTES:
|
|
111
|
+
raise FrameError(f"frame length {length} out of range")
|
|
112
|
+
body = _recv_exact(sock, length)
|
|
113
|
+
if body is None:
|
|
114
|
+
raise FrameError("connection closed mid-frame")
|
|
115
|
+
return Frame.decode(body)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _recv_exact(sock: socket.socket, count: int) -> bytes | None:
|
|
119
|
+
"""Read exactly ``count`` bytes; return ``None`` on a clean EOF before any byte."""
|
|
120
|
+
|
|
121
|
+
buffer = bytearray()
|
|
122
|
+
while len(buffer) < count:
|
|
123
|
+
try:
|
|
124
|
+
chunk = sock.recv(count - len(buffer))
|
|
125
|
+
except OSError:
|
|
126
|
+
return None
|
|
127
|
+
if not chunk:
|
|
128
|
+
if not buffer:
|
|
129
|
+
return None
|
|
130
|
+
return None
|
|
131
|
+
buffer.extend(chunk)
|
|
132
|
+
return bytes(buffer)
|