Skip to main content

Solutions: Understanding URDF for Humanoid Robot Description and Control

Solution for Exercise 1: Basic Humanoid URDF Creation

Complete URDF File for Simple Humanoid

<?xml version="1.0"?>
<robot name="simple_humanoid" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Torso (base link) -->
<link name="torso">
<visual>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
</collision>
<inertial>
<mass value="10.0"/>
<inertia ixx="0.3" ixy="0.0" ixz="0.0" iyy="0.3" iyz="0.0" izz="0.3"/>
</inertial>
</link>

<!-- Head -->
<link name="head">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="neck_joint" type="revolute">
<parent link="torso"/>
<child link="head"/>
<origin xyz="0 0 0.35" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="10.0" velocity="1.0"/>
</joint>

<!-- Left Arm -->
<link name="left_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="left_upper_arm"/>
<origin xyz="0.15 0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="left_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="left_elbow_joint" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Right Arm -->
<link name="right_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="right_upper_arm"/>
<origin xyz="0.15 -0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="right_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="right_elbow_joint" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

</robot>

How to Test

  1. Save the URDF as simple_humanoid.urdf
  2. Load in RViz: ros2 run rviz2 rviz2
  3. Add RobotModel display and set the URDF path
  4. Verify that all links and joints are displayed correctly

Solution for Exercise 2: Leg Structure Implementation

Complete URDF with Leg Structure

<?xml version="1.0"?>
<robot name="humanoid_with_legs" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Torso (base link) -->
<link name="torso">
<visual>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
</collision>
<inertial>
<mass value="10.0"/>
<inertia ixx="0.3" ixy="0.0" ixz="0.0" iyy="0.3" iyz="0.0" izz="0.3"/>
</inertial>
</link>

<!-- Head -->
<link name="head">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="neck_joint" type="revolute">
<parent link="torso"/>
<child link="head"/>
<origin xyz="0 0 0.35" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="10.0" velocity="1.0"/>
</joint>

<!-- Left Arm -->
<link name="left_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="left_upper_arm"/>
<origin xyz="0.15 0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="left_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="left_elbow_joint" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Right Arm -->
<link name="right_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="right_upper_arm"/>
<origin xyz="0.15 -0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="right_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="right_elbow_joint" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Left Leg -->
<link name="left_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_hip_joint" type="revolute">
<parent link="torso"/>
<child link="left_hip"/>
<origin xyz="-0.1 0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="left_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="left_knee_joint" type="revolute">
<parent link="left_hip"/>
<child link="left_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="left_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="left_ankle_joint" type="revolute">
<parent link="left_thigh"/>
<child link="left_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

<!-- Right Leg -->
<link name="right_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_hip_joint" type="revolute">
<parent link="torso"/>
<child link="right_hip"/>
<origin xyz="-0.1 -0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="right_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="right_knee_joint" type="revolute">
<parent link="right_hip"/>
<child link="right_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="right_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="right_ankle_joint" type="revolute">
<parent link="right_thigh"/>
<child link="right_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

</robot>

Solution for Exercise 3: xacro Macros for Reusability

Complete xacro File with Macros

<?xml version="1.0"?>
<robot name="macro_humanoid" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Define constants -->
<xacro:property name="M_PI" value="3.1415926535897931" />
<xacro:property name="torso_mass" value="10.0" />
<xacro:property name="arm_mass" value="1.0" />
<xacro:property name="leg_mass" value="3.0" />
<xacro:property name="head_mass" value="2.0" />

<!-- Macro for creating a generic link -->
<xacro:macro name="generic_link" params="name mass geometry_type size material_color *origin *inertia_values">
<link name="${name}">
<visual>
<xacro:insert_block name="origin" />
<geometry>
<xacro:if value="${geometry_type == 'box'}">
<box size="${size}"/>
</xacro:if>
<xacro:if value="${geometry_type == 'cylinder'}">
<cylinder radius="${size.split(' ')[0]}" length="${size.split(' ')[1]}"/>
</xacro:if>
<xacro:if value="${geometry_type == 'sphere'}">
<sphere radius="${size}"/>
</xacro:if>
</geometry>
<material name="material_${name}">
<color rgba="${material_color}"/>
</material>
</visual>
<collision>
<xacro:insert_block name="origin" />
<geometry>
<xacro:if value="${geometry_type == 'box'}">
<box size="${size}"/>
</xacro:if>
<xacro:if value="${geometry_type == 'cylinder'}">
<cylinder radius="${size.split(' ')[0]}" length="${size.split(' ')[1]}"/>
</xacro:if>
<xacro:if value="${geometry_type == 'sphere'}">
<sphere radius="${size}"/>
</xacro:if>
</geometry>
</collision>
<inertial>
<mass value="${mass}"/>
<xacro:insert_block name="inertia_values" />
</inertial>
</link>
</xacro:macro>

