101 lines
3.8 KiB
Python
101 lines
3.8 KiB
Python
"""Physical endpoint measurements shared by native verification and app replay."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
import mujoco
|
|
import numpy as np
|
|
|
|
from mjrl.controllers.model import finite, object_id, vector
|
|
|
|
ENDPOINT_TOLERANCES = {
|
|
"joint_positions": "position_atol",
|
|
"joint_velocities": "velocity_atol",
|
|
"site_positions": "site_atol_m",
|
|
"base_displacement_m": "base_displacement_atol_m",
|
|
"base_velocity_m_s": "base_velocity_atol_m_s",
|
|
"base_angular_velocity_rad_s": "base_angular_velocity_atol_rad_s",
|
|
}
|
|
|
|
|
|
def parse_endpoints(probe: Mapping[str, Any]) -> dict[str, Any]:
|
|
"""Parse explicitly declared endpoint quantities."""
|
|
expected = {}
|
|
for field in sorted(ENDPOINT_TOLERANCES.keys() & probe.keys()):
|
|
raw = probe[field]
|
|
if field in {"joint_positions", "joint_velocities", "site_positions"}:
|
|
if (
|
|
not isinstance(raw, Mapping)
|
|
or not raw
|
|
or not all(isinstance(name, str) and name for name in raw)
|
|
):
|
|
raise ValueError(f"{field} must be a nonempty mapping of object names")
|
|
if field == "site_positions":
|
|
expected[field] = {
|
|
name: vector(value, 3, f"{field}.{name}") for name, value in raw.items()
|
|
}
|
|
else:
|
|
expected[field] = {
|
|
name: finite(value, f"{field}.{name}") for name, value in raw.items()
|
|
}
|
|
else:
|
|
expected[field] = vector(raw, 3, field)
|
|
return expected
|
|
|
|
|
|
def measure_endpoints(
|
|
model: mujoco.MjModel,
|
|
data: mujoco.MjData,
|
|
root: int,
|
|
expected: dict[str, Any],
|
|
start_pos: np.ndarray,
|
|
) -> dict[str, Any]:
|
|
motion = np.zeros(6)
|
|
obj = mujoco.mjtObj
|
|
mujoco.mj_objectVelocity(model, data, obj.mjOBJ_BODY, root, motion, 0)
|
|
values = {}
|
|
for field, spec in expected.items():
|
|
if field in {"joint_positions", "joint_velocities"}:
|
|
address, state = (
|
|
(model.jnt_qposadr, data.qpos)
|
|
if field == "joint_positions"
|
|
else (model.jnt_dofadr, data.qvel)
|
|
)
|
|
values[field] = {}
|
|
for name in spec:
|
|
jid = object_id(model, obj.mjOBJ_JOINT, name)
|
|
if model.body_rootid[model.jnt_bodyid[jid]] != root or model.jnt_type[jid] not in {
|
|
mujoco.mjtJoint.mjJNT_HINGE,
|
|
mujoco.mjtJoint.mjJNT_SLIDE,
|
|
}:
|
|
raise ValueError(
|
|
f"{field} requires a named scalar joint on the robot: {name!r}"
|
|
)
|
|
values[field][name] = float(state[address[jid]])
|
|
elif field == "site_positions":
|
|
values[field] = {
|
|
name: data.site_xpos[object_id(model, obj.mjOBJ_SITE, name)].copy() for name in spec
|
|
}
|
|
elif field == "base_displacement_m":
|
|
values[field] = data.xpos[root] - start_pos
|
|
elif field == "base_velocity_m_s":
|
|
values[field] = motion[3:].copy()
|
|
else:
|
|
values[field] = motion[:3].copy()
|
|
return values
|
|
|
|
|
|
def compare_endpoint(expected: Any, observed: Any) -> tuple[float, Any, Any]:
|
|
if isinstance(expected, dict):
|
|
items = {name: compare_endpoint(value, observed[name]) for name, value in expected.items()}
|
|
return (
|
|
max(result[0] for result in items.values()),
|
|
{name: result[1] for name, result in items.items()},
|
|
{name: result[2] for name, result in items.items()},
|
|
)
|
|
if isinstance(expected, np.ndarray):
|
|
return float(np.linalg.norm(expected - observed)), expected.tolist(), observed.tolist()
|
|
return abs(expected - observed), expected, observed
|