How to Build an Indoor Map Using ROS and LIDAR-based SLAM

In this tutorial, I will show you how to build a map using LIDAR, ROS 1 (Melodic), Hector SLAM, and NVIDIA Jetson Nano. We will go through the entire process, step-by-step. You can combine what you will learn in this tutorial with an obstacle avoiding robot to build a map of any indoor environment. Below is a small robot I built that wanders around the room while generating a map.

Go through this tutorial slowly so that you set up everything correctly. To go fast, go slow. There is no hurry. Follow along, click-by-click, part-by-part as we develop a complete LIDAR-based SLAM system from scratch.

Real-World Applications

This project has a number of real-world applications: 

  • Indoor Delivery Robots
  • Mapping of Underground Mines, Caves, and Hard-to-Reach Environments
  • Robot Vacuums
  • Order Fulfillment
  • Factories

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Once you finish this project, you can mount your Jetson Nano and LIDAR on anything that moves (drone, robot car, etc.) so you can map an environment.

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Test the Power Bank

The portable power bank that I purchased can be used to power your Jetson Nano. It outputs 5V/3A. Having a portable power bank enables you to move the Jetson Nano and LIDAR around a room freely so you can build a map.

First, take the jumper off of pin J48 so your Jetson Nano knows we are going to plug in the portable battery into the 5V/2A USB Micro B connection (J28) rather than the 5V/4A barrel power jack (J25).

nvidia-jetson-nano-b01-pinout
2021-04-08-10.20.04

Turn on your Jetson Nano. If your Jetson Nano turns on successfully, the power bank is working.

2021-04-08-10.23.52

Shutdown your Jetson Nano.

sudo shutdown -h now

Connect Your RPLIDAR

Connect your RPLIDAR to the Jetson Nano using the USB to microUSB cable.

2021-04-09-11.52.33
2021-04-09-11.56.53-1
2021-04-09-11.57.27
2021-04-09-11.58.45
2021-04-09-12.03.56
2021-04-09-12.12.18
2021-04-09-13.08.13

Turn on your Jetson Nano.

Open a terminal window, and check the permissions. You can copy and paste this command below into the terminal. Note that -l is a lowercase L.

ls -l /dev | grep ttyUSB

Here is the output you should see:

2021-04-09-12.23.41

Type this command to change the permissions.

sudo chmod 666 /dev/ttyUSB0

Set Up a Catkin Workspace and Install RPLIDAR ROS Packages

Open a terminal window.

Update the package list.

sudo apt-get update

Install the following dependencies.

sudo apt-get install cmake python-catkin-pkg python-empy python-nose python-setuptools libgtest-dev python-rosinstall python-rosinstall-generator python-wstool build-essential git

Create the catkin workspace.

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/

So we don’t have to source the setup.bash file every time we open a new Linux terminal, let’s add the ~/catkin_ws/devel/setup.bash command to the .bashrc file. Open a new Linux terminal window.

Type the following command to edit the .bashrc text file:

gedit ~/.bashrc

If you don’t have gedit installed, you can install it using this command.

sudo apt-get install gedit

Make sure these two lines are at the end of the .bashrc file.

source /opt/ros/melodic/setup.bash
source ~/catkin_ws/devel/setup.bash

Save the file, and close it.

Type this command in the current terminal (we won’t need to do this again since we’ve added this command to our bash file).

source ~/catkin_ws/devel/setup.bash

Go to the source folder.

cd src

Clone the RPLIDAR ROS package to your src folder.

sudo git clone https://github.com/Slamtec/rplidar_ros.git

Go to the root of the workspace.

cd ~/catkin_ws/

Compile the workspace.

catkin_make

I saw an error that says “C++11 requires space between literal and string macro.”

The error is occurring in the node.cpp file.

Open that file.

cd ~/catkin_ws/src/rplidar_ros/src
sudo chmod +x node.cpp
sudo gedit node.cpp

We go to line 212.

Change this:

ROS_INFO("RPLIDAR running on ROS package rplidar_ros. SDK Version:"RPLIDAR_SDK_VERSION"");

To this:

ROS_INFO("RPLIDAR running on ROS package rplidar_ros. SDK Version:" RPLIDAR_SDK_VERSION "");

Go to the root of the workspace.

cd ~/catkin_ws/

Compile the workspace.

catkin_make