<!-- Macro for creating a generic joint -->
<xacro:macro name="generic_joint" params="name type parent child xyz rpy axis limits">
<joint name="${name}" type="${type}">
<parent link="${parent}"/>
<child link="${child}"/>
<origin xyz="${xyz}" rpy="${rpy}"/>
<axis xyz="${axis}"/>
<xacro:if value="${limits != ''}">
<limit lower="${limits.split(' ')[0]}" upper="${limits.split(' ')[1]}"
effort="${limits.split(' ')[2]}" velocity="${limits.split(' ')[3]}"/>
</xacro:if>
</joint>
</xacro:macro>

<!-- Macro for creating an arm -->
<xacro:macro name="humanoid_arm" params="side parent_pos_x parent_pos_y">
<!-- Upper arm -->
<xacro:generic_link name="${side}_upper_arm" mass="${arm_mass}"
geometry_type="cylinder" size="0.04 0.3"
material_color="0.0 0.0 1.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</xacro:generic_link>

<xacro:generic_joint name="${side}_shoulder_joint" type="revolute"
parent="torso" child="${side}_upper_arm"
xyz="${parent_pos_x} ${parent_pos_y} 0.15" rpy="0 0 0"
axis="0 1 0"
limits="-1.57 1.57 50.0 1.0"/>

<!-- Lower arm -->
<xacro:generic_link name="${side}_lower_arm" mass="0.5"
geometry_type="cylinder" size="0.03 0.25"
material_color="0.0 0.0 1.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</xacro:generic_link>

<xacro:generic_joint name="${side}_elbow_joint" type="revolute"
parent="${side}_upper_arm" child="${side}_lower_arm"
xyz="0 0 -0.3" rpy="0 0 0"
axis="0 1 0"
limits="-1.57 1.57 30.0 1.0"/>
</xacro:macro>

<!-- Macro for creating a leg -->
<xacro:macro name="humanoid_leg" params="side parent_pos_y">
<!-- Hip -->
<xacro:generic_link name="${side}_hip" mass="1.5"
geometry_type="cylinder" size="0.06 0.1"
material_color="1.0 0.0 0.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</xacro:generic_link>

<xacro:generic_joint name="${side}_hip_joint" type="revolute"
parent="torso" child="${side}_hip"
xyz="-0.1 ${parent_pos_y} -0.3" rpy="0 0 0"
axis="1 0 0"
limits="-0.5 0.5 100.0 0.5"/>

<!-- Thigh -->
<xacro:generic_link name="${side}_thigh" mass="${leg_mass}"
geometry_type="cylinder" size="0.07 0.45"
material_color="1.0 0.0 0.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</xacro:generic_link>

<xacro:generic_joint name="${side}_knee_joint" type="revolute"
parent="${side}_hip" child="${side}_thigh"
xyz="0 0 -0.1" rpy="0 0 0"
axis="1 0 0"
limits="0 1.57 150.0 0.5"/>

<!-- Shin -->
<xacro:generic_link name="${side}_shin" mass="2.5"
geometry_type="cylinder" size="0.06 0.45"
material_color="1.0 0.0 0.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</xacro:generic_link>

<xacro:generic_joint name="${side}_ankle_joint" type="revolute"
parent="${side}_thigh" child="${side}_shin"
xyz="0 0 -0.45" rpy="0 0 0"
axis="1 0 0"
limits="-0.5 0.5 80.0 0.5"/>
</xacro:macro>

<!-- Create torso -->
<xacro:generic_link name="torso" mass="${torso_mass}"
geometry_type="box" size="0.3 0.25 0.6"
material_color="0.5 0.5 0.5 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.3" ixy="0.0" ixz="0.0" iyy="0.3" iyz="0.0" izz="0.3"/>
</xacro:generic_link>

<!-- Create head -->
<xacro:generic_link name="head" mass="${head_mass}"
geometry_type="sphere" size="0.1"
material_color="1.0 1.0 1.0 1.0">
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</xacro:generic_link>

