2019.11.22 How to use Ydlidar using Python, without ROS?

Today, I tried to use Ydlidar X4 using only python, without ROS.

pip3 install PyLidar3

With this command, I could install the lidar library to python3. Pylidar3 support Ydlidar X4 library. I tested with the samplcode given in the website using X4. It worked well.

import PyLidar3
import time # Time module
#Serial port to which lidar connected, Get it from device manager windows
#In linux type in terminal -- ls /dev/tty* 
#port = input("Enter port name which lidar is connected:") #windows
port = "/dev/ttyUSB0" #linux
Obj = PyLidar3.YdLidarX4(port) #PyLidar3.your_version_of_lidar(port,chunk_size)
if(Obj.Connect()):
    print(Obj.GetDeviceInfo())
    gen = Obj.StartScanning()
    t = time.time() # start time 
    while (time.time() - t) < 30: #scan for 30 seconds
        print(next(gen))
        time.sleep(0.5)
    Obj.StopScanning()
    Obj.Disconnect()
else:
    print("Error connecting to device")

However, when the program stopped in the middle of the processing, YDlidar has a connection problem at the next running. So, Wait until the processing would be finished.

댓글 남기기