Humanoid URDF to SDF Conversion
Estimated Reading Time: 18 minutes Prerequisites: ROS 2 URDF/Xacro basics, Gazebo Setup Difficulty: Intermediate
Introduction
You have a robot model. It's in URDF format. ROS 2 uses it. But Gazebo needs SDF format. You need to convert.
This chapter shows you how. You'll learn the differences. You'll convert models. You'll fix common problems.
By the end, your robot runs in Gazebo. Perfectly.
Why Two Formats?
URDF: ROS Native
URDF was created for ROS. It describes robots simply. It works great with ROS tools.
Advantages:
- Simple structure
- ROS-native
- Xacro support
- Wide adoption
Limitations:
- ROS-specific
- Limited simulation features
- No world modeling
SDF: Gazebo Native
SDF was created for Gazebo. It's more powerful. It handles complex simulations.
Advantages:
- Rich simulation features
- World and environment support
- Advanced physics
- Better sensors
Limitations:
- More complex
- Less ROS integration
Example 1: Format Comparison
Type: Comparison
Context: You have a humanoid robot. You want to use it in both ROS 2 and Gazebo.
Explanation: Keep your master model in URDF. Use it for ROS 2 tools. Robot state publisher. MoveIt. Everything ROS. Then convert to SDF for Gazebo simulation. SDF handles physics better. It simulates sensors accurately. It models environments. Use both formats. Each for its strength.
Takeaway: URDF for ROS integration. SDF for simulation. Convert between them as needed.
Understanding URDF Structure
URDF files describe robots in XML. Key elements:
Links
Links are rigid bodies:
<link name="torso">
<visual>
<geometry>
<box size="0.3 0.2 0.4"/>
</geometry>
</visual>
<collision>
<geometry>
<box size="0.3 0.2 0.4"/>
</geometry>
</collision>
<inertial>
<mass value="5.0"/>
<inertia ixx="0.1" iyy="0.1" izz="0.1"
ixy="0" ixz="0" iyz="0"/>
</inertial>
</link>
This defines a torso. It has visual geometry. Collision geometry. Mass properties.
Joints
Joints connect links:
<joint name="left_hip" type="revolute">
<parent link="torso"/>
<child link="left_thigh"/>
<origin xyz="0 0.1 -0.2" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57"
effort="100" velocity="2.0"/>
</joint>
This connects the torso to left thigh. It rotates. It has limits.
Understanding SDF Structure
SDF is similar but more detailed:
Links in SDF
<link name="torso">
<pose>0 0 0 0 0 0</pose>
<inertial>
<mass>5.0</mass>
<inertia>
<ixx>0.1</ixx>
<iyy>0.1</iyy>
<izz>0.1</izz>
<ixy>0</ixy>
<ixz>0</ixz>
<iyz>0</iyz>
</inertia>
</inertial>
<collision name="collision">
<geometry>
<box>
<size>0.3 0.2 0.4</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.3 0.2 0.4</size>
</box>
</geometry>
</visual>
</link>
Notice the differences. Each element has a name. Syntax is slightly different.
Joints in SDF
<joint name="left_hip" type="revolute">
<parent>torso</parent>
<child>left_thigh</child>
<pose>0 0.1 -0.2 0 0 0</pose>
<axis>
<xyz>0 1 0</xyz>
<limit>
<lower>-1.57</lower>
<upper>1.57</upper>
<effort>100</effort>
<velocity>2.0</velocity>
</limit>
</axis>
</joint>
The structure is nested differently. The axis contains limits.
Conversion Methods
Method 1: Automatic Conversion
Gazebo includes a converter:
# Library: Gazebo Harmonic (gz command)
# Last updated: 2025-12-05
# Tested on: Gazebo Harmonic 8.0
gz sdf -p your_robot.urdf > your_robot.sdf
This converts automatically. It handles most cases.
Method 2: Xacro Processing First
If using Xacro:
# Process Xacro to URDF
ros2 run xacro xacro your_robot.urdf.xacro > temp.urdf
# Convert to SDF
gz sdf -p temp.urdf > your_robot.sdf
# Clean up
rm temp.urdf
This is a two-step process.
Method 3: Conversion Script
Automate the process:
# Library: subprocess (Python stdlib)
# Last updated: 2025-12-05
# Tested on: Python 3.10, Gazebo Harmonic
#!/usr/bin/env python3
import subprocess
import sys
def convert_to_sdf(urdf_file, sdf_file):
"""Convert URDF to SDF"""
try:
result = subprocess.run(
['gz', 'sdf', '-p', urdf_file],
capture_output=True,
text=True,
check=True
)
with open(sdf_file, 'w') as f:
f.write(result.stdout)
print(f"✓ Converted {urdf_file} to {sdf_file}")
return True
except subprocess.CalledProcessError as e:
print(f"✗ Error: {e.stderr}")
return False
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: convert_robot.py input.urdf output.sdf")
sys.exit(1)
urdf_file = sys.argv[1]
sdf_file = sys.argv[2]
if convert_to_sdf(urdf_file, sdf_file):
sys.exit(0)
else:
sys.exit(1)
Save this script. Use it repeatedly.
Example 2: Choosing a Conversion Method
Type: Decision Guide
Context: You have multiple robot variants. You update them frequently.
Explanation: Manual conversion is tedious. You convert. You test. You change the URDF. You convert again. This wastes time. Use the script instead. One command converts any robot. Update your URDF. Run the script. Done. Even better, add it to your build process. Every time you build, the SDF updates automatically.
Takeaway: Automate repetitive tasks. Scripts save time and prevent errors.
Key Conversion Differences
Attribute vs Element Syntax
URDF uses attributes:
<box size="1 2 3"/>
SDF uses elements:
<box>
<size>1 2 3</size>
</box>
Pose Representation
URDF uses origin:
<origin xyz="1 2 3" rpy="0 0 0"/>
SDF uses pose:
<pose>1 2 3 0 0 0</pose>
Parent-Child Syntax
URDF:
<parent link="torso"/>
<child link="arm"/>
SDF:
<parent>torso</parent>
<child>arm</child>
Testing Converted Models
Load in Gazebo
gz sim your_robot.sdf
The robot appears. Check these things:
Visual Check:
- Robot looks correct
- Links are positioned right
- Colors and textures work
Physics Check:
- Robot doesn't fall through ground
- Joints move properly
- No violent explosions
Performance Check:
- Simulation runs smoothly
- Real-time factor near 1.0
Verify Joint Behavior
Move joints manually. In Gazebo GUI:
- Right-click the robot
- Select "View Joints"
- Move sliders
Joints should move smoothly. No jitter. No instability.
Check Collisions
Add objects to the world. Make the robot touch them. Collisions should work correctly.
Common Conversion Problems
Problem 1: Robot Falls Through Ground
Symptoms: Robot spawns and immediately falls.
Causes:
- Missing collision geometry
- Incorrect contact parameters
- Wrong spawn height
Solutions:
Check collisions exist:
<collision name="collision">
<geometry>
<box>
<size>0.3 0.2 0.4</size>
</box>
</geometry>
</collision>
Set proper spawn height:
<pose>0 0 1.0 0 0 0</pose>
1 meter above ground. The robot falls safely.
Problem 2: Robot Explodes
Symptoms: Parts fly apart violently.
Causes:
- Wrong inertial values
- Overlapping collision geometry
- Too-small time step
Solutions:
Check mass is realistic:
<mass>5.0</mass>
Not 0.001. Not 10000. Human-scale robots: 1-100 kg.
Check inertia values:
<inertia>
<ixx>0.1</ixx>
<iyy>0.1</iyy>
<izz>0.1</izz>
</inertia>
Use realistic values. Don't set to zero.
Problem 3: Joints Are Frozen
Symptoms: Joints don't move at all.
Causes:
- Missing axis definition
- Static model
- Wrong joint type
Solutions:
Ensure model is dynamic:
<static>false</static>
Check axis is defined:
<axis>
<xyz>0 1 0</xyz>
</axis>
Verify joint type:
<joint name="knee" type="revolute">
Not fixed if you want movement.
Problem 4: Meshes Don't Load
Symptoms: Parts are invisible or wrong color.
Causes:
- Wrong mesh paths
- Missing mesh files
- Incorrect file format
Solutions:
Use relative paths:
<mesh>
<uri>model://my_robot/meshes/torso.stl</uri>
</mesh>
Or absolute paths:
<mesh>
<uri>file:///path/to/torso.stl</uri>
</mesh>
Check file exists:
ls /path/to/torso.stl
Advanced Tuning
Contact Parameters
Improve foot-ground contact:
<collision name="foot_collision">
<geometry>
<box>
<size>0.1 0.05 0.02</size>
</box>
</geometry>
<surface>
<friction>
<ode>
<mu>1.0</mu>
<mu2>1.0</mu2>
</ode>
</friction>
<contact>
<ode>
<kp>1e7</kp>
<kd>1.0</kd>
</ode>
</contact>
</surface>
</collision>
High friction prevents slipping. Stiff contact prevents sinking.
Joint Dynamics
Add damping for stability:
<joint name="knee" type="revolute">
<axis>
<dynamics>
<damping>0.7</damping>
<friction>0.1</friction>
</dynamics>
</axis>
</joint>
Damping slows oscillations. Friction resists movement.
Complete Example
Here's a minimal humanoid in SDF:
<!-- Library: Gazebo Harmonic SDF 1.9
Last updated: 2025-12-05
Tested on: Gazebo Harmonic 8.0 -->
<?xml version="1.0"?>
<sdf version="1.9">
<model name="simple_humanoid">
<pose>0 0 1.0 0 0 0</pose>
<static>false</static>
<!-- Torso -->
<link name="torso">
<inertial>
<mass>10.0</mass>
<inertia>
<ixx>0.2</ixx>
<iyy>0.2</iyy>
<izz>0.1</izz>
</inertia>
</inertial>
<collision name="collision">
<geometry>
<box>
<size>0.3 0.2 0.5</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.3 0.2 0.5</size>
</box>
</geometry>
<material>
<ambient>0.2 0.2 0.8 1</ambient>
<diffuse>0.2 0.2 0.8 1</diffuse>
</material>
</visual>
</link>
<!-- Left Leg -->
<link name="left_leg">
<pose>0 0.1 -0.25 0 0 0</pose>
<inertial>
<mass>3.0</mass>
<inertia>
<ixx>0.05</ixx>
<iyy>0.05</iyy>
<izz>0.01</izz>
</inertia>
</inertial>
<collision name="collision">
<geometry>
<cylinder>
<radius>0.05</radius>
<length>0.4</length>
</cylinder>
</geometry>
</collision>
<visual name="visual">
<geometry>
<cylinder>
<radius>0.05</radius>
<length>0.4</length>
</cylinder>
</geometry>
</visual>
</link>
<!-- Hip Joint -->
<joint name="left_hip" type="revolute">
<parent>torso</parent>
<child>left_leg</child>
<pose>0 0 0.2 0 0 0</pose>
<axis>
<xyz>0 1 0</xyz>
<limit>
<lower>-1.57</lower>
<upper>1.57</upper>
<effort>100</effort>
<velocity>2.0</velocity>
</limit>
<dynamics>
<damping>0.7</damping>
</dynamics>
</axis>
</joint>
</model>
</sdf>
This is a complete working model. Torso. One leg. One joint. You can extend it.
Best Practices
Keep URDF as Source
Don't edit SDF files directly. Edit URDF. Then reconvert. This keeps your workflow clean.
Validate Before Converting
Check URDF first:
check_urdf your_robot.urdf
Fix errors before converting.
Test Incrementally
Don't convert a 100-link robot at once. Start with one link. Then two. Then three. Find problems early.
Version Control
Use git. Track both URDF and SDF. This helps debugging.
Summary
You've learned URDF to SDF conversion. You understand the differences. You can convert automatically. You can fix common problems.
Key points:
- URDF is for ROS integration
- SDF is for simulation
- Convert using gz sdf command
- Test thoroughly in Gazebo
- Fix physics issues carefully
Next Steps
Your robot model works in Gazebo. Now add functionality.
Continue to: Sensor Simulation
Related topics: