Files
UniversalTrain-ai/tests/test_template_command_ranges.py
T
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

72 lines
3.5 KiB
Python

"""Biped training starts at full command coverage; other families keep staging."""
from importlib import import_module
from pathlib import Path
from types import SimpleNamespace
import pytest
import torch
from mjlab.tasks.velocity.mdp.velocity_command import UniformVelocityCommandCfg
from mjrl.tasks.commands import AxisBalancedVelocityCommand, AxisBalancedVelocityCommandCfg
from mjrl.tasks.operator import OperatorVelocityCommand, OperatorVelocityCommandCfg
from tasks.robot_spec import read_robot_spec
@pytest.mark.parametrize("template,ceilings", [
("Biped", (.7, .35, .8)),
("Quadruped", (1.1, .6, 1.)),
("Wheeled_Biped", (1.3, 0., 1.1)),
("Multipod", (.6, .45, .75)),
])
def test_family_training_command_ranges_and_curriculum(template, ceilings):
examples = (Path(__file__).resolve().parents[1] / "tasks").glob("*_example/robot_spec.py")
robot = next(spec for spec in map(read_robot_spec, examples) if spec.family == template.lower())
env_cfg = import_module(f"tasks.templates.{template}_Template.env_cfg").env_cfg
train, play = env_cfg(robot), env_cfg(robot, play=True)
assert type(train.commands["twist"]) is (
AxisBalancedVelocityCommandCfg if template == "Biped" else UniformVelocityCommandCfg
)
assert type(play.commands["twist"]) is OperatorVelocityCommandCfg
fractions = (1.,) if template == "Biped" else (.4, .7, 1.)
curriculum = train.curriculum["command"].params
assert curriculum["fractions"] == fractions
assert curriculum["ceilings"] == ceilings
assert play.curriculum == {}
for axis, ceiling in zip(("lin_vel_x", "lin_vel_y", "ang_vel_z"), ceilings, strict=True):
training = getattr(train.commands["twist"].ranges, axis)
playing = getattr(play.commands["twist"].ranges, axis)
assert playing == (-ceiling, ceiling)
assert training == (-ceiling * fractions[0], ceiling * fractions[0])
if template == "Biped":
assert training == playing
elif ceiling:
assert training != playing # Detect accidentally widening every family.
if template == "Biped":
assert "swing_air_time" not in train.rewards
assert "single_support" in train.rewards
@pytest.mark.parametrize("command", [(.2, -.1, .3), (0., 0., -.3), (0., 0., 0.)])
def test_biped_fixed_replay_bypasses_training_axis_mask_and_operator(neutral_robot, command):
from mjrl.replay import configure_velocity_replay
from tasks.templates.Biped_Template.env_cfg import env_cfg
train, play = env_cfg(neutral_robot), env_cfg(neutral_robot, play=True)
checkpoint = {"infos": {"curriculum": {"command": {"level": 0}}}}
report = configure_velocity_replay(play, train, checkpoint, command)
assert report["fixed_body_command"] == list(command)
assert train.commands["twist"].isolated_axis_fraction == .3
zeros = torch.zeros(2, 3)
env = SimpleNamespace(num_envs=2, device="cpu", step_dt=.02, scene={
"robot": SimpleNamespace(data=SimpleNamespace(root_link_lin_vel_b=zeros, root_link_ang_vel_b=zeros)),
})
term = play.commands["twist"].build(env)
assert not isinstance(term, (AxisBalancedVelocityCommand, OperatorVelocityCommand))
term.reset(torch.arange(2))
for _ in range(3):
term.compute(8.) # Exceeds maximum sample duration; exercise real resampling.
term.reset(torch.tensor([1]))
term.compute(0., env_ids=torch.tensor([1]))
torch.testing.assert_close(term.command, torch.tensor([command, command]), atol=0, rtol=0)