-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththermocouple.py
132 lines (106 loc) · 3.95 KB
/
thermocouple.py
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
#!/usr/bin/python3
"""
Plots the temperature of a thermocouple connected to Channel 1
of the Attys. The 2nd Channel is reading the cold junction temperature
from the internal thermometer. Connect the negative input of channel
one also to ground so that it's not floating. Wiring: Thermo+ -> Ch1+,
Thermo- -> Ch1-,GND.
Requires pyqtgraph.
"""
import threading
from time import sleep
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import pyattyscomm as c
# create a global QT application object
app = QtGui.QApplication(sys.argv)
ch1 = c.AttysComm.INDEX_Analogue_channel_1
ch2 = c.AttysComm.INDEX_Analogue_channel_2
# Offset of channel 1 (thermocouple) in V
# This is the voltage measures if short circuited.
ch1Offset = 0.0006
# signals to all threads in endless loops that we'd like to run these
running = True
class QtTemperaturePlot:
def __init__(self):
self.lock = threading.Lock()
self.hot = 20
self.cold = 20
self.bufferlen = 1000
self.plt = pg.PlotWidget()
self.plt.setYRange(0,1000)
self.plt.setXRange(0,self.bufferlen/250)
self.plt.setLabel('left',u"T/\u00b0C")
self.plt.setLabel('bottom',u"t/sec")
self.curve = self.plt.plot()
self.curve.setPen(pg.mkPen(color='#00FF00', width=3))
self.data = []
self.w = QtGui.QWidget()
self.w.setStyleSheet("background: #191919;color: #DDDDDD; color: white; border: 0px;")
self.label = QtGui.QLabel()
self.label.setStyleSheet("font-size:48px")
self.temperatureThermo = QtGui.QLineEdit('20C')
self.temperatureThermo.setStyleSheet("font-size:48px; color: yellow; background: black;")
self.temperatureThermo.setFixedSize(300,70)
self.temperatureThermo.setReadOnly(True);
self.temperatureCold = QtGui.QLineEdit('20C')
self.temperatureCold.setReadOnly(True);
self.temperatureCold.setStyleSheet("font-size:12px; color: #808080;")
self.mainlayout = QtGui.QGridLayout()
self.mainlayout.addWidget(self.label,0,0)
self.mainlayout.addWidget(self.temperatureThermo,1,0)
self.mainlayout.addWidget(self.temperatureCold,2,0)
self.mainlayout.addWidget(self.plt, 0, 1, 4, 1)
self.w.setLayout(self.mainlayout)
self.w.show()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(100)
def update(self):
self.temperatureThermo.setText(u"{:10.1f}\u00b0C".format(self.hot))
self.temperatureCold.setText(u"Cold junction = {:2.1f}\u00b0C".format(self.cold))
self.data=self.data[-self.bufferlen:]
if self.data:
self.lock.acquire()
self.curve.setData(np.linspace(0,self.bufferlen/250,len(self.data)),np.hstack(self.data))
self.lock.release()
def addData(self,hot,cold):
self.hot = hot
self.cold = cold
self.lock.acquire()
self.data.append(hot)
self.lock.release()
def getDataThread(qtTemperaturePlot):
while running:
# loop as fast as we can to empty the kernel buffer
while c.hasSampleAvailable():
sample = c.getSampleFromBuffer()
cold = c.phys2temperature(sample[ch2])
hot = cold + (sample[ch1] - ch1Offset) / 39E-6
qtTemperaturePlot.addData(hot,cold)
# let Python do other stuff and sleep a bit
sleep(0.1)
s = c.AttysScan()
s.scan()
c = s.getAttysComm(0)
if not c:
print("No Attys connected and/or paired")
sys.exit()
plot = QtTemperaturePlot()
c.setAdc1_mux_index(c.ADC_MUX_TEMPERATURE)
# start data acquisition
c.start()
# create a thread which gets the data from the USB-DUX
t = threading.Thread(target=getDataThread,args=(plot,))
# start the thread getting the data
t.start()
# showing all the windows
app.exec_()
# Signal the Thread to stop
running = False
c.quit()
# Waiting for the thread to stop
t.join()
print("finished")