mpu6050使用(3)—(获取数据)

上机位 Test2.py

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import serial
import serial.tools.list_ports


win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('Scrolling Plots Mode 1')

p1 = win.addPlot()
data1 = np.random.normal(size=300)
data2 = np.random.normal(size=300)
data3 = np.random.normal(size=300)
dataf1 = np.random.normal(size=300)
dataf2 = np.random.normal(size=300)
dataf3 = np.random.normal(size=300)
curve1 = p1.plot(data1)
curve2 = p1.plot(data2)
curve3 = p1.plot(data3)
curvef1 = p1.plot(dataf1)
curvef2 = p1.plot(dataf2)
curvef3 = p1.plot(dataf3)


def get_serial_port(_keywords='COM'):
port_list = list(serial.tools.list_ports.comports())
for port in port_list:
if _keywords in str(port):
port_name = str(port).split('-')[0]
return port_name.strip()


def get_imu():
ser = serial.Serial(port=get_serial_port('Silicon'), baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
imu_return = str(ser.readline())
return imu_return


def update1():
global data1, data2, data3, ptr1
data1[:-1] = data1[1:] # shift data in the array one sample left
data2[:-1] = data2[1:]
data3[:-1] = data3[1:]
dataf1[:-1] = dataf1[1:] # shift data in the array one sample left
dataf2[:-1] = dataf2[1:]
dataf3[:-1] = dataf3[1:]
a = get_imu().strip()
a = a.split(",")
try:
g1 = a[0].split(":")
g2 = a[1].split(":")
g3 = a[2].split(":")
a1 = a[4].split(":")
a2 = a[5].split(":")
a3 = a[6].split(":")
c1 = str(a1[0])
c2 = str(a2[0])
c3 = str(a3[0])
f1 = str(g1[0])
f1 = f1.split("{")
f2 = str(g2[0])
f3 = str(g3[0])
if f1[1] == "'GyZ'":
d1 = int(g1[1])
dataf1[-1] = d1
if f2 == " 'GyY'":
d2 = int(g2[1])
dataf2[-1] = d2
elif f2 == " 'GyX'":
d3 = int(g2[1])
dataf3[-1] = d3
if f3 == " 'GyX'":
d3 = int(g3[1])
dataf3[-1] = d3
if c1 == " 'AcZ'":
b1 = int(a1[1])
data1[-1] = b1
elif c1 == " 'AcY'":
b2 = int(a1[1])
data2[-1] = b2
elif c1 == " 'AcX'":
b3 = int(a1[1])
data3[-1] = b3
if c2 == " 'AcY'":
b2 = int(a2[1])
data2[-1] = b2
elif c2 == " 'AcX'":
b3 = int(a2[1])
data3[-1] = b3
if c3 == " 'AcX'":
b3 = a3[1].split("}")
b3 = b3[0]
data3[-1] = b3
except:
pass
curve1.setData(data1)
curve2.setData(data2)
curve3.setData(data3)
curvef1.setData(dataf1)
curvef2.setData(dataf2)
curvef3.setData(dataf3)

timer = pg.QtCore.QTimer()
timer.timeout.connect(update1)
timer.start(50)


## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()