Main Content

Read Lidar Laser Scan Data over ROS from Raspberry Pi

This example shows how to use Simulink® Support Package for Raspberry Pi® Hardware to read and receive a 2-D lidar scan data of an indoor environment from a ROS network using the Raspberry Pi hardware board.

Prerequisites

Required Hardware

  • Raspberry Pi hardware board (Model 3B or Model 4B)

  • Slamtec RPLiDAR

  • Micro USB connector

Hardware Setup

Use a USB connector to connect one end to the Raspberry Pi board and the other end to the USB adaptor of the lidar sensor. This example uses the A1 RPLiDAR.

Install and Set Up ROS on Raspberry Pi

Install ROS Melodic on your Raspberry Pi hardware board. For more information, see Install ROS Melodic on Raspberry Pi.

Install RPLiDAR Package

To download and install the RPLiDAR package on your Raspberry Pi hardware board, execute these commands on your Raspberry Pi terminal.

cd ~/catkin_ws
cd src;
wget https://github.com/Slamtec/rplidar_ros/archive/refs/heads/master.zip
unzip master.zip
cd ..
catkin_make
source /opt/ros/melodic/setup.bash

Configure Simulink Model and Calibrate Parameters

Open the rosberrypi_lidar_data Simulink model.

Verify that the Subscribe block is subscribing to the /scan topic, so that it can read the LaserScan message from the ROS network. The Read Scan block extracts range and angle data from the message. The Read Scan block then converts the range and angle data to Cartesian coordinates. The plotLidarScan MATLAB Function visualizes the scan data in a 2-D plot.

Run Simulink Model

Before you run the Simulink model, perform these steps on the Raspberry Pi terminal.

1. Start the ROS master on your host machine. Enter the host name or IP address.

rosinit('NodeHost',<IP address of your computer>)

2. Initialize the ROS environment and set the ROS master to the IP address of the host computer.

pi@raspberrypi:~ $ source ~/catkin_ws/devel/setup.bash
pi@raspberrypi:~ $ export ROS_MASTER_URI=http://<Enter your host computer's IP address>:11311

3. Change the current working directory to Simulink catkin_ws.

cd ~/catkin_ws
source ~/catkin_ws/devel/setup.bash

4. Launch the RPLIDAR package.

roslaunch rplidar_ros rplidar.launch

3. On the Simulation tab of the Simulink model, click Run. The plot displays the points in the laser scan. The X-_Y_ coordinates of the lidar are placed at (0, 0).

Other Things to Try

  • On the ROS tab of the Simulink model, set Hardware board to Raspberry Pi - Robot Operating System and run the model in external mode.

  • Use the 2-D lidar scan data from the Simulink model and develop a self-navigating robot using RPLiDAR and a Raspberry Pi hardware board.

See Also