--- sidebar_position: 5 --- # Chapter 4: URDF and Humanoid Robot Models ## Learning Objectives - Understand the Unified Robot Description Format (URDF) for robot modeling - Create URDF files for complex humanoid robot structures - Learn about joints, links, inertial properties, and visual/collision elements - Integrate URDF models with ROS 2 simulation environments - Validate and test humanoid robot models ## Introduction to URDF URDF (Unified Robot Description Format) is an XML format used in ROS to describe robot models. It defines the physical structure of a robot including links, joints, inertial properties, visual representations, and collision models. For humanoid robots, URDF is essential for simulation, visualization, and motion planning. ### Why URDF is Important for Humanoid Robots 1. **Simulation**: Gazebo and other simulators use URDF to create physics models 2. **Visualization**: RViz uses URDF to display robots in 3D 3. **Motion Planning**: Planning algorithms need URDF models to understand robot kinematics 4. **Control**: Robot controllers use URDF for kinematic calculations 5. **Standardization**: URDF provides a standard way to model robots across the ROS community ## URDF Structure Overview A URDF file consists of: - **Links**: Rigid parts of the robot (e.g., torso, limb segments) - **Joints**: Connections between links (e.g., hinges, prismatic joints) - **Materials**: Visual properties (colors, textures) - **Gazebos**: Simulation-specific properties ### Basic URDF Example ```xml ``` ## Links in URDF Links represent rigid bodies in the robot. Each link contains: 1. **Visual Properties**: How the link appears in visualization 2. **Collision Properties**: How the link interacts in physics simulation 3. **Inertial Properties**: Physical properties for physics simulation ### Visual and Collision Elements ```xml ``` ## Joints in URDF Joints define how links connect and move relative to each other. Common joint types: 1. **Fixed**: No movement (welded joint) 2. **Revolute**: Rotational movement around single axis (like hinges) 3. **Continuous**: Like revolute but unlimited rotation 4. **Prismatic**: Linear sliding movement 5. **Floating**: 6DOF movement 6. **Planar**: Movement in a plane ### Joint Definitions ```xml ``` ## Creating a Humanoid Robot Model Let's create a simplified humanoid model with legs, torso, arms, and head: ```xml ``` ## Xacro for Complex Models Xacro (XML Macros) is a macro language that extends URDF with features like variables, constants, and macros to simplify complex robot models: ```xml ``` ## URDF for Simulation with Gazebo To use URDF in Gazebo simulation, we need to add Gazebo-specific tags: ```xml Gazebo/White false /humanoid_robot gazebo_ros_control/DefaultRobotHWSim transmission_interface/SimpleTransmission hardware_interface/EffortJointInterface hardware_interface/EffortJointInterface 1 ``` ## Validating URDF Models ### Tools for URDF Validation 1. **check_urdf**: Command-line tool to validate URDF syntax ```bash check_urdf /path/to/robot.urdf ``` 2. **urdf_to_graphiz**: Generate a visual graph of the kinematic tree ```bash urdf_to_graphiz /path/to/robot.urdf ``` 3. **RViz**: Visualize the robot model ### Common URDF Issues 1. **Incorrect Mass Values**: Too light or too heavy links 2. **Invalid Inertial Values**: Non-positive definite inertia matrices 3. **Undefined Materials**: Referenced materials that don't exist 4. **Joint Limit Issues**: Invalid or unrealistic joint limits 5. **Kinematic Loops**: URDF only supports tree structures, not loops ## Loading URDF in ROS 2 To use your URDF model in ROS 2, you need to: 1. **Launch the robot state publisher**: ```xml from launch import LaunchDescription from launch.substitutions import Command, PathJoinSubstitution from launch_ros.actions import Node from launch_ros.substitutions import FindPackageShare def generate_launch_description(): # Get URDF via xacro robot_description_content = Command( [ PathJoinSubstitution([FindPackageShare("your_robot_description"), "urdf", "robot.urdf.xacro"]), ] ) robot_description = {"robot_description": robot_description_content} node_robot_state_publisher = Node( package="robot_state_publisher", executable="robot_state_publisher", output="screen", parameters=[robot_description], ) return LaunchDescription([ node_robot_state_publisher, ]) ``` 2. **Using it in a ROS 2 node**: ```python import rclpy from rclpy.node import Node from sensor_msgs.msg import JointState from tf2_ros import TransformBroadcaster import math class HumanoidController(Node): def __init__(self): super().__init__('humanoid_controller') # Subscribe to joint states self.joint_state_sub = self.create_subscription( JointState, '/joint_states', self.joint_state_callback, 10 ) # Publisher for joint commands self.joint_cmd_pub = self.create_publisher( JointState, '/joint_commands', 10 ) # TF broadcaster for robot transforms self.tf_broadcaster = TransformBroadcaster(self) # Timer for control loop self.control_timer = self.create_timer(0.02, self.control_loop) # 50 Hz self.current_joint_states = JointState() self.get_logger().info('Humanoid Controller initialized') def joint_state_callback(self, msg): """Update current joint states""" self.current_joint_states = msg def control_loop(self): """Main control loop""" # Example: move left arm in a simple pattern cmd_msg = JointState() cmd_msg.header.stamp = self.get_clock().now().to_msg() cmd_msg.name = ['left_shoulder_pitch', 'right_shoulder_pitch'] # Generate a simple oscillating pattern t = self.get_clock().now().nanoseconds / 1e9 # Time in seconds left_pos = 0.5 * math.sin(t) # Oscillate between -0.5 and 0.5 right_pos = 0.5 * math.sin(t + math.pi) # Out of phase cmd_msg.position = [left_pos, right_pos] self.joint_cmd_pub.publish(cmd_msg) def main(args=None): rclpy.init(args=args) controller = HumanoidController() try: rclpy.spin(controller) except KeyboardInterrupt: pass finally: controller.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` ## Advanced URDF Techniques ### Using Mesh Files For more detailed robot models, use mesh files instead of primitive shapes: ```xml ``` ### Gazebo-Specific Enhancements ```xml 30.0 1.3962634 800 600 R8G8B8 0.02 300 head_camera_frame image_raw ``` ## Troubleshooting Common Issues ### 1. Robot Falls Through Ground - Check inertial values (mass and inertia matrix) - Verify joint limits and dynamics parameters - Ensure proper collision geometries ### 2. Robot Explodes in Simulation - Check mass values (not too low or negative) - Verify inertia matrix is positive definite - Check joint dynamics (damping and friction) ### 3. RViz Shows Incorrect Model - Check for URDF syntax errors - Verify that the robot_state_publisher is running - Ensure joint states are being published correctly ## Summary URDF is a fundamental component for representing humanoid robots in ROS 2. This chapter covered: - The structure of URDF files with links, joints, visual, collision, and inertial properties - How to model a complete humanoid robot with legs, torso, arms, and head - The use of Xacro to simplify complex robot descriptions - Integration with Gazebo simulation and ROS 2 systems - Validation techniques and common troubleshooting approaches Properly modeling your humanoid robot is crucial for successful simulation and control. The URDF serves as the bridge between the physical robot design and its digital representation in the ROS 2 ecosystem. ## Exercises 1. Create a URDF file for a simple humanoid robot with at least 10 joints 2. Visualize your robot model in RViz 3. Add a sensor (like a camera or IMU) to your robot model ## Next Steps With a complete understanding of ROS 2 fundamentals, communication patterns, AI integration, and robot modeling, you're now ready to explore more advanced topics in humanoid robotics. The next module will cover physics simulation and sensor systems using Gazebo and other simulation tools.