A macro in Xacro (XML Macros) is a reusable block of XML code that allows you to define a component once and use it multiple times with different parameters. This makes URDF files more modular, flexible, and maintainable.

How Does a Macro Work?

A macro is defined using <xacro:macro>, and it can have parameters that allow customization.

Syntax:

<xacro:macro name="macro_name" params="param1 param2 ...">
    <!-- URDF/Xacro code that uses parameters -->
</xacro:macro>

To use the macro, you call it like this:

<xacro:macro_name param1_value param2_value .../>

In both the right_wheel_link and the left_wheel_link, the same code is repeated twice. In order to make the code more flexible we can use macro as shown below:

    <xacro:macro name="wheel_link" params="prefix">
        <link name="${prefix}_wheel_link">
            <visual>
                <geometry>
                    <cylinder radius="${wheel_radius}" length="${wheel_length}" />
                </geometry>
                <origin xyz="0 0 0" rpy="${pi / 2.0} 0 0" />
                <material name="grey" />
            </visual>
        </link>
    </xacro:macro>

    <xacro:wheel_link prefix="right" />
    <xacro:wheel_link prefix="left" />