Files
UniversalTrain-ai/tests/test_adapter_motor_frames.py
chenlin ceca51b0d8
Checks / Layout, translation and Colab checks (push) Has been cancelled
Checks / Ruff (push) Has been cancelled
Checks / Framework tests (native backend, CPU) (push) Has been cancelled
Checks / Portable Rust controller (push) Has been cancelled
Improve generic robot onboarding, walking controls and end-effector interaction
Default humanoid intake to command-following gait and support bounded Cartesian target dragging for arms. Carry model and actuator semantics through calibration, native verification and packaging. Document reusable intake decisions and cover them with regression tests.
2026-11-06 10:54:58 +08:00

81 lines
3.1 KiB
Python

"""Calibrated actuator and sensor semantics survive metadata review/export."""
import copy
import json
import mujoco
import pytest
from mjrl.adapter import build_layout
from mjrl.onboarding.upstream import extract_policy_metadata
def metadata():
return {
"schema": "mjrl-policy-adapter/1",
"wire_joint_order": ["joint"], "obs_joint_order": ["joint"],
"action_joint_order": ["joint"], "default_joint_pos": {"joint": 0},
"joint_limits": {"joint": [-1, 1]}, "action_scale": 1,
"control": {"kp": 1, "kd": 0, "effort_limit": 0.64, "control_hz": 50},
"observation": {
"terms": [{"name": "base_ang_vel"}, {"name": "joint_pos"}],
"frames": {"base_ang_vel": {"kind": "site", "name": "imu"}},
},
"motor_model": {
"kind": "bam", "motor_name": "xl330", "model": "m6", "kp": 100,
"vin": 7.0, "max_current": 1.5, "vin_drop_resistance": 0.1, "vin_min": 6,
},
}
def test_calibrated_metadata_roundtrip(tmp_path):
doc = metadata()
source = tmp_path / "reviewed.json"
source.write_text(json.dumps(doc))
report = extract_policy_metadata([source])
assert report["ready"], report
assert report["metadata"] == doc
layout = build_layout(report["metadata"])
assert layout["_schema"] == "mjrl-native-layout/1"
assert layout["motor_model"] == doc["motor_model"]
assert layout["observation"]["frames"] == doc["observation"]["frames"]
assert layout["control"]["kp"] == 1 # BAM firmware gain stays separate.
assert layout["motor_model"]["kp"] == 100
@pytest.mark.parametrize("frames", [
{"actions": {"kind": "site", "name": "imu"}},
{"projected_gravity": {"kind": "site", "name": "imu"}},
{"base_ang_vel": {"kind": "sensor", "name": "imu"}},
{"base_ang_vel": {"kind": "site", "name": ""}},
{"base_ang_vel": {"kind": "site", "name": "imu", "ignored": 1}},
])
def test_unknown_or_unobserved_frames_fail_closed(frames):
doc = metadata()
doc["observation"]["frames"] = frames
with pytest.raises(ValueError, match="[Ff]rame"):
build_layout(doc)
def test_frame_must_exist_in_exact_model():
model = mujoco.MjModel.from_xml_string('''<mujoco><compiler angle="radian"/>
<worldbody><body><joint name="joint" range="-1 1"/>
<geom size=".1"/><site name="actual_imu"/></body></worldbody>
<actuator><motor joint="joint"/></actuator></mujoco>''')
doc = metadata()
with pytest.raises(ValueError, match="no observation frame"):
build_layout(doc, model=model)
doc["observation"]["frames"]["base_ang_vel"]["name"] = "actual_imu"
assert build_layout(doc, model=model)["observation"]["frames"]
def test_motor_metadata_rejects_unknown_keys_and_velocity_targets():
doc = metadata()
invalid = copy.deepcopy(doc)
invalid["motor_model"]["typo"] = 1
with pytest.raises(ValueError, match="BAM fields"):
build_layout(invalid)
doc["action"] = {"targets": {"joint": {"kind": "velocity", "offset": 0}}}
doc["control"]["kp"], doc["control"]["kd"] = 0, 1
with pytest.raises(ValueError, match="requires position targets"):
build_layout(doc)