<xacro:generic_joint name="neck_joint" type="revolute"
parent="torso" child="head"
xyz="0 0 0.35" rpy="0 0 0"
axis="0 1 0"
limits="-0.5 0.5 10.0 1.0"/>

<!-- Create arms using macro -->
<xacro:humanoid_arm side="left" parent_pos_x="0.15" parent_pos_y="0.1"/>
<xacro:humanoid_arm side="right" parent_pos_x="0.15" parent_pos_y="-0.1"/>

<!-- Create legs using macro -->
<xacro:humanoid_leg side="left" parent_pos_y="0.05"/>
<xacro:humanoid_leg side="right" parent_pos_y="-0.05"/>

</robot>

Solution for Exercise 4: Inertial Properties Calculation

Complete URDF with Realistic Inertial Properties

<?xml version="1.0"?>
<robot name="inertial_humanoid" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Constants -->
<xacro:property name="M_PI" value="3.1415926535897931" />
<xacro:property name="material_density" value="1000"/> <!-- kg/m^3 -->

<!-- Macro for calculating box inertial -->
<xacro:macro name="box_inertia" params="m x y z">
<inertial>
<mass value="${m}" />
<origin xyz="0 0 0" />
<inertia ixx="${m*(y*y+z*z)/12}" ixy="0" ixz="0"
iyy="${m*(x*x+z*z)/12}" iyz="0"
izz="${m*(x*x+y*y)/12}" />
</inertial>
</xacro:macro>

<!-- Macro for calculating cylinder inertial -->
<xacro:macro name="cylinder_inertia" params="m r h">
<inertial>
<mass value="${m}" />
<origin xyz="0 0 0" />
<inertia ixx="${m*(3*r*r+h*h)/12}" ixy="0" ixz="0"
iyy="${m*(3*r*r+h*h)/12}" iyz="0"
izz="${m*r*r/2}" />
</inertial>
</xacro:macro>

<!-- Macro for calculating sphere inertial -->
<xacro:macro name="sphere_inertia" params="m r">
<inertial>
<mass value="${m}" />
<origin xyz="0 0 0" />
<inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
iyy="${2*m*r*r/5}" iyz="0"
izz="${2*m*r*r/5}" />
</inertial>
</xacro:macro>

<!-- Torso: box with mass calculated from volume -->
<link name="torso">
<visual>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
</collision>
<!-- Volume = 0.3 * 0.25 * 0.6 = 0.045 m^3, Mass = 0.045 * 1000 = 45 kg -->
<xacro:box_inertia m="45" x="0.3" y="0.25" z="0.6"/>
</link>

<!-- Head: sphere with radius 0.1m -->
<link name="head">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.1"/>
</geometry>
</collision>
<!-- Volume = (4/3)*pi*r^3 = (4/3)*pi*0.1^3 = 0.004189 m^3, Mass = 4.189 kg -->
<xacro:sphere_inertia m="4.189" r="0.1"/>
</link>

<joint name="neck_joint" type="revolute">
<parent link="torso"/>
<child link="head"/>
<origin xyz="0 0 0.35" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="10.0" velocity="1.0"/>
</joint>

<!-- Left upper arm: cylinder with radius 0.04m and length 0.3m -->
<link name="left_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<!-- Volume = pi*r^2*h = pi*0.04^2*0.3 = 0.001508 m^3, Mass = 1.508 kg -->
<xacro:cylinder_inertia m="1.508" r="0.04" h="0.3"/>
</link>

<joint name="left_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="left_upper_arm"/>
<origin xyz="0.15 0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<!-- Left lower arm: cylinder with radius 0.03m and length 0.25m -->
<link name="left_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<!-- Volume = pi*r^2*h = pi*0.03^2*0.25 = 0.000707 m^3, Mass = 0.707 kg -->
<xacro:cylinder_inertia m="0.707" r="0.03" h="0.25"/>
</link>

<joint name="left_elbow_joint" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Right upper arm -->
<link name="right_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<xacro:cylinder_inertia m="1.508" r="0.04" h="0.3"/>
</link>

<joint name="right_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="right_upper_arm"/>
<origin xyz="0.15 -0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<!-- Right lower arm -->
<link name="right_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<xacro:cylinder_inertia m="0.707" r="0.03" h="0.25"/>
</link>

