YD LiDAR Setup for ROS2 Humble

Posted on Jan 20, 2025

The resources present on the internet for setting up the YDLidar SDK and integrating with ROS has been quite vague. So I am documenting the steps here for future reference.

Here is my setup for reference:
Ubuntu 22.04
ROS2 Humble

LiDAR Used: YDLidar X2

YD LiDAR SDK Setup

First of all we need to install the YDLidar SDK from their official github repo. The link for the same can be found here: YDLidar-SDK

Following the steps for build and install

sudo apt install cmake pkg-config

In the YDLidar SDK directory, run the following commands to compile the project. If theres no build directory then make one for installation.

git clone https://github.com/YDLIDAR/YDLidar-SDK.git
cd YDLidar-SDK/build
cmake ..
make
sudo make install

Now the Python API installation was not successful in my case due to some installation issues. But here are the steps in case it gets fixed in the future.

cd YDLidar-SDK
pip install .

# Another method
python setup.py build
python setup.py install

Test the installation with the help of the tests that have been setup in your build directory

cd YDLidar-SDK/build
./tri_test

You should see the cli for the YDLidar test, it should look something like this:
LiDAR Tri Test Image LiDAR Tri Test Output

Depending on the LiDAR select your Baud Rate from the table below

LiDARModelBaudrateSampleRate(K)Range(m)Frequency(HZ)Intenstiy(bit)SingleChannelvoltage(V)
F4111520040.12~125~12falsefalse4.8~5.2
S4411520040.10~8.05~12 (PWM)falsefalse4.8~5.2
S4B4/1115360040.10~8.05~12(PWM)true(8)false4.8~5.2
S24/1211520030.10~8.04~8(PWM)falsetrue4.8~5.2
G452304009/8/40.28/0.26/0.1~165~12falsefalse4.8~5.2
X4612800050.12~105~12(PWM)falsefalse4.8~5.2
X2/X2L611520030.10~8.04~8(PWM)falsetrue4.8~5.2
G4PRO72304009/8/40.28/0.26/0.1~165~12falsefalse4.8~5.2
F4PRO82304004/60.12~125~12falsefalse4.8~5.2
R2923040050.12~165~12falsefalse4.8~5.2
G61351200018/16/80.28/0.26/0.1~255~12falsefalse4.8~5.2
G2A1423040050.12~125~12falsefalse4.8~5.2
G21523040050.28~165~12true(8)false4.8~5.2
G2C1611520040.1~125~12falsefalse4.8~5.2
G4B17512000100.12~165~12true(10)false4.8~5.2
G4C1811520040.1~125~12falsefalse4.8~5.2
G11923040090.28~165~12falsefalse4.8~5.2
G5202304009/8/40.28/0.26/0.1~165~12falsefalse4.8~5.2
G72151200018/16/80.28/0.26/0.1~255~12falsefalse4.8~5.2
TX8  10011520040.1~84~8(PWM)falsetrue4.8~5.2
TX20  10011520040.1~204~8(PWM)falsetrue4.8~5.2
TG15  10051200020/18/100.05~303~16falsefalse4.8~5.2
TG30  10151200020/18/100.05~303~16falsefalse4.8~5.2
TG50  10251200020/18/100.05~503~16falsefalse4.8~5.2
T15  2008000200.05~155~35truefalse4.8~5.2
T30  2008000200.05~305-35truefalse4.8~5.2

YD LiDAR ROS2 Humble Integration

There are 2 options for setting up the LiDAR for ROS2 Humble.
The 1st one is from the official repository for the YD LiDAR: repo
The 2nd one is from Rigbetel Labs repo

The 2nd approach is the one that worked for me.
Clone the repo

git clone https://github.com/rigbetellabs/ydlidar_ros2_driver-humble.git ydlidar_ros2_ws/src/ydlidar_ros2_driver

Build the downloaded package

cd ydlidar_ros2_ws
colcon build --symlink-install

Install the environment setup

source ./install/setup.bash

Preferably add the package installation in the bashrc file so that the package in initialized when you open a new terminal

echo "source ~/ydlidar_ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

Change the LiDAR parameters according to your LiDAR Model

ydlidar_ros2_driver_node:
  ros__parameters:
    port: /dev/ttyUSB0
    frame_id: laser_frame
    ignore_array: ""
    baudrate: 230400
    lidar_type: 1
    device_type: 0
    sample_rate: 9
    abnormal_check_count: 4
    resolution_fixed: true
    reversion: true
    inverted: true
    auto_reconnect: true
    isSingleChannel: false
    intensity: false
    support_motor_dtr: false
    angle_max: 180.0
    angle_min: -180.0
    range_max: 64.0
    range_min: 0.01
    frequency: 10.0
    invalid_range_is_inf: false

The above paramters are for YD LiDAR X2

Test installation and working of the LiDAR

ros2 launch ydlidar_ros2_driver ydlidar_launch.py 

OR

ros2 launch ydlidar_ros2_driver ydlidar_launch_view.py 

RViz Test

Additional Content:
If you are not able to access the USB port for communication to the LiDAR. Disconnect and reconnect the LiDAR once. Or if the port for communication is inaccessible, run the command below\

sudo chmod 0666 /dev/ttyUSB0

(assuming communication is over ttyUSB0 else replace)

Also install terminator for using multiple terminals in a single screen.

sudo apt install terminator

References

[1] https://github.com/YDLIDAR/YDLidar-SDK
[2] https://github.com/YDLIDAR/ydlidar_ros2_driver/tree/humble
[3] https://github.com/rigbetellabs/ydlidar_ros2_driver-humble