79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
"""Small reusable fixtures for simulation and robot discovery tests."""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def neutral_robot():
|
|
"""A two-joint floating fixture for verifying the training plumbing."""
|
|
from tasks.robot_spec import JointSpec, RobotSpec
|
|
|
|
return RobotSpec(
|
|
robot_id="locomotion_fixture",
|
|
family="biped",
|
|
model_path=Path(__file__).parent / "fixtures" / "locomotion.xml",
|
|
base_body="base",
|
|
base_height=0.255,
|
|
foot_geoms=("contact_0", "contact_1"),
|
|
position_joints=(JointSpec("leg_0", 10, 0.6, 10, 0.2), JointSpec("leg_1", 10, 0.6, 10, 0.2)),
|
|
controls_path=Path(__file__).parent / "fixtures" / "controls" / "velocity.yaml",
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def legacy_export(tmp_path_factory):
|
|
"""A deterministic ONNX and legacy scalar layout for controller integration."""
|
|
onnx = pytest.importorskip("onnx")
|
|
import json
|
|
|
|
import numpy as np
|
|
from onnx import TensorProto, helper, numpy_helper
|
|
|
|
export = tmp_path_factory.mktemp("legacy_policy")
|
|
wire = ["base_yaw", "left_joint", "right_joint", "tool_joint"]
|
|
observed = ["right_joint", "left_joint"]
|
|
action = ["left_joint", "right_joint"]
|
|
terms = [
|
|
{"name": "commands", "dim": 3},
|
|
{"name": "joint_pos", "dim": 2},
|
|
{"name": "joint_vel", "dim": 2},
|
|
{"name": "base_ang_vel", "dim": 3},
|
|
{"name": "projected_gravity", "dim": 3},
|
|
{"name": "actions", "dim": 2},
|
|
]
|
|
width = sum(term["dim"] for term in terms)
|
|
layout = {
|
|
"_schema": "isaac_layout v1",
|
|
"wire_joint_order": wire,
|
|
"obs_joint_order": observed,
|
|
"action_joint_order": action,
|
|
"default_joint_pos": dict(zip(wire, [-0.25, 0.35, -0.4, 0.2])),
|
|
"action_scale": 0.25,
|
|
"use_default_offset": True,
|
|
"control": {"kp": 10.0, "kd": 0.5, "effort_limit": 2.0, "control_hz": 50.0},
|
|
"observation": {"dim": width, "history_length": 1, "terms": terms},
|
|
"action": {"dim": len(action)},
|
|
"command_ranges": {
|
|
"twist": {
|
|
"lin_vel_x": [-0.6, 0.6], "lin_vel_y": [-0.4, 0.4], "ang_vel_z": [-1.0, 1.0],
|
|
},
|
|
},
|
|
}
|
|
(export / "layout.json").write_text(json.dumps(layout) + "\n", encoding="utf-8")
|
|
# Nonzero weights make the generated reference exercise the full
|
|
# observation, inference, action and joint target paths.
|
|
weights = np.arange(width * len(action), dtype=np.float32).reshape(width, len(action))
|
|
graph = helper.make_graph(
|
|
[helper.make_node("MatMul", ["observation", "weights"], ["action"])],
|
|
"legacy_policy_fixture",
|
|
[helper.make_tensor_value_info("observation", TensorProto.FLOAT, [1, width])],
|
|
[helper.make_tensor_value_info("action", TensorProto.FLOAT, [1, len(action)])],
|
|
[numpy_helper.from_array(weights / 500, name="weights")],
|
|
)
|
|
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 17)], ir_version=8)
|
|
onnx.checker.check_model(model)
|
|
onnx.save(model, export / "actor.onnx")
|
|
return export
|