Tutorial XI Lidar and Publishing Lidar Topics in ROS2

From Waveshare Wiki
Jump to: navigation, search

Modules Usage Tutorial

Simple Test of Lidar in Windows

The first section is a simple test of the lidar in Windows. Since ROS2 is relatively complex to use, we provide this simple method to test the lidar.

Prepare Test Software

Prepare Test Hardware

  • General Driver for Robots
  • Lidar (PH2.0 4P to ZH1.5 4P cable is included)
  • USB cable type A male port to type C male port

How to Test

Connect LD19 and the driver board with a PH2.0 to ZH1.5 4PIN cable, and note that the interface is LiDAR (next to the IIC). Then connect the driver board to the computer with a USB cable, note that the interface of the USB cable to the driver board is the Type-C interface with LiDAR written next to it.
Open the software and select LDS19, select the port, and then click "Start". The image and numbers appear, and the numbers change when moving the LD19 image, which means the test passes.
When selecting the COM port, if you are not sure which COM port to choose, you can remember it before connecting the USB cable. The COM port of the lidar is the new one. If there is no response after connecting the USB cable, then you need to install the CP2102 driver.
Publish lidar topics01.png
Publish lidar topics02.png
Publish lidar topics03.png

Lidar Connection for the Raspberry Pi Host

General Driver for Robots has an onboard LIDAR interface for connecting to the LIDAR and sending the LIDAR serial data to the host PC via USB.
The LD19 LIDAR is used by default, if you need to connect other radars, you may need to recreate the connection cable to adapt the TX, 5V, and GND pins of other radars.

The General Driver for Robots does not read LiDAR data by default. Its function is similar to that of a LiDAR adapter board (UART-to-USB). If you want to use the ESP32 module on the General Driver for Robots to process LiDAR data, you can connect the RX and LIDAR pins on the board (at the edge of the expansion pin header). The RX pin is connected to the RX pin of UART0 on the ESP32 module, and the LIDAR pin is connected to the TX pin of the LiDAR. However, it is important to note that processing LiDAR data requires a lot of computing power and may affect other functions of the ESP32 as a robot's slave computer. Therefore, this tutorial mainly introduces how to use LiDAR data on a slave computer.
The connection between the LiDAR and the host computer is shown below:
Publish lidar topics04.jpg

Publishing LIDAR Topics in ROS2

In this section, we use Raspberry Pi as the host computer. You can also use this method in an Ubuntu virtual machine to use LiDAR. Here, we use ROS2 as an example to demonstrate how to publish LiDAR topics in ROS2.
To use Raspberry Pi, you need to run the official system. If you don't know how to create an image, you can refer to this tutorial to program the image onto an SD card.

Install ROS2

After booting up the Raspberry Pi, you can use a display or the Raspberry Pi headless mode. To install the ROS2 system and related software and tools, you can refer to the ROS2 official documentation.
Please make sure Ubuntu Universe repository is available:

sudo apt install software-properties-common
sudo add-apt-repository universe

Add ROS2 GPG Key to apt:

sudo apt update && sudo apt install curl
sudo curl -sSL  https://raw.githubusercontent.com/ros/rosdistro/master/ros.key sudo curl -sSL

Add the software library address to "sources":

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg]   http://packages.ros.org/ros2/ubuntu/ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http

Update the software library:

sudo apt update

Upgrade the software:

sudo apt upgrade

Install ROS2, RViz, example, and tutorial:

sudo apt install ros-humble-desktop

Install the development tool, compiler, and other tools for building the ROS package:

sudo apt install ros-dev-tools

Adding sourcing to the shell eliminates the need to manually set up the environment each time you boot in the future.

cecho "source /opt/ros/humble/setup.bash" >> ~/.bashrc

Close the current terminal window, open a new one, and proceed with the subsequent tutorials.

Download and Compile the Radar Package

0. Get the ROS2 Function Package for Radar

cd ~

mkdir -p ldlidar_ros2_ws/src

cd ldlidar_ros2_ws/src

git clone  https://github.com/ldrobotSensorTeam/ldlidar_stl_ros2.git
#Or
git clone  https://gitee.com/ldrobotSensorTeam/ldlidar_stl_ros2.git

