我正在使用Arduino创建一个围绕它的区域的三维激光雷达扫描,并通过串行发送x,y,z坐标到python脚本以实时显示数据。我遇到的问题是mplotlib太慢而无法跟上流。我有一种感觉这是由于完全重绘了情节,但我还没有找到解决方案。我通过串口写s1000来启动激光雷达扫描,每秒发送一次x,y,z坐标。
import serial
import numpy
import matplotlib.pyplot as plt #import matplotlib library
from mpl_toolkits.mplot3d import Axes3D
from drawnow import *
from matplotlib import animation
import time
ser = serial.Serial('COM7',9600,timeout=5)
ser.flushInput()
time.sleep(5)
ser.write(bytes(b's1000'))
plt.ion()
fig = plt.figure(figsize=(16,12))
ax = fig.add_subplot(111, projection="3d")
ax.set_xlim3d(-255, 255)
ax.set_ylim3d(-255, 255)
ax.set_zlim3d(-255, 255)
x=list()
y=list()
z=list()
while True:
try:
ser_bytes = ser.readline()
data = str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
xyz = data.split(", ")
dx = float(xyz[0])
dy = float(xyz[1])
dz = float(xyz[2].replace(";",""))
x.append(dx);
y.append(dy);
z.append(dz);
ax.scatter(x,y,z, c='r',marker='o')
plt.draw()
plt.pause(0.0001)
except:
print("Keyboard Interrupt")
ser.close()
break