<joint name="right_elbow_joint" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Left hip: cylinder with radius 0.06m and length 0.1m -->
<link name="left_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<!-- Volume = pi*r^2*h = pi*0.06^2*0.1 = 0.001131 m^3, Mass = 1.131 kg -->
<xacro:cylinder_inertia m="1.131" r="0.06" h="0.1"/>
</link>

<joint name="left_hip_joint" type="revolute">
<parent link="torso"/>
<child link="left_hip"/>
<origin xyz="-0.1 0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<!-- Left thigh: cylinder with radius 0.07m and length 0.45m -->
<link name="left_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<!-- Volume = pi*r^2*h = pi*0.07^2*0.45 = 0.006927 m^3, Mass = 6.927 kg -->
<xacro:cylinder_inertia m="6.927" r="0.07" h="0.45"/>
</link>

<joint name="left_knee_joint" type="revolute">
<parent link="left_hip"/>
<child link="left_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<!-- Left shin: cylinder with radius 0.06m and length 0.45m -->
<link name="left_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<!-- Volume = pi*r^2*h = pi*0.06^2*0.45 = 0.005089 m^3, Mass = 5.089 kg -->
<xacro:cylinder_inertia m="5.089" r="0.06" h="0.45"/>
</link>

<joint name="left_ankle_joint" type="revolute">
<parent link="left_thigh"/>
<child link="left_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

<!-- Right leg components -->
<link name="right_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<xacro:cylinder_inertia m="1.131" r="0.06" h="0.1"/>
</link>

<joint name="right_hip_joint" type="revolute">
<parent link="torso"/>
<child link="right_hip"/>
<origin xyz="-0.1 -0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="right_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<xacro:cylinder_inertia m="6.927" r="0.07" h="0.45"/>
</link>

<joint name="right_knee_joint" type="revolute">
<parent link="right_hip"/>
<child link="right_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="right_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<xacro:cylinder_inertia m="5.089" r="0.06" h="0.45"/>
</link>

<joint name="right_ankle_joint" type="revolute">
<parent link="right_thigh"/>
<child link="right_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

</robot>

Solution for Exercise 5: Gazebo Integration

Complete URDF with Gazebo Integration

<?xml version="1.0"?>
<robot name="gazebo_humanoid" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Gazebo plugin for ros_control -->
<gazebo>
<plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
<robotNamespace>/humanoid</robotNamespace>
</plugin>
</gazebo>

<!-- Gazebo material definitions -->
<gazebo reference="torso">
<material>Gazebo/Gray</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
<kp>1000000.0</kp>
<kd>100.0</kd>
</gazebo>

<gazebo reference="head">
<material>Gazebo/White</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="left_upper_arm">
<material>Gazebo/Blue</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="left_lower_arm">
<material>Gazebo/Blue</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="right_upper_arm">
<material>Gazebo/Blue</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="right_lower_arm">
<material>Gazebo/Blue</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="left_hip">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="left_thigh">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="left_shin">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="right_hip">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="right_thigh">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<gazebo reference="right_shin">
<material>Gazebo/Red</material>
<mu1>0.2</mu1>
<mu2>0.2</mu2>
</gazebo>

<!-- Joint transmissions for Gazebo control -->
<transmission name="left_shoulder_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="left_shoulder_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="left_shoulder_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="left_elbow_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="left_elbow_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="left_elbow_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="right_shoulder_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="right_shoulder_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="right_shoulder_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="right_elbow_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="right_elbow_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="right_elbow_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="left_hip_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="left_hip_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="left_hip_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="left_knee_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="left_knee_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="left_knee_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="left_ankle_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="left_ankle_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="left_ankle_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="right_hip_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="right_hip_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="right_hip_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="right_knee_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="right_knee_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="right_knee_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<transmission name="right_ankle_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="right_ankle_joint">
<hardwareInterface>hardware_interface/PositionJointInterface</hardwareInterface>
</joint>
<actuator name="right_ankle_motor">
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>

<!-- Robot model -->
<link name="torso">
<visual>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
</collision>
<inertial>
<mass value="10.0"/>
<inertia ixx="0.3" ixy="0.0" ixz="0.0" iyy="0.3" iyz="0.0" izz="0.3"/>
</inertial>
</link>

<link name="head">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="neck_joint" type="revolute">
<parent link="torso"/>
<child link="head"/>
<origin xyz="0 0 0.35" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="10.0" velocity="1.0"/>
</joint>

