|
8 | 8 | from pathlib import Path |
9 | 9 |
|
10 | 10 | # Third Party |
| 11 | +import numpy as np |
11 | 12 | import pytest |
| 13 | +import yaml |
12 | 14 |
|
13 | 15 | from curobo._src.geom.sphere_fit.types import SphereFitType |
14 | 16 |
|
15 | 17 | # CuRobo |
16 | 18 | from curobo._src.robot.builder.builder_robot import RobotBuilder |
| 19 | +from curobo._src.robot.loader.kinematics_loader_cfg import KinematicsLoaderCfg |
| 20 | +from curobo._src.robot.types.joint_types import JointType |
| 21 | +from curobo._src.robot.types.link_params import LinkParams |
17 | 22 | from curobo._src.types.device_cfg import DeviceCfg |
18 | 23 | from curobo._src.util_file import get_assets_path, get_robot_configs_path, join_path, load_yaml |
19 | 24 |
|
20 | 25 | FAST_ITERATIONS = 50 |
21 | 26 |
|
22 | 27 |
|
| 28 | +@pytest.mark.parametrize("output_format", ["yaml", "yaml_without_cspace_generation", "xrdf"]) |
| 29 | +def test_safe_export( |
| 30 | + tmp_path: Path, |
| 31 | + cpu_device_cfg: DeviceCfg, |
| 32 | + monkeypatch: pytest.MonkeyPatch, |
| 33 | + output_format: str, |
| 34 | +) -> None: |
| 35 | + """Export typed cspace and extra links as portable data without changing the source.""" |
| 36 | + data = load_yaml(join_path(get_robot_configs_path(), "simple_mimic_robot.yml")) |
| 37 | + config = KinematicsLoaderCfg(**data["robot_cfg"]["kinematics"], device_cfg=cpu_device_cfg) |
| 38 | + link = LinkParams( |
| 39 | + link_name="sensor", |
| 40 | + parent_link_name="ee_link", |
| 41 | + joint_name="sensor_joint", |
| 42 | + joint_type=JointType.FIXED, |
| 43 | + fixed_transform=np.array( |
| 44 | + [[0, -1, 0, 1], [1, 0, 0, 2], [0, 0, 1, 3]], dtype=np.float32 |
| 45 | + ), |
| 46 | + link_com=np.zeros(3, dtype=np.float32), |
| 47 | + link_inertia=np.zeros(6, dtype=np.float32), |
| 48 | + ) |
| 49 | + config.extra_links = {"sensor": link} |
| 50 | + config.collision_spheres = { |
| 51 | + "ee_link": [{"center": np.zeros(3, dtype=np.float32), "radius": np.float32(0.1)}] |
| 52 | + } |
| 53 | + # Isolate export from mesh loading and collision fitting in the constructor. |
| 54 | + builder = RobotBuilder.__new__(RobotBuilder) |
| 55 | + # Exercise a cspace dictionary that already carries device and tensor fields. |
| 56 | + builder._cspace_config = vars(config.cspace).copy() |
| 57 | + output_path = tmp_path / "robot.yaml" |
| 58 | + if output_format == "xrdf": |
| 59 | + builder.save_xrdf(config, str(output_path)) |
| 60 | + else: |
| 61 | + builder.save(config, str(output_path), include_cspace=output_format == "yaml") |
| 62 | + output = yaml.safe_load(output_path.read_text()) |
| 63 | + assert "!!python" not in output_path.read_text() |
| 64 | + if output_format == "xrdf": |
| 65 | + assert output["cspace"]["joint_names"] == ["active_joint_2"] |
| 66 | + frame = output["modifiers"][-1]["add_frame"] |
| 67 | + assert frame["joint_type"] == "FIXED" |
| 68 | + assert frame["fixed_transform"]["position"] == [1.0, 2.0, 3.0] |
| 69 | + else: |
| 70 | + saved = output["kinematics"] |
| 71 | + assert "device_cfg" not in saved |
| 72 | + assert "device_cfg" not in saved["cspace"] |
| 73 | + assert saved["cspace"]["default_joint_position"] == pytest.approx([0.3, 0.0]) |
| 74 | + # LinkParams.create currently defaults to CUDA; keep this I/O test on CPU. |
| 75 | + monkeypatch.setattr( |
| 76 | + "curobo._src.robot.types.link_params.DeviceCfg", lambda: cpu_device_cfg |
| 77 | + ) |
| 78 | + restored = KinematicsLoaderCfg(**saved, device_cfg=cpu_device_cfg) |
| 79 | + np.testing.assert_allclose( |
| 80 | + restored.extra_links["sensor"].fixed_transform, link.fixed_transform, atol=1e-6 |
| 81 | + ) |
| 82 | + assert restored.extra_links["sensor"].joint_type == JointType.FIXED |
| 83 | + assert config.extra_links["sensor"] is link |
| 84 | + assert config.cspace.device_cfg is cpu_device_cfg |
| 85 | + assert builder._cspace_config["device_cfg"] is cpu_device_cfg |
| 86 | + |
| 87 | + |
23 | 88 | def default_fit_type() -> SphereFitType: |
24 | 89 | """Get default fit type.""" |
25 | 90 | return SphereFitType.MORPHIT |
|
0 commit comments