我正在尝试使用arduino来绘制来自mpu6050 imu的数据。 MPU6050比绘图更快地发送数据。 Arduino代码提供6个数据,分别来自串口的偏航,俯仰,滚动,斧头,ay和az。我需要快速绘图的建议。
Python代码:
import serial
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
ser = serial.Serial('COM9', 115200)
yaw = 0.0
pitch =0.0
roll =0.0
ax =0.0
ay =0.0
az =0.0
o_yaw= [0]
o_pitch= [0]
o_roll= [0]
o_ax= [0]
o_ay= [0]
o_az= [0]
plt.ion()
cnt=0
def makeFig():
plt.ylim(-1000,1000)
plt.grid(True)
plt.ylabel('Magnitude')
plt.plot(olculen_ax, 'ro-', label='ax')
plt.plot(olculen_ay, 'bo-', label='ay')
plt.plot(olculen_az, 'go-', label='az')
plt.legend()
while True:
incoming=ser.readline()
if ("hand" in incoming):
incoming=incoming.split(":")
if len(incoming)==8:
yaw = float(incoming[1])
pitch = float(incoming[2])
roll = float(incoming[3])
ax = float(incoming[4])
ay = float(incoming[5])
az = float(incoming[6])
print "Split works"
else:
print incoming
o_ax.append(ax)
o_ay.append(ay)
o_az.append(az)
o_yaw.append(yaw)
o_pitch.append(pitch)
o_roll.append(roll)
drawnow(makeFig)
plt.pause(.00001)
cnt=cnt+1
if(cnt>50):
o_ax.pop(0)
o_ay.pop(0)
o_az.pop(0)
Arduino代码(我只是添加了从this派生的循环代码):
void loop() {
if (!dmpReady) return;
while (!mpuInterrupt && fifoCount < packetSize) {
}
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
fifoCount = mpu.getFIFOCount();
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
mpu.resetFIFO();
//Serial.println(F("FIFO overflow!"));
} else if (mpuIntStatus & 0x02) {
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print("hand:");
Serial.print(ypr[0] * 180/M_PI);
Serial.print(":");
Serial.print(ypr[1] * 180/M_PI);
Serial.print(":");
Serial.print(ypr[2] * 180/M_PI);
Serial.print(":");
Serial.print(aaWorld.x);
Serial.print(":");
Serial.print(aaWorld.y);
Serial.print(":");
Serial.print(aaWorld.z);
Serial.println(":");
}
}
答案 0 :(得分:3)
pyqtgraph模块是一个很好的解决方案。它非常快速和简单。
这是新代码:
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
import serial
app = QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()
data = [0]
raw=serial.Serial('COM9', 115200)
def update():
global curve, data
line = raw.readline()
if ("hand" in line):
line=line.split(":")
if len(line)==8:
data.append(float(line[4]))
xdata = np.array(data, dtype='float64')
curve.setData(xdata)
app.processEvents()
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
答案 1 :(得分:1)
在Matplotlib中实时绘图?你要购买的所有电力都要付出代价。自动缩放,自动化等等......这一切都需要时间!
很多更快的解决方案是使用Gtk等工具包,并使用GooCanvas等画布自己进行绘图。
但是,您可以 做一些事情来加快您的绘图速度。