Xacro macros help you write robot descriptions more easily by letting you reuse parts and avoid repeating code.
Xacro macros for URDF
Start learning this pattern below
Jump into concepts and practice - no test required
<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.
<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"/>
<xacro:macro name="sensor"> <link name="${sensor_name}"> <sensor type="camera"/> </link> </xacro:macro> <xacro:sensor sensor_name="front_camera"/>
This robot description uses a macro to create two links with different sizes and colors, making the code shorter and easier to change.
<?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>
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.
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
xacro:macro in a URDF file?Solution
Step 1: Understand what
xacro:macrodoesxacro:macrolets you define a piece of robot description once and reuse it multiple times with different settings.Step 2: Identify the main purpose
It helps to avoid repeating code and makes the URDF easier to maintain by allowing parameter customization.Final Answer:
To define reusable robot parts with customizable parameters -> Option AQuick Check:
Reusability and customization [OK]
- Thinking macros run robot commands
- Confusing macros with visualization tools
- Believing macros compile code
wheel with a parameter radius in Xacro?Solution
Step 1: Recall Xacro macro syntax
Macros are defined with<xacro:macro>tag and parameters are listed in theparamsattribute as a space-separated string.Step 2: Match the correct syntax
<xacro:macro name="wheel" params="radius">...</xacro:macro>correctly usesparams="radius"inside<xacro:macro>tag.Final Answer:
<xacro:macro name="wheel" params="radius">...</xacro:macro> -> Option BQuick Check:
Params attribute [OK]
- Using attributes other than 'params' for parameters
- Omitting the 'params' attribute
- Using incorrect tag names like <macro> or
<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?
Solution
Step 1: Understand macro parameter substitution
The macro uses${length}to insert the parameter value into the link name and box size.Step 2: Substitute the parameter value
The call passeslength="2.0", solink_${length}becomeslink_2.0.Final Answer:
link_2.0 -> Option CQuick Check:
Parameter substitution = link_2.0 [OK]
- Not substituting parameter, leaving ${length} literal
- Using parameter name instead of value
- Leaving name empty
<xacro:link_with_length length=""/>
What is the most likely problem with this call if the macro expects a numeric
length parameter?Solution
Step 1: Check parameter value in macro call
The call passeslength="", which is empty and not a valid number.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.Final Answer:
The length parameter is empty, causing invalid geometry size -> Option DQuick Check:
Empty parameter causes invalid size [OK]
- Ignoring empty parameter values
- Assuming macro name or tag is wrong
- Confusing parameter names
Solution
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.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.Final Answer:
Define a macro with a length parameter and call it multiple times with different lengths -> Option AQuick Check:
Reusable macro with parameters [OK]
- Duplicating code manually instead of using macros
- Using macros without parameters losing flexibility
- Changing parameters outside macro calls
