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
For more information on how to use Simulink Support Package for Raspberry Pi Hardware, see Get Started with Simulink Support Package for Raspberry Pi Hardware.
For more information on how to get started with using ROS on your Raspberry Pi hardware, see Get Started with Robot Operating System on Raspberry Pi.
Required Hardware
Raspberry Pi hardware board (Model 3B or Model 4B)
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 (ROS Toolbox) block is subscribing to the /scan topic, so that it can read the LaserScan
message from the ROS network. The ROS Read Scan, ROS 2 Read Scan (ROS Toolbox) 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.