CodeTheRobot

ROS 2 Lyrical Luth · Lesson 42 of 86

URDF collision and visual geometry

Visual meshes vs simplified collision shapes, and performance trade-offs.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain urdf collision and visual geometry 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

Each link can have a <visual> (what you see in RViz) and a <collision> (what the physics engine and planners test). Visuals may use detailed meshes (STL, DAE); collisions should be simple primitives (box, cylinder, sphere) or low-polygon meshes, because collision checking is expensive.

Give collision geometry a slightly larger safety margin than the visual if needed, and check that neighbouring links do not collide in their default pose. Origins are relative to the link frame. Use metres and package:// URIs for meshes.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the urdf collision and visual geometry 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

<link name="base_link">
<!-- A link with visual and collision parts. -->
  <visual><geometry><mesh filename="package://robot_description/meshes/base.stl"/></geometry></visual>
<!-- A detailed mesh for display only. -->
  <collision><geometry><box size="0.4 0.3 0.15"/></geometry></collision>
<!-- A simple box used for physics and planning. -->
</link>
<!-- End of the link. -->

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 URDF collision and visual geometry. Expected result: RViz2 shows a box around the base when Collision Enabled is on

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.

<link name="base_link">
  <visual><geometry><box size="0.4 0.3 0.15"/></geometry></visual>
  <!-- TODO: add a collision element with a box of size 0.4 0.3 0.15 -->
</link>

Expected output

RViz2 shows a box around the base when Collision Enabled is on
Stuck? Show the full solution (try it yourself first)
<link name="base_link">
  <visual><geometry><box size="0.4 0.3 0.15"/></geometry></visual>
  <collision><geometry><box size="0.4 0.3 0.15"/></geometry></collision>
</link>

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

Give every link of your robot a simple collision primitive, view collisions in RViz2 (RobotModel → Collision Enabled), and compare simulation speed with a detailed mesh used as collision.

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.