<link name="left_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="left_upper_arm"/>
<origin xyz="0.15 0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="left_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="left_elbow_joint" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<link name="right_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_shoulder_joint" type="revolute">
<parent link="torso"/>
<child link="right_upper_arm"/>
<origin xyz="0.15 -0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="right_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="right_elbow_joint" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<link name="left_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_hip_joint" type="revolute">
<parent link="torso"/>
<child link="left_hip"/>
<origin xyz="-0.1 0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="left_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="left_knee_joint" type="revolute">
<parent link="left_hip"/>
<child link="left_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="left_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="left_ankle_joint" type="revolute">
<parent link="left_thigh"/>
<child link="left_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

<link name="right_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_hip_joint" type="revolute">
<parent link="torso"/>
<child link="right_hip"/>
<origin xyz="-0.1 -0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="right_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="right_knee_joint" type="revolute">
<parent link="right_hip"/>
<child link="right_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="right_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="right_ankle_joint" type="revolute">
<parent link="right_thigh"/>
<child link="right_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

</robot>

Solution for Exercise 6: Advanced Humanoid Features (Advanced)

Complete URDF with Advanced Features

<?xml version="1.0"?>
<robot name="advanced_humanoid" xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Gazebo plugin for ros_control -->
<gazebo>
<plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
<robotNamespace>/humanoid</robotNamespace>
</plugin>
</gazebo>

<!-- Gazebo plugins for sensors -->
<gazebo reference="head">
<!-- IMU sensor -->
<sensor name="imu_sensor" type="imu">
<always_on>true</always_on>
<update_rate>50</update_rate>
<visualize>true</visualize>
<imu>
<angular_velocity>
<x>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.02</stddev>
</noise>
</x>
<y>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.02</stddev>
</noise>
</y>
<z>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.02</stddev>
</noise>
</z>
</angular_velocity>
<linear_acceleration>
<x>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.017</stddev>
</noise>
</x>
<y>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.017</stddev>
</noise>
</y>
<z>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>0.017</stddev>
</noise>
</z>
</linear_acceleration>
</imu>
</sensor>
</gazebo>

<!-- Camera sensor -->
<gazebo reference="head">
<sensor name="camera" type="camera">
<always_on>true</always_on>
<update_rate>30.0</update_rate>
<camera name="head_camera">
<horizontal_fov>1.3962634</horizontal_fov>
<image>
<width>640</width>
<height>480</height>
<format>R8G8B8</format>
</image>
<clip>
<near>0.1</near>
<far>100</far>
</clip>
</camera>
<plugin name="camera_controller" filename="libgazebo_ros_camera.so">
<frame_name>head_camera_optical_frame</frame_name>
</plugin>
</sensor>
</gazebo>

<!-- Force/Torque sensors -->
<gazebo>
<plugin name="ft_sensor_left_foot" filename="libgazebo_ros_ft_sensor.so">
<update_rate>100</update_rate>
<topicName>ft_sensor_left_foot</topicName>
<jointName>left_ankle_joint</jointName>
</plugin>
</gazebo>

<gazebo>
<plugin name="ft_sensor_right_foot" filename="libgazebo_ros_ft_sensor.so">
<update_rate>100</update_rate>
<topicName>ft_sensor_right_foot</topicName>
<jointName>right_ankle_joint</jointName>
</plugin>
</gazebo>

<!-- Robot model -->
<link name="base_link">
<visual>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.25 0.6"/>
</geometry>
</collision>
<inertial>
<mass value="10.0"/>
<inertia ixx="0.3" ixy="0.0" ixz="0.0" iyy="0.3" iyz="0.0" izz="0.3"/>
</inertial>
</link>

<!-- Head with sensors -->
<link name="head">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="neck_joint" type="revolute">
<parent link="base_link"/>
<child link="head"/>
<origin xyz="0 0 0.35" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="10.0" velocity="1.0"/>
</joint>

<!-- Arms -->
<link name="left_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_shoulder_joint" type="revolute">
<parent link="base_link"/>
<child link="left_upper_arm"/>
<origin xyz="0.15 0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="left_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="left_elbow_joint" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<link name="right_upper_arm">
<visual>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.04" length="0.3"/>
</geometry>
</collision>
<inertial>
<mass value="1.0"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_shoulder_joint" type="revolute">
<parent link="base_link"/>
<child link="right_upper_arm"/>
<origin xyz="0.15 -0.1 0.15" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="50.0" velocity="1.0"/>
</joint>

