createTemperatureLookup.py

Sat, 07 Nov 2015 13:24:46 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:24:46 +0100
changeset 1
b584642d4f58
parent 0
2c8ba1964db7
permissions
-rw-r--r--

several modifications to support laser enable - still needs cleanup

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 #!/usr/bin/python
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 #
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 # Creates a C code lookup table for doing ADC to temperature conversion
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4 # on a microcontroller
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 # based on: http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 """Thermistor Value Lookup Table Generator
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 Generates lookup to temperature values for use in a microcontroller in C format based on:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 The main use is for Arduino programs that read data from the circuit board described here:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 http://make.rrrf.org/ts-1.0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 Usage: python createTemperatureLookup.py [options]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 Options:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 -h, --help show this help
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 --r0=... thermistor rating where # is the ohm rating of the thermistor at t0 (eg: 10K = 10000)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 --t0=... thermistor temp rating where # is the temperature in Celsuis to get r0 (from your datasheet)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 --beta=... thermistor beta rating. see http://reprap.org/bin/view/Main/MeasuringThermistorBeta
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 --r1=... R1 rating where # is the ohm rating of R1 (eg: 10K = 10000)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22 --r2=... R2 rating where # is the ohm rating of R2 (eg: 10K = 10000)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23 --num-temps=... the number of temperature points to calculate (default: 20)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 --max-adc=... the max ADC reading to use. if you use R1, it limits the top value for the thermistor circuit, and thus the possible range of ADC values
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 """
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 from math import *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 import sys
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 import getopt
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 class Thermistor:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 "Class to do the thermistor maths"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 def __init__(self, r0, t0, beta, r1, r2):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 self.r0 = r0 # stated resistance, e.g. 10K
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 self.t0 = t0 + 273.15 # temperature at stated resistance, e.g. 25C
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 self.beta = beta # stated beta, e.g. 3500
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 self.vadc = 5.0 # ADC reference
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 self.vcc = 5.0 # supply voltage to potential divider
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 self.k = r0 * exp(-beta / self.t0) # constant part of calculation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 if r1 > 0:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 self.vs = r1 * self.vcc / (r1 + r2) # effective bias voltage
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 self.rs = r1 * r2 / (r1 + r2) # effective bias impedance
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 else:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 self.vs = self.vcc # effective bias voltage
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 self.rs = r2 # effective bias impedance
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 def temp(self,adc):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 "Convert ADC reading into a temperature in Celcius"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 v = adc * self.vadc / 1024 # convert the 10 bit ADC value to a voltage
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 r = self.rs * v / (self.vs - v) # resistance of thermistor
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 return (self.beta / log(r / self.k)) - 273.15 # temperature
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 def setting(self, t):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 "Convert a temperature into a ADC value"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 v = self.vs * r / (self.rs + r) # the voltage at the potential divider
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 return round(v / self.vadc * 1024) # the ADC reading
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 def main(argv):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 r0 = 10000;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 t0 = 25;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 beta = 3947;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 r1 = 680;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 r2 = 1600;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 num_temps = int(20);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 try:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 opts, args = getopt.getopt(argv, "h", ["help", "r0=", "t0=", "beta=", "r1=", "r2="])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71 except getopt.GetoptError:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 usage()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 sys.exit(2)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 for opt, arg in opts:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 if opt in ("-h", "--help"):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 usage()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 sys.exit()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 elif opt == "--r0":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80 r0 = int(arg)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81 elif opt == "--t0":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 t0 = int(arg)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 elif opt == "--beta":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 beta = int(arg)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85 elif opt == "--r1":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 r1 = int(arg)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 elif opt == "--r2":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 r2 = int(arg)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 if r1:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 max_adc = int(1023 * r1 / (r1 + r2));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 else:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 max_adc = 1023
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 increment = int(max_adc/(num_temps-1));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 t = Thermistor(r0, t0, beta, r1, r2)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 adcs = range(1, max_adc, increment);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 # adcs = [1, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 130, 150, 190, 220, 250, 300]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 first = 1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102 print "// Thermistor lookup table for RepRap Temperature Sensor Boards (http://make.rrrf.org/ts)"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 print "// Made with createTemperatureLookup.py (http://svn.reprap.org/trunk/reprap/firmware/Arduino/utilities/createTemperatureLookup.py)"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 print "// ./createTemperatureLookup.py --r0=%s --t0=%s --r1=%s --r2=%s --beta=%s --max-adc=%s" % (r0, t0, r1, r2, beta, max_adc)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 print "// r0: %s" % (r0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 print "// t0: %s" % (t0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 print "// r1: %s" % (r1)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 print "// r2: %s" % (r2)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 print "// beta: %s" % (beta)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 print "// max adc: %s" % (max_adc)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 print "#define NUMTEMPS %s" % (len(adcs))
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112 print "short temptable[NUMTEMPS][2] = {"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 counter = 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 for adc in adcs:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 counter = counter +1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 if counter == len(adcs):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 print " {%s, %s}" % (adc, int(t.temp(adc)))
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 else:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 print " {%s, %s}," % (adc, int(t.temp(adc)))
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 print "};"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 def usage():
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 print __doc__
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 if __name__ == "__main__":
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 main(sys.argv[1:])

mercurial