Launch the RPLIDAR launch file.

roslaunch rplidar_ros rplidar.launch

Check out the currently active topics.

Open a new terminal window, and type:

rostopic list
2021-04-09-13.07.33

Let’s see what LIDAR data is being published to the /scan ROS topic. Open a new terminal window, and type:

rostopic echo /scan

Here is what you should see.

2021-04-09-13.08.42
Readings from the LIDAR. Sorry for all the glare on the left side of the screen.

Keep the terminal window open for the next section.

Run rviz

Now, we will launch rviz, a 3D visualization software program for ROS.

Launch RPLIDAR.

roslaunch rplidar_ros rplidar.launch

Open a new terminal window, and launch rviz.

rviz

Here is the window that you should see.

2021-04-09-13.10.02

Change the Fixed Frame to laser.

Click the Add button in the bottom-left of the window.

Select LaserScan, and click OK.

Set the LaserScan topic to /scan.

2021-04-09-13.11.31

Increase the size to 0.03 so that you can see the data more easily.

2021-04-09-13.11.58

If you see output like this, it means everything is working correctly.

Press CTRL + C in all terminal windows to close everything down.

Build a Map Using the Hector-SLAM ROS Package

In this section, we will build a map of our environment using the ROS Hector-SLAM package. 

Hector-SLAM is an algorithm that uses laser scan data to create a map. The advantage of Hector-SLAM over other SLAM techniques is that it only requires laser scan data to do its job. It doesn’t need odometry data

Hector stands for “Heterogeneous Cooperating Team of Robots”, the name of a robotics research group at TU Darmstadt. The original research paper is available here if you want to learn more about the details (you don’t need to know the details of the algorithm in order to use it in your applications).

SLAM stands for Simultaneous Localization and Mapping. SLAM is a popular technique in which a robot generates a map of an unknown environment (i.e. mapping) while simultaneously keeping track of its position within that map (i.e. localization).

Ok, let’s get started.

Install Qt4

Install Qt4. Qt4 is a software that is used to generate graphical user interfaces.

sudo apt-get install qt4-qmake qt4-dev-tools

Type Y and press Enter to complete the installation. The whole process should take a few minutes.

Download the Hector-SLAM Package

Move to your catkin workspace’s source folder.

cd ~/catkin_ws/src

Clone the Hector-SLAM package into your workspace.

Make sure you have an Internet connection.

ping google.com

If you see streaming data, your Internet is connected properly.

Now download the Hector-SLAM package.

git clone https://github.com/tu-darmstadt-ros-pkg/hector_slam.git

Set the Coordinate Frame Parameters

We need to set the frame names and options correctly.

Go to the launch file for Hector-SLAM. All of this below is a single command, so you can just copy and paste.

sudo gedit ~/catkin_ws/src/hector_slam/hector_mapping/launch/mapping_default.launch

Search for these lines (lines 5 and 6 in my code).

<arg name="base_frame" default="base_footprint"/>
<arg name="odom_frame" default="nav"/>

Change those lines to this:

<arg name="base_frame" default="base_link"/>
<arg name="odom_frame" default="base_link"/>

Now go to the end of this file, and find these lines (line 54 in my code).

<!--<node pkg="tf" type="static_transform_publisher" name="map_nav_broadcaster" args="0 0 0 0 0 0 map nav 100"/>-->

