mccoyn
-
Python,
pasted
on Feb 14:
|
#!/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.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 max_adc(self):
"Find the maximum value of adc with a resistance greater than 0"
return 1024 * self.vs / self.vadc - 1
def voltage(self,adc):
"Convert ADC reading into a temperature in Celcius"
return adc * self.vadc / 1024 # convert the 10 bit ADC value to a voltage
def resistance(self,adc):
"Convert ADC reading into a temperature in Celcius"
v = self.voltage(adc)
r = self.rs * v / (self.vs - v)
return r # resistance of thermistor
def temp(self,adc):
"Convert ADC reading into a temperature in Celcius"
return (self.beta / log(self.resistance(adc) / 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 = 12120;
t0 = 17.8;
beta = 3796.577472;
r1 = 0;
r2 = 1465;
num_temps = int(20);
max_adc = int(1022);
try:
opts, args = getopt.getopt(argv, "h", ["help", "r0=", "t0=", "beta=", "r1=", "r2=", "max-adc="])
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)
elif opt == "--max-adc":
max_adc = int(arg)
t = Thermistor(r0, t0, beta, r1, r2)
if t.max_adc() < max_adc:
max_adc = int(t.max_adc())
increment = (max_adc - 2)/float(num_temps-1)
adcs = [int(x * increment + 2) for x in range (0, num_temps)]
first = 1
print "// Thermistor lookup table for RepRap Temperature Sensor Boards (http://make.rrrf.org/ts)"
print "// Made with createTemperatureLookup.py (http://svn.reprap.org/trunk/reprap/firmware/Arduino/utilities/createTemperatureLookup.py)"
print "// ./createTemperatureLookup.py --r0=%s --t0=%s --r1=%s --r2=%s --beta=%s --max-adc=%s" % (r0, t0, r1, r2, beta, max_adc)
print "// r0: %s" % (r0)
print "// t0: %s" % (t0)
print "// r1: %s" % (r1)
print "// r2: %s" % (r2)
print "// beta: %s" % (beta)
print "// max adc: %s" % (max_adc)
print "#define NUMTEMPS %s" % (len(adcs))
print "short temptable[NUMTEMPS][2] = {"
counter = 0
for adc in adcs:
counter = counter +1
if counter == len(adcs):
comma = " "
else:
comma = ","
resistance = t.resistance(adc)
voltage = t.voltage(adc)
heat = 1000 * voltage * voltage / resistance
t0 = t.temp(adc-1)
t1 = t.temp(adc)
t2 = t.temp(adc+1)
dt = (t0 - t2) / 2
ddt = (t0 - t1) - (t1 - t2)
print " {%4d, %4d}%s // %.2f V %.2f mW %.3f C/step %.5f C/(step^2)" % (adc, int(t1), comma, voltage, heat, dt, ddt)
print "};"
def usage():
print __doc__
if __name__ == "__main__":
main(sys.argv[1:])
|
Output:
|
// Thermistor lookup table for RepRap Temperature Sensor Boards (http://make.rrrf.org/ts)
// Made with createTemperatureLookup.py (http://svn.reprap.org/trunk/reprap/firmware/Arduino/utilities/createTemperatureLookup.py)
// ./createTemperatureLookup.py --r0=12120 --t0=17.8 --r1=0 --r2=1465 --beta=3796.577472 --max-adc=1022
// r0: 12120
// t0: 17.8
// r1: 0
// r2: 1465
// beta: 3796.577472
// max adc: 1022
#define NUMTEMPS 20
short temptable[NUMTEMPS][2] = {
{ 2, 534}, // 0.01 V 0.03 mW 102.154 C/step 75.69317 C/(step^2)
{ 55, 197}, // 0.27 V 0.87 mW 1.121 C/step 0.02457 C/(step^2)
{ 109, 157}, // 0.53 V 1.62 mW 0.502 C/step 0.00523 C/(step^2)
{ 163, 136}, // 0.80 V 2.28 mW 0.322 C/step 0.00211 C/(step^2)
{ 216, 121}, // 1.05 V 2.84 mW 0.241 C/step 0.00111 C/(step^2)
{ 270, 109}, // 1.32 V 3.31 mW 0.194 C/step 0.00066 C/(step^2)
{ 324, 100}, // 1.58 V 3.69 mW 0.166 C/step 0.00042 C/(step^2)
{ 377, 92}, // 1.84 V 3.97 mW 0.147 C/step 0.00028 C/(step^2)
{ 431, 84}, // 2.10 V 4.16 mW 0.135 C/step 0.00019 C/(step^2)
{ 485, 77}, // 2.37 V 4.25 mW 0.127 C/step 0.00012 C/(step^2)
{ 538, 70}, // 2.63 V 4.26 mW 0.122 C/step 0.00006 C/(step^2)
{ 592, 64}, // 2.89 V 4.16 mW 0.120 C/step 0.00001 C/(step^2)
{ 646, 57}, // 3.15 V 3.97 mW 0.121 C/step -0.00004 C/(step^2)
{ 699, 51}, // 3.41 V 3.70 mW 0.125 C/step -0.00011 C/(step^2)
{ 753, 44}, // 3.68 V 3.32 mW 0.133 C/step -0.00020 C/(step^2)
{ 807, 36}, // 3.94 V 2.85 mW 0.148 C/step -0.00036 C/(step^2)
{ 860, 28}, // 4.20 V 2.30 mW 0.174 C/step -0.00066 C/(step^2)
{ 914, 17}, // 4.46 V 1.64 mW 0.227 C/step -0.00146 C/(step^2)
{ 968, 2}, // 4.73 V 0.88 mW 0.377 C/step -0.00532 C/(step^2)
{1022, -52} // 4.99 V 0.03 mW 6.975 C/step -3.22975 C/(step^2)
};
|
|