summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhoekstar <hoekstar@cb376a5e-1013-0410-a455-b6b1f9ac8223>2007-12-18 23:06:28 +0000
committerhoekstar <hoekstar@cb376a5e-1013-0410-a455-b6b1f9ac8223>2007-12-18 23:06:28 +0000
commit69ce5eaeb5d8bfc3c5939eb85177ecffde883dce (patch)
tree43d0cc731046a8a6a1347726b7de702c98094b9d
parentc717df114d77852eff5338ed5fd1cbff268e57c1 (diff)
downloadreprap-backup-69ce5eaeb5d8bfc3c5939eb85177ecffde883dce.tar.gz
reprap-backup-69ce5eaeb5d8bfc3c5939eb85177ecffde883dce.zip
worked on the temperature lookup script a bit.
git-svn-id: https://reprap.svn.sourceforge.net/svnroot/reprap@1192 cb376a5e-1013-0410-a455-b6b1f9ac8223
-rw-r--r--trunk/users/hoeken/arduino/utilities/createTemperatureLookup.py148
1 files changed, 106 insertions, 42 deletions
diff --git a/trunk/users/hoeken/arduino/utilities/createTemperatureLookup.py b/trunk/users/hoeken/arduino/utilities/createTemperatureLookup.py
index ae304fa4..2d219047 100644
--- a/trunk/users/hoeken/arduino/utilities/createTemperatureLookup.py
+++ b/trunk/users/hoeken/arduino/utilities/createTemperatureLookup.py
@@ -1,52 +1,116 @@
+#!/usr/bin/python
+#
# Creates a C code lookup table for doing ADC to temperature conversion
# on a microcontroller
# based on: http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
+"""Thermistor Value Lookup Table Generator
+
+Generates lookup to temperature values for use in a microcontroller in C format based on:
+http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html
+
+The main use is for Arduino programs that read data from the circuit board described here:
+http://make.rrrf.org/ts-1.0
+
+Usage: python createTemperatureLookup.py [options]
+
+Options:
+ -h, --help show this help
+ --r0=... thermistor rating where # is the ohm rating of the thermistor at t0 (eg: 10K = 10000)
+ --t0=... thermistor temp rating where # is the temperature in Celsuis to get r0 (from your datasheet)
+ --beta=... thermistor beta rating. see http://reprap.org/bin/view/Main/MeasuringThermistorBeta
+ --r1=... R1 rating where # is the ohm rating of R1 (eg: 10K = 10000)
+ --r2=... R2 rating where # is the ohm rating of R2 (eg: 10K = 10000)
+ --num-temps=... the number of temperature points to calculate (default: 20)
+ --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
+"""
from math import *
+import sys
+import getopt
class Thermistor:
- "Class to do the thermistor maths"
- def __init__(self, r0, t0, beta, r1, r2):
- self.r0 = r0 # stated resistance, e.g. 10K
- self.t0 = t0 + 273.15 # temperature at stated resistance, e.g. 25C
- self.beta = beta # stated beta, e.g. 3500
- self.vadc = 5.0 # ADC reference
- self.vcc = 5.0 # supply voltage to potential divider
- self.vs = r1 * self.vcc / (r1 + r2) # effective bias voltage
- self.rs = r1 * r2 / (r1 + r2) # effective bias impedance
- self.k = r0 * exp(-beta / self.t0) # constant part of calculation
-
- def temp(self,adc):
- "Convert ADC reading into a temperature in Celcius"
- v = adc * self.vadc / 1024 # convert the 10 bit ADC value to a voltage
- r = self.rs * v / (self.vs - v) # resistance of thermistor
- return (self.beta / log(r / self.k)) - 273.15 # temperature
-
- def setting(self, t):
- "Convert a temperature into a ADC value"
- r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
- v = self.vs * r / (self.rs + r) # the voltage at the potential divider
- return round(v / self.vadc * 1024) # the ADC reading
-
-t = Thermistor(10000, 25, 3947, 1000000, 1000)
-
-adcs = [1, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 130, 150, 190, 220, 250, 300]
-first = 1
-
-print "#define NUMTEMPS ", len(adcs)
-
-print "short temptable[NUMTEMPS][2] = {"
-print "// { adc , temp }"
-for adc in adcs:
- if first==1:
- first = 0
- else:
- print ","
- print " {", adc, ", ", int(t.temp(adc)), "}",
-print
-print "};"
+ "Class to do the thermistor maths"
+ def __init__(self, r0, t0, beta, r1, r2):
+ self.r0 = r0 # stated resistance, e.g. 10K
+ self.t0 = t0 + 273.15 # temperature at stated resistance, e.g. 25C
+ self.beta = beta # stated beta, e.g. 3500
+ self.vadc = 5.0 # ADC reference
+ self.vcc = 5.0 # supply voltage to potential divider
+ self.k = r0 * exp(-beta / self.t0) # constant part of calculation
+
+ if r1 > 0:
+ self.vs = r1 * self.vcc / (r1 + r2) # effective bias voltage
+ self.rs = r1 * r2 / (r1 + r2) # effective bias impedance
+ else:
+ self.vs = self.vcc # effective bias voltage
+ self.rs = r2 # effective bias impedance
+
+ def temp(self,adc):
+ "Convert ADC reading into a temperature in Celcius"
+ v = adc * self.vadc / 1024 # convert the 10 bit ADC value to a voltage
+ r = self.rs * v / (self.vs - v) # resistance of thermistor
+ return (self.beta / log(r / self.k)) - 273.15 # temperature
+
+ def setting(self, t):
+ "Convert a temperature into a ADC value"
+ r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
+ v = self.vs * r / (self.rs + r) # the voltage at the potential divider
+ return round(v / self.vadc * 1024) # the ADC reading
+
+def main(argv):
+
+ r0 = 10000;
+ t0 = 25;
+ beta = 3947;
+ r1 = 680;
+ r2 = 1600;
+ num_temps = int(20);
+ max_adc = int(1023);
+ try:
+ opts, args = getopt.getopt(argv, "h", ["help", "r0=", "t0=", "beta=", "r1=", "r2="])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(2)
+
+ for opt, arg in opts:
+ if opt in ("-h", "--help"):
+ usage()
+ sys.exit()
+ elif opt == "--r0":
+ r0 = int(arg)
+ elif opt == "--t0":
+ t0 = int(arg)
+ elif opt == "--beta":
+ beta = int(arg)
+ elif opt == "--r1":
+ r1 = int(arg)
+ elif opt == "--r2":
+ r2 = int(arg)
+
+ increment = int(max_adc/(num_temps-1));
+ t = Thermistor(r0, t0, beta, r1, r2)
+
+ adcs = range(1, max_adc, increment);
+# adcs = [1, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 130, 150, 190, 220, 250, 300]
+ first = 1
+
+ print "#define NUMTEMPS ", len(adcs)
+
+ print "short temptable[NUMTEMPS][2] = {"
+ print "// { adc , temp }"
+ for adc in adcs:
+ if first==1:
+ first = 0
+ else:
+ print ","
+ print " {", adc, ", ", int(t.temp(adc)), "}",
+ print
+ print "};"
-#print t.setting(250)
-#print t.temp(200)
+def usage():
+ print __doc__
+
+if __name__ == "__main__":
+ main(sys.argv[1:]) \ No newline at end of file