Imagine opening RViz2 and seeing a cleanly rendered mobile robot, only to spawn that exact same robot in a physics simulator and watch it fall straight through the floor—or drive into walls during autonomous navigation because its laser scanner frame points backward. In nearly every case, the problem is not the 3D CAD mesh file (.stl or .dae), but the numbers and tags inside the URDF (Unified Robot Description Format) file. While a 3D mesh merely says “this part looks like these triangles,” a URDF file acts as a structural contract stating “this rigid body sits at this coordinate offset, rotates around this unit vector, and carries this exact mass and inertia tensor.”
With a containerized workspace ready—as configured in reproducible ROS 2 Jazzy environments with Docker and VS Code Dev Containers—the next step is defining the robot’s physical skeleton in code. This guide explains what URDF defines (and what it cannot represent), previews the complete 13-link tree of the myros_sketch mobile robot, breaks down <link> attributes (visual, collision, inertial) and <joint> types (fixed, continuous, revolute), derives <origin> radian rotations and <axis> conventions, and demonstrates how to refactor repetitive XML into xacro macros verified with check_urdf.
What URDF Defines: The Single Source of Truth in ROS 2
As outlined in the official ROS 2 Jazzy URDF tutorials, URDF is an XML schema that describes a robot’s physical structure. If a visualization tool, a coordinate transform publisher, and a physics engine each read separate configuration files, frame offsets inevitably drift apart. ROS 2 avoids this by treating the URDF model as the Single Source of Truth shared across three core subsystems:
| URDF XML Element | Physical or Geometric Meaning | Subsystem Consuming This Data in ROS 2 |
|---|---|---|
<link> | A single rigid body with its own local coordinate frame | robot_state_publisher (TF2 frames), physics rigid bodies |
<joint> | Parent-to-child frame transform and kinematic degree of freedom | Static/dynamic TF2 transforms, ros2_control actuator interfaces |
<visual> | Surface geometry and color (<material>) for rendering | rviz2 display, Gazebo camera rendering pipelines |
<collision> | Simplified bounding volume for contact and obstacle checks | Gazebo physics contact solver, MoveIt2 collision avoidance |
<inertial> | Mass (kg), center of mass (<origin>), and 3×3 inertia matrix | Dynamic simulation of gravity, wheel slip, torque, and acceleration |
Understanding what standard URDF cannot do is equally important when deciding how classical robot modeling fits into modern learning pipelines, a trade-off examined in when and how deep to learn ROS 2 for Physical AI:
- No closed kinematic loops: Every URDF must form a strict tree where each link has at most one parent link. Four-bar linkages, parallel manipulators, and closed suspension loops cannot be expressed natively in pure URDF and require SDF or custom gazebo plugins.
- No deformable bodies: Every
<link>is modeled as a strictly rigid body; soft robotics, cables, and fluids are outside the specification. - No sensor physics or controller gains: LiDAR scan rates, camera distortion coefficients, surface friction (
mu1,mu2), and motor PID loops live inside<gazebo>extension blocks orros2_controlYAML files rather than core URDF tags.
Building a Robot Tree with Links and Joints: The myros_sketch Blueprint
At its core, writing a URDF file means dividing a robot into distinct rigid bodies (<link>) and connecting each body to its parent through a kinematic constraint (<joint>). In our differential-drive educational robot myros_sketch, the kinematic tree starts at the ground projection frame (base_footprint) and branches outward through base_link and chassis to the wheels, casters, compute boards, and sensors:
base_footprint (ground-plane projection reference)
└── [base_joint: fixed] ──> base_link (kinematic center reference)
└── [chassis_joint: fixed] ──> chassis (main robot body box)
├── [left_wheel_joint: continuous] ──> left_wheel (left drive wheel)
├── [right_wheel_joint: continuous] ──> right_wheel (right drive wheel)
├── [front_caster_joint: fixed] ──> front_caster (front support caster)
├── [rear_caster_joint: fixed] ──> rear_caster (rear support caster)
├── [esp32s3_joint: fixed] ──> esp32s3_frame (MCU board)
├── [imu_joint: fixed] ──> imu_link (inertial measurement unit)
├── [rpi_base_joint: fixed] ──> rpi_base ──> rpi_frame (onboard SBC)
├── [lidar_base_joint: fixed] ──> lidar_base_frame ──> laser_frame (2D LiDAR)
└── [camera_joint: fixed] ──> camera_link ──> camera_link_optical (camera)
| Subsystem Group | Component Links in myros_sketch | Joint Type Used | Key Modeling Role |
|---|---|---|---|
| Base Coordinate Frames | base_footprint, base_link, chassis | fixed | Establishes ground clearance (z) and chassis center offset |
| Differential Drive Wheels | left_wheel, right_wheel | continuous | Unbounded rotation around the Y-axis (0 1 0) |
| Passive Support Casters | front_caster, rear_caster | fixed | Keeps the chassis level against the ground plane |
| Compute and Sensor Frames | esp32s3_frame, imu_link, rpi_frame, laser_frame, camera_link_optical | fixed | Publishes exact TF2 coordinate origins matching sensor frame_id headers |
Notice that links such as base_link, laser_frame, or camera_link_optical can be declared as empty elements (<link name="laser_frame"/>) without any <visual> geometry at all. In ROS 2, a <link> is fundamentally a 3D coordinate frame, not a graphical mesh; even if it is invisible in RViz2, robot_state_publisher publishes its TF2 transform so LiDAR scans and camera point clouds align properly in space.
Anatomy of a link Element: visual, collision, inertial, and Geometry Primitives
According to the official ROS Wiki URDF XML specification, a complete physical <link> contains three child blocks: <visual>, <collision>, and <inertial>. Below is a minimal, self-contained URDF model that passes check_urdf and loads cleanly into both RViz2 and Gazebo:
<?xml version="1.0"?>
<robot name="myros_sketch">
<material name="dark_grey">
<color rgba="0.3 0.3 0.3 1.0"/>
</material>
<link name="base_link"/>
<link name="chassis">
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.20 0.15 0.05"/>
</geometry>
<material name="dark_grey"/>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.20 0.15 0.05"/>
</geometry>
</collision>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="0.45"/>
<inertia ixx="9.375e-4" ixy="0.0" ixz="0.0"
iyy="1.594e-3" iyz="0.0" izz="2.344e-3"/>
</inertial>
</link>
<joint name="chassis_joint" type="fixed">
<parent link="base_link"/>
<child link="chassis"/>
<origin xyz="0 0 0.05" rpy="0 0 0"/>
</joint>
</robot>
1. Separating visual, collision, and inertial Properties
| Child Block | Functional Purpose | Used by RViz2? | Used by Gazebo Physics? | Recommended Authoring Practice |
|---|---|---|---|---|
<visual> | Renders external shape and <material> color (rgba) | Yes (default view) | Only for simulated optical cameras | Detailed STL/DAE meshes or composed primitives |
<collision> | Defines contact surfaces for physics and motion planning | Optional toggle | Yes (every physics solver step) | Always prefer simple box, cylinder, or sphere primitives |
<inertial> | Specifies mass (kg), center of mass, and 3×3 rotational inertia | Optional gizmo | Yes (mandatory for dynamic links) | Never leave mass at 0; compute positive diagonal moments (ixx, iyy, izz) |
Beginners frequently define <visual> blocks and skip <collision> and <inertial>. While the model looks fine in RViz2, spawning it in Gazebo causes the physics engine to either ignore the zero-mass link or let the robot drop straight through the ground plane. Conversely, pasting a 500,000-vertex CAD .stl mesh into <collision> forces the physics engine to test hundreds of thousands of triangles every millisecond, cutting the simulation Real Time Factor in half.
2. The Four Geometry Primitives and REP-0103 SI Units
Per REP-0103 (Standard Units of Measure and Coordinate Conventions), every URDF file uses meters (m) for length, kilograms (kg) for mass, and radians (rad) for angles, following the right-handed coordinate convention (+X forward, +Y left, +Z up). Writing radius="32.5" instead of radius="0.0325" creates a wheel 65 meters tall.
| Geometry Tag | XML Syntax Example | Parameter Meaning (Units: meters) | Typical Robot Component |
|---|---|---|---|
<box> | <box size="0.30 0.20 0.06"/> | Full length along X, width along Y, height along Z | Main chassis, Raspberry Pi board, battery pack |
<cylinder> | <cylinder radius="0.0325" length="0.026"/> | radius (m) and full length along the local Z-axis | Drive wheels, cylindrical 2D LiDAR turrets |
<sphere> | <sphere radius="0.015"/> | radius (m) from the local origin | Passive ball casters, spherical joints |
<mesh> | <mesh filename="package://my_pkg/meshes/body.stl" scale="0.001 0.001 0.001"/> | Package URI and per-axis scale (0.001 converts mm STL to m) | Complex 3D-printed shells and sensor brackets |
Anatomy of a joint Element: Types, origin Radian Rotations, and axis Vectors
A <joint> connects a <parent> link to a <child> link and defines how the child coordinate frame moves relative to the parent frame. The ROS Wiki urdf/XML/joint specification defines six joint types:
| Joint Type | Degrees of Freedom (DOF) | Requires <limit> Tag? | Kinematic Behavior and Typical Application |
|---|---|---|---|
fixed | 0 | No | Rigidly welds child to parent (sensor mounts, chassis-to-base offsets) |
continuous | 1 (rotational) | No angle bounds (effort/velocity advised) | Unbounded rotation around <axis> (differential-drive wheels) |
revolute | 1 (rotational) | Yes (lower, upper, effort, velocity) | Hinged rotation within strict angle limits (robot arm joints, steering servos) |
prismatic | 1 (translational) | Yes (lower, upper, effort, velocity) | Linear sliding motion along <axis> within bounds (elevators, parallel grippers) |
floating | 6 (3 linear + 3 angular) | No | Unconstrained 6-DOF body in 3D space (specialized free-body simulations) |
planar | 3 (2 linear + 1 angular) | No | Motion constrained to a 2D surface plane (rarely used in standard robots) |
Why wheels must use continuous rather than revolute is straightforward: a revolute joint enforces hard <limit lower="..." upper="..."/> stops, meaning a wheel would physically slam to a halt once its accumulated rotation reached the upper angle limit. For articulated robot arms where revolute and prismatic joints chain together to position an end-effector, see the robot arm kinematics and pick-and-place minimum math guide.
1. Understanding origin xyz Offsets and Fixed-Axis rpy Radian Rotations
Inside a <joint> (or inside a link’s <visual>/<collision> block), <origin xyz="x y z" rpy="roll pitch yaw"/> defines the pose of the child frame expressed in the parent frame. xyz specifies translation in meters, while rpy specifies Euler rotations in radians applied around the fixed axes in X (roll) → Y (pitch) → Z (yaw) order:
The single most common syntax error in URDF authoring is typing degrees into rpy. Writing rpy="0 90 0" does not rotate a sensor by 90 degrees—it rotates it by 90 radians (approximately 5,156.6 degrees), leaving the link skewed at an arbitrary angle. Always use radian equivalents:
- $90^\circ = \frac{\pi}{2} \approx 1.5708\text{ rad}$
- $-90^\circ = -\frac{\pi}{2} \approx -1.5708\text{ rad}$
- $180^\circ = \pi \approx 3.14159\text{ rad}$
For example, a <cylinder> primitive stands upright along the local Z-axis by default. To orient that cylinder sideways as a wheel rolling along the Y-axis, apply a 90-degree roll rotation (rpy="1.5708 0 0") inside the wheel’s <visual> and <collision> <origin> tags.
2. The axis Vector and the Right-Hand Rule
<joint name="left_wheel_joint" type="continuous">
<parent link="chassis"/>
<child link="left_wheel"/>
<origin xyz="0 0.08 -0.02" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit effort="1.5" velocity="20.0"/>
<dynamics damping="0.05" friction="0.02"/>
</joint>
The <axis xyz="0 1 0"/> tag specifies the unit vector of rotation in the joint’s local frame (defaulting to 1 0 0 if omitted). Under the right-hand rule with +X pointing forward and +Y pointing left, pointing your right thumb along +Y (0 1 0) curls your fingers from +Z (top of the wheel) toward +X (front of the robot). Consequently, both the left wheel and the right wheel should use <axis xyz="0 1 0"/> so that a positive angular velocity drives both wheels forward. Flipping the right wheel’s axis to 0 -1 0 causes positive motor commands to spin the two wheels in opposite directions.
Eliminating Repetition with xacro Macros and Validating with check_urdf
Writing left and right drive wheels or four identical standoffs in raw URDF duplicates dozens of XML lines that differ only by a + or - sign on the Y-coordinate. Xacro (XML Macros) extends URDF with constants (<xacro:property>), math expressions (${...} including built-in pi), and parameterized blocks (<xacro:macro>):
<?xml version="1.0"?>
<robot name="myros_sketch" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:property name="wheel_radius" value="0.0325"/>
<xacro:property name="wheel_length" value="0.026"/>
<xacro:property name="wheel_separation" value="0.16"/>
<link name="base_link"/>
<xacro:macro name="drive_wheel" params="prefix y_reflect">
<link name="${prefix}_wheel">
<visual>
<origin xyz="0 0 0" rpy="${pi/2} 0 0"/>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}"/>
</geometry>
</visual>
<collision>
<origin xyz="0 0 0" rpy="${pi/2} 0 0"/>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}"/>
</geometry>
</collision>
</link>
<joint name="${prefix}_wheel_joint" type="continuous">
<parent link="base_link"/>
<child link="${prefix}_wheel"/>
<origin xyz="0 ${y_reflect * wheel_separation / 2} 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit effort="1.5" velocity="20.0"/>
</joint>
</xacro:macro>
<xacro:drive_wheel prefix="left" y_reflect="1"/>
<xacro:drive_wheel prefix="right" y_reflect="-1"/>
</robot>
Verification Commands and Troubleshooting Table
Before launching a complex robot description in Gazebo, validate its XML syntax and kinematic tree topology from the terminal:
# 1. Expand the xacro file into plain URDF XML to verify macro evaluation
xacro myros_sketch.urdf.xacro > /tmp/myros_sketch.urdf
# 2. Validate tree structure, single root link, and parent-child references
check_urdf /tmp/myros_sketch.urdf
# 3. Export a visual diagram of the link-and-joint hierarchy (requires graphviz)
urdf_to_graphviz /tmp/myros_sketch.urdf
# 4. Inspect visual alignment and test joint sliders interactively in RViz2
ros2 launch urdf_tutorial display.launch.py model:=/tmp/myros_sketch.urdf
| Verification Tool or Symptom | Root Cause | Corrective Action |
|---|---|---|
check_urdf: Two root links found | An unattached link exists without a parent joint | Ensure every non-root link is referenced as <child> in exactly one <joint> |
| Link invisible in RViz2 | Missing <visual>, broken package:// mesh path, or unbuilt package | Check mesh URI and scale, and run colcon build --symlink-install |
| Wheel lies flat horizontally instead of standing upright | Cylinder default Z-axis was not rotated to align with the Y axle | Add <origin xyz="0 0 0" rpy="${pi/2} 0 0"/> inside the wheel’s <visual> and <collision> |
| Component twisted at an unexpected angle | Degree values (e.g., 90) entered in rpy instead of radians | Replace degree values with radians (1.5708) or xacro expressions (${pi/2}) |
| Robot explodes or sinks through floor in Gazebo | Missing <collision> or zero/invalid <inertial> mass and inertia tensor | Add <collision> primitives and positive <mass> + diagonal inertia moments (ixx, iyy, izz) |