ROS2 Tutorials

XACRO

The .xacro (XML Macros) file format is an extension of the Unified Robot Description Format (URDF). It is commonly used in the Robot Operating System (ROS) to create modular and reusable robot descriptions. By allowing parameterization and the use of macros, .xacro files simplify the creation and maintenance of complex robot models.

Understanding XACRO

What is a .xacro File?

A .xacro file is essentially a URDF file enhanced with XML macros and parameters. It allows you to:

Why Use .xacro Files?

While URDF files are static and repetitive, .xacro files offer several advantages:

  1. Reusability: Define components like links and joints as macros to reuse them in different parts of the robot.
  2. Parameterization: Use variables to adjust dimensions or configurations without modifying multiple lines of code.
  3. Maintainability: Simplify updates by editing only the macro or parameter instead of duplicating changes.

Key Features of .xacro Files

  1. Variables
    • Define variables to store reusable values, making it easier to update robot dimensions and properties.
    • Example:
1
2
3
       <xacro:property name="base_length" value="1.0"/>
       <xacro:property name="base_width" value="0.5"/>
       
  1. Macros
    • Create macros to encapsulate repetitive elements like links, joints, or groups of components.
    • Example:
1
2
3
4
5
6
7
8
9
10
11
        <xacro:macro name="wheel" params="name radius width">
          <link name="${name}">
            <visual>
              <geometry>
                <cylinder radius="${radius}" length="${width}"/>
              </geometry>
              <material name="black"/>
            </visual>
          </link>
        </xacro:macro>
        
  1. Including Other Files
    • Divide your robot description into smaller files and include them in the main .xacro file.
    • Example:
1
2
     <xacro:include filename="sensor.xacro"/>
     
  1. Conditional Statements
    • Use conditional logic to include or exclude parts of the robot model based on parameters.
    • Example:
1
2
3
4
       <xacro:if value="${use_sensor}">
         <xacro:include filename="sensor.xacro"/>
       </xacro:if>
       

Converting .xacro to URDF

       xacro robot.xacro > robot.urdf
       

Example: Modular Robot Base

Here’s an example of a .xacro file defining a modular robot base:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="modular_robot">

  <!-- Define properties -->
  <xacro:property name="base_length" value="1.0"/>
  <xacro:property name="base_width" value="0.5"/>
  <xacro:property name="wheel_radius" value="0.2"/>
  <xacro:property name="wheel_width" value="0.1"/>

  <!-- Define wheel macro -->
  <xacro:macro name="wheel" params="name x y">
    <link name="${name}">
      <visual>
        <geometry>
          <cylinder radius="${wheel_radius}" length="${wheel_width}"/>
        </geometry>
        <material name="gray"/>
      </visual>
    </link>
    <joint name="${name}_joint" type="fixed">
      <parent link="base_link"/>
      <child link="${name}"/>
      <origin xyz="${x} ${y} -${wheel_radius}" rpy="0 0 0"/>
    </joint>
  </xacro:macro>

  <!-- Base link -->
  <link name="base_link">
    <visual>
      <geometry>
        <box size="${base_length} ${base_width} 0.2"/>
      </geometry>
      <material name="blue"/>
    </visual>
  </link>

  <!-- Add wheels -->
  <xacro:wheel name="front_left_wheel" x="0.5" y="0.25"/>
  <xacro:wheel name="front_right_wheel" x="0.5" y="-0.25"/>
  <xacro:wheel name="rear_left_wheel" x="-0.5" y="0.25"/>
  <xacro:wheel name="rear_right_wheel" x="-0.5" y="-0.25"/>

</robot>

Advantages of Using .xacro Files

  1. Simplified Robot Modeling: Reduce redundancy in robot descriptions.
  2. Parameter Flexibility: Dynamically adjust robot configurations based on specific use cases.
  3. Code Modularity: Keep your robot description organized and maintainable.

By incorporating .xacro files in your ROS2 workflow, you can efficiently manage complex robot models and enhance their scalability for various applications.