Change those lines to this (be sure to remove the comment tags (<!– and –>):

<node pkg="tf" type="static_transform_publisher" name="base_to_laser_broadcaster" args="0 0 0 0 0 0 base_link laser 100"/>

Save the file, and return to the terminal window.

Type the following command.

cd ~/catkin_ws/src/hector_slam/hector_slam_launch/launch

Open the tutorial.launch file.

gedit tutorial.launch

Find this line (line 7 in my code).

<param name="/use_sim_time" value="true"/>

Change that line to:

<param name="/use_sim_time" value="false"/>

Save the file, and close it.

Open a new terminal window, and type this command:

cd ~/catkin_ws/

Build the packages.

catkin_make
2021-04-09-13.52.46

If you see this error message…

Project ‘cv_bridge’ specifies ‘/usr/include/opencv’ as an include dir, which is not found. It does neither exist as an absolute directory nor in…

Type:

cd /usr/include
sudo ln -s opencv4/ opencv

Build the packages again.

cd ~/catkin_ws/
catkin_make

Wait a minute or two while the Hector-SLAM package builds.

Let’s reboot the computer. I’ll do a complete shutdown then turn the Jetson Nano on again.

sudo shutdown -h now

Launch Mapping

Turn on the Jetson Nano.

Open a new terminal window, and launch RPLIDAR.

cd ~/catkin_ws/
sudo chmod 666 /dev/ttyUSB0
roslaunch rplidar_ros rplidar.launch

Now that the LIDAR is running, let’s start mapping. Open a new terminal, and type the following command:

roslaunch hector_slam_launch tutorial.launch
2021-04-09-14.33.20

This command above launches three nodes as well as rviz:

If you move the LIDAR around the room, make sure that it moves very slowly so that you can create a good map. Map-making works best at slow speeds. Here are some photos of the robot I built. It combines obstacle avoidance and LIDAR-based mapping.

2021-04-12-18.18.31-1
2021-04-12-18.18.53
2021-04-12-17.55.37

Save the Map

Method 1

Let’s save the map. Open a new terminal window, and type:

rostopic pub syscommand std_msgs/String "savegeotiff"

When you’re done running mapping, type CTRL + C on all terminal windows.

Go to ~/catkin_ws/src/hector_slam/hector_geotiff/maps.
2021-04-09-14.37.26

You can find your own map like hector_slam_map_##:##:##.tfw.

Method 2

Another way to save a map is to use a package called map_server. This package will save map data to a yaml and pgm formatted file.

Before you launch the mapping process, you need to install map_server.

sudo apt-get install ros-melodic-map-server

Create a maps folder. This is where we will store our maps.

mkdir ~/catkin_ws/maps

Launch the mapping process (see Launch Mapping section above).

When you are happy with the map that you see in rviz, you can save the map as my_map.yaml and my_map.pgm. Open a new terminal.

cd ~/catkin_ws/maps
rosrun map_server map_saver -f my_map
2021-04-09-14.41.17

Maps will save to the ~/catkin_ws/maps directory.

Press CTRL + C on all terminal windows to shut everything down.

Load a Saved Map

To view the map, you can run the following command in a new terminal window to get the ROS Master started..

cd ~/catkin_ws/maps
roscore

Now load the map. In a new terminal window, type:

rosrun map_server map_server my_map.yaml

Open rviz in another terminal.

rviz

Click Add in the bottom left, and add the Map display.

Under Topic under the Map section, select /map.

You should see the saved map on your screen.

2021-04-09-14.49.45

Press CTRL + C to close everything down.

Convert the Map into png Format

To convert the map to a png image file, you can use imagemagick.

sudo apt-get install imagemagick
convert my_map.pgm my_map.png

Edit the Map

To edit the map, I recommend using a program like GIMP.

Install gimp.

sudo apt-get update
sudo apt-get install gimp

Run gimp.

gimp

To load the image, go to File -> Open, and then locate your image.

2021-04-09-15.00.25

That’s it. Keep building!

References

This tutorial was especially helpful.

Obstacle Avoiding Robot Using a DC Motor and Arduino

In this tutorial, I will show you my setup for an obstacle avoiding robot that has two DC motors and uses Arduino as its “brain”.

Here is the motor I am working with, but you can use any motor that looks like this one.

1_dc_motor_encoder_wheels-1

Real-World Applications

This project has a number of real-world applications: 

  • Indoor Delivery Robots
  • Robot Vacuums
  • Factories

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the HC-SR04 Ultrasonic Distance Sensor

The first thing we need to do is set up the hardware. Here is the wiring diagram:

hcsr04_ultrasonic_distance_sensor_bb
  • VCC on the sensor connects to 5V on the Arduino
  • Echo on the sensor connects to Digital Pin 12 on the Arduino
  • Trig (stands for trigger) on the sensor connects to Digital Pin 11 on the Arduino
  • GND (stands for Ground) on the sensor connects to GND on the Arduino

Test the Ultrasonic Distance Sensor

Now, upload the following sketch to the Arduino to test the ultrasonic sensor. I named my program test_ultrasonic_distance_sensor.ino.

/**
 *  This program tests the ultrasonic
 *  distance sensor
 * 
 * @author Addison Sears-Collins
 * @version 2.0 2021-04-11
 */
 
/* Give a name to a constant value before
 * the program is compiled. The compiler will 
 * replace references to Trigger and Echo with 
 * 11 and 12, respectively, at compile time.
 * These defined constants don't take up 
 * memory space on the Arduino.
 */
#define Trigger 11
#define Echo 12
 
/*   
 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
 */
void setup(){
 
  // Set the baud rate to 9600. 9600 means that 
  // the serial port is capable of transferring 
  // a maximum of 9600 bits per second.
  Serial.begin(9600);
 
  // Define each pin as an input or output.
  pinMode(Echo, INPUT);
  pinMode(Trigger, OUTPUT);
}
 
void loop(){
 
  // Make the Trigger LOW (0 volts) 
  // for 2 microseconds
  digitalWrite(Trigger, LOW);
  delayMicroseconds(2);
 
  // Emit high frequency 40kHz sound pulse
  // (i.e. pull the Trigger) 
  // by making Trigger HIGH (5 volts) 
  // for 10 microseconds
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigger, LOW); 
 
  // Detect a pulse on the Echo pin 8. 
  // pulseIn() measures the time in 
  // microseconds until the sound pulse
  // returns back to the sensor.
  int distance = pulseIn(Echo, HIGH);
 
  // Speed of sound is:
  // 13511.811023622 inches per second
  // 13511.811023622/10^6 inches per microsecond
  // 0.013511811 inches per microsecond
  // Taking the reciprocal, we have:
  // 74.00932414 microseconds per inch 
  // Below, we convert microseconds to inches by 
  // dividing by 74 and then dividing by 2
  // to account for the roundtrip time.
  distance = distance / 74 / 2;
 
  // Print the distance in inches
  Serial.println(distance);
 
  // Pause for 100 milliseconds
  delay(100);
}

