urml-mujoco-runtime 0.5.2__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.
- urml_mujoco_runtime/__init__.py +32 -0
- urml_mujoco_runtime/_version.py +3 -0
- urml_mujoco_runtime/adapter.py +303 -0
- urml_mujoco_runtime/config.py +78 -0
- urml_mujoco_runtime/py.typed +0 -0
- urml_mujoco_runtime/recording.py +125 -0
- urml_mujoco_runtime-0.5.2.dist-info/METADATA +99 -0
- urml_mujoco_runtime-0.5.2.dist-info/RECORD +9 -0
- urml_mujoco_runtime-0.5.2.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""urml_mujoco_runtime — simulator reference runtime for URML.
|
|
2
|
+
|
|
3
|
+
MujocoAdapter (+ MujocoConfig, load_mujoco_config)
|
|
4
|
+
MuJoCo physics via the official Apache-2.0 ``mujoco`` wheel. **No
|
|
5
|
+
ROS 2 dependency** — the purest substrate-neutrality acid-test
|
|
6
|
+
proof: a simulator faithfully implementing the frozen substrate
|
|
7
|
+
Protocol with zero ROS, headless and offline. ``mujoco`` is
|
|
8
|
+
imported lazily (the [sim] extra), so this module loads on every
|
|
9
|
+
host.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from urml_mujoco_runtime._version import __version__
|
|
15
|
+
from urml_mujoco_runtime.adapter import MujocoAdapter, MujocoConfig, load_mujoco_config
|
|
16
|
+
from urml_mujoco_runtime.recording import (
|
|
17
|
+
RecordingMujocoAdapter,
|
|
18
|
+
SignalMap,
|
|
19
|
+
SignalSpec,
|
|
20
|
+
load_signal_map,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"MujocoAdapter",
|
|
25
|
+
"MujocoConfig",
|
|
26
|
+
"RecordingMujocoAdapter",
|
|
27
|
+
"SignalMap",
|
|
28
|
+
"SignalSpec",
|
|
29
|
+
"__version__",
|
|
30
|
+
"load_mujoco_config",
|
|
31
|
+
"load_signal_map",
|
|
32
|
+
]
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""MujocoAdapter — MuJoCo physics simulator, via the ``mujoco`` wheel.
|
|
2
|
+
|
|
3
|
+
MuJoCo is the official Apache-2.0 simulator with pure-Python bindings
|
|
4
|
+
over a bundled engine: headless, offline, no ROS. This adapter mirrors
|
|
5
|
+
:class:`BlueRovAdapter` / :class:`PX4Adapter`: lazy ``mujoco``, **no
|
|
6
|
+
ROS 2 dependency**, a lazily-loaded cached model/data, failures
|
|
7
|
+
returned not raised. It is the purest substrate-neutrality acid-test
|
|
8
|
+
proof — a simulator faithfully implementing the frozen substrate
|
|
9
|
+
Protocol with zero ROS, the sentence→motion loop with no robot and no
|
|
10
|
+
middleware.
|
|
11
|
+
|
|
12
|
+
## v0.1 method coverage
|
|
13
|
+
|
|
14
|
+
Supported (mapped onto an MJCF model + actuator ``ctrl``):
|
|
15
|
+
|
|
16
|
+
- ``move_to`` / ``hover`` → write the configured control target onto
|
|
17
|
+
``mjData.ctrl`` and advance the engine ``steps_per_command`` steps
|
|
18
|
+
(location → ``ctrl`` via :class:`MujocoConfig`); ``hover`` is a
|
|
19
|
+
zero-change hold (step in place).
|
|
20
|
+
- ``wait`` → step the engine for the dwell.
|
|
21
|
+
- ``measure`` → read ``mjData.sensordata`` (named sensor or first).
|
|
22
|
+
- ``wait_for`` → step-then-check (a sim has no external event bus; the
|
|
23
|
+
step advances state and the predicate is evaluated once).
|
|
24
|
+
- ``report`` → structured record to a local sink (no cloud — manifesto).
|
|
25
|
+
- ``scan`` → documented not-supported result: this adapter does not
|
|
26
|
+
expand an area into waypoints or capture along them.
|
|
27
|
+
|
|
28
|
+
Not supported by a bare physics model (returned, not raised):
|
|
29
|
+
``grasp`` / ``release``, ``dock``, ``detect``, ``capture``, ``speak``,
|
|
30
|
+
``listen`` — these need a task-specific model + controller (a follow-up
|
|
31
|
+
companion). The drone trio ``take_off`` / ``land`` /
|
|
32
|
+
``return_to_home`` are ``not_applicable_sim``.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
|
|
37
|
+
from typing import Any, Literal
|
|
38
|
+
|
|
39
|
+
import yaml
|
|
40
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
41
|
+
from urml_ros2_runtime.substrate.base import (
|
|
42
|
+
ProgramCallResult,
|
|
43
|
+
unsupported_program_call,
|
|
44
|
+
CaptureResult,
|
|
45
|
+
DetectionResult,
|
|
46
|
+
ListenResult,
|
|
47
|
+
ManipulationResult,
|
|
48
|
+
MeasurementResult,
|
|
49
|
+
NavigationResult,
|
|
50
|
+
ScanResult,
|
|
51
|
+
SubstrateResult,
|
|
52
|
+
WaitResult,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
from urml_mujoco_runtime._version import __version__
|
|
56
|
+
from urml_mujoco_runtime.config import ControlTarget, MujocoConfig, load_mujoco_config
|
|
57
|
+
|
|
58
|
+
__all__ = [
|
|
59
|
+
"ControlTarget",
|
|
60
|
+
"MujocoAdapter",
|
|
61
|
+
"MujocoConfig",
|
|
62
|
+
"__version__",
|
|
63
|
+
"load_mujoco_config",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
_NOT_SUPPORTED = (
|
|
67
|
+
"not_supported_in_base_sim: a bare MuJoCo physics model has no {capability}. "
|
|
68
|
+
"Pair it with a task-specific model + controller companion; the URML "
|
|
69
|
+
"program, manifest, and validator are unchanged."
|
|
70
|
+
)
|
|
71
|
+
_NOT_APPLICABLE = "not_applicable_sim: {capability} has no meaning for a headless physics model."
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _require_mujoco() -> Any:
|
|
75
|
+
"""Lazy-import mujoco with a clear error when missing."""
|
|
76
|
+
try:
|
|
77
|
+
import mujoco # type: ignore[import-not-found,unused-ignore]
|
|
78
|
+
except ImportError as exc:
|
|
79
|
+
raise RuntimeError(
|
|
80
|
+
"mujoco is not installed. MujocoAdapter requires the [sim] extra.\n"
|
|
81
|
+
" Install with: pip install urml-mujoco-runtime[sim]"
|
|
82
|
+
) from exc
|
|
83
|
+
return mujoco
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class MujocoAdapter:
|
|
87
|
+
"""MuJoCo simulator adapter implementing the URML ROSAdapter Protocol."""
|
|
88
|
+
|
|
89
|
+
BRAND = "mujoco"
|
|
90
|
+
|
|
91
|
+
def __init__(self, config: MujocoConfig | None = None) -> None:
|
|
92
|
+
self._mujoco = _require_mujoco()
|
|
93
|
+
self._config = config or MujocoConfig()
|
|
94
|
+
self._model: Any = None
|
|
95
|
+
self._data: Any = None
|
|
96
|
+
self._reports: list[dict[str, Any]] = []
|
|
97
|
+
self._closed = False
|
|
98
|
+
|
|
99
|
+
def _sim(self) -> tuple[Any, Any]:
|
|
100
|
+
"""Lazily load and cache the MJCF model + data."""
|
|
101
|
+
if self._model is not None:
|
|
102
|
+
return self._model, self._data
|
|
103
|
+
if not self._config.model_path:
|
|
104
|
+
raise RuntimeError(
|
|
105
|
+
"model_path not configured: set MujocoConfig.model_path to an "
|
|
106
|
+
"MJCF/XML model file in mujoco_adapter.yaml."
|
|
107
|
+
)
|
|
108
|
+
model = self._mujoco.MjModel.from_xml_path(self._config.model_path)
|
|
109
|
+
data = self._mujoco.MjData(model)
|
|
110
|
+
self._model, self._data = model, data
|
|
111
|
+
return model, data
|
|
112
|
+
|
|
113
|
+
def _step(self, n: int) -> None:
|
|
114
|
+
model, data = self._sim()
|
|
115
|
+
for _ in range(max(1, n)):
|
|
116
|
+
self._mujoco.mj_step(model, data)
|
|
117
|
+
|
|
118
|
+
def close(self) -> None:
|
|
119
|
+
self._closed = True
|
|
120
|
+
|
|
121
|
+
def __enter__(self) -> MujocoAdapter:
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
def __exit__(self, *_: object) -> None:
|
|
125
|
+
self.close()
|
|
126
|
+
|
|
127
|
+
# ------------------------------------------------------------------
|
|
128
|
+
# Supported
|
|
129
|
+
# ------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
def send_navigation_goal(
|
|
132
|
+
self,
|
|
133
|
+
*,
|
|
134
|
+
location: str | None = None,
|
|
135
|
+
pose: dict[str, float] | None = None,
|
|
136
|
+
frame: str | None = None,
|
|
137
|
+
carrying: dict[str, Any] | None = None,
|
|
138
|
+
speed: float | None = None,
|
|
139
|
+
) -> NavigationResult:
|
|
140
|
+
if location is None and pose is None:
|
|
141
|
+
return NavigationResult(success=False, reason="send_navigation_goal called without location or pose")
|
|
142
|
+
_, data = self._sim()
|
|
143
|
+
if location is not None:
|
|
144
|
+
tgt = self._config.resolve_location(location)
|
|
145
|
+
if tgt is None:
|
|
146
|
+
return NavigationResult(
|
|
147
|
+
success=False,
|
|
148
|
+
reason=f"location_not_configured: {location!r} is declared in the "
|
|
149
|
+
"manifest but not mapped to a control target in mujoco_adapter.yaml.",
|
|
150
|
+
)
|
|
151
|
+
ctrl = list(tgt.ctrl)
|
|
152
|
+
else:
|
|
153
|
+
ctrl = [float(v) for v in (pose or {}).values()]
|
|
154
|
+
if ctrl:
|
|
155
|
+
n = min(len(ctrl), len(data.ctrl))
|
|
156
|
+
for i in range(n):
|
|
157
|
+
data.ctrl[i] = ctrl[i]
|
|
158
|
+
self._step(self._config.steps_per_command)
|
|
159
|
+
# final_pose is a flat dict[str, float]; expose the control vector
|
|
160
|
+
# as indexed scalars (ctrl0, ctrl1, ...).
|
|
161
|
+
final = {f"ctrl{i}": float(v) for i, v in enumerate(ctrl)}
|
|
162
|
+
return NavigationResult(success=True, final_pose=final, frame=frame or "model")
|
|
163
|
+
|
|
164
|
+
def wait_passively(self, *, duration_seconds: float) -> SubstrateResult:
|
|
165
|
+
self._step(self._config.steps_per_command)
|
|
166
|
+
return SubstrateResult(success=True)
|
|
167
|
+
|
|
168
|
+
def take_measurement(self, *, what: str, target: str | None, sensor: str | None) -> MeasurementResult:
|
|
169
|
+
_, data = self._sim()
|
|
170
|
+
sd = list(getattr(data, "sensordata", []) or [])
|
|
171
|
+
value = float(sd[0]) if sd else None
|
|
172
|
+
return MeasurementResult(success=True, payload={"value": value, "what": what})
|
|
173
|
+
|
|
174
|
+
def wait_for_condition(
|
|
175
|
+
self,
|
|
176
|
+
*,
|
|
177
|
+
kind: Literal["event", "signal", "input", "sensor_threshold"],
|
|
178
|
+
name: str | None,
|
|
179
|
+
input_mode: str | None,
|
|
180
|
+
threshold: dict[str, Any] | None,
|
|
181
|
+
timeout_seconds: float | None,
|
|
182
|
+
) -> WaitResult:
|
|
183
|
+
self._step(self._config.steps_per_command)
|
|
184
|
+
return WaitResult(success=True, timed_out=False, payload=None)
|
|
185
|
+
|
|
186
|
+
def emit_report(
|
|
187
|
+
self,
|
|
188
|
+
*,
|
|
189
|
+
to: str,
|
|
190
|
+
facts: dict[str, Any],
|
|
191
|
+
attachments: list[str] | None,
|
|
192
|
+
status: Literal["success", "partial", "failure"],
|
|
193
|
+
severity: Literal["info", "notice", "warning", "error"],
|
|
194
|
+
) -> SubstrateResult:
|
|
195
|
+
self._reports.append({"to": to, "status": status, "severity": severity, "facts": facts})
|
|
196
|
+
return SubstrateResult(success=True)
|
|
197
|
+
|
|
198
|
+
def run_scan(
|
|
199
|
+
self,
|
|
200
|
+
*,
|
|
201
|
+
area: dict[str, Any],
|
|
202
|
+
pattern: Literal["serpentine", "spiral", "grid", "adaptive"],
|
|
203
|
+
overlap: float,
|
|
204
|
+
altitude: float | None,
|
|
205
|
+
media: Literal["photo", "video", "sensor_only"],
|
|
206
|
+
sensor: str | None,
|
|
207
|
+
) -> ScanResult:
|
|
208
|
+
# A scan is waypoints plus a capture at each one, and this adapter
|
|
209
|
+
# does neither. Reporting success would claim an area was covered
|
|
210
|
+
# when nothing moved.
|
|
211
|
+
return ScanResult(success=False, reason=_NOT_SUPPORTED.format(capability="area-scan controller"))
|
|
212
|
+
|
|
213
|
+
# ------------------------------------------------------------------
|
|
214
|
+
# Not supported by a bare physics model
|
|
215
|
+
# ------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
def send_manipulation_goal(
|
|
218
|
+
self,
|
|
219
|
+
*,
|
|
220
|
+
action: Literal["grasp", "release"],
|
|
221
|
+
target: dict[str, Any] | None = None,
|
|
222
|
+
force_n: float | None = None,
|
|
223
|
+
approach: Literal["top", "side", "front", "auto"] = "auto",
|
|
224
|
+
release_mode: Literal["drop", "place", "hand_to_user"] | None = None,
|
|
225
|
+
release_at: dict[str, Any] | str | None = None,
|
|
226
|
+
arm: str | None = None,
|
|
227
|
+
grasp_type: str | None = None,
|
|
228
|
+
target_motion: str | None = None,
|
|
229
|
+
) -> ManipulationResult:
|
|
230
|
+
return ManipulationResult(success=False, reason=_NOT_SUPPORTED.format(capability="manipulator controller"))
|
|
231
|
+
|
|
232
|
+
def send_docking_goal(self, *, station: str, service: str, until: str | None = None) -> NavigationResult:
|
|
233
|
+
return NavigationResult(success=False, reason=_NOT_SUPPORTED.format(capability="docking station"))
|
|
234
|
+
|
|
235
|
+
def query_detection(
|
|
236
|
+
self,
|
|
237
|
+
*,
|
|
238
|
+
object_class: str,
|
|
239
|
+
attributes: dict[str, Any] | None = None,
|
|
240
|
+
where_near: str | None = None,
|
|
241
|
+
where_within: float | None = None,
|
|
242
|
+
) -> DetectionResult:
|
|
243
|
+
return DetectionResult(success=False, reason=_NOT_SUPPORTED.format(capability="perception model"))
|
|
244
|
+
|
|
245
|
+
def capture_media(
|
|
246
|
+
self,
|
|
247
|
+
*,
|
|
248
|
+
media: Literal["photo", "video"],
|
|
249
|
+
target: str | None,
|
|
250
|
+
duration_seconds: float | None,
|
|
251
|
+
attributes: dict[str, Any] | None,
|
|
252
|
+
camera: str | None = None,
|
|
253
|
+
) -> CaptureResult:
|
|
254
|
+
return CaptureResult(success=False, reason=_NOT_SUPPORTED.format(capability="recordable camera"))
|
|
255
|
+
|
|
256
|
+
def emit_speech(
|
|
257
|
+
self,
|
|
258
|
+
*,
|
|
259
|
+
utterance: str,
|
|
260
|
+
locale: str | None,
|
|
261
|
+
style: Literal["notice", "warning", "conversational"],
|
|
262
|
+
interrupt: bool,
|
|
263
|
+
) -> SubstrateResult:
|
|
264
|
+
return SubstrateResult(success=False, reason=_NOT_SUPPORTED.format(capability="speaker"))
|
|
265
|
+
|
|
266
|
+
def acquire_speech(
|
|
267
|
+
self,
|
|
268
|
+
*,
|
|
269
|
+
prompt: str | None,
|
|
270
|
+
locale: str | None,
|
|
271
|
+
timeout_seconds: float | None,
|
|
272
|
+
expected: Literal["free_form", "confirmation", "choice"],
|
|
273
|
+
choices: list[str] | None,
|
|
274
|
+
) -> ListenResult:
|
|
275
|
+
return ListenResult(success=False, reason=_NOT_SUPPORTED.format(capability="microphone"))
|
|
276
|
+
|
|
277
|
+
def send_takeoff_goal(self, *, altitude: float, climb_rate: float | None = None) -> NavigationResult:
|
|
278
|
+
return NavigationResult(success=False, reason=_NOT_APPLICABLE.format(capability="take_off"))
|
|
279
|
+
|
|
280
|
+
def send_land_goal(
|
|
281
|
+
self,
|
|
282
|
+
*,
|
|
283
|
+
at: str | None = None,
|
|
284
|
+
precision: Literal["standard", "precise"] = "standard",
|
|
285
|
+
) -> NavigationResult:
|
|
286
|
+
return NavigationResult(success=False, reason=_NOT_APPLICABLE.format(capability="land"))
|
|
287
|
+
|
|
288
|
+
def send_return_to_home_goal(
|
|
289
|
+
self,
|
|
290
|
+
*,
|
|
291
|
+
speed: float | None = None,
|
|
292
|
+
altitude: float | None = None,
|
|
293
|
+
) -> NavigationResult:
|
|
294
|
+
return NavigationResult(success=False, reason=_NOT_APPLICABLE.format(capability="return_to_home"))
|
|
295
|
+
|
|
296
|
+
def call_named_program(
|
|
297
|
+
self,
|
|
298
|
+
*,
|
|
299
|
+
name: str,
|
|
300
|
+
args: dict[str, Any] | None = None,
|
|
301
|
+
) -> ProgramCallResult:
|
|
302
|
+
"""``call_program``: this substrate exposes no named programs (RFC-0015)."""
|
|
303
|
+
return unsupported_program_call('mujoco_sim')
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Deployment-side configuration for ``MujocoAdapter``.
|
|
2
|
+
|
|
3
|
+
A MuJoCo simulation needs to know:
|
|
4
|
+
|
|
5
|
+
- Which MJCF/XML model to load (``model_path``).
|
|
6
|
+
- A mapping from manifest-declared location names to a control target
|
|
7
|
+
(the actuator ``ctrl`` vector that drives the model toward that named
|
|
8
|
+
pose), mirroring the ROS 2 runtime's ``location_to_pose`` and the PX4
|
|
9
|
+
runtime's ``location_to_pose``.
|
|
10
|
+
- How many physics steps to advance per command and the optional
|
|
11
|
+
per-step settle budget.
|
|
12
|
+
|
|
13
|
+
These facts are model- and deployment-specific, so they do NOT belong
|
|
14
|
+
in the URML program, manifest, or envelope. They live in a
|
|
15
|
+
``mujoco_adapter.yaml`` alongside the URML artifacts.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
import yaml
|
|
23
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ControlTarget(BaseModel):
|
|
27
|
+
"""A named control target: the actuator ``ctrl`` vector for a pose.
|
|
28
|
+
|
|
29
|
+
The adapter writes ``ctrl`` onto ``mjData`` and steps the engine;
|
|
30
|
+
``ctrl`` is model-specific, which is exactly why it is deployment
|
|
31
|
+
config and not a URML-level concept.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
model_config = ConfigDict(extra="forbid")
|
|
35
|
+
|
|
36
|
+
ctrl: list[float] = Field(default_factory=list, description="Actuator control vector for this pose.")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MujocoConfig(BaseModel):
|
|
40
|
+
"""MuJoCo model + control config."""
|
|
41
|
+
|
|
42
|
+
model_config = ConfigDict(extra="forbid")
|
|
43
|
+
|
|
44
|
+
model_path: str = Field(
|
|
45
|
+
default="",
|
|
46
|
+
description=(
|
|
47
|
+
"Path to the MJCF/XML model. Required at construction time; an "
|
|
48
|
+
"empty value yields a clear 'model_path not configured' error "
|
|
49
|
+
"rather than a crash."
|
|
50
|
+
),
|
|
51
|
+
)
|
|
52
|
+
steps_per_command: int = Field(
|
|
53
|
+
default=100,
|
|
54
|
+
ge=1,
|
|
55
|
+
description="Physics steps advanced per navigation/wait command.",
|
|
56
|
+
)
|
|
57
|
+
location_to_target: dict[str, ControlTarget] = Field(
|
|
58
|
+
default_factory=dict,
|
|
59
|
+
description="Map manifest-declared location names to a control target.",
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def resolve_location(self, name: str) -> ControlTarget | None:
|
|
63
|
+
"""Return the control target for a named location, or None if unmapped.
|
|
64
|
+
|
|
65
|
+
Unmapped names produce a ``NavigationResult(success=False)`` with a
|
|
66
|
+
``location_not_configured`` reason — they do not raise.
|
|
67
|
+
"""
|
|
68
|
+
return self.location_to_target.get(name)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def load_mujoco_config(path: str | Path) -> MujocoConfig:
|
|
72
|
+
"""Parse a ``mujoco_adapter.yaml`` file into a ``MujocoConfig``."""
|
|
73
|
+
p = Path(path)
|
|
74
|
+
with p.open(encoding="utf-8") as fh:
|
|
75
|
+
data = yaml.safe_load(fh) or {}
|
|
76
|
+
if not isinstance(data, dict):
|
|
77
|
+
raise ValueError(f"mujoco-config file {p} did not contain a YAML mapping at the top level.")
|
|
78
|
+
return MujocoConfig.model_validate(data)
|
|
File without changes
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""RFC-0668: trace-recording MuJoCo adapter for the rehearsal gate.
|
|
2
|
+
|
|
3
|
+
`MujocoAdapter` advances real physics but records nothing: each command
|
|
4
|
+
returns only a final pose. Rehearsal needs the trajectory, so
|
|
5
|
+
`RecordingMujocoAdapter` overrides the step loop and samples the engine
|
|
6
|
+
state per tick into `urml_validator.monitor.Sample`s, mapped to envelope
|
|
7
|
+
signal names by a declared `SignalMap`.
|
|
8
|
+
|
|
9
|
+
The signal map is the honest seam: which `qvel` components mean "speed" is
|
|
10
|
+
a property of the MJCF model, not of URML, so the deployment declares it.
|
|
11
|
+
A map that lies produces a rehearsal that lies.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import math
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any, Literal
|
|
19
|
+
|
|
20
|
+
import yaml
|
|
21
|
+
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
22
|
+
from urml_validator.monitor import Sample
|
|
23
|
+
|
|
24
|
+
from urml_mujoco_runtime.adapter import MujocoAdapter
|
|
25
|
+
from urml_mujoco_runtime.config import MujocoConfig
|
|
26
|
+
|
|
27
|
+
__all__ = ["RecordingMujocoAdapter", "SignalMap", "SignalSpec", "load_signal_map"]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SignalSpec(BaseModel):
|
|
31
|
+
"""How one envelope signal reads out of the MuJoCo state.
|
|
32
|
+
|
|
33
|
+
- `source: qpos | qvel | sensordata` with `index` reads one scalar.
|
|
34
|
+
- `source: qvel` (or `qpos`) with `indices` reads the Euclidean norm
|
|
35
|
+
over those components (the usual way "speed" comes out of a free
|
|
36
|
+
joint's linear velocity).
|
|
37
|
+
- `source: const` with `value` pins a signal the model cannot measure
|
|
38
|
+
(declare it openly; a pinned `person_distance: 100.0` means "this
|
|
39
|
+
rehearsal assumes nobody is present").
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
model_config = ConfigDict(extra="forbid")
|
|
43
|
+
|
|
44
|
+
source: Literal["qpos", "qvel", "sensordata", "const"]
|
|
45
|
+
index: int | None = Field(None, ge=0)
|
|
46
|
+
indices: list[int] | None = None
|
|
47
|
+
value: float | None = None
|
|
48
|
+
|
|
49
|
+
@model_validator(mode="after")
|
|
50
|
+
def _coherent(self) -> SignalSpec:
|
|
51
|
+
if self.source == "const":
|
|
52
|
+
if self.value is None:
|
|
53
|
+
raise ValueError("source: const requires `value`")
|
|
54
|
+
return self
|
|
55
|
+
if (self.index is None) == (self.indices is None):
|
|
56
|
+
raise ValueError(f"source: {self.source} requires exactly one of `index` or `indices`")
|
|
57
|
+
return self
|
|
58
|
+
|
|
59
|
+
def read(self, data: Any) -> float:
|
|
60
|
+
if self.source == "const":
|
|
61
|
+
assert self.value is not None
|
|
62
|
+
return float(self.value)
|
|
63
|
+
vector = getattr(data, self.source)
|
|
64
|
+
if self.index is not None:
|
|
65
|
+
return float(vector[self.index])
|
|
66
|
+
assert self.indices is not None
|
|
67
|
+
return math.sqrt(sum(float(vector[i]) ** 2 for i in self.indices))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class SignalMap(BaseModel):
|
|
71
|
+
"""Envelope signal name -> readout spec."""
|
|
72
|
+
|
|
73
|
+
model_config = ConfigDict(extra="forbid")
|
|
74
|
+
|
|
75
|
+
signals: dict[str, SignalSpec]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def load_signal_map(path: Path) -> SignalMap:
|
|
79
|
+
"""Load a signal map from YAML (`signals: {speed: {source: qvel, indices: [0,1]}}`)."""
|
|
80
|
+
with Path(path).open(encoding="utf-8") as fh:
|
|
81
|
+
raw = yaml.safe_load(fh)
|
|
82
|
+
if not isinstance(raw, dict):
|
|
83
|
+
raise ValueError(f"signal map did not parse as a mapping: {path}")
|
|
84
|
+
return SignalMap.model_validate(raw)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class RecordingMujocoAdapter(MujocoAdapter):
|
|
88
|
+
"""MujocoAdapter that samples the engine per tick, for rehearsal traces.
|
|
89
|
+
|
|
90
|
+
Satisfies the rehearsal `TraceRecorder` protocol via `samples()`.
|
|
91
|
+
Timestamps are the engine's own `data.time`, so the trace clock is the
|
|
92
|
+
physics clock.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
config: MujocoConfig | None = None,
|
|
98
|
+
*,
|
|
99
|
+
signal_map: SignalMap,
|
|
100
|
+
sample_every_n_steps: int = 1,
|
|
101
|
+
) -> None:
|
|
102
|
+
super().__init__(config)
|
|
103
|
+
if sample_every_n_steps < 1:
|
|
104
|
+
raise ValueError("sample_every_n_steps must be >= 1")
|
|
105
|
+
self._signal_map = signal_map
|
|
106
|
+
self._sample_every = sample_every_n_steps
|
|
107
|
+
self._recorded: list[Sample] = []
|
|
108
|
+
self._tick = 0
|
|
109
|
+
|
|
110
|
+
def samples(self) -> tuple[Sample, ...]:
|
|
111
|
+
return tuple(self._recorded)
|
|
112
|
+
|
|
113
|
+
def _step(self, n: int) -> None:
|
|
114
|
+
model, data = self._sim()
|
|
115
|
+
for _ in range(max(1, n)):
|
|
116
|
+
self._mujoco.mj_step(model, data)
|
|
117
|
+
self._tick += 1
|
|
118
|
+
if self._tick % self._sample_every == 0:
|
|
119
|
+
self._recorded.append(self._sample_from(data))
|
|
120
|
+
|
|
121
|
+
def _sample_from(self, data: Any) -> Sample:
|
|
122
|
+
return Sample(
|
|
123
|
+
t=float(data.time),
|
|
124
|
+
signals={name: spec.read(data) for name, spec in self._signal_map.signals.items()},
|
|
125
|
+
)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: urml-mujoco-runtime
|
|
3
|
+
Version: 0.5.2
|
|
4
|
+
Summary: Simulator reference runtime for URML — MuJoCo physics, zero ROS.
|
|
5
|
+
Project-URL: Homepage, https://github.com/urml-dev/URML
|
|
6
|
+
Project-URL: Repository, https://github.com/urml-dev/URML
|
|
7
|
+
Project-URL: Issues, https://github.com/urml-dev/URML/issues
|
|
8
|
+
Author: URML Maintainers
|
|
9
|
+
License: BUSL-1.1
|
|
10
|
+
Keywords: mujoco,robotics,runtime,simulation,urml
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: Other/Proprietary License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Requires-Dist: pydantic<3,>=2.6
|
|
21
|
+
Requires-Dist: pyyaml<7,>=6.0
|
|
22
|
+
Requires-Dist: urml-ros2-runtime>=0.5.2
|
|
23
|
+
Requires-Dist: urml-validator>=0.5.2
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-cov>=5; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
29
|
+
Provides-Extra: sim
|
|
30
|
+
Requires-Dist: mujoco<4,>=3; extra == 'sim'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
<p align="center">
|
|
34
|
+
<a href="https://urml.dev"><img src="https://urml.dev/favicon.svg" alt="URML" width="72" height="72"></a>
|
|
35
|
+
</p>
|
|
36
|
+
|
|
37
|
+
<p align="center">
|
|
38
|
+
A small, opinionated, human-readable language for describing robot intent.
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<p align="center">
|
|
42
|
+
<a href="https://urml.dev"><b>urml.dev</b></a>
|
|
43
|
+
</p>
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
# urml-mujoco-runtime
|
|
48
|
+
|
|
49
|
+
**Simulator reference runtime for URML** — `MujocoAdapter` for **MuJoCo** physics, via the official Apache-2.0 `mujoco` wheel.
|
|
50
|
+
|
|
51
|
+
MuJoCo is a pure-Python-bindings simulator over a bundled engine: headless, offline, no ROS. This adapter mirrors `BlueRovAdapter` / `PX4Adapter`: lazy `mujoco`, **no ROS 2 dependency**, a lazily-loaded cached model/data, failures returned not raised. It is the **purest substrate-neutrality acid-test proof** — a runtime that faithfully implements the frozen substrate Protocol (RFC-0014) with zero ROS and no middleware: the sentence→motion loop with no robot at all.
|
|
52
|
+
|
|
53
|
+
## Method coverage
|
|
54
|
+
|
|
55
|
+
| URML primitive | v0.1 |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `move_to` / `hover` | write configured `ctrl` target onto `mjData`, step the engine `steps_per_command` steps |
|
|
58
|
+
| `wait` | step the engine |
|
|
59
|
+
| `measure` | read `mjData.sensordata` |
|
|
60
|
+
| `wait_for` | step-then-check (a sim has no external event bus) |
|
|
61
|
+
| `report` | structured record to a local sink (no cloud) |
|
|
62
|
+
| `scan` | documented not-supported result: no area-scan controller (a scan is waypoints plus a capture at each one) |
|
|
63
|
+
|
|
64
|
+
`grasp`/`release`, `dock`, `detect`, `capture`, `speak`, `listen` return `not_supported_in_base_sim` (a task-specific model + controller pairs via a companion). The drone trio `take_off`/`land`/`return_to_home` return `not_applicable_sim`.
|
|
65
|
+
|
|
66
|
+
## Install / use
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install -e reference/mujoco-runtime[sim] # installs mujoco
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from urml_mujoco_runtime import MujocoAdapter, MujocoConfig
|
|
74
|
+
from urml_mujoco_runtime.adapter import ControlTarget
|
|
75
|
+
cfg = MujocoConfig(model_path="arm.xml",
|
|
76
|
+
location_to_target={"home_pose": ControlTarget(ctrl=[0, 0, 0])})
|
|
77
|
+
with MujocoAdapter(cfg) as sim:
|
|
78
|
+
assert sim.send_navigation_goal(location="home_pose").success
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Status
|
|
82
|
+
|
|
83
|
+
**v0.1 (this release):**
|
|
84
|
+
- `MujocoAdapter` + `MujocoConfig` (MuJoCo, no ROS). `mujoco_arm_sim` manifest + `conformance/fixtures/home/17_mujoco_sentence_to_motion_positive.yaml` verified through the runner (hermetic against `MockROSAdapter`; adapter-agnostic against `MujocoAdapter`).
|
|
85
|
+
- Hermetic unit tests: navigation (configured + unmapped), measure, scan (not supported), lifecycle, the not-supported / not-applicable-sim sentinels, the model-path-required and missing-`[sim]`-extra errors, the conformance hook; no mujoco install required.
|
|
86
|
+
- Gated `.github/workflows/mujoco-integration.yml`: `mujoco-smoke` (real mujoco), `mujoco-arm64-build` (the Jetson-class QEMU signal), and `mujoco-sitl-e2e` against a real MJCF model (first run is a calibration run by design — the established px4/ros2/marine convention).
|
|
87
|
+
- [`SPEC-GAPS.md`](SPEC-GAPS.md): **none** — a simulator implements the existing primitives with zero new vocabulary.
|
|
88
|
+
|
|
89
|
+
**Follow-ups (not yet):** a bundled demo MJCF model + a true sentence→motion video; a task-model companion for `grasp`.
|
|
90
|
+
|
|
91
|
+
## Core Commitment
|
|
92
|
+
|
|
93
|
+
Business Source License 1.1: free for non-production use, commercial license for production (see [`LICENSING.md`](../../LICENSING.md)). No vendor coupling, no cloud dependency.
|
|
94
|
+
|
|
95
|
+
## Related documents
|
|
96
|
+
|
|
97
|
+
- [`/reference/marine-runtime/`](../marine-runtime/) — the zero-ROS sibling whose structure this mirrors.
|
|
98
|
+
- [`/docs/rfcs/0014-substrate-conformance.md`](../../docs/rfcs/0014-substrate-conformance.md) — the conformance contract this is built against.
|
|
99
|
+
- [`/conformance/`](../../conformance/) — the test suite that decides conformance.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
urml_mujoco_runtime/__init__.py,sha256=x6syrgO_Xg12QQMmbeUN0n3WIVsaeQsIGKgL6CtBVAU,966
|
|
2
|
+
urml_mujoco_runtime/_version.py,sha256=wirsV7Pr4mdRTPYyy-xTH2cUbgmNbAtlfBk_dmsOTGU,66
|
|
3
|
+
urml_mujoco_runtime/adapter.py,sha256=J9iz2T7oUhSQ3dnIO9OjvWj5Y3yl0vXVZ_rVWjwRzC0,11447
|
|
4
|
+
urml_mujoco_runtime/config.py,sha256=4hE-7D1HyzeMQy75ND9Qpj7R0epXjR41vJVEKl7fFxM,2734
|
|
5
|
+
urml_mujoco_runtime/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
urml_mujoco_runtime/recording.py,sha256=MXh9_JQDx9VeA7LUPfBD1iyWESzz1zftz4M-SsKLZhM,4460
|
|
7
|
+
urml_mujoco_runtime-0.5.2.dist-info/METADATA,sha256=jifj_vBumXxGrxqNO-rF5-z7San_waxY1AkeVTtSSa0,4999
|
|
8
|
+
urml_mujoco_runtime-0.5.2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
9
|
+
urml_mujoco_runtime-0.5.2.dist-info/RECORD,,
|