Skip to main content

Adding The LiDAR and IMU Sensors

Updated 4 min readMarkdown
Part of seriesSimulating and Building a Mobile Robot
Adding The LiDAR and IMU Sensors

In the previous post, we learnt how to structure our robot’s URDF file using links, joints, and Xacro macros and finally simulated in Gazebo. Now that our robot has a body, it’s time to give it senses - specifically, an IMU (Inertial Measurement Unit) and a LiDAR sensor. These sensors will help our robot perceive its environment and understand its own motion.

Updating the URDF file

We’ll start by adding the IMU and LiDAR to the main robot URDF file. This is where we define how and where each sensor is physically attached to the robot.

Open your robot.urdf file and add the following code inside the main <robot> tags.

LiDAR Sensor

The LiDAR is mounted on the robot’s chassis, slightly offset on the X-axis and raised above the base. It’s modeled as a small cylinder representing the sensor body.

<!-- Lidar Sensor -->
<joint name="laser_joint" type="fixed">
    <parent link="chassis_link"/>
    <child link="laser_frame"/>
    <origin xyz="0.1 0 0.175" rpy="0 0 0"/>
</joint>

<link name="laser_frame">
    <visual>
        <origin xyz="0 0 0"/>
        <geometry>
            <cylinder length="0.04" radius="0.05"/>
        </geometry>
        <material name="laser_material">
            <color rgba="0.13 0.13 0.13 1.0"/>
        </material>
    </visual>

    <collision>
        <origin xyz="0 0 0"/>
        <geometry>
            <cylinder length="0.04" radius="0.05"/>
        </geometry>
    </collision>

    <xacro:inertial_cylinder mass="0.1" length="0.04" radius="0.05">
        <origin xyz="0 0 0" rpy="0 0 0"/>
    </xacro:inertial_cylinder>
</link>

This creates a fixed joint between the chassis and the LiDAR frame and gives it a simple cylindrical geometry so that it shows up correctly in RViz or Gazebo. If we were to spin this up now we'd see the following in RViz.

IMU Sensor

Next, we’ll attach the IMU to the base link of the robot. Since it’s usually mounted inside the robot’s body, we don’t need any visual geometry - just a reference link.

<!-- IMU Sensor -->
<joint name="imu_joint" type="fixed">
    <origin xyz="0 0 0" rpy="0 0 0" />
    <parent link="base_link"/>
    <child link="imu_link" />
</joint>

<link name="imu_link" />

This defines a fixed joint between the base_link and the imu_link, allowing Gazebo and RViz to know where the IMU is located.

This creates a simulated IMU that continuously publishes orientation, angular velocity, and linear acceleration data to the /imu topic at 200 Hz. The visualize tag allows you to see the sensor in Gazebo for debugging.

Defining Sensors in Gazebo

Now that our robot knows where the sensors are, we need to tell Gazebo how to simulate them.
Open the robot.gazebo file and add the following tags inside the main <robot> tag.

IMU Sensor Configuration

<gazebo reference="imu_link">
    <sensor name="imu" type="imu">
        <always_on>true</always_on>
        <update_rate>200</update_rate>
        <visualize>true</visualize>
        <topic>imu</topic>
        <enable_metrics>true</enable_metrics>
        <gz_frame_id>imu_link</gz_frame_id>
    </sensor>
</gazebo>

This creates a simulated IMU that continuously publishes orientation, angular velocity, and linear acceleration data to the /imu topic at 200 Hz. The visualize tag allows you to see the sensor in Gazebo for debugging.

LiDAR Sensor Configuration

<gazebo reference="laser_frame">
    <sensor name="laser" type="gpu_lidar">
        <pose>0 0 0 0 0 0</pose>
        <always_on>true</always_on>
        <visualize>true</visualize>
        <update_rate>10</update_rate>
        <topic>scan</topic>
        <gz_frame_id>laser_frame</gz_frame_id>

        <lidar>
            <scan>
                <horizontal>
                    <samples>720</samples>
                    <resolution>1</resolution>
                    <min_angle>-3.14</min_angle>
                    <max_angle>3.14</max_angle>
                </horizontal>
            </scan>
            <range>
                <min>0.05</min>
                <max>10.0</max>
                <resolution>0.01</resolution>
            </range>
            <noise>
                <type>gaussian</type>
                <mean>0.0</mean>
                <stddev>0.01</stddev>
            </noise>
            <frame_id>laser_frame</frame_id>
        </lidar>
    </sensor>
</gazebo>

Here, we’re simulating a 2D GPU-based LiDAR with 720 samples per full rotation, a maximum range of 10 m, and a small Gaussian noise for realism. The LiDAR publishes data to the /scan topic - which you’ll be able to visualize later in RViz.

Build and Launch the Robot

Once the URDF and Gazebo configurations are ready, rebuild your ROS 2 package and launch the simulation. Run the following commands from your workspace root:

cd ~/ros2_ws
colcon build --packages-select diffbot_tut --symlink-install
source install/setup.bash
ros2 launch diffbot_tut robot.launch.py

After launching, you should see the robot appear in Gazebo with the IMU and LiDAR sensors active.
You can verify their topics by running:

ros2 topic list

You should see /imu and /scan among the topics being published.

In the screenshot below I've launched it in a world that I created. You can see that Gazebo is simulating the laser with the virtual beams(You may have to manually add the Visualize Laser option in Gazebo)

To view the output in RViz we can first set it up like normal (add a RobotDescription and set the topic to /robot_decription, set the Fixed Frame to odom). Then, we can click Add->LaserScan, and in the dropdown set the topic to /scan.

Now that your robot can sense its motion (IMU) and surroundings (LiDAR), the next step is to process this data - for example, by using it for mapping, localization, or obstacle avoidance.

About the author

No comments yet. Be the first to comment.

Simulating and Building a Mobile Robot

Part 4 of 4

In this series, I will be building and simulating a differential drive robot in Gazebo Harmonic with ROS 2 Jazzy Jalisco.

Start from the beginningProject OverviewBuilding and Simulating a Robot With ROS 2 Jazzy Jalisco and Gazebo Harmonic