-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperature_plotter.py
More file actions
179 lines (147 loc) · 5.56 KB
/
Temperature_plotter.py
File metadata and controls
179 lines (147 loc) · 5.56 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
import time
import serial.tools.list_ports
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# ----------------------------
# 1. Device connection (run once)
# ----------------------------
def connect_devices():
devices = serial.tools.list_ports.comports()
ctc100A = None
ctc100B = None
model224 = None
model372 = None
for device in devices:
if 'FT230X' in device.description:
if 'DK0CDLQP' in device.serial_number:
ctc100B = CTC100Device(address=device.device, name='CTC100B')
elif 'DK0CDKFB' in device.serial_number:
ctc100A = CTC100Device(address=device.device, name='CTC100A')
elif '224' in device.description:
model224 = LakeShore224Device(port=device.device, name='LakeshoreModel224')
elif '372' in device.description:
model372 = LakeShore372Device(port=device.device, name='LakeshoreModel372')
connected = {
"CTC100A": ctc100A,
"CTC100B": ctc100B,
"LakeshoreModel224": model224,
"LakeshoreModel372": model372
}
# Remove None entries
return {k: v for k, v in connected.items() if v is not None}
# ----------------------------
# 2. Poll temperatures each frame
# ----------------------------
def read_temperatures(devices):
readings = {}
if "CTC100A" in devices:
dev = devices["CTC100A"]
readings["CTC100A"] = {
"4switch": dev.get_temperature("4switch"),
"4pump": dev.get_temperature("4pump"),
"3switch": dev.get_temperature("3switch"),
"3pump": dev.get_temperature("3pump"),
}
if "CTC100B" in devices:
dev = devices["CTC100B"]
readings["CTC100B"] = {
"4switch": dev.get_temperature("4switch"),
"4pump": dev.get_temperature("4pump"),
"3switch": dev.get_temperature("3switch"),
"3pump": dev.get_temperature("3pump"),
}
if "LakeshoreModel224" in devices:
dev = devices["LakeshoreModel224"]
readings["LakeshoreModel224"] = {
"C1": dev.get_temperature("C1"),
"B": dev.get_temperature("B"),
"C2": dev.get_temperature("C2"),
"D1": dev.get_temperature("D1"),
"A": dev.get_temperature("A"),
"D2": dev.get_temperature("D2"),
"D3": dev.get_temperature("D3"),
}
if "LakeshoreModel372" in devices:
dev = devices["LakeshoreModel372"]
readings["LakeshoreModel372"] = {
"1": dev.get_temperature("1"),
"A": dev.get_temperature("A"),
}
return readings
# ----------------------------
# 3. Setup plots
# ----------------------------
def setup_plots(device_data):
num_devices = len(device_data)
nrows = (num_devices + 1) // 2
ncols = 2 if num_devices > 1 else 1
fig, axes = plt.subplots(nrows, ncols, figsize=(12, nrows*3))
if num_devices == 1:
axes = [axes]
else:
axes = axes.flatten()
lines = {}
data = {}
for ax, (dev_name, sensors) in zip(axes, device_data.items()):
ax.set_title(dev_name)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Temperature (K)")
ax.set_ylim(0, 300) # adjust for expected range
lines[dev_name] = []
data[dev_name] = {ch: [] for ch in sensors.keys()}
data[dev_name]['times'] = []
for ch in sensors.keys():
(line,) = ax.plot([], [], lw=2, label=ch)
lines[dev_name].append(line)
ax.legend()
plt.tight_layout()
return fig, axes, lines, data
# ----------------------------
# 4. Animate updates
# ----------------------------
def main():
devices = connect_devices()
if not devices:
print("No devices found. Exiting.")
return
# Initial read to get channels
initial_data = read_temperatures(devices)
fig, axes, lines, data = setup_plots(initial_data)
# Sliding window in seconds (None = full history)
window_seconds = 60
start_time = time.time()
def update(frame):
current_time = time.time() - start_time
readings = read_temperatures(devices)
for dev_name, sensors in readings.items():
data[dev_name]['times'].append(current_time)
for i, (ch, temp) in enumerate(sensors.items()):
data[dev_name][ch].append(temp)
# Apply sliding window if set
if window_seconds:
# Find the index where time > current_time - window_seconds
times = data[dev_name]['times']
start_idx = 0
for j, t in enumerate(times):
if t >= current_time - window_seconds:
start_idx = j
break
xdata = times[start_idx:]
ydata = data[dev_name][ch][start_idx:]
else:
xdata = data[dev_name]['times']
ydata = data[dev_name][ch]
lines[dev_name][i].set_data(xdata, ydata)
# Update x-axis
ax = axes[list(devices.keys()).index(dev_name)]
if window_seconds:
ax.set_xlim(max(0, current_time - window_seconds), current_time)
else:
ax.set_xlim(0, current_time)
ax.relim()
ax.autoscale_view(scalex=False, scaley=True)
return [l for sub in lines.values() for l in sub]
ani = animation.FuncAnimation(fig, update, interval=2000, blit=False)
plt.show()
if __name__ == "__main__":
main()