我目前正在开展一个项目,我正在从Raspberry Pi 3 Model B发送模数转换器(ADC)的2通道数据。我已经写过了一个C代码,它通过2个UDP端口成功地将数据发送到计算机,我将在2个单独的图中实时绘制这2个数据。当我尝试仅显示1个绘图时,它会显示实时值,因为当我从生成器切割信号时,我可以在绘图中看到它。然而,当我尝试2个单独的图时,它绘制了值但不是实时的。当我从发电机切断信号时,我仍然可以看到有信号的情节。起初我认为它的缓冲区大小问题,所以我将缓冲区大小从1024更改为32(因为RPi发送浮点数据值)。我也搞砸了暂停时间。它们都不是我的解决方案。
这是我的Python代码。
#!/usr/bin/env python3
import time, random
import math
from collections import deque
import socket
UDP_IP = "192.168.180.25"
UDP_PORT1 = 5013
UDP_PORT2 = 5012
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.bind((UDP_IP, UDP_PORT1))
sock2 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock2.bind((UDP_IP, UDP_PORT2))
start = time.time()
class RealtimePlot:
def __init__(self, axes, max_entries=100):
self.axis_x = deque(maxlen=max_entries)
self.axis_y = deque(maxlen=max_entries)
self.axes = axes
self.max_entries = max_entries
self.lineplot, = axes.plot([], [], "ro-")
self.axes.set_autoscaley_on(True)
def add(self, x, y):
self.axis_x.append(x)
self.axis_y.append(y)
self.lineplot.set_data(self.axis_x, self.axis_y)
self.axes.set_xlim(self.axis_x[0], self.axis_x[-1] + 1e-15)
self.axes.relim();
self.axes.autoscale_view() # rescale the y-axis
def animate(self, figure, callback, interval=50):
import matplotlib.animation as animation
def wrapper(frame_index):
self.add(*callback(frame_index))
self.axes.relim();
self.axes.autoscale_view() # rescale the y-axis
return self.lineplot
animation.FuncAnimation(figure, wrapper, interval=interval)
def main():
from matplotlib import pyplot as plt
fig, axes = plt.subplots(2)
display1 = RealtimePlot(axes[0])
display2 = RealtimePlot(axes[1])
while True:
data1, addr = sock1.recvfrom(32)
display1.add(time.time() - start, data1)
data2, addr = sock2.recvfrom(32)
display2.add(time.time() - start, data2)
plt.pause(0.0001)
if __name__ == "__main__": main()
编辑:我向两个频道发送相同的信号。所以不要介意图中的情节是一样的。
答案 0 :(得分:0)
我目前设法通过将时间间隔从10000μs( 0.01s )增加到100000μs( 0.1s )来解决问题从Raspberry Pi发送的每个数据。