1. System Setting

  • Step 1: Connect the radar to your system's board by means of an onboard UART or USB to serial module (e.g., cp2102 module).
  • Step 2: Set the serial device-x permissions for the radar to be mounted on your system (take /dev/ttyUSB0 as an example).
    • In actual use, set it according to the actual mounting situation of the radar in your system, which can be viewed using the command "ls -l /dev".
1 cd ~/ldlidar_ros2_ws
2 
3 sudo chmod 777 /dev/ttyUSB0
  • Step 3: Modify.

The port_name values in the launch file correspond to the radar product model in the launch/ directory, taking ld06.launch.p and /dev/ttyUSB0 as examples, as follows:

#!/usr/bin/env python3
from launch import LaunchDescription
from launch_ros.actions import Node

'''
Parameter Description:
---
- Set laser scan directon: 
  1. Set counterclockwise, example: {'laser_scan_dir': True}
  2. Set clockwise,        example: {'laser_scan_dir': False}
- Angle crop setting, Mask data within the set angle range:
  1. Enable angle crop fuction:
    1.1. enable angle crop,  example: {'enable_angle_crop_func': True}
    1.2. disable angle crop, example: {'enable_angle_crop_func': False}
  2. Angle cropping interval setting:
  - The distance and intensity data within the set angle range will be set to 0.
  - angle >= 'angle_crop_min' and angle <= 'angle_crop_max' which is [angle_crop_min, angle_crop_max], unit is degress.
    example:
      {'angle_crop_min': 135.0}
      {'angle_crop_max': 225.0}
      which is [135.0, 225.0], angle unit is degress.
'''

def generate_launch_description():
  # LDROBOT LiDAR publisher node
  ldlidar_node = Node(
      package='ldlidar_stl_ros2',
      executable='ldlidar_stl_ros2_node',
      name='LD06',
      output='screen',
      parameters=[
        {'product_name': 'LDLiDAR_LD06'},
        {'topic_name': 'scan'},
        {'frame_id': 'base_laser'},
        {'port_name': '/dev/ttyUSB0'},
        {'port_baudrate': 230400},
        {'laser_scan_dir': True},
        {'enable_angle_crop_func': False},
        {'angle_crop_min': 135.0},
        {'angle_crop_max': 225.0}
      ]
  )

  # base_link to base_laser tf node
  base_link_to_laser_tf_node = Node(
    package='tf2_ros',
    executable='static_transform_publisher',
    name='base_link_to_base_laser_ld06',
    arguments=['0','0','0.18','0','0','0','base_link','base_laser']
  )


  # Define LaunchDescription variable
  ld = LaunchDescription()

  ld.add_action(ldlidar_node)
  ld.add_action(base_link_to_laser_tf_node)

  return ld

2. How to Compile

Compile with colcon:

1 cd ~/ldlidar_ros2_ws
2
3 colcon build

3. Running Method

3.1 Setting up Environment Variables for Function Packages

  • After compiling, the relevant files generated by the compilation need to be added to the environment variable for recognition by the ROS environment. The command for adding the environment variable to the terminal temporarily is shown below. This command means that if you open a new terminal, you will need to execute the following command again. (It is recommended to open a new terminal. Usually, compilation and execution need to be performed in different terminal windows, otherwise, there may be problems.)
1 cd ~/ldlidar_ros2_ws
2
3 source install/setup.bash
  • To permanently avoid executing the command to add the environment variables mentioned above every time you reopen the terminal, you can perform the following steps.
1 echo source ~/ldlidar_ros2_ws/install/setup.bash >> ~/.bashrc
2
3 source ~/.bashrc

3.2 Enable Lidar Node

  • The product model is LDROBOT LiDAR LD19.
    • Start the ld19 lidar node (if you don't have a display connected, you should have already completed starting the lidar node and publishing the topic up to this step).
ros2 launch ldlidar_stl_ros2 ld19.launch.py
  • Start the ld19 lidar node and display the laser data on Rviz2 (if you have a connected display, you can use this method to display the scan results in Rviz2):
ros2 launch ldlidar_stl_ros2 viewer_ld19.launch.py

4. Test

The code supports testing on Ubuntu 20.04 with ROS2 Foxy or higher versions and can be visualized using rviz2.

  • Open a new terminal (Ctrl + Alt + T), run the command:

rviz2, and open the ldlidar.rviz file in the rviz2 folder of the directory where the readme file is located using the Rviz2 tool.

rviz2