1 |
ron |
1 |
#!/usr/bin/python
|
|
|
2 |
"""Thermistor Value Lookup Table Generator
|
|
|
3 |
|
|
|
4 |
Generates lookup to temperature values for use in a microcontroller in C format based on:
|
|
|
5 |
http://en.wikipedia.org/wiki/Steinhart-Hart_equation
|
|
|
6 |
|
|
|
7 |
The main use is for Arduino programs that read data from the circuit board described here:
|
|
|
8 |
http://reprap.org/wiki/Temperature_Sensor_v2.0
|
|
|
9 |
|
|
|
10 |
Usage: python createTemperatureLookupMarlin.py [options]
|
|
|
11 |
|
|
|
12 |
Options:
|
|
|
13 |
-h, --help show this help
|
|
|
14 |
--rp=... pull-up resistor
|
|
|
15 |
--t1=ttt:rrr low temperature temperature:resistance point (around 25 degC)
|
|
|
16 |
--t2=ttt:rrr middle temperature temperature:resistance point (around 150 degC)
|
|
|
17 |
--t3=ttt:rrr high temperature temperature:resistance point (around 250 degC)
|
|
|
18 |
--num-temps=... the number of temperature points to calculate (default: 36)
|
|
|
19 |
"""
|
|
|
20 |
|
|
|
21 |
from math import *
|
|
|
22 |
import sys
|
|
|
23 |
import getopt
|
|
|
24 |
|
|
|
25 |
"Constants"
|
|
|
26 |
ZERO = 273.15 # zero point of Kelvin scale
|
|
|
27 |
VADC = 5 # ADC voltage
|
|
|
28 |
VCC = 5 # supply voltage
|
|
|
29 |
ARES = pow(2,10) # 10 Bit ADC resolution
|
|
|
30 |
VSTEP = VADC / ARES # ADC voltage resolution
|
|
|
31 |
TMIN = 0 # lowest temperature in table
|
|
|
32 |
TMAX = 350 # highest temperature in table
|
|
|
33 |
|
|
|
34 |
class Thermistor:
|
|
|
35 |
"Class to do the thermistor maths"
|
|
|
36 |
def __init__(self, rp, t1, r1, t2, r2, t3, r3):
|
|
|
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 * (l1**2 + l2**2 + l1*l2)
|
|
|
47 |
a = y1 - (b + l1**2 *c)*l1
|
|
|
48 |
|
|
|
49 |
if c < 0:
|
|
|
50 |
print "//////////////////////////////////////////////////////////////////////////////////////"
|
|
|
51 |
print "// WARNING: negative coefficient 'c'! Something may be wrong with the measurements! //"
|
|
|
52 |
print "//////////////////////////////////////////////////////////////////////////////////////"
|
|
|
53 |
c = -c
|
|
|
54 |
self.c1 = a # Steinhart-Hart coefficients
|
|
|
55 |
self.c2 = b
|
|
|
56 |
self.c3 = c
|
|
|
57 |
self.rp = rp # pull-up resistance
|
|
|
58 |
|
|
|
59 |
def resol(self, adc):
|
|
|
60 |
"Convert ADC reading into a resolution"
|
|
|
61 |
res = self.temp(adc)-self.temp(adc+1)
|
|
|
62 |
return res
|
|
|
63 |
|
|
|
64 |
def voltage(self, adc):
|
|
|
65 |
"Convert ADC reading into a Voltage"
|
|
|
66 |
return adc * VSTEP # convert the 10 bit ADC value to a voltage
|
|
|
67 |
|
|
|
68 |
def resist(self, adc):
|
|
|
69 |
"Convert ADC reading into a resistance in Ohms"
|
|
|
70 |
r = self.rp * self.voltage(adc) / (VCC - self.voltage(adc)) # resistance of thermistor
|
|
|
71 |
return r
|
|
|
72 |
|
|
|
73 |
def temp(self, adc):
|
|
|
74 |
"Convert ADC reading into a temperature in Celcius"
|
|
|
75 |
l = log(self.resist(adc))
|
|
|
76 |
Tinv = self.c1 + self.c2*l + self.c3* l**3 # inverse temperature
|
|
|
77 |
return (1/Tinv) - ZERO # temperature
|
|
|
78 |
|
|
|
79 |
def adc(self, temp):
|
|
|
80 |
"Convert temperature into a ADC reading"
|
|
|
81 |
x = (self.c1 - (1.0 / (temp+ZERO))) / (2*self.c3)
|
|
|
82 |
y = sqrt((self.c2 / (3*self.c3))**3 + x**2)
|
|
|
83 |
r = exp((y-x)**(1.0/3) - (y+x)**(1.0/3))
|
|
|
84 |
return (r / (self.rp + r)) * ARES
|
|
|
85 |
|
|
|
86 |
def main(argv):
|
|
|
87 |
"Default values"
|
|
|
88 |
t1 = 25 # low temperature in Kelvin (25 degC)
|
|
|
89 |
r1 = 100000 # resistance at low temperature (10 kOhm)
|
|
|
90 |
t2 = 150 # middle temperature in Kelvin (150 degC)
|
|
|
91 |
r2 = 1641.9 # resistance at middle temperature (1.6 KOhm)
|
|
|
92 |
t3 = 250 # high temperature in Kelvin (250 degC)
|
|
|
93 |
r3 = 226.15 # resistance at high temperature (226.15 Ohm)
|
|
|
94 |
rp = 4700; # pull-up resistor (4.7 kOhm)
|
|
|
95 |
num_temps = 36; # number of entries for look-up table
|
|
|
96 |
|
|
|
97 |
try:
|
|
|
98 |
opts, args = getopt.getopt(argv, "h", ["help", "rp=", "t1=", "t2=", "t3=", "num-temps="])
|
|
|
99 |
except getopt.GetoptError as err:
|
|
|
100 |
print str(err)
|
|
|
101 |
usage()
|
|
|
102 |
sys.exit(2)
|
|
|
103 |
|
|
|
104 |
for opt, arg in opts:
|
|
|
105 |
if opt in ("-h", "--help"):
|
|
|
106 |
usage()
|
|
|
107 |
sys.exit()
|
|
|
108 |
elif opt == "--rp":
|
|
|
109 |
rp = int(arg)
|
|
|
110 |
elif opt == "--t1":
|
|
|
111 |
arg = arg.split(':')
|
|
|
112 |
t1 = float(arg[0])
|
|
|
113 |
r1 = float(arg[1])
|
|
|
114 |
elif opt == "--t2":
|
|
|
115 |
arg = arg.split(':')
|
|
|
116 |
t2 = float(arg[0])
|
|
|
117 |
r2 = float(arg[1])
|
|
|
118 |
elif opt == "--t3":
|
|
|
119 |
arg = arg.split(':')
|
|
|
120 |
t3 = float(arg[0])
|
|
|
121 |
r3 = float(arg[1])
|
|
|
122 |
elif opt == "--num-temps":
|
|
|
123 |
num_temps = int(arg)
|
|
|
124 |
|
|
|
125 |
t = Thermistor(rp, t1, r1, t2, r2, t3, r3)
|
|
|
126 |
increment = int((ARES-1)/(num_temps-1));
|
|
|
127 |
step = (TMIN-TMAX) / (num_temps-1)
|
|
|
128 |
low_bound = t.temp(ARES-1);
|
|
|
129 |
up_bound = t.temp(1);
|
|
|
130 |
min_temp = int(TMIN if TMIN > low_bound else low_bound)
|
|
|
131 |
max_temp = int(TMAX if TMAX < up_bound else up_bound)
|
|
|
132 |
temps = range(max_temp, TMIN+step, step);
|
|
|
133 |
|
|
|
134 |
print "// Thermistor lookup table for Marlin"
|
|
|
135 |
print "// ./createTemperatureLookupMarlin.py --rp=%s --t1=%s:%s --t2=%s:%s --t3=%s:%s --num-temps=%s" % (rp, t1, r1, t2, r2, t3, r3, num_temps)
|
|
|
136 |
print "// Steinhart-Hart Coefficients: a=%.15g, b=%.15g, c=%.15g " % (t.c1, t.c2, t.c3)
|
|
|
137 |
print "// Theoretical limits of thermistor: %.2f to %.2f degC" % (low_bound, up_bound)
|
|
|
138 |
print
|
|
|
139 |
print "const short temptable[][2] PROGMEM = {"
|
|
|
140 |
|
|
|
141 |
for temp in temps:
|
|
|
142 |
adc = t.adc(temp)
|
|
|
143 |
print " { OV(%7.2f), %4s }%s // v=%.3f\tr=%.3f\tres=%.3f degC/count" % (adc , temp, \
|
|
|
144 |
',' if temp != temps[-1] else ' ', \
|
|
|
145 |
t.voltage(adc), \
|
|
|
146 |
t.resist( adc), \
|
|
|
147 |
t.resol( adc) \
|
|
|
148 |
)
|
|
|
149 |
print "};"
|
|
|
150 |
|
|
|
151 |
def usage():
|
|
|
152 |
print __doc__
|
|
|
153 |
|
|
|
154 |
if __name__ == "__main__":
|
|
|
155 |
main(sys.argv[1:])
|