Simulating a laser scanner

In this section, we will see how to simulate a laser scanner in Gazebo. We can simulate it by providing custom parameters according to our application. When you install ROS, you also automatically install several default Gazebo plugins, which includes the Gazebo laser scanner plugin.

We can simply use this plugin and apply our custom parameters. For demonstration, you can use a tutorial package inside chapter_10_ws called sensor_sim_gazebo (https://github.com/PacktPublishing/ROS-Robotics-Projects-SecondEdition/tree/master/chapter_10_ws/sensor_sim_gazebo). You can simply copy the package to the workspace and build it using the catkin_make command. This package contains a basic simulation of the laser scanner, camera, IMU, ultrasonic sensor, and GPS.

Before starting with this package, you should install a package called hector-gazebo-plugins using the following command:

$ sudo apt-get install ros-melodic-hector-gazebo-plugins 

This package contains Gazebo plugins of several sensors that can be used in self-driving car simulations.

To start the laser scanner simulation, just use the following command:

$ roslaunch sensor_sim_gazebo laser.launch 

We'll first look at the output of the laser scanner and then dig into the code.

When you launch the preceding command, you will see an empty world with an orange box. The orange box is our laser scanner. You can use any mesh file to replace this shape according to your application. To show laser scan data, we can place some objects in Gazebo, as shown here:

Simulation of a laser scanner in Gazebo

You can add models from Gazebo's top panel. You can visualize the laser data in RViz, as shown in the next screenshot. The topic to which the laser data is coming is /laser/scan. You can add a LaserScan display type to view this data:

Visualization of laser scanner data in RViz

You have to set the Fixed Frame to a world frame and enable the RobotModel and Axes display types in RViz. The following is the list of topics generated while simulating this sensor:

List of topics from the laser scanner simulation

You should be able to see /laser/scan as highlighted in the preceding screenshot. Let's understand how the code works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset