<?xml version="1.0"?>
<robot name="master_7dof">

  <!-- Links -->
  <link name="master_base">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="1.0"/>
      <inertia ixx="1e-2" iyy="1e-2" izz="1e-2" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_shoulder">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_link_sp">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_link_sy">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_upper_arm">
    <visual>
      <!-- cylinder aligned to X axis -->
      <origin xyz="0 -0.140 0" rpy="1.57079632679 0 0"/>
      <geometry>
        <cylinder length="0.280" radius="0.02"/>
      </geometry>
      <material name="upper_arm_mat">
        <color rgba="0.6 0.6 0.6 1"/>
      </material>
    </visual>
    <collision>
      <origin xyz="0 -0.140 0" rpy="1.57079632679 0 0"/>
      <geometry>
        <cylinder length="0.280" radius="0.02"/>
      </geometry>
    </collision>
    <inertial>
      <origin xyz="0 -0.140 0" rpy="1.57079632679 0 0"/>
      <mass value="1.0"/>
      <inertia ixx="0.01" iyy="0.01" izz="0.01" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_forearm">
    <visual>
      <origin xyz="0 -0.150 0" rpy="1.57079632679 0 0"/>
      <geometry>
        <cylinder length="0.300" radius="0.018"/>
      </geometry>
      <material name="forearm_mat">
        <color rgba="0.7 0.7 0.9 1"/>
      </material>
    </visual>
    <collision>
      <origin xyz="0 -0.150 0" rpy="1.57079632679 0 0"/>
      <geometry>
        <cylinder length="0.300" radius="0.018"/>
      </geometry>
    </collision>
    <inertial>
      <origin xyz="0 -0.150 0" rpy="1.57079632679 0 0"/>
      <mass value="0.8"/>
      <inertia ixx="0.008" iyy="0.008" izz="0.008" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_wrist">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_link_wr">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_link_wy">
    <inertial>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <mass value="0.1"/>
      <inertia ixx="1e-4" iyy="1e-4" izz="1e-4" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>

  <link name="master_ee">
    <visual>
      <origin xyz="0 -0.05 0" rpy="0 0 0"/>
      <geometry>
        <box size="0.02 0.10 0.02"/>
      </geometry>
      <material name="ee_mat">
        <color rgba="0.8 0.2 0.2 1"/>
      </material>
    </visual>
    <collision>
      <origin xyz="0 -0.05 0" rpy="0 0 0"/>
      <geometry>
        <box size="0.02 0.10 0.02"/>
      </geometry>
    </collision>
    <inertial>
      <origin xyz="0 -0.05 0" rpy="0 0 0"/>
      <mass value="0.2"/>
      <inertia ixx="0.001" iyy="0.001" izz="0.001" ixy="0" ixz="0" iyz="0"/>
    </inertial>
  </link>


  <!-- Joints -->

  <!-- ✅ 关键：把整条手臂在 master_base 坐标系里转到 -Y 方向
       +X -> -Y 等价于绕 Z 轴 -90° -->
  <joint name="master_base_to_shoulder" type="fixed">
    <parent link="master_base"/>
    <child link="master_shoulder"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
  </joint>

  <!-- Shoulder order: pitch(Y) -> yaw(Z) -> roll(X) -->
  <joint name="master_shoulder_pitch_joint" type="revolute">
    <parent link="master_shoulder"/>
    <child link="master_link_sp"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="1 0 0"/>
    <limit lower="-3.1416" upper="3.1416" effort="50" velocity="4.0"/>
  </joint>

  <joint name="master_shoulder_yaw_joint" type="revolute">
    <parent link="master_link_sp"/>
    <child link="master_link_sy"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="0 0 1"/>
    <limit lower="-3.1416" upper="3.1416" effort="50" velocity="4.0"/>
  </joint>

  <joint name="master_shoulder_roll_joint" type="revolute">
    <parent link="master_link_sy"/>
    <child link="master_upper_arm"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="0 1 0"/>
    <limit lower="-3.1416" upper="3.1416" effort="50" velocity="4.0"/>
  </joint>

  <!-- Elbow at x = 0.280 -->
  <joint name="master_elbow_flex_joint" type="revolute">
    <parent link="master_upper_arm"/>
    <child link="master_forearm"/>
    <origin xyz="0 -0.280 0" rpy="0 0 0"/>
    <axis xyz="0 0 1"/>
    <limit lower="-3.1416" upper="3.1416" effort="30" velocity="4.0"/>
  </joint>

  <!-- Wrist fixed at x = 0.300 -->
  <joint name="master_forearm_to_wrist" type="fixed">
    <parent link="master_forearm"/>
    <child link="master_wrist"/>
    <origin xyz="0 -0.300 0" rpy="0 0 0"/>
  </joint>

  <!-- Wrist order: roll(X) -> yaw(Y) -> pitch(Z) -->
  <joint name="master_wrist_roll_joint" type="revolute">
    <parent link="master_wrist"/>
    <child link="master_link_wr"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="0 1 0"/>
    <limit lower="-3.1416" upper="3.1416" effort="20" velocity="4.0"/>
  </joint>

  <joint name="master_wrist_yaw_joint" type="revolute">
    <parent link="master_link_wr"/>
    <child link="master_link_wy"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="1 0 0"/>
    <limit lower="-3.1416" upper="3.1416" effort="20" velocity="4.0"/>
  </joint>

  <joint name="master_wrist_pitch_joint" type="revolute">
    <parent link="master_link_wy"/>
    <child link="master_ee"/>
    <origin xyz="0 0 0" rpy="0 0 0"/>
    <axis xyz="0 0 1"/>
    <limit lower="-3.1416" upper="3.1416" effort="20" velocity="4.0"/>
  </joint>

</robot>