120 lines
5.9 KiB
Python
120 lines
5.9 KiB
Python
|
|
from pathlib import Path
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
from ui.robot_models import (
|
||
|
|
DEFAULT_ROBOT_MODEL, ROBOT_MODELS, RobotDescription,
|
||
|
|
available_robot_arms, load_robot_description,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class RobotModelTests(unittest.TestCase):
|
||
|
|
def test_bundled_models_have_expected_joint_order_and_pose_links(self):
|
||
|
|
expected = {
|
||
|
|
"xiaoyan": [
|
||
|
|
f"{side}_{joint}"
|
||
|
|
for side in ("L", "R")
|
||
|
|
for joint in (
|
||
|
|
"SHOULDER_P", "SHOULDER_R", "SHOULDER_Y", "ELBOW_R",
|
||
|
|
"WRIST_P", "WRIST_Y", "WRIST_R",
|
||
|
|
)
|
||
|
|
],
|
||
|
|
"xiaozhao": [
|
||
|
|
f"joint{index}-{side}"
|
||
|
|
for side in ("left", "right")
|
||
|
|
for index in range(1, 8)
|
||
|
|
] + [f"neck-joint{index}" for index in range(1, 4)],
|
||
|
|
}
|
||
|
|
self.assertEqual(DEFAULT_ROBOT_MODEL, "xiaoyan")
|
||
|
|
for key, profile in ROBOT_MODELS.items():
|
||
|
|
with self.subTest(model=key):
|
||
|
|
self.assertTrue(profile.urdf_path.is_absolute())
|
||
|
|
description = load_robot_description(profile.urdf_path)
|
||
|
|
self.assertEqual(description.joint_names, expected[key])
|
||
|
|
self.assertEqual(len(description.link_names), 20 if key == "xiaozhao" else 17)
|
||
|
|
self.assertIn(profile.base_link, description.link_names)
|
||
|
|
self.assertIn(profile.ee_link, description.link_names)
|
||
|
|
|
||
|
|
def test_arm_profiles_map_to_service_devices_and_urdf_chains(self):
|
||
|
|
expected = {
|
||
|
|
"xiaoyan": [("right", "right_arm", "R_FINGER_TIP"), ("left", "left_arm", "L_WRIST_R_S")],
|
||
|
|
"xiaozhao": [
|
||
|
|
("right", "xiaozhao_right_arm", "tcp-right"),
|
||
|
|
("left", "xiaozhao_left_arm", "tcp-left"),
|
||
|
|
("head", "xiaozhao_head", "neck_Link3"),
|
||
|
|
],
|
||
|
|
}
|
||
|
|
for key, profile in ROBOT_MODELS.items():
|
||
|
|
with self.subTest(model=key):
|
||
|
|
description = load_robot_description(profile.urdf_path)
|
||
|
|
arms = available_robot_arms(profile, description)
|
||
|
|
self.assertEqual([(arm.side, arm.device_id, arm.ee_link) for arm in arms], expected[key])
|
||
|
|
self.assertEqual(profile.ee_link, arms[0].ee_link)
|
||
|
|
for arm in arms:
|
||
|
|
self.assertEqual(len(arm.joint_names), 3 if arm.side == "head" else 7)
|
||
|
|
self.assertTrue(arm.label)
|
||
|
|
# URDF order is left, right, head; the default device remains right.
|
||
|
|
ordered_joints = [
|
||
|
|
joint for side in ("left", "right", "head")
|
||
|
|
for arm in arms if arm.side == side for joint in arm.joint_names
|
||
|
|
]
|
||
|
|
self.assertEqual(ordered_joints, description.joint_names)
|
||
|
|
|
||
|
|
def test_available_arms_require_their_complete_joint_chain_and_pose_links(self):
|
||
|
|
for key, profile in ROBOT_MODELS.items():
|
||
|
|
description = load_robot_description(profile.urdf_path)
|
||
|
|
for arm in profile.arms:
|
||
|
|
with self.subTest(model=key, side=arm.side):
|
||
|
|
single_arm = RobotDescription(list(arm.joint_names), description.link_names)
|
||
|
|
self.assertEqual(available_robot_arms(profile, single_arm), [arm])
|
||
|
|
incomplete = RobotDescription(list(arm.joint_names[1:]), description.link_names)
|
||
|
|
self.assertEqual(available_robot_arms(profile, incomplete), [])
|
||
|
|
for missing_link in (arm.ee_link, profile.base_link):
|
||
|
|
missing_pose_link = RobotDescription(
|
||
|
|
list(arm.joint_names),
|
||
|
|
[link for link in description.link_names if link != missing_link],
|
||
|
|
)
|
||
|
|
self.assertEqual(available_robot_arms(profile, missing_pose_link), [])
|
||
|
|
|
||
|
|
def test_metadata_loading_does_not_require_mesh_files(self):
|
||
|
|
content = '''<robot name="example">
|
||
|
|
<link name="base"><visual><geometry>
|
||
|
|
<mesh filename="package://missing/meshes/missing.STL" />
|
||
|
|
</geometry></visual></link>
|
||
|
|
<link name="tip" />
|
||
|
|
<joint name="move" type="revolute" />
|
||
|
|
<joint name="fixed_tip" type="fixed" />
|
||
|
|
</robot>'''
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
path = Path(directory) / "example.urdf"
|
||
|
|
path.write_text(content)
|
||
|
|
description = load_robot_description(path)
|
||
|
|
self.assertEqual(description.joint_names, ["move"])
|
||
|
|
self.assertEqual(description.link_names, ["base", "tip"])
|
||
|
|
|
||
|
|
def test_invalid_models_are_rejected(self):
|
||
|
|
invalid = {
|
||
|
|
"malformed": "<robot>",
|
||
|
|
"wrong root": '<model><link name="base" /></model>',
|
||
|
|
"no links": '<robot><joint name="j" type="revolute" /></robot>',
|
||
|
|
"no moving joints": '<robot><link name="base" /><joint name="j" type="fixed" /></robot>',
|
||
|
|
"empty link": '<robot><link name=" " /><joint name="j" type="revolute" /></robot>',
|
||
|
|
"duplicate links": '<robot><link name="base" /><link name="base" /><joint name="j" type="revolute" /></robot>',
|
||
|
|
"empty joint": '<robot><link name="base" /><joint type="revolute" /></robot>',
|
||
|
|
"duplicate joints": '<robot><link name="base" /><joint name="j" type="revolute" /><joint name="j" type="fixed" /></robot>',
|
||
|
|
"missing joint type": '<robot><link name="base" /><joint name="j" /></robot>',
|
||
|
|
}
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
path = Path(directory) / "invalid.urdf"
|
||
|
|
for case, content in invalid.items():
|
||
|
|
with self.subTest(case=case):
|
||
|
|
path.write_text(content)
|
||
|
|
with self.assertRaisesRegex(ValueError, "URDF"):
|
||
|
|
load_robot_description(path)
|
||
|
|
with self.assertRaisesRegex(ValueError, "Cannot read URDF"):
|
||
|
|
load_robot_description(Path(directory) / "missing.urdf")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|