Step 1: Create New File named camera.xacro in the urdf Folder

image.png


Step 2: Code for the camera_link

The following is the code in the camera.xacro file:

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

    <xacro:property name="camera_length" value="0.01" />
    <xacro:property name="camera_width" value="0.1" />
    <xacro:property name="camera_height" value="0.05" />

    <link name="camera_link">
        <visual>
            <geometry>
                <box size="${camera_length} ${camera_width} ${camera_height}" />
            </geometry>
            <material name="grey" />
        </visual>
        <collision>
            <geometry>
                <box size="${camera_length} ${camera_width} ${camera_height}" />
            </geometry>       
        </collision>
        <xacro:box_inertia m="0.1" l="${camera_length}" w="${camera_width}" h="${camera_height}"
                           xyz="0 0 0" rpy="0 0 0" />
    </link>

    <joint name="base_camera_joint" type="fixed">
        <parent link="base_link" />
        <child link="camera_link" />
        <origin xyz="${(base_length + camera_length) / 2.0} 0 ${base_height / 2.0}" rpy="0 0 0" />
    </joint>
    
</robot>

Step 3: Include camera.xacro File in the my_robot.urdf.xacro File

Add the following line of code:

<xacro:include filename="camera.xacro" />

Step 4: Launch camera.xacro File and view Results on Rviz

Use following commands:

cd ros2_ws/
colcon build
source install/setup.bash
ros2 launch my_robot_description display.launch.xml