CodeTheRobot

ROS 2 Lyrical Luth · Lesson 19 of 86

Xacro macros

Properties, macros, math and includes to avoid repeating URDF.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain xacro macros 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

Xacro is a macro language for URDF. Properties (xacro:property) hold constants, macros (xacro:macro) generate repeated parts such as wheels, and ${...} evaluates arithmetic. You write .urdf.xacro and convert with xacro file.urdf.xacro > robot.urdf.

Use xacro for anything with repeated structure: two wheels, four legs, a six-joint arm. Pass arguments (xacro:arg) for options such as a simulation flag, and include shared files with xacro:include.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the xacro macros 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

<robot name="diff_bot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- Root element with the xacro namespace. -->
  <xacro:property name="wheel_radius" value="0.05"/>
<!-- A named constant used below. -->
  <xacro:macro name="wheel" params="name y">
<!-- A reusable block with two parameters. -->
    <link name="${name}_link"/>
<!-- A link whose name comes from the parameter. -->
    <joint name="${name}_joint" type="continuous"><parent link="base_link"/><child link="${name}_link"/><origin xyz="0 ${y} 0"/><axis xyz="0 1 0"/></joint>
<!-- A wheel joint that spins forever, placed at y. -->
  </xacro:macro>
<!-- End of the macro. -->
  <xacro:wheel name="left" y="0.15"/>
<!-- Create the left wheel. -->
  <xacro:wheel name="right" y="-0.15"/>
<!-- Create the right wheel by reusing the macro. -->
</robot>
<!-- End of the robot. -->

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 Xacro macros. Expected result: left_link and right_link both exist

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.

<robot name="diff_bot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  <link name="base_link"/>
  <xacro:macro name="wheel" params="name y">
    <link name="${name}_link"/>
    <joint name="${name}_joint" type="continuous">
      <parent link="base_link"/><child link="${name}_link"/>
      <origin xyz="0 ${y} 0"/><axis xyz="0 1 0"/>
    </joint>
  </xacro:macro>
  <!-- TODO: call the wheel macro for "left" at y = 0.15 and "right" at y = -0.15 -->
</robot>

Expected output

left_link and right_link both exist
Stuck? Show the full solution (try it yourself first)
<robot name="diff_bot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  <link name="base_link"/>
  <xacro:macro name="wheel" params="name y">
    <link name="${name}_link"/>
    <joint name="${name}_joint" type="continuous">
      <parent link="base_link"/><child link="${name}_link"/>
      <origin xyz="0 ${y} 0"/><axis xyz="0 1 0"/>
    </joint>
  </xacro:macro>
  <xacro:wheel name="left" y="0.15"/>
  <xacro:wheel name="right" y="-0.15"/>
</robot>

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 a URDF with two identical wheels into xacro using a macro and a wheel_radius property, generate the URDF with the xacro command, and confirm both wheels appear in check_urdf.

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.