For Reference this is the code that we used previously to create the robot:

<?xml version="1.0"?>
<robot name="my_robot" xmlns:xacro="<http://www.ros.org/wiki/xacro>">

    <material name="blue">
        <color rgba="0 0 0.5 1" />
    </material>

    <material name="grey">
        <color rgba="0.5 0.5 0.5 1" />
    </material>

    <link name="base_footprint" />

    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.6 0.4 0.2" />
            </geometry>
            <origin xyz="0 0 0.1" rpy="0 0 0" />
            <material name="blue" />
        </visual>
    </link>

    <link name="right_wheel_link">
        <visual>
            <geometry>
                <cylinder radius="0.1" length="0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
            <material name="grey" />
        </visual>
    </link>

    <link name="left_wheel_link">
        <visual>
            <geometry>
                <cylinder radius="0.1" length="0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
            <material name="grey" />
        </visual>
    </link>

    <link name="caster_wheel_link">
        <visual>
            <geometry>
                <sphere radius="0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <material name="grey" />
        </visual>
    </link>

    <joint name="base_joint" type="fixed">
        <parent link="base_footprint" />
        <child link="base_link" />
        <origin xyz="0 0 0.1" rpy="0 0 0"/>
    </joint>

    <joint name="base_right_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="right_wheel_link" />
        <origin xyz="-0.15 -0.225 0" rpy="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>

    <joint name="base_left_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="left_wheel_link" />
        <origin xyz="-0.15 0.225 0" rpy="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>

    <joint name="base_caster_wheel_joint" type="fixed">
        <parent link="base_link" />
        <child link="caster_wheel_link" />
        <origin xyz="0.2 0 -0.05" rpy="0 0 0" />
    </joint>

</robot>

Constants in Xacro

In the above code, in both the right_wheel_link and left_wheel_link we had a roll by 1.57 radians, which is pi/2. Using Xacro we can use pi constant to have more accurate result.

Below is the update code:

    <link name="right_wheel_link">
        <visual>
            <geometry>
                <cylinder radius="0.1" length="0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="${pi / 2.0} 0 0" />
            <material name="grey" />
        </visual>
    </link>

    <link name="left_wheel_link">
        <visual>
            <geometry>
                <cylinder radius="0.1" length="0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="${pi / 2.0} 0 0" />
            <material name="grey" />
        </visual>
    </link>

Creating a Variable

To create a variable use the below syntax:

<xacro:property name="variable_name" value="value" />

Lets’s add a base_length variable to our code:

<?xml version="1.0"?>
<robot name="my_robot" xmlns:xacro="<http://www.ros.org/wiki/xacro>">

    <xacro:property name="base_length" value="0.6" />

    <material name="blue">
        <color rgba="0 0 0.5 1" />
    </material>
    ......

In the base_link we can change 0.6 by the variable base_length:

    <link name="base_link">
        <visual>
            <geometry>
                <box size="${base_length} 0.4 0.2" />
            </geometry>
            <origin xyz="0 0 0.1" rpy="0 0 0" />
            <material name="blue" />
        </visual>
    </link>

In both the base_right_wheel_joint and the base_left_wheel_joint, the origin of the x-axis was -0.15, which was calculated as - 0.6/4. Instead of using 0.6, we can use the base_length variable:

    <joint name="base_right_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="right_wheel_link" />
        <origin xyz="${-base_length / 4.0} -0.225 0" rpy="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>

    <joint name="base_left_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="left_wheel_link" />
        <origin xyz="${-base_length / 4.0} 0.225 0" rpy="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>