Bird
Raised Fist0
ROSframework~5 mins

Xacro macros for URDF

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Xacro macros help you write robot descriptions more easily by letting you reuse parts and avoid repeating code.

When you want to define a robot part once and use it many times with different settings.
When your robot description is getting long and hard to manage.
When you want to change a robot parameter in one place and have it update everywhere.
When you want to organize your URDF files into smaller, reusable pieces.
When you want to make your robot model easier to read and maintain.
Syntax
ROS
<xacro:macro name="macro_name">
  <!-- URDF elements here -->
</xacro:macro>

<!-- Use macro -->
<xacro:macro_name param1="value1" param2="value2"/>

Macros are defined with the <xacro:macro> tag and called with <xacro:macro_name/>.

You can pass parameters to macros to customize their content.

Examples
This macro creates a wheel link with customizable name, length, and radius.
ROS
<xacro:macro name="wheel">
  <link name="${name}">
    <visual>
      <geometry>
        <cylinder length="${length}" radius="${radius}"/>
      </geometry>
    </visual>
  </link>
</xacro:macro>

<xacro:wheel name="left_wheel" length="0.1" radius="0.05"/>
This macro defines a sensor link with a given name.
ROS
<xacro:macro name="sensor">
  <link name="${sensor_name}">
    <sensor type="camera"/>
  </link>
</xacro:macro>

<xacro:sensor sensor_name="front_camera"/>
Sample Program

This robot description uses a macro to create two links with different sizes and colors, making the code shorter and easier to change.

ROS
<?xml version="1.0"?>
<robot xmlns:xacro="http://ros.org/wiki/xacro" name="simple_robot">
  <xacro:macro name="arm_link">
    <link name="${link_name}">
      <visual>
        <geometry>
          <box size="${size}"/>
        </geometry>
        <material name="${color}"/>
      </visual>
    </link>
  </xacro:macro>

  <xacro:arm_link link_name="base_link" size="1 0.2 0.2" color="blue"/>
  <xacro:arm_link link_name="arm_link" size="0.5 0.1 0.1" color="red"/>
</robot>
OutputSuccess
Important Notes

Always include the xmlns:xacro namespace in your root <robot> tag.

Use ${parameter} syntax inside macros to insert parameter values.

Macros help keep your URDF files clean and reduce mistakes from copying code.

Summary

Xacro macros let you reuse robot parts by defining them once and calling them with different settings.

This makes your robot description easier to write, read, and update.

Use parameters in macros to customize each part when you call it.

Practice

(1/5)
1. What is the main purpose of using xacro:macro in a URDF file?
easy
A. To define reusable robot parts with customizable parameters
B. To execute robot movement commands
C. To compile the URDF into machine code
D. To visualize the robot in 3D

Solution

  1. Step 1: Understand what xacro:macro does

    xacro:macro lets you define a piece of robot description once and reuse it multiple times with different settings.
  2. Step 2: Identify the main purpose

    It helps to avoid repeating code and makes the URDF easier to maintain by allowing parameter customization.
  3. Final Answer:

    To define reusable robot parts with customizable parameters -> Option A
  4. Quick Check:

    Reusability and customization [OK]
Hint: Macros = reusable parts with parameters [OK]
Common Mistakes:
  • Thinking macros run robot commands
  • Confusing macros with visualization tools
  • Believing macros compile code
2. Which of the following is the correct syntax to define a macro named wheel with a parameter radius in Xacro?
easy
A. <xacro:macro name="wheel" radius="1.0">...</xacro:macro>
B. <xacro:macro name="wheel" params="radius">...</xacro:macro>
C. <macro name="wheel" param="radius">...</macro>
D. <xacro:define name="wheel" radius>...</xacro:define>

Solution

  1. Step 1: Recall Xacro macro syntax

    Macros are defined with <xacro:macro> tag and parameters are listed in the params attribute as a space-separated string.
  2. Step 2: Match the correct syntax

    <xacro:macro name="wheel" params="radius">...</xacro:macro> correctly uses params="radius" inside <xacro:macro> tag.
  3. Final Answer:

    <xacro:macro name="wheel" params="radius">...</xacro:macro> -> Option B
  4. Quick Check:

    Params attribute [OK]
Hint: Use params="param1 param2" inside [OK]
Common Mistakes:
  • Using attributes other than 'params' for parameters
  • Omitting the 'params' attribute
  • Using incorrect tag names like <macro> or
3. Given the following Xacro macro and call:
<xacro:macro name="link_with_length" params="length">
  <link name="link_${length}">
    <visual>
      <geometry>
        <box size="${length} 0.1 0.1"/>
      </geometry>
    </visual>
  </link>
</xacro:macro>

<xacro:link_with_length length="2.0"/>

What will be the name of the generated link element?
medium
A. link_
B. link_length
C. link_2.0
D. link_${length}

Solution

  1. Step 1: Understand macro parameter substitution

    The macro uses ${length} to insert the parameter value into the link name and box size.
  2. Step 2: Substitute the parameter value

    The call passes length="2.0", so link_${length} becomes link_2.0.
  3. Final Answer:

    link_2.0 -> Option C
  4. Quick Check:

    Parameter substitution = link_2.0 [OK]
Hint: Parameter values replace ${param} in macro body [OK]
Common Mistakes:
  • Not substituting parameter, leaving ${length} literal
  • Using parameter name instead of value
  • Leaving name empty
4. Consider this Xacro macro call:
<xacro:link_with_length length=""/>

What is the most likely problem with this call if the macro expects a numeric length parameter?
medium
A. The parameter should be named 'size' instead of 'length'
B. The macro name is incorrect
C. The macro call is missing closing tag
D. The length parameter is empty, causing invalid geometry size

Solution

  1. Step 1: Check parameter value in macro call

    The call passes length="", which is empty and not a valid number.
  2. Step 2: Understand impact on geometry

    The macro uses ${length} for box size, so empty string leads to invalid or zero size, causing errors or unexpected behavior.
  3. Final Answer:

    The length parameter is empty, causing invalid geometry size -> Option D
  4. Quick Check:

    Empty parameter causes invalid size [OK]
Hint: Always provide valid parameter values in macro calls [OK]
Common Mistakes:
  • Ignoring empty parameter values
  • Assuming macro name or tag is wrong
  • Confusing parameter names
5. You want to create a robot arm with multiple identical segments but different lengths using Xacro macros. Which approach best achieves this?
hard
A. Define a macro with a length parameter and call it multiple times with different lengths
B. Write separate link elements manually for each segment with hardcoded lengths
C. Use a single macro call without parameters and change lengths later in the URDF
D. Create a macro without parameters and duplicate it multiple times

Solution

  1. Step 1: Identify the need for reusable segments with different lengths

    You want to reuse the same segment design but customize length for each segment.
  2. Step 2: Choose the approach that supports reuse and customization

    Defining a macro with a length parameter and calling it multiple times with different lengths allows reuse and easy updates.
  3. Final Answer:

    Define a macro with a length parameter and call it multiple times with different lengths -> Option A
  4. Quick Check:

    Reusable macro with parameters [OK]
Hint: Use parameterized macros for repeated parts with variations [OK]
Common Mistakes:
  • Duplicating code manually instead of using macros
  • Using macros without parameters losing flexibility
  • Changing parameters outside macro calls