|
@@ -23,68 +23,60 @@ import sys
|
23
|
23
|
import getopt
|
24
|
24
|
|
25
|
25
|
"Constants"
|
|
26
|
+ZERO = 273.15 # zero point of Kelvin scale
|
|
27
|
+VADC = 5 # ADC voltage
|
|
28
|
+VCC = 5 # supply voltage
|
26
|
29
|
ARES = pow(2,10) # 10 Bit ADC resolution
|
|
30
|
+VSTEP = VADC / ARES # ADC voltage resolution
|
27
|
31
|
TMIN = 0 # lowest temperature in table
|
28
|
32
|
TMAX = 350 # highest temperature in table
|
29
|
33
|
|
30
|
34
|
class Thermistor:
|
31
|
35
|
"Class to do the thermistor maths"
|
32
|
36
|
def __init__(self, rp, t1, r1, t2, r2, t3, r3):
|
33
|
|
- t1 = t1 + 273.15 # low temperature (25C)
|
34
|
|
- r1 = r1 # resistance at low temperature
|
35
|
|
- t2 = t2 + 273.15 # middle temperature (150C)
|
36
|
|
- r2 = r2 # resistance at middle temperature
|
37
|
|
- t3 = t3 + 273.15 # high temperature (250C)
|
38
|
|
- r3 = r3 # resistance at high temperature
|
39
|
|
- self.rp = rp # pull-up resistance
|
40
|
|
- self.vadc = 5.0 # ADC reference
|
41
|
|
- self.vcc = 5.0 # supply voltage to potential divider
|
42
|
|
- a1 = log(r1)
|
43
|
|
- a2 = log(r2)
|
44
|
|
- a3 = log(r3)
|
45
|
|
- z = a1 - a2
|
46
|
|
- y = a1 - a3
|
47
|
|
- x = 1/t1 - 1/t2
|
48
|
|
- w = 1/t1 - 1/t3
|
49
|
|
- v = pow(a1,3) - pow(a2,3)
|
50
|
|
- u = pow(a1,3) - pow(a3,3)
|
51
|
|
- c3 = (x-z*w/y)/(v-z*u/y)
|
52
|
|
- c2 = (x-c3*v)/z
|
53
|
|
- c1 = 1/t1-c3*pow(a1,3)-c2*a1
|
54
|
|
- self.c1 = c1
|
55
|
|
- self.c2 = c2
|
56
|
|
- self.c3 = c3
|
57
|
|
-
|
58
|
|
- def res(self,adc):
|
|
37
|
+ l1 = log(r1)
|
|
38
|
+ l2 = log(r2)
|
|
39
|
+ l3 = log(r3)
|
|
40
|
+ y1 = 1.0 / (t1 + ZERO) # adjust scale
|
|
41
|
+ y2 = 1.0 / (t2 + ZERO)
|
|
42
|
+ y3 = 1.0 / (t3 + ZERO)
|
|
43
|
+ x = (y2 - y1) / (l2 - l1)
|
|
44
|
+ y = (y3 - y1) / (l3 - l1)
|
|
45
|
+ c = (y - x) / ((l3 - l2) * (l1 + l2 + l3))
|
|
46
|
+ b = x - c * (pow(l1,2) + pow(l2,2) + l1*l2)
|
|
47
|
+ a = y1 - (b + pow(l1,2)*c)*l1
|
|
48
|
+ self.c1 = a # Steinhart-Hart coefficients
|
|
49
|
+ self.c2 = b
|
|
50
|
+ self.c3 = c
|
|
51
|
+ self.rp = rp # pull-up resistance
|
|
52
|
+
|
|
53
|
+ def res(self, adc):
|
59
|
54
|
"Convert ADC reading into a resolution"
|
60
|
55
|
res = self.temp(adc)-self.temp(adc+1)
|
61
|
56
|
return res
|
62
|
57
|
|
63
|
|
- def v(self,adc):
|
|
58
|
+ def v(self, adc):
|
64
|
59
|
"Convert ADC reading into a Voltage"
|
65
|
|
- v = adc * self.vadc / (1024 ) # convert the 10 bit ADC value to a voltage
|
66
|
|
- return v
|
|
60
|
+ return adc * VSTEP # convert the 10 bit ADC value to a voltage
|
67
|
61
|
|
68
|
|
- def r(self,adc):
|
|
62
|
+ def r(self, adc):
|
69
|
63
|
"Convert ADC reading into a resistance in Ohms"
|
70
|
|
- v = adc * self.vadc / (1024 ) # convert the 10 bit ADC value to a voltage
|
71
|
|
- r = self.rp * v / (self.vcc - v) # resistance of thermistor
|
|
64
|
+ r = self.rp * self.v(adc) / (VCC - self.v(adc)) # resistance of thermistor
|
72
|
65
|
return r
|
73
|
66
|
|
74
|
|
- def temp(self,adc):
|
|
67
|
+ def temp(self, adc):
|
75
|
68
|
"Convert ADC reading into a temperature in Celcius"
|
76
|
|
- v = adc * self.vadc / (1024 ) # convert the 10 bit ADC value to a voltage
|
77
|
|
- r = self.rp * v / (self.vcc - v) # resistance of thermistor
|
|
69
|
+ r = self.rp * self.v(adc) / (VCC - self.v(adc)) # resistance of thermistor
|
78
|
70
|
lnr = log(r)
|
79
|
71
|
Tinv = self.c1 + (self.c2*lnr) + (self.c3*pow(lnr,3))
|
80
|
|
- return (1/Tinv) - 273.15 # temperature
|
|
72
|
+ return (1/Tinv) - ZERO # temperature
|
81
|
73
|
|
82
|
|
- def adc(self,temp):
|
|
74
|
+ def adc(self, temp):
|
83
|
75
|
"Convert temperature into a ADC reading"
|
84
|
|
- y = (self.c1 - (1/(temp+273.15))) / (2*self.c3)
|
85
|
|
- x = sqrt(pow(self.c2 / (3*self.c3),3) + pow(y,2))
|
86
|
|
- r = exp(pow(x-y,1.0/3) - pow(x+y,1.0/3)) # resistance of thermistor
|
87
|
|
- return (r / (self.rp + r)) * (1024)
|
|
76
|
+ x = (self.c1 - (1.0 / (temp+ZERO))) / (2*self.c3)
|
|
77
|
+ y = sqrt(pow(self.c2 / (3*self.c3),3) + pow(x,2))
|
|
78
|
+ r = exp(pow(y-x,1.0/3) - pow(y+x,1.0/3)) # resistance of thermistor
|
|
79
|
+ return (r / (self.rp + r)) * ARES
|
88
|
80
|
|
89
|
81
|
def main(argv):
|
90
|
82
|
"Default values"
|