Files
UniversalTrain-ai/tests/test_command_contract.py
T
chenlin c32df70326
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
release: v0.1 generic robot training and application platform
2026-11-05 11:05:01 +08:00

64 lines
3.0 KiB
Python

"""Train and replay agree on the range recorded in a template checkpoint."""
from dataclasses import replace
from pathlib import Path
import pytest
pytest.importorskip("mjlab")
from controller.input import deflections, scale, spans
from mjrl.replay import configure_velocity_replay
from tasks.robot_spec import JointSpec
from tasks.templates.Biped_Template.env_cfg import env_cfg
from tasks.templates.Wheeled_Biped_Template.env_cfg import env_cfg as wheeled_env_cfg
def test_training_range_matches_replay_at_each_checkpoint_level(neutral_robot):
train = env_cfg(neutral_robot)
params = train.curriculum["command"].params
axes = ("lin_vel_x", "lin_vel_y", "ang_vel_z")
ranges = train.commands["twist"].ranges
for axis, ceiling in zip(axes, params["ceilings"], strict=True):
cap = ceiling * params["fractions"][0]
assert getattr(ranges, axis) == pytest.approx((-cap, cap))
for level, fraction in enumerate(params["fractions"]):
replay = env_cfg(neutral_robot, play=True)
checkpoint = {"infos": {"curriculum": {"command": {"level": level}}}}
report = configure_velocity_replay(replay, train, checkpoint)
assert report["source"] == "checkpoint_curriculum"
for axis, ceiling in zip(axes, params["ceilings"], strict=True):
cap = ceiling * fraction
assert report["ranges"][axis] == pytest.approx([-cap, cap])
def test_the_operator_reaches_each_trained_limit(neutral_robot):
train = env_cfg(neutral_robot)
replay = env_cfg(neutral_robot, play=True)
checkpoint = {"infos": {"curriculum": {"command": {"level": 1}}}}
report = configure_velocity_replay(replay, train, checkpoint)
command = replay.commands["twist"]
controls = command.controls
edges = spans(controls.command("twist"), command)
for axis, (binding,) in controls.bindings.items():
values = dict.fromkeys(("Lx", "Ly", "Rx", "Ry", "LT", "RT"), 0.0)
values[binding.source] = binding.sign
positive = deflections(values, False, controls)[axis]
assert scale(positive, edges[axis]) == pytest.approx(report["ranges"][axis][1])
def test_wheeled_replay_rejects_lateral_commands(neutral_robot):
robot = replace(
neutral_robot, family="wheeled_biped",
model_path=Path(__file__).parents[1] / "assets/hybrid_wheeled_smoke/robot.xml",
foot_geoms=("left_tire", "right_tire"),
position_joints=(JointSpec("left_hip", 10, .6, 8, .2), JointSpec("right_hip", 10, .6, 8, .2)),
velocity_joints=(JointSpec("left_wheel_joint", 0, 1, 8, 3),
JointSpec("right_wheel_joint", 0, 1, 8, 3)),
)
train, play = wheeled_env_cfg(robot), wheeled_env_cfg(robot, play=True)
checkpoint = {"infos": {"curriculum": {"command": {"level": 2}}}}
assert configure_velocity_replay(play, train, checkpoint)["ranges"]["lin_vel_y"] == [0, 0]
with pytest.raises(ValueError, match="lin_vel_y"):
configure_velocity_replay(play, train, checkpoint, (0, 0.1, 0))