实时绘图与延迟

时间:2018-01-22 16:57:56

标签: python real-time

大家好,感谢您的帮助。

我试图向人们展示一个用于教育目的的实时地震图。我通过连接到arduino的串口获取数据,但问题是信号和屏幕上的图形seismogram plot之间存在很大的延迟(8-20秒)。

我已经检查了代码,但我看不出任何错误,至少就我理解代码而言。有人能帮助我吗?

提前致谢。

import time
import serial
import sys
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
arduino = serial.Serial('COM4', baudrate=115200, timeout=1.0)     


start = time.time()
ad = []
ed = []
ec = []
es = []
delta=0
duracion=120

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure(figsize=(16,8))
ax = plt.axes(xlim=(0, duracion), ylim=(-1000, 1000))
ax.set_xlabel('segundos')
ax.set_ylabel('cuentas')
plt.title('Haz tu propio sismograma')
fig.canvas.set_window_title("Sismograma")
line, = ax.plot([], [], lw=0.5)
line2, = ax.plot([],[],lw=0.5)
line3, = ax.plot([],[],lw=0.5)
# initialization function: plot the background of each frame
def init():
    arduino.readline()
    ad = []
    ed = []
    ec = []
    es = []
    start = time.time()

    delta=0

    line.set_data([],[])
    line2.set_data([],[])
    line3.set_data([],[])
    return line, line2, line3

# animation function.  This is called sequentially
def animate(i):
    global start
    global ad,ed,ec,es
    global delta
    delta = time.time() - start
    linea = arduino.readline()
    texto=linea.decode('ascii', errors='replace')

    valora=texto.split(',')

    try:

        valor=int(valora[0])+400
        valor2=int(valora[1])
        valor3=int(valora[2])-400
        #print(valor)
        ad.append(delta)
        ed.append(valor)
        ec.append(valor2)
        es.append(valor3)
        line.set_data(ad, ed)
        line2.set_data(ad, ec)
        line3.set_data(ad, es)

    except ValueError:
        ed.append(ed[-1])
        ec.append(ec[-1])
        es.append(es[-1])
        ad.append(delta)
    except RuntimeError:
        ed.append(ed[-1])
        ec.append(ec[-1])
        es.append(es[-1])
        ad.append(delta)
    except IndexError:
        ed.append(ed[-1])
        ec.append(ec[-1])
        es.append(es[-1])
        ad.append(delta)
    #print(delta)
    if delta>duracion:
        i=0
        ad=[0]
        ed=[0]
        ec=[0]
        es=[0]
        delta=0
        start = time.time()
        fig.clf()
        run_animation
    return line, line2,line3
    #time.sleep(0)


# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate,frames=30, interval=1, init_func=init, blit=True)

def run_animation():
    init
    delta=0
    start = time.time()
    animate(0)
    plt.show()

plt.show()

0 个答案:

没有答案