Friday, November 21, 2014

Adding a Hokuyo LIDAR to a Turtlebot in ROS Indigo (+ Gazebo Functionality)

We're using ROS as the basis of our software development for three vehicles this year at the UCF Robotics Club.  While the vehicles are being built, we've had access to a Turtlebot for prototyping and exploring ROS functionality.

Part of this process required replacing the Kinect that comes with the Turtlebot with a real LIDAR for laser scan data.  Apart from the growing pains with understanding robot specifications that ROS requires (urdf files and such) we found that existing documentation for this process is either terribly out of date (2+ years with most information pertaining to Groovy) or inadequate (specifically when getting Hokuyo simulation working in Gazebo).  I'd like to explain how we combined existing tutorials and guides to get everything working nicely.

 Existing Sources of Info

Before delving into the step by step guide I want to list the sources used in working our way towards the current solution.  Various bits and pieces of other guides were mashed together to create what we have now.

Creating the Hokuyo Description

We'll start by creating a description of the Hokuyo physical specifications like size and position on our Turtlebot.  Create a new file in /turtlebot_testing/urdf/sensors called hokuyo.urdf.xacro.  If you're unaware, xacro is a language that supports macros extending XML functionality.  This allows us to write macros in their own file, and then decide when we want to expand those macros in other (shorter and cleaner) files.  Copy the following into the created file.

This is mostly taken from the iRoboApp Wiki, with some slight modifications.  We create a macro named "sensor_hokuyo" that specifies all of the necessary information for the laser joint and link.  The only major change is our call to the hokuyo_laser macro just before the closing tag. 

Setting Up the Gazebo Plugin

This macro we defined in turtlebot_gazebo.urdf.xacro for setting up the plugin in Gazebo.  It is almost exactly as defined in the Gazebo tutorial linked above.  Add the following code to your turtlebot_gazebo.urdf.xacro after the </xacro:macro> tag closing the Kinect macro.

This macro will set up the plugin in Gazebo when expanded from the hokuyo.urdf.xacro file.  We now add the Hokuyo urdf to the Turtlebot xacro library file in /turtlebot_description/urdf/turtlebot_library.urdf.xacro.  Add the following line to the end of the file before the </robot> tag.

Now we have access to the macros we created in any file that includes turtlebot_library.urdf.xacro!

Creating a Description File for the Hokuyo-equipped Bot

Now that we've got our macros defined, we want to create a robot description file that actually uses them to specify the robot as having the Hokuyo equipped.  Navigate to the /turtlebot_description/robots folder.  Create a copy of whichever file corresponds to your robot's configuration (ours was kobuki_hexagons_kinect.urdf.xacro) as <base>_<stack>_hokuyo.urdf.xacro.  As an example that left us with a copy named kobuki_hexagons_hokuyo.urdf.xacro.  Now edit that file to add the following line.

Now we can use this file to launch our Turtlebot with the Hokuyo equipped and working.

Using the Hokuyo URDF

We configured our setup to make use of existing structure for the Turtlebot launch files.  To specify you want to use the Hokuyo urdf you just created, run the following in the terminal:

export TURTLEBOT_3D_SENSOR="hokuyo"

Now when roslaunching any of the turtlebot_gazebo launch files, your robot should launch in Gazebo with simulated Hokuyo laser scans. Note that the Kinect will still be equipped and publishing fake laser scans on the /scan topic. To change this, modify the remap section of the launch file to remap the fake laser scans to another topic (/kinect_scan for example).

And that's it! If everything is set up correctly you should now have working Hokuyo laser scans in Gazebo, and should be able to make slight modifications following other documentation available for Hokuyo LIDARs to get a real laser working on your Turtlebot.


  1. This comment has been removed by the author.

  2. Thank you very much for the codes. It worked out pretty well for me but only for one robot. when i have 2 turtle bots, the laser scans kind of overwrite and i can always get the data of scan for second turtlebot. i tried to make 2 separate turtlebot_gazebo.urdf.xacro files but still its not working. can you please guide what should be modified in the code to get one laser corresponding to one turtlebot?

  3. How can I determine the position of the lidar sensor ?

  4. Thanks for sharing the codes with complete information. This is a good idea. It is little bit confusing also. How can we determine the position of LiDAR Sensor ? Please expain!

  5. This comment has been removed by the author.

  6. Thanks sir for sharing this precious code. It will help me in my project too.