我正在使用rplidar . 其旋转360度,每秒旋转100个样本(信号质量,角度和距离) . 使用Rplidar库我可以将这些数据作为多维数组获取([(15,90,300),(14,91,310)...]) . 现在我想将这些数据绘制为动画,而下面是代码 . 问题是grap没有更新 . 如果你能提供帮助我会很高兴的 . 非常感谢您的宝贵时间 .
from rplidar import RPLidar, RPLidarException
import matplotlib.pyplot as plt
import matplotlib.animation as animation
lidar = RPLidar('/dev/ttyUSB0')
info = lidar.get_info()
print(info)
health = lidar.get_health()
print(health)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='polar')
def run(i):
try:
angle = []
distance = []
for i, scan in enumerate(lidar.iter_scans()):
for d in scan: # d[0] : Quality of the measurement
# if 0< d[1] <200: #d[1] : Angle of the measurement
# print(d[2]/10) #d[2] : Distance of the measurement '''
angle.append(0.0174533 * d[1]) #angle to radian
distance.append(d[2])
print('angle:', angle)
ax.scatter(angle, distance, s=5)
angle.clear()
distance.clear()
except KeyboardInterrupt:
print("Stopping...")
lidar.stop()
lidar.stop_motor()
lidar.disconnect()
ani = animation.FuncAnimation(fig, run, interval=50)
plt.show()
为了更好地理解这里来自传感器的测试数据(质量,角度,距离):
[(15, 276.359375, 873.25), (15, 260.203125, 843.0), (15, 261.28125, 811.0), (15, 262.3125, 800.5), (15, 263.375, 798.0), (15, 264.328125, 810.0), (15, 265.171875, 860.0), (15, 266.46875, 779.0), (15, 267.5, 777.75), (15, 268.375, 808.0), (15, 269.375, 809.25), (15, 270.5, 808.0), (15, 271.46875, 775.25), (15, 272.5625, 750.0), (15, 273.6875, 751.5), (15, 276.921875, 871.5), (15, 277.9375, 873.5), (15, 278.9375, 869.5), (15, 279.984375, 860.0), (15, 281.109375, 840.25), (15, 282.09375, 838.0), (15, 283.171875, 845.0), (15, 284.21875, 860.0), (15, 285.1875, 880.5), (15, 286.171875, 894.75), (15, 287.21875, 899.5), (15, 288.53125, 794.0), (15, 289.578125, 789.0), (15, 290.484375, 790.75), (15, 291.609375, 797.5), (15, 292.5, 830.25), (15, 293.53125, 861.75), (15, 294.46875, 871.75), (15, 295.484375, 874.25), (15, 296.625, 880.5), (15, 297.59375, 904.0), (15, 298.53125, 946.25), (15, 299.71875, 958.25), (15, 300.828125, 955.75), (15, 301.890625, 940.5), (15, 302.96875, 892.25), (15, 304.0, 884.0), (15, 305.15625, 896.0), (15, 306.0, 962.25), (15, 307.015625, 978.25), (15, 308.0625, 982.0), (15, 309.0625, 982.75), (15, 310.234375, 981.5), (15, 311.140625, 991.0), (15, 312.109375, 1081.75), (15, 313.1875, 1073.5), (15, 316.15625, 1261.75), (15, 317.109375, 1286.25), (15, 318.203125, 1304.75), (15, 319.171875, 1332.75), (15, 320.234375, 1354.75), (15, 321.21875, 1382.0), (15, 322.265625, 1401.5), (15, 323.65625, 1404.25), (15, 324.78125, 1382.25), (15, 325.828125, 1360.5), (15, 326.890625, 1342.5), (15, 328.015625, 1322.25), (15, 329.078125, 1307.25), (15, 330.171875, 1285.75), (15, 331.1875, 1270.75), (15, 332.328125, 1251.75), (15, 333.34375, 1240.25), (15, 334.40625, 1227.75), (15, 335.5, 1217.75), (15, 336.609375, 1202.25), (15, 337.65625, 1189.0), (15, 338.6875, 1179.0), (15, 339.75, 1167.5), (15, 340.828125, 1158.5), (15, 341.96875, 1148.5), (15, 342.96875, 1136.75), (15, 344.078125, 1131.75), (15, 345.140625, 1124.75), (15, 346.1875, 1117.75), (15, 347.546875, 1111.25), (15, 348.578125, 1105.5), (15, 349.625, 1097.25), (15, 350.765625, 1094.5)]
每个seceond我得到这些数据
1 回答
函数plt.show是阻塞的,这意味着它在数字打开时暂停代码 . 在你的代码中你有一个show命令,它停止该行的动画 . Addint参数block = False将阻止show停止,即plt.show(block = False) . 看看matplotlib中的this示例 . 我没有't have the lidar data hence I can' t运行你的代码 . 无论如何,您可以将plt.show移动到动画函数调用之下,或者添加block = False参数将解决您的问题 . 建议使用前者 . 包含示例代码 .
第二次编辑:我就是这样做的:
我个人并不喜欢funcAnim