124 lines
4.2 KiB
Python
124 lines
4.2 KiB
Python
|
|
"""Robot profiles and the URDF metadata used by the client UI."""
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
from pathlib import Path
|
||
|
|
import xml.etree.ElementTree as ET
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class RobotArm:
|
||
|
|
"""A controllable joint group (an arm or the head/neck chain)."""
|
||
|
|
|
||
|
|
side: str
|
||
|
|
label: str
|
||
|
|
device_id: str
|
||
|
|
joint_names: tuple[str, ...]
|
||
|
|
ee_link: str
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class RobotModel:
|
||
|
|
label: str
|
||
|
|
urdf_path: Path
|
||
|
|
base_link: str
|
||
|
|
ee_link: str
|
||
|
|
arms: tuple[RobotArm, ...]
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class RobotDescription:
|
||
|
|
joint_names: list[str]
|
||
|
|
link_names: list[str]
|
||
|
|
|
||
|
|
|
||
|
|
_MODEL_ROOT = Path(__file__).resolve().parents[1] / "robot_model"
|
||
|
|
_XIAOYAN_ARM_JOINTS = (
|
||
|
|
"SHOULDER_P", "SHOULDER_R", "SHOULDER_Y", "ELBOW_R",
|
||
|
|
"WRIST_P", "WRIST_Y", "WRIST_R",
|
||
|
|
)
|
||
|
|
DEFAULT_ROBOT_MODEL = "xiaoyan"
|
||
|
|
ROBOT_MODELS = {
|
||
|
|
"xiaoyan": RobotModel(
|
||
|
|
label="xiaoyan",
|
||
|
|
urdf_path=_MODEL_ROOT / "xiaoyan_description" / "dual_arm.urdf",
|
||
|
|
base_link="PELVIS_S",
|
||
|
|
ee_link="R_FINGER_TIP",
|
||
|
|
arms=(
|
||
|
|
RobotArm(
|
||
|
|
side="right", label="Right Arm", device_id="right_arm",
|
||
|
|
joint_names=tuple(f"R_{joint}" for joint in _XIAOYAN_ARM_JOINTS),
|
||
|
|
ee_link="R_FINGER_TIP",
|
||
|
|
),
|
||
|
|
RobotArm(
|
||
|
|
side="left", label="Left Arm", device_id="left_arm",
|
||
|
|
joint_names=tuple(f"L_{joint}" for joint in _XIAOYAN_ARM_JOINTS),
|
||
|
|
ee_link="L_WRIST_R_S",
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
"xiaozhao": RobotModel(
|
||
|
|
label="xiaozhao",
|
||
|
|
urdf_path=_MODEL_ROOT / "xiaozhao" / "urdf" / "xiaozhao.urdf",
|
||
|
|
base_link="base_link",
|
||
|
|
ee_link="tcp-right",
|
||
|
|
arms=(
|
||
|
|
RobotArm(
|
||
|
|
side="right", label="Right Arm", device_id="xiaozhao_right_arm",
|
||
|
|
joint_names=tuple(f"joint{index}-right" for index in range(1, 8)),
|
||
|
|
ee_link="tcp-right",
|
||
|
|
),
|
||
|
|
RobotArm(
|
||
|
|
side="left", label="Left Arm", device_id="xiaozhao_left_arm",
|
||
|
|
joint_names=tuple(f"joint{index}-left" for index in range(1, 8)),
|
||
|
|
ee_link="tcp-left",
|
||
|
|
),
|
||
|
|
RobotArm(
|
||
|
|
side="head", label="Head / Neck", device_id="xiaozhao_head",
|
||
|
|
joint_names=tuple(f"neck-joint{index}" for index in range(1, 4)),
|
||
|
|
ee_link="neck_Link3",
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def available_robot_arms(model: RobotModel, description: RobotDescription) -> list[RobotArm]:
|
||
|
|
"""Offer configured devices only when their joint group exists in this URDF."""
|
||
|
|
joints = set(description.joint_names)
|
||
|
|
links = set(description.link_names)
|
||
|
|
if model.base_link not in links:
|
||
|
|
return []
|
||
|
|
return [
|
||
|
|
arm for arm in model.arms
|
||
|
|
if arm.ee_link in links and set(arm.joint_names).issubset(joints)
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def load_robot_description(path: Path) -> RobotDescription:
|
||
|
|
"""Read names without loading meshes or requiring ROS package resolution."""
|
||
|
|
path = Path(path)
|
||
|
|
try:
|
||
|
|
root = ET.parse(path).getroot()
|
||
|
|
except (OSError, ET.ParseError) as exc:
|
||
|
|
raise ValueError(f"Cannot read URDF {path}: {exc}") from exc
|
||
|
|
if root.tag != "robot":
|
||
|
|
raise ValueError(f"Invalid URDF {path}: root element must be 'robot'.")
|
||
|
|
|
||
|
|
def names(elements: list[ET.Element], kind: str) -> list[str]:
|
||
|
|
result = [element.get("name", "").strip() for element in elements]
|
||
|
|
if any(not name for name in result) or len(result) != len(set(result)):
|
||
|
|
raise ValueError(f"Invalid URDF {path}: {kind} names must be nonempty and unique.")
|
||
|
|
return result
|
||
|
|
|
||
|
|
links = names(root.findall("link"), "link")
|
||
|
|
joints = root.findall("joint")
|
||
|
|
joint_names = names(joints, "joint")
|
||
|
|
if any(not joint.get("type") for joint in joints):
|
||
|
|
raise ValueError(f"Invalid URDF {path}: every joint must have a type.")
|
||
|
|
movable = [
|
||
|
|
name for joint, name in zip(joints, joint_names) if joint.get("type") != "fixed"
|
||
|
|
]
|
||
|
|
if not links or not movable:
|
||
|
|
raise ValueError(f"Invalid URDF {path}: at least one link and movable joint are required.")
|
||
|
|
return RobotDescription(joint_names=movable, link_names=links)
|