GeekIMU/4.Software/VisualizeTools 1.0/visualization/realtime_plot.py

68 lines
2.6 KiB
Python
Raw Normal View History

# visualization/realtime_plot.py
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtWidgets
from collections import deque
class RealTimePlot:
def __init__(self, data_source):
self.app = QtWidgets.QApplication([])
self.win = pg.GraphicsLayoutWidget(size=(1200, 800))
# 初始化绘图区域
self._init_plots()
# 数据源连接
self.data_source = data_source
self.data_buffer = {
'time': deque(maxlen=500),
'roll': deque(maxlen=500),
'pitch': deque(maxlen=500),
'yaw': deque(maxlen=500),
'acc_x': deque(maxlen=500),
'acc_y': deque(maxlen=500),
'gyro_z': deque(maxlen=500)
}
# 定时刷新
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(50)
def _init_plots(self):
"""三窗格波形显示配置"""
# 姿态角显示
self.attitude_plot = self.win.addPlot(title=" 姿态角")
self.attitude_plot.addLegend()
self.roll_curve = self.attitude_plot.plot(pen='r', name='Roll')
self.pitch_curve = self.attitude_plot.plot(pen='g', name='Pitch')
# 加速度计显示
self.win.nextRow()
self.acc_plot = self.win.addPlot(title=" 加速度")
self.acc_x_curve = self.acc_plot.plot(pen=(255,0,0), name='X')
self.acc_y_curve = self.acc_plot.plot(pen=(0,255,0), name='Y')
# 陀螺仪显示
self.win.nextRow()
self.gyro_plot = self.win.addPlot(title=" 角速度")
self.gyro_z_curve = self.gyro_plot.plot(pen=(0,0,255), name='Z')
def update(self):
data = self.data_source.get_latest_data()
if data is not None:
self.data_buffer['time'].append(data['timestamp'])
self.data_buffer['roll'].append(data['attitude']['roll'])
self.data_buffer['pitch'].append(data['attitude']['pitch'])
self.data_buffer['yaw'].append(data['attitude']['yaw'])
self.data_buffer['acc_x'].append(data['acc']['x'])
self.data_buffer['gyro_z'].append(data['gyro']['z'])
self.roll_curve.setData(self.data_buffer['time'], self.data_buffer['roll'])
# self.pitch_curve.setData(self.data_buffer['time'], self.data_buffer['pitch'])
self.acc_x_curve.setData(self.data_buffer['time'], self.data_buffer['pitch'])
self.gyro_z_curve.setData(self.data_buffer['time'], self.data_buffer['yaw'])
def run(self):
self.win.show()
self.app.exec_()