Skip to main content

Library for Lidar. Currently supports YdLidar(X4,G4) from http://www.ydlidar.com

Project description

PyLidar2

Build Status license
PyLidar2 is python 2 package to get data from Lidar device. Currently supports ydlidar from www.ydlidar.com/.

Source code

Source code is available on github's repository.
https://github.com/lakshmanmallidi/PyLidar2/blob/master/PyLidar2/__init.py__

Dependencies

  • pyserial
  • time
  • math
  • enum

Installation

Using Pip
pip install PyLidar2

You can also install using setup.py file from git repository.

Usage

This package consists of multiple classes representing the version of lidar you are using. The class structure is YdLidarX4 where X4 is version name ydlidar. Further contribution are actively accepted.

Class structure:
YdLidarX4

Arguments: port, chunk_size(default:6000).

port: serial port to which device is connected. Example: com4, /dev/ttyAMC0.

chunk_size: Number of bytes of data read from device. Increase in chunk_size results in more averaged angle:distance pairs but increase response time result in slower data acquisition. For faster data acquisition decrease chunk_size.

Note: Calibrate chunk size depends on your application and frequency of device. 
if the chunk size is not enough not all angles are covered. 
  • Connect -- Begin serial connection with Lidar by opening serial port. Return success status True/False.

  • StartScanning -- Begin the lidar and returns a generator which returns a dictionary consisting angle(degrees) and distance(meters).
    Return Format : {angle(0):distance, angle(2):distance,....................,angle(359):distance}

  • StopScanning -- Stops scanning but keeps serial connection alive.

  • GetHealthStatus -- Returns True if Health of lidar is good else returns False

  • GetDeviceInfo -- Returns Information of Lidar version, serial number etc.

  • Reset -- Reboot the lidar

  • Disconnect -- Stop scanning and close serial communication with Lidar.

YdLidarG4

Arguments: port, chunk_size(default:6000).

port: serial port to which device is connected. Example: com4, /dev/ttyAMC0.

chunk_size: Number of bytes of data read from device. Increase in chunk_size results in more averaged angle:distance pairs but increase response time result in slower data acquisition. For faster data acquisition decrease chunk_size.

Note: Calibrate chunk size depends on your application and frequency of device. 
if the chunk size is not enough not all angles are covered. 
  • Connect -- Begin serial connection with Lidar by opening serial port. Return success status True/False.

  • StartScanning -- Begin the lidar and returns a generator which returns a dictionary consisting angle(degrees) and distance(meters).
    Return Format : {angle(0):distance, angle(2):distance,....................,angle(359):distance}

  • StopScanning -- Stops scanning but keeps serial connection alive.

  • GetHealthStatus -- Returns True if Health of lidar is good else returns False

  • GetDeviceInfo -- Returns Information of Lidar version, serial number etc.

  • EnableLowPowerMode -- Enable Low Power Consumption Mode(Turn motor and distance-measuring unit off in StopScanning).

  • DisableLowPowerMode -- Disable Low Power Consumption Mode(Turn motor and distance-measuring unit on StopScanning).

  • GetLowPowerModeStatus -- Return True if Low Power Consumption Mode is Enable else return False.

class FrequencyStep(Enum):
    oneTenthHertz=1
    oneHertz=2
  • IncreaseCurrentFrequency -- Increase current frequency by oneTenth or one depends on enum FrequencyStep.

  • DecreaseCurrentFrequency -- Decrease current frequency by oneTenth or one depends on enum FrequencyStep.

import PyLidar2
port = raw_input("Enter port name which lidar is connected:") #windows
Obj = PyLidar2.YdLidarG4(port)
if(Obj.Connect()):
    print(Obj.GetDeviceInfo())
    print(Obj.GetCurrentFrequency())
    Obj.IncreaseCurrentFrequency(PyLidar2.FrequencyStep.oneTenthHertz)
    print(Obj.GetCurrentFrequency())
    Obj.DecreaseCurrentFrequency(PyLidar2.FrequencyStep.oneHertz)
    print(Obj.GetCurrentFrequency())
    Obj.Disconnect()
else:
    print("Error connecting to device")
  • EnableConstantFrequency -- Enables constant frequency default Enable.

  • DisableConstantFrequency -- Disable constant frequency.

  • SwitchRangingFrequency -- Switch between ranging frequencies 4khz, 8khz and 9khz, default 9khz.

  • GetCurrentRangingFrequency -- Returns current Ranging Frequency in khz.

  • Reset -- Reboot the lidar

  • Disconnect -- Stop scanning and close serial communication with Lidar.

Examples

This Example prints data from lidar

import PyLidar2
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 = raw_input("Enter port name which lidar is connected:") #windows
#port = "/dev/ttyUSB0" #linux
Obj = PyLidar2.YdLidarX4(port) #PyLidar2.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(gen.next())
        time.sleep(0.5)
    Obj.StopScanning()
    Obj.Disconnect()
else:
    print("Error connecting to device")

This Example plot the data. This example needs matplotlib library.

import threading
import PyLidar2
import matplotlib.pyplot as plt
import math    
import time

def draw():
    global is_plot
    while is_plot:
        plt.figure(1)
        plt.cla()
        plt.ylim(-9000,9000)
        plt.xlim(-9000,9000)
        plt.scatter(x,y,c='r',s=8)
        plt.pause(0.001)
    plt.close("all")


is_plot = True
x=[]
y=[]
for _ in range(360):
    x.append(0)
    y.append(0)

port =  raw_input("Enter port name which lidar is connected:") #windows
Obj = PyLidar2.YdLidarX4(port) #PyLidar2.your_version_of_lidar(port,chunk_size) 
threading.Thread(target=draw).start()
if(Obj.Connect()):
    print(Obj.GetDeviceInfo())
    gen = Obj.StartScanning()
    t = time.time() # start time 
    while (time.time() - t) < 30: #scan for 30 seconds
        data = gen.next()
        for angle in range(0,360):
            if(data[angle]>1000):
                x[angle] = data[angle] * math.cos(math.radians(angle))
                y[angle] = data[angle] * math.sin(math.radians(angle))
    is_plot = False
    Obj.StopScanning()
    Obj.Disconnect()
else:
    print("Error connecting to device")

Testing

A "tesing" branch is maintained in the git repository for testing, debugging and updating the code. Please visit Github repo https://github.com/lakshmanmallidi/PyLidar2 for further information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyLidar2-1.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

PyLidar2-1.6-py2-none-any.whl (6.9 kB view details)

Uploaded Python 2

File details

Details for the file PyLidar2-1.6.tar.gz.

File metadata

  • Download URL: PyLidar2-1.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for PyLidar2-1.6.tar.gz
Algorithm Hash digest
SHA256 e9b56cbf73a6d46d99b053372ecd6fa40af46023b84ef76db5eb082b658c0a4a
MD5 7646bc976c245ba1058698932aeeb490
BLAKE2b-256 dc7986ba49d58b64389f1118494af7e21dfda59b0ade3cf5319dc6cd31b8f6e5

See more details on using hashes here.

File details

Details for the file PyLidar2-1.6-py2-none-any.whl.

File metadata

  • Download URL: PyLidar2-1.6-py2-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for PyLidar2-1.6-py2-none-any.whl
Algorithm Hash digest
SHA256 3b26a673ba3e0fd2ce3bfa67fb431709a8a23cfb3c2fc9cd273abd25971b5b63
MD5 1c16843c7caf9f1a1a550172e5fe1aea
BLAKE2b-256 6f3bf7c4f2bbe6f40a60105c2dc8dcd8823d5ef16fbc74fb80dbf878d0b39388

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page