Laser Range Scanner

Laser Range Scanner is becoming popular with the advent of Autonomous vehicle and AI robots. RPLidar A2 is a laser range scanner produced by Slamtec, and it’s one of those popular ones you will easily find around. Although the RPLidar A2 was designed for the Raspberry Pi, it can be used by relatively any USB host.

RPLidar A2

Working with the RPLidar A2 with a Raspberry is possible using their provided SDK here, their SDK only supports language in C++, and unless you are proficient in C++, then you might have issues using the RPLidar A2 with languages like Python.

Fortunately for Python users, there is a Python library by SkoltechRobotics for you to get started using the RPLidar A2. Unfortunately, depending on the nature of your application, you might find out that the Library is unreliable. It is slow and comes with frequent errors.

SLAM

For my project, I was working on SLAM application, i.e. I needed to be able to localize a mobile robot, and also make the robot able to build a map of its environment while still running on the Raspberry Pi 3. This requirement makes the idea of using the RPLidar Python library unreliable. SLAM is a crucial aspect for autonomous robot navigation, and this is a topic I talk about in some later post.

To solve the issues with the current available Python Library, I decided to write my own library called FastestRplidar. FastestRplidar is a python wrapper for the Slamtec C++ SDK, and the underlying code is a C++ code that does the initialization, fetching scan data, starting the motor, and others. I used SWIG, a software development tool that connects programs written in C and C++ with a variety of high-level programming languages, and I was able to generate a Python wrapper code that can be imported in any Python 3 projects.

The source files are also included, so it is possible to port the library to work with other platforms like Windows, Linux, and others.

Fastest Rplidar features

  • Supports Python 3
  • Works only on Raspberry Pi boards
  • Can fetch a full 360-degree scan in a go
  • Health monitor included.
  • Supports multiple scan modes.
  • Ability to start and stop the Lidar motor

Working with the library is pretty straight forward.

The below code uses the compatible RPLidar library “myRplidar” to fetch ten scan data. The myRplidar helper code inherits directly from the “fastestrplidar” codebase:

from myRplidar import RPlidar
# uses the default port ttyUSB0
lidar = RPlidar()
 
health = lidar.get_health()
print(health)
 
for i, scan in enumerate(lidar.iter_scans()):
    print('%d: Got %d measurments' % (i, len(scan)))
    if i > 10:
        break
 
# stops lidar and disconnect the driver
lidar.stopmotor()

The Library is available on Github, and it comes with different examples.

Privacy Preference Center