Set Up LIDAR for a Simulated Mobile Robot in ROS 2

5-output-for-gazebo

In this tutorial, we will set up the LIDAR sensor for a simulated mobile robot.

This tutorial is the fourth tutorial in my Ultimate Guide to the ROS 2 Navigation Stack (also known as Nav2).

You can get the entire code for this project here.

If you are using ROS Galactic or newer, you can get the code here.

Let’s get started!

Prerequisites 

You have completed the first three tutorials of this series:

  1. How to Create a Simulated Mobile Robot in ROS 2 Using URDF
  2. Set Up the Odometry for a Simulated Mobile Robot in ROS 2
  3. Sensor Fusion Using the Robot Localization Package – ROS 2

Modify the SDF File for the Robot

Open a new terminal window, and type:

colcon_cd basic_mobile_robot
cd models
cd basic_mobile_bot_description

Type the following command:

gedit model.sdf

Here is my sdf file. You can copy and paste those lines inside your sdf file.

Save the file and close it to return to the terminal.

You’ll notice that we added the LIDAR link, joint, and Gazebo sensor plugin. I also added an inertial section to the front caster wheel.

Make sure that you have this model.config file inside the same folder as your sdf file.

Test Your SDF File

Now let’s run Gazebo so that we can see our model. Open a new terminal window, and type the following command:

gazebo

On the left-hand side, click the “Insert” tab.

On the left panel, find your robot. 

You should see your robot in the empty Gazebo environment. You can place it wherever you want by clicking inside the environment.

1-robot-scan-empty-environment

If you open a new terminal window and check out the ROS topics, you will see our /scan topic.

ros2 topic list
2-ros2-topic-list

To check out this topic, type:

ros2 topic info /scan

You will see this /scan topic has one publisher (i.e. our LIDAR).

3-ros2-topic-info-scan

To see the output of the /scan topic, you type:

ros2 topic echo /scan

You will see that the readings for all 360 beams of the LIDAR is “.inf”, which means infinity. These readings make sense since there are no objects in the environment to detect.

4-infinity-scanner-readings

Once you’re done, press CTRL+C in all windows to close everything down.

Modify the URDF File

Open a new terminal window, and type:

colcon_cd basic_mobile_robot
cd models

Create a new file named basic_mobile_bot_v2.urdf.

gedit basic_mobile_bot_v2.urdf

Inside this file, we will add a link and a joint for the LIDAR. Leave the caster wheel inertial section as-is.

Type this code inside the URDF file.

Save and close the file.

Edit the Launch File

Now that we have our robot with LIDAR, we need to modify the launch file.

colcon_cd basic_mobile_robot
cd launch
gedit basic_mobile_bot_v4.launch.py

Copy and paste this code into the file.

Save the file, and close it.

Build the Package

Now build the package by opening a terminal window, and typing the following command:

cd ~/dev_ws
colcon build

Launch the Robot

Open a new terminal, and launch the robot.

cd ~/dev_ws/
ros2 launch basic_mobile_robot basic_mobile_bot_v4.launch.py

Here is the output for Gazebo:

5-output-for-gazebo-1

Here is the output for RViz.

6-output-for-rviz

Under the TF dropdown in RViz, you should see “Transform OK” for all links.

7-transform-ok

Let’s visualize the sensor readings from the LIDAR. In RViz, make sure the Fixed Frame under Global Options is set to odom.

Click the Add button at the bottom-left part of the RViz window. Then go to the By topic tab, and select the LaserScan option under /scan. Then click OK.

8-laser-scan-topic

Under the Topic dropdown menu under LaserScan in the left panel of RViz, set the Reliability Policy to Best Effort. Set the size to 0.1 m.

9-laser-scan-settings

Now go back to a terminal window, and move the robot forward a bit so that it can start detecting that wall on the front end of the playground in Gazebo.

rqt_robot_steering

If you are using ROS 2 Galactic or newer, type:

sudo apt-get install ros-galactic-rqt-robot-steering

Where the syntax is:

sudo apt-get install ros-<ros-distribution>-rqt-robot-steering

Then type:

ros2 run rqt_robot_steering rqt_robot_steering --force-discover

You can see how the laser scan readings show up in RViz.

That’s it! 

In the next tutorial, we’ll learn how to localize and create a map using this LIDAR in conjunction with the ROS 2 Navigation Stack. Stay tuned!