CodeTheRobot

ROS 2 Lyrical Luth · Lesson 27 of 86

SDF vs URDF

What each format is for, what SDF adds and how Gazebo converts URDF.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain sdf vs urdf in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

URDF describes one robot's tree of links and joints for ROS. SDF (Simulation Description Format) describes whole worlds for Gazebo: models, lights, physics, sensors and plugins, and it supports closed loops and multiple robots. Gazebo can load a URDF by converting it to SDF, but URDF cannot describe a world.

Keep one source of truth: usually a xacro/URDF robot, spawned into an SDF world. Simulation-only details (sensors, plugins) go in <gazebo> tags in the URDF or in the SDF itself. Convert to check the result with gz sdf -p robot.urdf.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the sdf vs urdf example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

<sdf version="1.9">
<!-- SDF root; version 1.9 is used by Gazebo Harmonic. -->
  <world name="empty_world">
<!-- A world holds models, lights and physics. -->
    <light type="directional" name="sun"><direction>-0.5 0.1 -0.9</direction></light>
<!-- A sun so the scene is visible. -->
    <model name="ground"><static>true</static><link name="link"><collision name="c"><geometry><plane><normal>0 0 1</normal></plane></geometry></collision></link></model>
<!-- A static ground plane. -->
  </world>
<!-- End of the world. -->
</sdf>
<!-- End of the file. -->

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Robot arm

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

tip x110 y58ROBOT ARM

Demo for SDF vs URDF. Expected result: An SDF version of the robot is printed

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# TODO: print the SDF produced from mini_arm.urdf using gz sdf -p

Expected output

An SDF version of the robot is printed
Stuck? Show the full solution (try it yourself first)
gz sdf -p mini_arm.urdf

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Convert your URDF with gz sdf -p and compare the result with the original. List three things the SDF world file can describe that the URDF cannot.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.