Files
UniversalTrain-ai/tests/test_onboarding_explicit_contacts.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.6 KiB
Python

"""Explicit pairs establish contact geometry; empty frames do not add robots."""
import pytest
mujoco = pytest.importorskip("mujoco")
from mjrl.onboarding._physics import contact_groups
from mjrl.onboarding.inspect import inspect_asset
def _model(*, pairs=True, extra='<body name="empty_frame"><body/></body>'):
contact = '<contact><pair geom1="floor" geom2="left_sole"/>' \
'<pair geom1="floor" geom2="right_sole"/></contact>' if pairs else ''
return f'''<mujoco><default><geom contype="0" conaffinity="0"/></default>
<worldbody><geom name="floor" type="plane" size="1 1 .1"/>
<body name="base"><freejoint/><geom size=".1"/>
<body name="left_foot"><joint name="left_hip"/>
<geom name="left_sole" size=".1"/></body>
<body name="right_foot"><joint name="right_hip"/>
<geom name="right_sole" size=".1"/>
<geom name="visual_only" size=".05"/></body></body>{extra}
</worldbody>{contact}</mujoco>'''
def test_explicit_pairs_enable_intake_and_contact_groups(tmp_path):
path = tmp_path / 'robot.xml'
path.write_text(_model())
report = inspect_asset(path)
geoms = {geom['name']: geom for geom in report['model']['geoms']}
assert geoms['left_sole']['collision_enabled']
assert not geoms['visual_only']['collision_enabled']
assert report['morphology_candidates']['family'] == 'biped'
assert report['morphology_candidates']['floating_base']
assert report['recommendation']['complete_model']
model = mujoco.MjModel.from_xml_path(str(path))
root = model.body('base').id
for selector, names in [('foot_geoms', ['left_sole', 'right_sole']),
('foot_bodies', ['left_foot', 'right_foot'])]:
kind, groups = contact_groups(model, {selector: names}, root)
assert kind == selector
assert set(groups) == set(names)
with pytest.raises(ValueError, match='collision-disabled'):
contact_groups(model, {'foot_geoms': ['visual_only']}, root)
def test_zero_masks_without_pairs_remain_disabled(tmp_path):
path = tmp_path / 'robot.xml'
path.write_text(_model(pairs=False))
report = inspect_asset(path)
assert not report['recommendation']['complete_model']
assert report['morphology_candidates']['family'] is None
model = mujoco.MjModel.from_xml_path(str(path))
for config in ({'foot_geoms': ['left_sole']}, {'foot_bodies': ['left_foot']}):
with pytest.raises(ValueError, match='collision-disabled|no direct collision'):
contact_groups(model, config, model.body('base').id)
@pytest.mark.parametrize('extra', [
'<body name="obstacle"><geom size=".1"/></body>',
'<body name="second_robot"><freejoint/><geom size=".1"/></body>',
])
def test_physical_extra_roots_still_require_selection(tmp_path, extra):
path = tmp_path / 'robot.xml'
path.write_text(_model(extra=extra))
report = inspect_asset(path)
assert report['morphology_candidates']['base_body'] is None
assert not next(item['passed'] for item in report['checks']
if item['name'] == 'single_robot_root')
def test_calibration_ground_does_not_inherit_collision_disabled_defaults():
from mjrl.onboarding._physics import attach_calibration_ground
spec = mujoco.MjSpec.from_string(_model(pairs=False))
name = attach_calibration_ground(spec, None)
model = spec.compile()
assert model.geom(name).contype[0] == 1
assert model.geom(name).conaffinity[0] == 1
# Authored masks remain untouched; only the generated terrain is enabled.
assert model.geom('floor').contype[0] == 0
assert model.geom('left_sole').conaffinity[0] == 0