ceca51b0d8
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.
179 lines
8.2 KiB
Python
179 lines
8.2 KiB
Python
"""Automatic recommendations must remain distinct from confirmed eligibility."""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("mujoco")
|
|
|
|
from mjrl.onboarding.inspect import inspect_asset
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@pytest.mark.parametrize("asset,family,count", [
|
|
("g1_example/g1.xml", "biped", 2),
|
|
("go1_example/go1.xml", "quadruped", 4),
|
|
("wheeled_biped_example/robot.xml", "wheeled_biped", 2),
|
|
("multipod_example/multipod.xml", "multipod", 6),
|
|
])
|
|
def test_automatic_legged_route(asset, family, count):
|
|
report = inspect_asset(ROOT / "assets" / asset)
|
|
candidates = report["morphology_candidates"]
|
|
assert candidates["family"] == family
|
|
assert candidates["support_branch_count"] == count
|
|
assert candidates["base_body"]
|
|
assert candidates["foot_geoms"]
|
|
assert len(candidates["wheel_joints"]) == (2 if family == "wheeled_biped" else 0)
|
|
recommendation = report["recommendation"]
|
|
assert recommendation["tier"] == 3
|
|
assert recommendation["status"] == "needs_profile"
|
|
assert recommendation["next_step"] == "review-profile"
|
|
assert 3 not in recommendation["eligible_tiers"]
|
|
assert "calibrate_stance" in recommendation["prerequisites"]
|
|
assert not next(c["passed"] for c in report["checks"]
|
|
if c["name"] == "explicit_core_legged_profile")
|
|
|
|
|
|
def test_explicit_control_intent_wins():
|
|
report = inspect_asset(ROOT / "assets/go1_example/go1.xml",
|
|
profile={"control_intent": "deterministic"})
|
|
assert report["morphology_candidates"]["family"] == "quadruped"
|
|
assert report["recommendation"]["tier"] == 2
|
|
|
|
|
|
def test_default_biped_objective_only_adapts_an_actor_that_observes_forward_and_turn(tmp_path, neutral_robot):
|
|
# Inspection validates metadata without executing any supplied actor graph.
|
|
actor = tmp_path / "actor.onnx"
|
|
actor.write_bytes(b"inventory only")
|
|
joints = [joint.name for joint in neutral_robot.position_joints]
|
|
axes = ["lin_vel_x", "ang_vel_z"]
|
|
metadata = {
|
|
"wire_joint_order": joints, "obs_joint_order": [], "action_joint_order": joints,
|
|
"default_joint_pos": dict.fromkeys(joints, 0.0), "action_scale": 1.0,
|
|
"control": {"kp": 10, "kd": 0.6, "effort_limit": 10, "control_hz": 50},
|
|
"observation": {"terms": [{"name": "commands"}], "command_order": axes},
|
|
"command_ranges": {"twist": {axis: [-0.5, 0.5] for axis in axes}},
|
|
}
|
|
profile = {"family": "biped", "base_body": neutral_robot.base_body,
|
|
"foot_geoms": list(neutral_robot.foot_geoms)}
|
|
def recommendation(intent=None):
|
|
selected = profile if intent is None else {**profile, "control_intent": intent}
|
|
return inspect_asset(neutral_robot.model_path, profile=selected, policy=actor,
|
|
policy_metadata=metadata)["recommendation"]
|
|
assert recommendation()["tier"] == 1
|
|
assert recommendation("retrain")["tier"] == 3
|
|
assert recommendation("deterministic")["tier"] == 2
|
|
metadata["command_ranges"]["twist"]["ang_vel_z"] = [0, 0]
|
|
assert recommendation()["tier"] == 3
|
|
|
|
|
|
def test_direct_drive_wheels_are_not_legs():
|
|
report = inspect_asset(ROOT / "assets/differential_drive/robot.xml")
|
|
assert report["morphology_candidates"]["family"] is None
|
|
assert report["morphology_candidates"]["wheel_joints"]
|
|
assert report["recommendation"]["tier"] == 2
|
|
|
|
|
|
def test_arm_and_misleading_foot_names(tmp_path):
|
|
path = tmp_path / "arm.xml"
|
|
path.write_text('''<mujoco><worldbody><body name="base">
|
|
<geom size=".1"/><body name="upper"><joint name="shoulder"/>
|
|
<geom size=".1"/><body name="hand"><joint name="elbow"/>
|
|
<geom name="left_foot" size=".1"/><geom name="right_foot" size=".1"/>
|
|
</body></body></body></worldbody></mujoco>''')
|
|
report = inspect_asset(path)
|
|
assert report["morphology_candidates"]["family"] == "arm"
|
|
assert report["morphology_candidates"]["support_branch_count"] == 1
|
|
assert report["recommendation"]["tier"] == 2
|
|
|
|
|
|
def test_urdf_without_effort_has_pending_training_route(tmp_path):
|
|
inertia = '''<inertial><mass value="1"/><inertia ixx=".01" ixy="0"
|
|
ixz="0" iyy=".01" iyz="0" izz=".01"/></inertial>'''
|
|
collision = '<collision><geometry><sphere radius=".05"/></geometry></collision>'
|
|
links = f'<link name="base">{inertia}{collision}</link>'
|
|
joints = ''
|
|
for side in ("left", "right"):
|
|
links += f'<link name="{side}_foot">{inertia}{collision}</link>'
|
|
joints += f'''<joint name="{side}_hip" type="continuous">
|
|
<parent link="base"/><child link="{side}_foot"/>
|
|
<origin xyz="0 {'.1' if side == 'left' else '-.1'} -.2"/>
|
|
<axis xyz="0 1 0"/></joint>'''
|
|
path = tmp_path / "robot.urdf"
|
|
path.write_text(f'<robot name="biped">{links}{joints}</robot>')
|
|
report = inspect_asset(path)
|
|
assert report["morphology_candidates"]["family"] == "biped"
|
|
assert report["morphology_candidates"]["foot_geoms"]
|
|
assert report["recommendation"]["tier"] == 3
|
|
assert report["recommendation"]["status"] == "needs_profile"
|
|
assert "build_floating_base_mjcf" in report["recommendation"]["prerequisites"]
|
|
assert "supply_actuation_and_effort_limits" in report["recommendation"]["prerequisites"]
|
|
assert 3 not in report["recommendation"]["eligible_tiers"]
|
|
|
|
|
|
def test_ambiguous_models_do_not_infer(tmp_path):
|
|
for name in ("a", "b"):
|
|
(tmp_path / f"{name}.xml").write_text('<mujoco/>')
|
|
report = inspect_asset(tmp_path)
|
|
assert report["morphology_candidates"] is None
|
|
assert report["recommendation"]["tier"] == 0
|
|
assert report["recommendation"]["status"] == "needs_selection"
|
|
assert report["recommendation"]["next_step"] == "select-model"
|
|
assert report["recommendation"]["prerequisites"] == ["select_model"]
|
|
assert inspect_asset(tmp_path / "a.xml")["recommendation"]["status"] == "ready"
|
|
|
|
|
|
def _shared_support_model(path, *, limbs, contact_joints, shared_joint):
|
|
"""Use anonymous links so only distal topology can separate the limbs."""
|
|
branches = []
|
|
for limb in range(limbs):
|
|
contacts = ''.join(
|
|
f'<body name="sole_{limb}_{toe}" pos="{toe * .05} 0 0">'
|
|
+ (f'<joint name="digit_{limb}_{toe}"/>' if contact_joints else '')
|
|
+ f'<geom name="contact_{limb}_{toe}" size=".025"/></body>'
|
|
for toe in range(2)
|
|
)
|
|
branches.append(
|
|
f'<body name="segment_{limb}_a" pos="0 {limb * .2} -.1">'
|
|
f'<joint name="axis_{limb}_a"/><geom size=".04"/>'
|
|
f'<body name="segment_{limb}_b" pos="0 0 -.1">'
|
|
f'<joint name="axis_{limb}_b"/><geom size=".04"/>'
|
|
f'{contacts}</body></body>'
|
|
)
|
|
shared = '<joint name="shared_axis"/>' if shared_joint else ''
|
|
path.write_text(
|
|
'<mujoco><worldbody><body name="base"><freejoint/><geom size=".1"/>'
|
|
f'<body name="shared">{shared}<geom size=".05"/>'
|
|
+ ''.join(branches) + '</body></body></worldbody></mujoco>'
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("shared_joint", [False, True])
|
|
@pytest.mark.parametrize("contact_joints", [False, True])
|
|
@pytest.mark.parametrize("limbs,family", [(2, "biped"), (4, "quadruped")])
|
|
def test_shared_ancestor_separates_distal_limbs_not_their_contacts(
|
|
tmp_path, shared_joint, contact_joints, limbs, family,
|
|
):
|
|
path = tmp_path / "shared.xml"
|
|
_shared_support_model(path, limbs=limbs, contact_joints=contact_joints,
|
|
shared_joint=shared_joint)
|
|
candidates = inspect_asset(path)["morphology_candidates"]
|
|
assert candidates["family"] == family
|
|
assert candidates["support_branch_count"] == limbs
|
|
assert len(candidates["supports"]) == 2 * limbs
|
|
for limb in range(limbs):
|
|
contacts = [support for support in candidates["supports"]
|
|
if support["body"].startswith(f"sole_{limb}_")]
|
|
assert len({support["branch_id"] for support in contacts}) == 1
|
|
|
|
|
|
@pytest.mark.parametrize("contact_joints", [False, True])
|
|
def test_single_limb_with_two_distal_contacts_is_not_a_biped(tmp_path, contact_joints):
|
|
path = tmp_path / "single.xml"
|
|
_shared_support_model(path, limbs=1, contact_joints=contact_joints, shared_joint=True)
|
|
candidates = inspect_asset(path)["morphology_candidates"]
|
|
assert len(candidates["supports"]) == 2
|
|
assert candidates["support_branch_count"] == 1
|
|
assert candidates["family"] is None
|