-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphingProcess.py
More file actions
186 lines (151 loc) · 7.03 KB
/
graphingProcess.py
File metadata and controls
186 lines (151 loc) · 7.03 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
# pyqt_plot_process.py
import sys
import time
import multiprocessing
from PyQt5 import QtWidgets, QtCore
import pyqtgraph as pg
from scipy.interpolate import interp1d
import numpy as np
class GraphingProcess(QtWidgets.QMainWindow):
def __init__(self, signalGraph, signalZero, micrometerQueue, powermeter1Queue, powermeter2Queue, phaseQueue, strainQueue):
super().__init__()
self.setWindowTitle("Multiple Subplots in a 2x2 Grid")
self.micrometerQueue = micrometerQueue
self.powermeter1Queue = powermeter1Queue
self.powermeter2Queue = powermeter2Queue
self.phaseQueue = phaseQueue
self.strainQueue = strainQueue
self.signalGraph = signalGraph
self.signalZero = signalZero
self.initial = 0.0
# Create a container widget and a QGridLayout
container = QtWidgets.QWidget()
layout = QtWidgets.QGridLayout(container)
# Subplot 1
self.plot1 = pg.PlotWidget()
self.plot1.setLabel('left', "Micrometer Position (mm)")
self.plot1.setLabel('bottom', "Time")
self.curve1 = self.plot1.plot([], [], pen='r')
layout.addWidget(self.plot1, 0, 0) # Row 0, Col 0
# Subplot 2
self.plot2 = pg.PlotWidget()
self.plot2.setLabel('left', "Power 1 (W)")
self.plot2.setLabel('bottom', "Time")
self.curve2 = self.plot2.plot([], [], pen='g')
layout.addWidget(self.plot2, 0, 1) # Row 0, Col 1
# Subplot 3
self.plot3 = pg.PlotWidget()
self.plot3.setLabel('left', "Power 2 (W)")
self.plot3.setLabel('bottom', "Time")
self.curve3 = self.plot3.plot([], [], pen='b')
layout.addWidget(self.plot3, 0, 2) # Row 1, Col 0
# Subplot 4
self.plot4 = pg.PlotWidget()
self.plot4.setLabel('left', "Phase")
self.plot4.setLabel('bottom', "Strain")
self.curve4 = self.plot4.plot([], [], pen='y')
layout.addWidget(self.plot4, 1, 1) # Row 1, Col 1
self.plot5 = pg.PlotWidget()
self.plot5.setLabel('left', "force")
self.plot5.setLabel('bottom', "Power difference Normalized") # unitless or “ε” if you prefer
self.curve5 = self.plot5.plot([], [], pen='orange')
layout.addWidget(self.plot5, 1, 2)
self.plot6 = pg.PlotWidget()
self.plot6.setLabel('left', "Power Difference (W)")
self.plot6.setLabel('bottom', "Time")
self.curve6 = self.plot6.plot([], [], pen='orange')
layout.addWidget(self.plot6, 1, 0) # Row 1, Col 1
# Put the container into the MainWindow
self.setCentralWidget(container)
# Data storage for each curve
self.x_data1 = []
self.y_data1 = []
self.x_data2 = []
self.y_data2 = []
self.x_data3 = []
self.y_data3 = []
self.x_data4 = []
self.y_data4 = []
self.force = []
self.diff = []
self.sum = []
# Timer to poll queue
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.check_queue)
self.timer.start(100) # 10 Hz
#NOTE: POWERMETER IS ACTUALLY POLLING QUITE SLOW.... so i made it update only when powermeter updates and the graph is way slower.
def check_queue(self):
"""Pull all items from the queue and update the plot."""
self.diff = None
while not self.signalGraph.empty():
if self.signalGraph.get_nowait() == "STOP":
self.plot1.clear()
self.plot2.clear()
self.plot3.clear()
self.plot4.clear()
self.plot5.clear()
self.plot6.clear()
# Recreate the curve items
self.curve1 = self.plot1.plot([], [], pen='r')
self.curve2 = self.plot2.plot([], [], pen='g')
self.curve3 = self.plot3.plot([], [], pen='b')
self.curve4 = self.plot4.plot([], [], pen='m')
self.curve5 = self.plot5.plot([], [], pen='y')
self.curve6 = self.plot6.plot([],[], pen='w')
# Clear data buffers
self.x_data1.clear()
self.y_data1.clear()
self.x_data2.clear()
self.y_data2.clear()
self.x_data3.clear()
self.y_data3.clear()
self.x_data4.clear()
self.y_data4.clear()
while not self.micrometerQueue.empty():
x, y = self.micrometerQueue.get_nowait()
self.x_data1.append(x)
self.y_data1.append(y)
if len(self.y_data1) <= 5 and len(self.y_data1) > 0:
self.initialMicrometerPosition = self.y_data1[0]
print("INITIAL HEIGHT IN GRAPHING PROCESS: ", self.initialMicrometerPosition)
while not self.powermeter1Queue.empty():
x,y = self.powermeter1Queue.get_nowait()
self.x_data2.append(x)
self.y_data2.append(y)
while not self.powermeter2Queue.empty():
x,y = self.powermeter2Queue.get_nowait()
self.x_data3.append(x)
self.y_data3.append(y)
if not self.phaseQueue == None and not self.strainQueue == None:
while not self.phaseQueue.empty() and not self.strainQueue.empty():
self.y_data4.append(self.phaseQueue.get_nowait())
print("PLOT")
self.x_data4.append(self.strainQueue.get_nowait())
while not self.strainQueue.empty():
x=self.strainQueue.get_nowait()
while not self.phaseQueue.empty():
y=self.phaseQueue.get_nowait()
if self.x_data3 and self.y_data3 and self.y_data2:
interp_func = interp1d(self.x_data3, self.y_data3, kind='linear', fill_value='extrapolate')
aligned_pow2 = interp_func(self.x_data2)
self.diff = self.y_data2 - aligned_pow2
self.sum = self.y_data2 + aligned_pow2
while not self.signalZero.empty():
self.curve5.clear()
self.initial = np.mean(self.diff/self.sum)
self.signalZero.get_nowait()
if self.diff is not None and self.sum is not None:
self.curve6.setData(self.x_data2, self.diff/self.sum)
if(self.initial != 0.0):
force = 5334.41 * (self.diff/self.sum - (self.initial))
print("INITIAL: ", self.initial)
self.curve5.setData(self.x_data2, force)
self.curve1.setData(self.x_data1, self.y_data1)
self.curve2.setData(self.x_data2, self.y_data2)
self.curve3.setData(self.x_data3, self.y_data3)
self.curve4.setData(self.x_data4, self.y_data4)
def run_pyqt_app(signalGraph, signalZero, micrometerQueue, powermeter1Queue, powermeter2Queue, phaseQueue, strainQueue):
app = QtWidgets.QApplication(sys.argv)
window = GraphingProcess(signalGraph, signalZero, micrometerQueue, powermeter1Queue, powermeter2Queue, phaseQueue, strainQueue)
window.show()
sys.exit(app.exec_())