CodeTheRobot

Ubuntu Linux for Robotics · Lesson 55 of 96

Xacro

Converting xacro to URDF on the command line and using it in launch files.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain xacro in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Xacro adds properties, macros and arithmetic to URDF so you do not repeat yourself. Convert with xacro robot.urdf.xacro > robot.urdf, and pass arguments as name:=value. In launch files run the xacro command to produce the robot_description parameter.

Always check the generated URDF with check_urdf, because a xacro error shows up only after expansion. Keep shared properties (wheel radius, body size) in one file and include it from the others with xacro:include.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the xacro commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

xacro robot.urdf.xacro > robot.urdf
# Expand the macros into plain URDF.
xacro robot.urdf.xacro use_sim:=true > robot_sim.urdf
# Pass an argument to switch simulation-only parts on.
check_urdf robot.urdf
# Validate the result.
xacro robot.urdf.xacro | grep -c '<joint'
# Count how many joints the expanded file contains.
ros2 param get /robot_state_publisher robot_description | head -c 200
# Inspect the URDF that a running node received.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

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 Xacro. Expected result: The file robot.urdf is created and check_urdf accepts it

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: expand robot.urdf.xacro into robot.urdf
check_urdf robot.urdf

Expected output

The file robot.urdf is created and check_urdf accepts it
Stuck? Show the full solution (try it yourself first)
xacro robot.urdf.xacro > robot.urdf
check_urdf robot.urdf

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Turn a URDF with two identical wheels into xacro with a macro and a radius property, expand it, validate it, and count the joints before and after.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.