My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createTemperatureLookupMarlin.py 5.4KB

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