激光雷达图书馆。目前从http://www.ydlidar.com调用ydlidar(x4,g4)
PyLidar2的Python项目详细描述
Pylidar2
pylidar2是python 2包,用于从lidar设备获取数据。目前支持来自www.ydlidar.com/的ydlidar。
源代码
源代码可以在github的存储库中找到。
https://github.com/lakshmanmallidi/PyLidar2/blob/master/PyLidar2/__init.py__
依赖关系
- pyserial
- 时间
- 数学
- 枚举
安装
使用pip
pip install PyLidar2
pip install PyLidar2
您还可以使用git存储库中的setup.py文件进行安装。
用法
这个包由多个类组成,这些类代表您正在使用的lidar版本。类结构是ydlidar x4,其中x4是版本名ydlidar。我们积极接受进一步的贡献。
等级结构:
ydlidarx4
Arguments
:端口,块大小(默认值:6000)。
port
:设备连接到的串行端口。示例:com4,/dev/ttyamc0。
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
——打开串行端口,开始与激光雷达的串行连接。返回成功状态true/false。StartScanning
——启动激光雷达并返回一个生成器,该生成器返回一个由角度(度)和距离(米)组成的字典。Return Format
:{角度(0):距离,角度(2):距离,………,角度(359):距离}StopScanning
——停止扫描,但保持串行连接活动。GetHealthStatus
——如果激光雷达的健康状况良好,则返回true否则返回falseGetDeviceInfo
——返回激光雷达版本、序列号等信息。Reset
——重新启动激光雷达Disconnect
——停止扫描并关闭与激光雷达的串行通信。
ydlidarg4
Arguments
:端口,块大小(默认值:6000)。
port
:设备连接到的串行端口。示例:com4,/dev/ttyamc0。
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
——打开串行端口,开始与激光雷达的串行连接。返回成功状态true/false。StartScanning
——启动激光雷达并返回一个生成器,该生成器返回一个由角度(度)和距离(米)组成的字典。Return Format
:{角度(0):距离,角度(2):距离,………,角度(359):距离}StopScanning
——停止扫描,但保持串行连接活动。GetHealthStatus
——如果激光雷达的健康状况良好,则返回true否则返回falseGetDeviceInfo
——返回激光雷达版本、序列号等信息。EnableLowPowerMode
——启用低功耗模式(停止扫描时关闭电机和测距装置)。DisableLowPowerMode
——禁用低功耗模式(停止扫描时转动电机和测距装置)。GetLowPowerModeStatus
——如果低功耗模式为enable,则返回true,否则返回false。
class FrequencyStep(Enum):
oneTenthHertz=1
oneHertz=2
IncreaseCurrentFrequency
——电流频率增加1/10或1取决于枚举频率。DecreaseCurrentFrequency
——降低电流频率1/10或1取决于枚举频率。
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
——启用恒定频率默认启用。DisableConstantFrequency
——禁用恒定频率。SwitchRangingFrequency
——在测距频率4kHz、8kHz和9kHz之间切换,默认为9kHz。GetCurrentRangingFrequency
——返回当前测距频率频率单位为千赫。Reset
——重新启动激光雷达Disconnect
——停止扫描并关闭与激光雷达的串行通信。
示例
此示例打印来自激光雷达的数据
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")
此示例绘制数据。这个例子需要matplotlib库。
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")
测试
git存储库中维护了一个“tesing”分支,用于测试、调试和更新代码。有关详细信息,请访问github repohttps://github.com/lakshmanmallidi/PyLidar2。