-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_realtime_qt.py
More file actions
187 lines (154 loc) · 7.27 KB
/
plot_realtime_qt.py
File metadata and controls
187 lines (154 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
High-performance real-time plotter for capacitive flicker sensor data using PyQtGraph
Usage: sudo make monitor | python3 plot_realtime_qt.py
Install dependencies:
pip3 install pyqtgraph PyQt5 numpy
"""
import sys
import numpy as np
import pyqtgraph as pg
from PyQt5 import QtWidgets, QtCore
from collections import deque
import csv
from datetime import datetime
class FlickerSensorPlotter(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# Configure PyQtGraph for better performance
pg.setConfigOptions(antialias=False) # Disable antialiasing for speed
pg.setConfigOption('background', 'k') # Black background
pg.setConfigOption('foreground', 'w') # White foreground
# Data storage
self.max_points = 2000
self.raw_data = deque(maxlen=self.max_points)
self.avg_data = deque(maxlen=self.max_points)
self.hp_data = deque(maxlen=self.max_points)
self.zero_cross_data = deque(maxlen=self.max_points)
self.timestamps = deque(maxlen=self.max_points)
self.x_data = deque(maxlen=self.max_points)
# Update control
self.update_counter = 0
self.update_interval = 32 # Update every 32 samples for even better performance
# CSV logging
self.csv_filename = f"Flicker_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
self.csv_file = open(self.csv_filename, 'w', newline='')
self.csv_writer = csv.writer(self.csv_file)
self.csv_writer.writerow(['timestamp', 'raw', 'avg', 'hp', 'zero_cross'])
# Sample counter
self.sample_count = 0
self.setup_ui()
self.setup_timer()
def setup_ui(self):
self.setWindowTitle('Capacitive Flicker Sensor - Real-time Data')
self.setGeometry(100, 100, 1200, 800)
# Central widget
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
layout = QtWidgets.QVBoxLayout(central_widget)
# Create plot widgets
self.plot_widget = pg.GraphicsLayoutWidget()
layout.addWidget(self.plot_widget)
# Create three subplots
self.plot1 = self.plot_widget.addPlot(row=0, col=0, title="Raw & Filtered Signals")
self.plot1.setLabel('left', 'ADC Value')
self.plot1.addLegend()
self.plot1.showGrid(x=True, y=True, alpha=0.3)
self.plot2 = self.plot_widget.addPlot(row=1, col=0, title="Flicker Detection Signal")
self.plot2.setLabel('left', 'Flicker Delta')
self.plot2.addLegend()
self.plot2.showGrid(x=True, y=True, alpha=0.3)
self.plot2.addLine(y=0, pen=pg.mkPen('r', style=QtCore.Qt.DashLine))
self.plot3 = self.plot_widget.addPlot(row=2, col=0, title="Zero Crossing Detection")
self.plot3.setLabel('left', 'Zero Cross')
self.plot3.setLabel('bottom', 'Samples')
self.plot3.addLegend()
self.plot3.showGrid(x=True, y=True, alpha=0.3)
self.plot3.setYRange(-0.1, 1.1) # Zero crossing is binary 0/1
# Create plot curves
self.raw_curve = self.plot1.plot(pen=pg.mkPen('c', width=1), name='Raw')
self.avg_curve = self.plot1.plot(pen=pg.mkPen('y', width=1), name='Avg')
self.hp_curve = self.plot2.plot(pen=pg.mkPen('g', width=1), name='Flicker Signal')
self.zero_cross_curve = self.plot3.plot(pen=pg.mkPen('m', width=2), name='Zero Cross', symbol='o', symbolSize=4)
# Status bar
self.status_bar = self.statusBar()
self.status_bar.showMessage("Ready - waiting for data...")
def setup_timer(self):
# Timer to read from stdin
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.read_data)
self.timer.start(1) # Read every 1ms for maximum responsiveness
def read_data(self):
try:
# Non-blocking read from stdin
import select
if select.select([sys.stdin], [], [], 0.0)[0]:
line = sys.stdin.readline()
if line.strip() and not line.startswith('Capacitive'):
parts = line.strip().split('\t')
if len(parts) == 4:
timestamp = datetime.now()
raw = int(parts[0])
avg = int(parts[1])
hp = int(parts[2])
zero_cross = int(parts[3])
# Store data
self.sample_count += 1
self.timestamps.append(timestamp)
self.raw_data.append(raw)
self.avg_data.append(avg)
self.hp_data.append(hp)
self.zero_cross_data.append(zero_cross)
self.x_data.append(self.sample_count)
# Log to CSV
self.csv_writer.writerow([timestamp.isoformat(), raw, avg, hp, zero_cross])
# Update plots periodically
self.update_counter += 1
if self.update_counter >= self.update_interval:
self.update_plots()
self.update_counter = 0
except Exception as e:
print(f"Error reading data: {e}", file=sys.stderr)
def update_plots(self):
if len(self.raw_data) < 2:
return
try:
# Convert to numpy arrays for faster plotting
x = np.array(self.x_data)
raw = np.array(self.raw_data)
avg = np.array(self.avg_data) >> 5 # Shift right by 5 to match your filtering
hp = np.array(self.hp_data)
zero_cross = np.array(self.zero_cross_data)
# Update curves
self.raw_curve.setData(x, raw)
self.avg_curve.setData(x, avg)
self.hp_curve.setData(x, hp)
self.zero_cross_curve.setData(x, zero_cross)
# Update status
sample_rate = len(self.raw_data) / max(1, (self.timestamps[-1] - self.timestamps[0]).total_seconds()) if len(self.timestamps) > 1 else 0
self.status_bar.showMessage(
f"Samples: {self.sample_count} | Rate: {sample_rate:.1f} Hz | "
f"Raw: {raw[-1]} | Flicker: {hp[-1]} | Zero Cross: {zero_cross[-1]} | File: {self.csv_filename}"
)
except Exception as e:
print(f"Error updating plots: {e}", file=sys.stderr)
def closeEvent(self, event):
"""Handle window close event"""
self.csv_file.close()
print(f"\nData saved to {self.csv_filename}")
print(f"Total samples collected: {self.sample_count}")
event.accept()
def main():
# Create Qt application
app = QtWidgets.QApplication(sys.argv)
# Create and show the plotter
plotter = FlickerSensorPlotter()
plotter.show()
try:
# Run the application
sys.exit(app.exec_())
except KeyboardInterrupt:
print("\nStopping data collection...")
plotter.close()
if __name__ == "__main__":
main()