<link name="right_lower_arm">
<visual>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
<material name="blue">
<color rgba="0.0 0.0 1.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.03" length="0.25"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.005" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
</inertial>
</link>

<joint name="right_elbow_joint" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="30.0" velocity="1.0"/>
</joint>

<!-- Legs -->
<link name="left_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_hip_joint" type="revolute">
<parent link="base_link"/>
<child link="left_hip"/>
<origin xyz="-0.1 0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="left_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="left_knee_joint" type="revolute">
<parent link="left_hip"/>
<child link="left_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="left_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="left_ankle_joint" type="revolute">
<parent link="left_thigh"/>
<child link="left_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

<link name="right_hip">
<visual>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_hip_joint" type="revolute">
<parent link="base_link"/>
<child link="right_hip"/>
<origin xyz="-0.1 -0.05 -0.3" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="100.0" velocity="0.5"/>
</joint>

<link name="right_thigh">
<visual>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.07" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="3.0"/>
<inertia ixx="0.06" ixy="0.0" ixz="0.0" iyy="0.06" iyz="0.0" izz="0.06"/>
</inertial>
</link>

<joint name="right_knee_joint" type="revolute">
<parent link="right_hip"/>
<child link="right_thigh"/>
<origin xyz="0 0 -0.1" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="0" upper="1.57" effort="150.0" velocity="0.5"/>
</joint>

<link name="right_shin">
<visual>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<collision>
<geometry>
<cylinder radius="0.06" length="0.45"/>
</geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="right_ankle_joint" type="revolute">
<parent link="right_thigh"/>
<child link="right_shin"/>
<origin xyz="0 0 -0.45" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<limit lower="-0.5" upper="0.5" effort="80.0" velocity="0.5"/>
</joint>

<!-- Create optical frame for camera -->
<joint name="head_camera_optical_joint" type="fixed">
<parent link="head"/>
<child link="head_camera_optical_frame"/>
<origin xyz="0.05 0 0" rpy="${-M_PI/2} 0 ${-M_PI/2}"/>
</joint>

<link name="head_camera_optical_frame"/>

</robot>

Implementation Guide

For Exercise 1 (Basic Humanoid):

  1. Create a simple URDF with torso, head, and arms
  2. Define visual, collision, and inertial properties
  3. Set appropriate joint limits and types
  4. Test in RViz to ensure proper visualization

For Exercise 2 (Leg Structure):

  1. Add hip, thigh, and shin links for each leg
  2. Create appropriate joints with correct axes of rotation
  3. Set realistic joint limits based on human anatomy
  4. Test the kinematic structure for proper movement

For Exercise 3 (xacro Macros):

  1. Define reusable macros for links and joints
  2. Create parameterized macros for body parts
  3. Use macros to build the complete model
  4. Verify reusability by creating different configurations

For Exercise 4 (Inertial Properties):

  1. Calculate volumes for each geometric shape
  2. Apply appropriate material density to calculate mass
  3. Use physics formulas to calculate inertia tensors
  4. Test in simulation to verify realistic behavior

For Exercise 5 (Gazebo Integration):

  1. Add Gazebo plugins for control and simulation
  2. Define material properties for Gazebo visualization
  3. Add transmission elements for joint control
  4. Test in Gazebo simulation environment

For Exercise 6 (Advanced Features):

  1. Add sensor plugins for IMU, camera, and force/torque sensors
  2. Create appropriate mounting points for sensors
  3. Define ROS 2 interfaces for sensor data
  4. Test sensor functionality in simulation

Best Practices

  1. Structure: Organize URDF files logically with clear comments
  2. Inertial Properties: Always calculate realistic inertial values for proper simulation
  3. Joint Limits: Set appropriate limits to prevent joint damage in simulation
  4. Testing: Test URDF files in RViz first, then in Gazebo
  5. Reusability: Use xacro macros to reduce code duplication
  6. Safety: Include safety controllers in joint definitions
  7. Documentation: Document all parameters and design decisions
  8. Validation: Use URDF validation tools to check for errors

These solutions provide complete implementations for each exercise, demonstrating best practices for URDF modeling of humanoid robots. Each solution builds on the previous ones, showing progressive complexity from basic structure to advanced features with sensors and simulation integration.