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
Xacro Macros for URDF Robot Description
📖 Scenario: You are creating a robot description file for a simple robot arm using ROS. To keep your URDF clean and reusable, you will use xacro macros to define repeated parts.
🎯 Goal: Build a xacro file that defines a macro for a robot link with a joint, then instantiate this macro twice with different parameters to create two connected links.
📋 What You'll Learn
Create a xacro macro named robot_link with parameters name and length
Inside the macro, define a link element with the given name and a visual element representing a box with the given length
Define a joint element connecting this link to the previous link, using the name parameter
Instantiate the robot_link macro twice with different name and length values to create two links connected in series
💡 Why This Matters
🌍 Real World
Robot developers use xacro macros to write clean, reusable robot description files that can be easily modified and extended.
💼 Career
Understanding xacro macros is essential for robotics engineers working with ROS to create modular and maintainable robot models.
Progress0 / 4 steps
1
Create the Xacro macro skeleton
Create a xacro macro named robot_link that takes parameters name and length. Inside the macro, define a link element with the attribute name="${name}".
ROS
Hint
Use the <xacro:macro> tag with name and params attributes. Inside, create a <link> element with name="${name}".
2
Add visual box to the link
Inside the link element of the robot_link macro, add a visual element with a geometry child that defines a box with size ${length} 0.1 0.1.
ROS
Hint
Use <visual> and inside it <geometry> with a <box> element. Set the size attribute using the length parameter and fixed width and height.
3
Add joint connecting to previous link
Still inside the robot_link macro, after the link element, add a joint element named ${name}_joint of type fixed. Set its parent to ${previous_link} and child to ${name}. Assume previous_link is a global property defined outside the macro.
ROS
Hint
Use a <joint> element with name and type attributes. Inside, add <parent> and <child> elements with the correct link names.
4
Instantiate the macro twice with different parameters
Outside the macro definition, set a property previous_link to base_link. Then instantiate the robot_link macro twice: first with name="link1" and length="1.0", then update previous_link to link1 and instantiate again with name="link2" and length="0.5".
ROS
Hint
Use <xacro:property> to set previous_link. Then call the macro with <xacro:robot_link> tags and the correct parameters.
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
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.
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 A
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
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.