As soon as uploading is finished and with the USB cable still connected to the Arduino, click on the green magnifying glass in the upper right of the IDE to open the Serial Monitor.

Make sure you have the following settings:

  • Autoscroll: selected
  • Line ending: No Line ending
  • Baud: 9600 baud

Place any object in front of the sensor and move it back and forth. You should see the readings on the Serial Monitor change accordingly.

1_test_ultrasonic_sensor

Code for Obstacle Avoidance

Now, upload the following sketch to your Arduino. I named my program obstacle_avoiding_robot_l298n.ino

/**
 * This robot avoids obstacles 
 * using an ultrasonic sensor.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2021-04-11
 */
 
// Motor A connections
const int enA = 9;
const int in1 = 5;
const int in2 = 6;

// Motor B connections
const int enB = 10;
const int in3 = 7;
const int in4 = 8;

// Set the speed (0 = off and 255 = max speed)
// If your wheels are not moving, check your connections,
// or increase the speed.
const int motorSpeed = 80;
 
/* Give a name to a constant value before
 * the program is compiled. The compiler will 
 * replace references to Trigger and Echo with 
 * 11 and 12, respectively, at compile time.
 * These defined constants don't take up 
 * memory space on the Arduino.
 */
#define Trigger 11
#define Echo 12
 
/*   
 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
 */
void setup(){
   
  // Set the baud rate to 9600. 9600 means that 
  // the serial port is capable of transferring 
  // a maximum of 9600 bits per second.
  //Serial.begin(9600);
 
  // Motor control pins are outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Set the motor speed
  analogWrite(enA, motorSpeed); 
  analogWrite(enB, motorSpeed); 
 
  // Define each pin as an input or output.
  pinMode(Echo, INPUT);
  pinMode(Trigger, OUTPUT);
 
  // Initializes the pseudo-random number generator
  // Needed for the robot to wander around the room
  randomSeed(analogRead(3));
 
  delay(200);     // Pause 200 milliseconds               
  go_forward();   // Go forward
}
 
/*
 * This is the main code that runs again and again while
 * the Arduino is connected to power.
 */
void loop(){
  int distance = doPing();
 
  // If obstacle <= 16 inches away
  if (distance >= 0 && distance <= 16) {  
      
    //Serial.println("Obstacle detected ahead");  
    go_backwards();   // Move in reverse
    delay(2000);
 
    /* Go left or right to avoid the obstacle*/
    if (random(2) == 0) {  // Generates 0 or 1, randomly        
      go_right();  // Turn right
    }
    else {
      go_left();  // Turn left
    }
    delay(3000);
    go_forward();  // Move forward
  }
  delay(50); // Wait 50 milliseconds before pinging again
}
 
