来自python草图的不稳定数据流

2024-09-30 06:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从四旋翼机输出时间、俯仰、横摇和偏航,并使用python创建图形。我有两个XBee模块将数据从quadrotor发送到计算机。我从python草图中收到的值非常不稳定,似乎存在大量错误,这似乎是由于数据流中随机出现不需要的数字造成的。我似乎无法解决这个问题,有人有什么想法吗???下面我附上了代码的副本

import serial # import Serial Library
import numpy as np # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *

time=[]
d0k0=[]
d0k1=[]
d0k2=[]
arduinoData = serial.Serial('COM4', 9600) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0
ydata = [0] * 50
#ax1 = plt.axes()

#line, = plt.plot(ydata)

def makeFig():    #Create a function that makes our desired plot
    plt.ylim(180,-180)                               #Set y min and max values
    plt.title('Stability')                          #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Kalman estimated angles')           #Set ylabels
    line.set_xdata(time)
    line.set_ydata(d0k0)
    #plt.plot(t, d2)
    #plt.plot(t, d3)

    plt.legend(['roll','pitch','yaw'], loc='upper left')
    plt.draw()


while True: # While loop that loops forever
    while (arduinoData.inWaiting()==0): #Wait here until there is data
        pass #do nothing
    data = arduinoData.readline()
    data = data.decode("ascii", "ignore")
    data = ''.join([c for c in data if c in '1234567890.,'])
    data = data.strip()

    data = data.split(",")
    if len(data) == 4:
        print(data)
        t = float( data[0])
        #print (t)
        d1 = float( data[1])
        d2 = float( data[2])
        d3 = float( data[3])           
        time.append(t)                     
        d0k0.append(d1)
        #d0k1.append(d2)
        #d0k2.append(d3)
        #drawnow(makeFig)                       
        #plt.pause(0.01)                     

Tags: importdatatimeplotmatplotliblineserialplt

热门问题