createTemperatureLookupMarlin.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python
  2. """Thermistor Value Lookup Table Generator
  3. Generates lookup to temperature values for use in a microcontroller in C format based on:
  4. http://en.wikipedia.org/wiki/Steinhart-Hart_equation
  5. The main use is for Arduino programs that read data from the circuit board described here:
  6. http://reprap.org/wiki/Temperature_Sensor_v2.0
  7. Usage: python createTemperatureLookupMarlin.py [options]
  8. Options:
  9. -h, --help show this help
  10. --rp=... pull-up resistor
  11. --t1=ttt:rrr low temperature temperature:resistance point (around 25 degC)
  12. --t2=ttt:rrr middle temperature temperature:resistance point (around 150 degC)
  13. --t3=ttt:rrr high temperature temperature:resistance point (around 250 degC)
  14. --num-temps=... the number of temperature points to calculate (default: 36)
  15. """
  16. from __future__ import print_function
  17. from __future__ import division
  18. from math import *
  19. import sys
  20. import getopt
  21. "Constants"
  22. ZERO = 273.15 # zero point of Kelvin scale
  23. VADC = 5 # ADC voltage
  24. VCC = 5 # supply voltage
  25. ARES = pow(2,10) # 10 Bit ADC resolution
  26. VSTEP = VADC / ARES # ADC voltage resolution
  27. TMIN = 0 # lowest temperature in table
  28. TMAX = 350 # highest temperature in table
  29. class Thermistor:
  30. "Class to do the thermistor maths"
  31. def __init__(self, rp, t1, r1, t2, r2, t3, r3):
  32. l1 = log(r1)
  33. l2 = log(r2)
  34. l3 = log(r3)
  35. y1 = 1.0 / (t1 + ZERO) # adjust scale
  36. y2 = 1.0 / (t2 + ZERO)
  37. y3 = 1.0 / (t3 + ZERO)
  38. x = (y2 - y1) / (l2 - l1)
  39. y = (y3 - y1) / (l3 - l1)
  40. c = (y - x) / ((l3 - l2) * (l1 + l2 + l3))
  41. b = x - c * (l1**2 + l2**2 + l1*l2)
  42. a = y1 - (b + l1**2 *c)*l1
  43. if c < 0:
  44. print("//////////////////////////////////////////////////////////////////////////////////////")
  45. print("// WARNING: negative coefficient 'c'! Something may be wrong with the measurements! //")
  46. print("//////////////////////////////////////////////////////////////////////////////////////")
  47. c = -c
  48. self.c1 = a # Steinhart-Hart coefficients
  49. self.c2 = b
  50. self.c3 = c
  51. self.rp = rp # pull-up resistance
  52. def resol(self, adc):
  53. "Convert ADC reading into a resolution"
  54. res = self.temp(adc)-self.temp(adc+1)
  55. return res
  56. def voltage(self, adc):
  57. "Convert ADC reading into a Voltage"
  58. return adc * VSTEP # convert the 10 bit ADC value to a voltage
  59. def resist(self, adc):
  60. "Convert ADC reading into a resistance in Ohms"
  61. r = self.rp * self.voltage(adc) / (VCC - self.voltage(adc)) # resistance of thermistor
  62. return r
  63. def temp(self, adc):
  64. "Convert ADC reading into a temperature in Celcius"
  65. l = log(self.resist(adc))
  66. Tinv = self.c1 + self.c2*l + self.c3* l**3 # inverse temperature
  67. return (1/Tinv) - ZERO # temperature
  68. def adc(self, temp):
  69. "Convert temperature into a ADC reading"
  70. x = (self.c1 - (1.0 / (temp+ZERO))) / (2*self.c3)
  71. y = sqrt((self.c2 / (3*self.c3))**3 + x**2)
  72. r = exp((y-x)**(1.0/3) - (y+x)**(1.0/3))
  73. return (r / (self.rp + r)) * ARES
  74. def main(argv):
  75. "Default values"
  76. t1 = 25 # low temperature in Kelvin (25 degC)
  77. r1 = 100000 # resistance at low temperature (10 kOhm)
  78. t2 = 150 # middle temperature in Kelvin (150 degC)
  79. r2 = 1641.9 # resistance at middle temperature (1.6 KOhm)
  80. t3 = 250 # high temperature in Kelvin (250 degC)
  81. r3 = 226.15 # resistance at high temperature (226.15 Ohm)
  82. rp = 4700; # pull-up resistor (4.7 kOhm)
  83. num_temps = 36; # number of entries for look-up table
  84. try:
  85. opts, args = getopt.getopt(argv, "h", ["help", "rp=", "t1=", "t2=", "t3=", "num-temps="])
  86. except getopt.GetoptError as err:
  87. print(str(err))
  88. usage()
  89. sys.exit(2)
  90. for opt, arg in opts:
  91. if opt in ("-h", "--help"):
  92. usage()
  93. sys.exit()
  94. elif opt == "--rp":
  95. rp = int(arg)
  96. elif opt == "--t1":
  97. arg = arg.split(':')
  98. t1 = float(arg[0])
  99. r1 = float(arg[1])
  100. elif opt == "--t2":
  101. arg = arg.split(':')
  102. t2 = float(arg[0])
  103. r2 = float(arg[1])
  104. elif opt == "--t3":
  105. arg = arg.split(':')
  106. t3 = float(arg[0])
  107. r3 = float(arg[1])
  108. elif opt == "--num-temps":
  109. num_temps = int(arg)
  110. t = Thermistor(rp, t1, r1, t2, r2, t3, r3)
  111. increment = int((ARES-1)/(num_temps-1));
  112. step = (TMIN-TMAX) / (num_temps-1)
  113. low_bound = t.temp(ARES-1);
  114. up_bound = t.temp(1);
  115. min_temp = int(TMIN if TMIN > low_bound else low_bound)
  116. max_temp = int(TMAX if TMAX < up_bound else up_bound)
  117. temps = list(range(max_temp, TMIN+step, step));
  118. print("// Thermistor lookup table for Marlin")
  119. 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))
  120. print("// Steinhart-Hart Coefficients: a=%.15g, b=%.15g, c=%.15g " % (t.c1, t.c2, t.c3))
  121. print("// Theoretical limits of thermistor: %.2f to %.2f degC" % (low_bound, up_bound))
  122. print()
  123. print("const short temptable[][2] PROGMEM = {")
  124. for temp in temps:
  125. adc = t.adc(temp)
  126. print(" { OV(%7.2f), %4s }%s // v=%.3f\tr=%.3f\tres=%.3f degC/count" % (adc , temp, \
  127. ',' if temp != temps[-1] else ' ', \
  128. t.voltage(adc), \
  129. t.resist( adc), \
  130. t.resol( adc) \
  131. ))
  132. print("};")
  133. def usage():
  134. print(__doc__)
  135. if __name__ == "__main__":
  136. main(sys.argv[1:])