瀏覽代碼

Added new temperature lookup table creation tool. This one uses stein hart-hart equations.

You need 3 temperature resistance:combinations. This gives a better result then using beta.
Erik vd Zalm 12 年之前
父節點
當前提交
5cd7a90b32
共有 1 個檔案被更改,包括 138 行新增0 行删除
  1. 138
    0
      Marlin/createTemperatureLookupMarlin.py

+ 138
- 0
Marlin/createTemperatureLookupMarlin.py 查看文件

@@ -0,0 +1,138 @@
1
+#!/usr/bin/python
2
+#
3
+# Creates a C code lookup table for doing ADC to temperature conversion
4
+# on a microcontroller
5
+# based on: http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
6
+"""Thermistor Value Lookup Table Generator
7
+
8
+Generates lookup to temperature values for use in a microcontroller in C format based on: 
9
+http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
10
+
11
+The main use is for Arduino programs that read data from the circuit board described here:
12
+http://make.rrrf.org/ts-1.0
13
+
14
+Usage: python createTemperatureLookup.py [options]
15
+
16
+Options:
17
+  -h, --help        show this help
18
+  --rp=...          pull-up resistor
19
+  --t0=ttt:rrr      low temperature temperature:resistance point (around 25C)
20
+  --t1=ttt:rrr      middle temperature temperature:resistance point (around 150C)
21
+  --t2=ttt:rrr      high temperature temperature:resistance point (around 250C)
22
+  --num-temps=...   the number of temperature points to calculate (default: 20)
23
+"""
24
+
25
+from math import *
26
+import sys
27
+import getopt
28
+
29
+class Thermistor:
30
+    "Class to do the thermistor maths"
31
+    def __init__(self, rp, t1, r1, t2, r2, t3, r3):
32
+        t1 = t1 + 273.15               # low temperature (25C)
33
+        r1 = r1                        # resistance at low temperature
34
+        t2 = t2 + 273.15               # middle temperature (150C)
35
+        r2 = r2                        # resistance at middle temperature
36
+        t3 = t3 + 273.15               # high temperature (250C)
37
+        r3 = r3                        # resistance at high temperature
38
+        self.rp = rp                   # pull-up resistance
39
+        self.vadc = 5.0                # ADC reference
40
+        self.vcc = 5.0                 # supply voltage to potential divider
41
+        a1 = log(r1)
42
+        a2 = log(r2)
43
+        a3 = log(r3)
44
+        z = a1 - a2
45
+        y = a1 - a3
46
+        x = 1/t1 - 1/t2
47
+        w = 1/t1 - 1/t3
48
+        v = pow(a1,3) - pow(a2,3)
49
+        u = pow(a1,3) - pow(a3,3)
50
+        c3 = (x-z*w/y)/(v-z*u/y)
51
+        c2 = (x-c3*v)/z
52
+        c1 = 1/t1-c3*pow(a1,3)-c2*a1
53
+        self.c1 = c1
54
+        self.c2 = c2
55
+        self.c3 = c3
56
+
57
+    def temp(self,adc):
58
+        "Convert ADC reading into a temperature in Celcius"
59
+        v = adc * self.vadc / (1024 * 16)   # convert the 10 bit ADC value to a voltage
60
+        r = self.rp * v / (self.vcc - v)    # resistance of thermistor
61
+        lnr = log(r)
62
+        Tinv = self.c1 + (self.c2*lnr) + (self.c3*pow(lnr,3))
63
+        return (1/Tinv) - 273.15        # temperature
64
+
65
+    def adc(self,temp):
66
+        "Convert temperature into a ADC reading"
67
+        y = (self.c1 - (1/(temp+273.15))) / (2*self.c3)
68
+	x = sqrt(pow(self.c2 / (3*self.c3),3) + pow(y,2))
69
+        r = exp(pow(x-y,1.0/3) - pow(x+y,1.0/3)) # resistance of thermistor
70
+        return (r / (self.rp + r)) * (1024*16)
71
+
72
+def main(argv):
73
+
74
+    rp = 4700;
75
+    t1 = 25;
76
+    r1 = 100000;
77
+    t2 = 150;
78
+    r2 = 1641.9;
79
+    t3 = 250;
80
+    r3 = 226.15;
81
+    num_temps = int(36);
82
+    
83
+    try:
84
+        opts, args = getopt.getopt(argv, "h", ["help", "rp=", "t1=", "t2=", "t3=", "num-temps="])
85
+    except getopt.GetoptError:
86
+        usage()
87
+        sys.exit(2)
88
+        
89
+    for opt, arg in opts:
90
+        if opt in ("-h", "--help"):
91
+            usage()
92
+            sys.exit()
93
+        elif opt == "--rp":
94
+            rp = int(arg)
95
+        elif opt == "--t1":
96
+            arg =  arg.split(':')
97
+            t1 = float( arg[0])
98
+            r1 = float( arg[1])
99
+        elif opt == "--t2":
100
+            arg =  arg.split(':')
101
+            t2 = float( arg[0])
102
+            r2 = float( arg[1])
103
+        elif opt == "--t3":
104
+            arg =  arg.split(':')
105
+            t3 = float( arg[0])
106
+            r3 = float( arg[1])
107
+        elif opt == "--num-temps":
108
+            num_temps =  int(arg)
109
+
110
+    max_adc = (1024 * 16) - 1
111
+    min_temp = 0
112
+    max_temp = 350
113
+    increment = int(max_adc/(num_temps-1));
114
+            
115
+    t = Thermistor(rp, t1, r1, t2, r2, t3, r3)
116
+    tmp = (min_temp - max_temp) / (num_temps-1)
117
+    print tmp
118
+    temps = range(max_temp, min_temp + tmp, tmp);
119
+
120
+    print "// Thermistor lookup table for Marlin"
121
+    print "// ./createTemperatureLookup.py --rp=%s --t1=%s:%s --t2=%s:%s --t3=%s:%s --num-temps=%s" % (rp, t1, r1, t2, r2, t3, r3, num_temps)
122
+    print "#define NUMTEMPS %s" % (len(temps))
123
+    print "short temptable[NUMTEMPS][2] = {"
124
+
125
+    counter = 0
126
+    for temp in temps:
127
+        counter = counter +1
128
+        if counter == len(temps):
129
+            print "   {%s, %s}" % (int(t.adc(temp)), temp)
130
+        else:
131
+            print "   {%s, %s}," % (int(t.adc(temp)), temp)
132
+    print "};"
133
+    
134
+def usage():
135
+    print __doc__
136
+
137
+if __name__ == "__main__":
138
+    main(sys.argv[1:])

Loading…
取消
儲存