/*
 * Returns the distance to the obstacle as an integer
 */
int doPing () {
  int distance = 0;
  int average = 0;
 
  // Grab four measurements of distance and calculate
  // the average.
  for (int i = 0; i < 4; i++) {
 
    // Make the Trigger LOW (0 volts) 
    // for 2 microseconds
    digitalWrite(Trigger, LOW);
    delayMicroseconds(2);
 
     
    // Emit high frequency 40kHz sound pulse
    // (i.e. pull the Trigger) 
    // by making Trigger HIGH (5 volts) 
    // for 10 microseconds
    digitalWrite(Trigger, HIGH);
    delayMicroseconds(10);
    digitalWrite(Trigger, LOW);
      
    // Detect a pulse on the Echo pin 8. 
    // pulseIn() measures the time in 
    // microseconds until the sound pulse
    // returns back to the sensor.    
    distance = pulseIn(Echo, HIGH);
 
    // Speed of sound is:
    // 13511.811023622 inches per second
    // 13511.811023622/10^6 inches per microsecond
    // 0.013511811 inches per microsecond
    // Taking the reciprocal, we have:
    // 74.00932414 microseconds per inch 
    // Below, we convert microseconds to inches by 
    // dividing by 74 and then dividing by 2
    // to account for the roundtrip time.
    distance = distance / 74 / 2;
 
    // Compute running sum
    average += distance;
 
    // Wait 10 milliseconds between pings
    delay(10);
  }
 
  // Return the average of the four distance 
  // measurements
  return (average / 4);
}
 
/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void go_backwards() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void stop_all() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Disconnect the USB cable from the Arduino.

Place your robot on the floor.

Turn the power ON for the motors.

Plug in the Arduino’s power.

Watch the Obstacle Avoiding Robot move!

If your wheels are not moving, check your connections, or increase the speed.

Related Articles

How to Connect DC Motors to Arduino and the L298N

In this tutorial, we learn how to connect DC motors to Arduino and the L298N motor driver.  

Here is the motor we will work with, but you can use any motor that looks like this one.

1_dc_motor_encoder_wheels-1

Prerequisites

You have the Arduino IDE (Integrated Development Environment) installed on your PC (Windows, MacOS, or Linux).

You Will Need

This section is the complete list of components you will need for this project (#ad).

OR

  • Self-Balancing Car Kit (which includes everything above and more…Elegoo and Osoyoo are good brands you can find on Amazon.com)

I also bought the following:

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the Hardware

The first thing we need to do is set up the hardware. Below is the wiring diagram, and here is the pdf version so you can see the pin numbers better:

connect_dc_motor_l298n_bb

On my motors, the outer pins are the ones that drive the motors. These pins need to be connected to the L298N OUT1-OUT4 pins.

Remove the two black jumpers that are on the board that cover the ENA and ENB pins. Do not remove the other jumper that is near the power input pins.

Write the Code

Now let’s write the code. You will need to:

  • Load this code to your Arduino from your PC.
  • Remove the Arduino from your PC.
  • Turn on your Arduino using the 9V battery.
/*
 * Author: Automatic Addison
 * Website: https://automaticaddison.com
 * Description: Controls the speed and direction of two DC motors.
 */

// Motor A connections
const int enA = 9;
const int in1 = 5;
const int in2 = 6;

// Motor B connections
const int enB = 10; 
const int in3 = 7;
const int in4 = 8;

// Set the speed (0 = off and 255 = max speed)
const int motorSpeed = 128;

void setup() {
  
  // Motor control pins are outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Set the motor speed
  analogWrite(enA, motorSpeed); 
  analogWrite(enB, motorSpeed); 
}

void loop() {

  // Go forwards
  go_forward();
  delay(3000);

  // Go backwards
  go_backwards();
  delay(3000);

  // Go right
  go_right();
  delay(3000);

  // Go left
  go_left();
  delay(3000);

  // Stop
  stop_all();
  delay(3000);
}

/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void go_backwards() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void stop_all() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Run the Code

When you run this code, you will see your motors move forwards, backwards, right turn, left turn, and then stop. If your motors don’t do this exact sequence, consider switching the position of the wire connections that both motors have to the L298N.

References