Ugv02 Using LiDAR Tutorial

From Waveshare Wiki
Jump to: navigation, search

Feature Pack Introduction

In ROS 2 mobile robot mapping and navigation, LiDAR plays a crucial role by providing environmental perception and localization information. It aids the robot in tasks such as mapping, localization, obstacle detection, and avoidance, as well as path planning and navigation.
1. Mapping: The LiDAR scans the surroundings and obtains information about the distance of objects. It aids the robot in tasks such as mapping, localization, obstacle detection, and avoidance, as well as path planning and navigation.
2. Localization: The LiDAR can help the robot in positioning, that is, check the position and gesture of the robot on the map. By matching with the map, the robot can determine its position and achieve precise localization during the navigation process.
3. Obstacle Detection and Avoidance: LiDAR can detect obstacles in the surrounding environment, such as walls, furniture, or other objects. The robot can use this information to avoid obstacles and ensure safe navigation.
4. Path Planning and Navigation: Based on the environmental map and localization information provided by the LiDAR, the robot can perform path planning and navigation. It can calculate the optimal path based on the target and current positions, utilizing LiDAR data for real-time obstacle detection and avoidance, enabling autonomous navigation.


Download Function Pack

Go to the src folder in the ugv02 working directory:

cd ~/ugv02/src

Pull a package to your local machine using Git:

git clone

Edit Function Pack

Viewing the Raspberry Pi's connected serial ports:

ls -l /dev/ttyUSB*

If you only have one device connected to the LiDAR, then the LiDAR's serial port is ttyUSB0, if you have more than one device, pull and plug it to determine the serial port number the LiDAR is connected to.
Modify the /ldlidar_stl_ros2/launch/ file:

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

def generate_launch_description():
  # LDROBOT LiDAR publisher node
  ldlidar_node = Node(
        {'product_name': 'LDLiDAR_LD19'},
        {'topic_name': 'scan'},
        # Modify the LiDAR published coordinates  
        {'frame_id': 'laser_link'},
        # Modify it as your own LiDAR serial port number  
        {'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(
    # Offset of the radar center and direction relative to the vehicle center and direction  

  # Define LaunchDescription variable
  ld = LaunchDescription()


  return ld


Return to the workspace and compile.

cd ~/ugv02
colcon build

Running Test

Enter the workspace:

cd ~/ugv02
source ./install/setup.bash

To grant permissions to the Raspberry Pi's LiDAR serial port (this needs to be done each time the Raspberry Pi restarts for the port to be accessible), you can follow these steps:

sudo chmod 777 /dev/ttyUSB0

Turn on the LiDAR node:

ros2 launch ldlidar_stl_ros2

In a computer (virtual machine) with Ubuntu and ros2, make sure that the computer is connected to the same network or wifi as the Raspberry Pi, and open a new terminal.
Check whether the ros2 system of the PC and Raspberry Pi is in normal communication:

ros2 topic list

Normally, you will see at least two of the following topic outputs.
UGV02 LiDAR 02.png
Check the network configuration if these two topics are not shown.
Run rviz2 on your computer (virtual machine) to view the point cloud information.


Modify Fixed_Frame as base_link.

Globoal frame base link.png

Click the add button in the bottom left corner of rviz to add a control.
Add display robot axes in rviz with chassis frame as base_link.

Rviz Axis.png

To modify the coordinate frame and ideally adjust the size of the coordinate axes in RViz. This will result in displaying a coordinate frame for the robot base, where the red axis represents the forward direction of the robot. You can rotate the robot to observe changes in the coordinate axes.

Add scan topic.png

Next will be subscribing to the radar topic to display point clouds in rviz.

ADD ugv02 Axis.png
2d radar.png

If the point cloud is small and hard to see in rviz, you can change the Style of the point cloud to Points.

Ld19 Size.png