Files
UniversalTrain-ai/tests/test_robot_posture_sites.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

125 lines
5.5 KiB
Python

"""Posture allowances and clearance sites are explicit, portable robot data."""
from dataclasses import asdict, replace
import hashlib
from pathlib import Path
import pytest
from tasks.robot_spec import JointSpec, RobotSpec, read_robot_spec, scaffold_robot_spec
@pytest.fixture
def spec(tmp_path):
model = tmp_path / "robot.xml"
model.write_text('<mujoco><worldbody><geom size=".1"/></worldbody></mujoco>')
return RobotSpec(
robot_id="schema_robot", family="biped", model_path=model,
base_body="base", base_height=.5, foot_geoms=("left_foot", "right_foot"),
position_joints=(JointSpec("left", 10, 1, 20, .1), JointSpec("right", 20, 2, 30, .2)),
)
def test_old_literals_keep_default_posture_allowance_and_no_sites(spec):
old = asdict(spec)
old.pop("foot_sites")
for joint in old["position_joints"]:
joint.pop("posture_std")
loaded = RobotSpec.from_dict(old, base_dir=Path.cwd())
assert loaded.foot_sites == ()
assert [joint.posture_std for joint in loaded.position_joints] == [.35, .35]
def test_named_posture_and_sites_survive_literal_scaffolding(spec, tmp_path):
enriched = replace(
spec, foot_sites=("left_clearance", "right_clearance"),
position_joints=(replace(spec.position_joints[0], posture_std=.15),
replace(spec.position_joints[1], posture_std=.7)),
)
document = asdict(enriched)
document["foot_sites"] = list(enriched.foot_sites)
loaded = RobotSpec.from_dict(document, base_dir=tmp_path)
assert loaded.foot_sites == enriched.foot_sites
path = scaffold_robot_spec(loaded, tasks_root=tmp_path / "tasks")
assert read_robot_spec(path) == enriched
assert read_robot_spec(path).position_joints[0].posture_std != .35
assert read_robot_spec(path).position_joints[1].posture_std != .35
@pytest.mark.parametrize("value", [0, -1, float("nan"), float("inf"), float("-inf"), True, ".2", None])
def test_posture_std_requires_positive_finite_numeric_values(value):
with pytest.raises(ValueError, match="posture_std"):
JointSpec("hip", 10, 1, 20, .1, posture_std=value)
@pytest.mark.parametrize("sites", [
("one",), ("one", "two", "three"), ("one", "one"), ("", "two"),
(" ", "two"), ("robot/left", "right"), (None, "right"), "lr", None,
])
def test_sites_reject_wrong_count_duplicates_bad_names_and_nonlists(spec, sites):
with pytest.raises(ValueError):
replace(spec, foot_sites=sites)
document = asdict(spec)
document["foot_sites"] = sites
with pytest.raises(ValueError):
RobotSpec.from_dict(document, base_dir=Path.cwd())
def test_site_count_uses_selected_body_supports(spec):
bodies = replace(spec, foot_geoms=(), foot_bodies=("left", "right", "rear"))
with pytest.raises(ValueError, match="one site per named foot support"):
replace(bodies, foot_sites=("left_site", "right_site"))
assert replace(bodies, foot_sites=("left_site", "right_site", "rear_site")).foot_sites[-1] == "rear_site"
@pytest.fixture
def calibrated_input(spec):
mujoco = pytest.importorskip("mujoco")
from mjrl.onboarding._physics import compiled_model_sha256
# Exercise the metadata writer with an exact model fingerprint, without
# conflating this schema fixture with a physical calibration claim.
config = {"robot_id": spec.robot_id, "family": spec.family,
"model_path": str(spec.model_path), "action_scales": {"left": .1, "right": .2},
"foot_sites": ["left_site", "right_site"]}
report = {
"schema": "mjrl.onboarding.calibrate/1", "passed": True,
"model_sha256": hashlib.sha256(spec.model_path.read_bytes()).hexdigest(),
"compiled_model_sha256": compiled_model_sha256(mujoco.MjModel.from_xml_path(str(spec.model_path))),
"base_position": [0, 0, .5], "base_quaternion": [1, 0, 0, 0],
"root_body": "base", "foot_geoms": ["left_foot", "right_foot"],
"joint_order": [joint.name for joint in spec.position_joints],
"joints": {joint.name: asdict(joint) for joint in spec.position_joints},
}
return config, report
def test_calibrated_scaffold_maps_allowances_by_name_and_consumes_input_map(calibrated_input):
from mjrl.onboarding.scaffold import calibrated_spec_config
config, report = calibrated_input
output = calibrated_spec_config(
{**config, "posture_stds": {"right": .6, "left": .15}}, report, base_dir=Path.cwd()
)
assert "posture_stds" not in output
assert "action_scales" not in output
spec = RobotSpec.from_dict(output, base_dir=Path.cwd())
assert [(joint.name, joint.posture_std) for joint in spec.position_joints] == [("left", .15), ("right", .6)]
assert spec.foot_sites == ("left_site", "right_site")
default = calibrated_spec_config(config, report, base_dir=Path.cwd())
assert [joint["posture_std"] for joint in default["position_joints"]] == [.35, .35]
@pytest.mark.parametrize("posture", [
{}, {"left": .2}, {"left": .2, "right": .3, "unknown": .4},
{"left": 0, "right": .3}, {"left": -1, "right": .3},
{"left": float("nan"), "right": .3}, {"left": float("inf"), "right": .3},
{"left": True, "right": .3}, [.2, .3], None,
])
def test_calibrated_scaffold_rejects_incomplete_extra_or_invalid_allowances(calibrated_input, posture):
from mjrl.onboarding.scaffold import calibrated_spec_config
config, report = calibrated_input
with pytest.raises(ValueError, match="posture_stds"):
calibrated_spec_config({**config, "posture_stds": posture}, report, base_dir=Path.cwd())