My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

createTemperatureLookupMarlin.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/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://make.rrrf.org/ts-1.0
  7. Usage: python createTemperatureLookup.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 math import *
  17. import sys
  18. import getopt
  19. "Constants"
  20. ZERO = 273.15 # zero point of Kelvin scale
  21. VADC = 5 # ADC voltage
  22. VCC = 5 # supply voltage
  23. ARES = pow(2,10) # 10 Bit ADC resolution
  24. VSTEP = VADC / ARES # ADC voltage resolution
  25. TMIN = 0 # lowest temperature in table
  26. TMAX = 350 # highest temperature in table
  27. class Thermistor:
  28. "Class to do the thermistor maths"
  29. def __init__(self, rp, t1, r1, t2, r2, t3, r3):
  30. l1 = log(r1)
  31. l2 = log(r2)
  32. l3 = log(r3)
  33. y1 = 1.0 / (t1 + ZERO) # adjust scale
  34. y2 = 1.0 / (t2 + ZERO)
  35. y3 = 1.0 / (t3 + ZERO)
  36. x = (y2 - y1) / (l2 - l1)
  37. y = (y3 - y1) / (l3 - l1)
  38. c = (y - x) / ((l3 - l2) * (l1 + l2 + l3))
  39. b = x - c * (pow(l1,2) + pow(l2,2) + l1*l2)
  40. a = y1 - (b + pow(l1,2)*c)*l1
  41. self.c1 = a # Steinhart-Hart coefficients
  42. self.c2 = b
  43. self.c3 = c
  44. self.rp = rp # pull-up resistance
  45. def resol(self, adc):
  46. "Convert ADC reading into a resolution"
  47. res = self.temp(adc)-self.temp(adc+1)
  48. return res
  49. def voltage(self, adc):
  50. "Convert ADC reading into a Voltage"
  51. return adc * VSTEP # convert the 10 bit ADC value to a voltage
  52. def resist(self, adc):
  53. "Convert ADC reading into a resistance in Ohms"
  54. r = self.rp * self.voltage(adc) / (VCC - self.voltage(adc)) # resistance of thermistor
  55. return r
  56. def temp(self, adc):
  57. "Convert ADC reading into a temperature in Celcius"
  58. l = log(self.resist(adc))
  59. Tinv = self.c1 + self.c2*l + self.c3*pow(l,3) # inverse temperature
  60. return (1/Tinv) - ZERO # temperature
  61. def adc(self, temp):
  62. "Convert temperature into a ADC reading"
  63. x = (self.c1 - (1.0 / (temp+ZERO))) / (2*self.c3)
  64. y = sqrt(pow(self.c2 / (3*self.c3),3) + pow(x,2))
  65. r = exp(pow(y-x,1.0/3) - pow(y+x,1.0/3)) # resistance of thermistor
  66. return (r / (self.rp + r)) * ARES
  67. def main(argv):
  68. "Default values"
  69. t1 = 25 # low temperature in Kelvin (25 degC)
  70. r1 = 100000 # resistance at low temperature (10 kOhm)
  71. t2 = 150 # middle temperature in Kelvin (150 degC)
  72. r2 = 1641.9 # resistance at middle temperature (1.6 KOhm)
  73. t3 = 250 # high temperature in Kelvin (250 degC)
  74. r3 = 226.15 # resistance at high temperature (226.15 Ohm)
  75. rp = 4700; # pull-up resistor (4.7 kOhm)
  76. num_temps = 36; # number of entries for look-up table
  77. try:
  78. opts, args = getopt.getopt(argv, "h", ["help", "rp=", "t1=", "t2=", "t3=", "num-temps="])
  79. except getopt.GetoptError as err:
  80. print str(err)
  81. usage()
  82. sys.exit(2)
  83. for opt, arg in opts:
  84. if opt in ("-h", "--help"):
  85. usage()
  86. sys.exit()
  87. elif opt == "--rp":
  88. rp = int(arg)
  89. elif opt == "--t1":
  90. arg = arg.split(':')
  91. t1 = float(arg[0])
  92. r1 = float(arg[1])
  93. elif opt == "--t2":
  94. arg = arg.split(':')
  95. t2 = float(arg[0])
  96. r2 = float(arg[1])
  97. elif opt == "--t3":
  98. arg = arg.split(':')
  99. t3 = float(arg[0])
  100. r3 = float(arg[1])
  101. elif opt == "--num-temps":
  102. num_temps = int(arg)
  103. t = Thermistor(rp, t1, r1, t2, r2, t3, r3)
  104. increment = int((ARES-1)/(num_temps-1));
  105. step = (TMIN-TMAX) / (num_temps-1)
  106. low_bound = t.temp(ARES-1);
  107. up_bound = t.temp(1);
  108. min_temp = int(TMIN if TMIN > low_bound else low_bound)
  109. max_temp = int(TMAX if TMAX < up_bound else up_bound)
  110. temps = range(max_temp, TMIN+step, step);
  111. print "// Thermistor lookup table for Marlin"
  112. 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)
  113. print "// Steinhart-Hart Coefficients: a=%.15g, b=%.15g, c=%.15g " % (t.c1, t.c2, t.c3)
  114. print "// Theoretical limits of termistor: %.2f to %.2f degC" % (low_bound, up_bound)
  115. print
  116. print "#define NUMTEMPS %s" % (len(temps))
  117. print "const short temptable[NUMTEMPS][2] PROGMEM = {"
  118. for temp in temps:
  119. adc = t.adc(temp)
  120. print " { (short) (%7.2f * OVERSAMPLENR ), %4s }%s // v=%.3f\tr=%.3f\tres=%.3f degC/count" % (adc , temp, \
  121. ',' if temp != temps[-1] else ' ', \
  122. t.voltage(adc), \
  123. t.resist( adc), \
  124. t.resol( adc) \
  125. )
  126. print "};"
  127. def usage():
  128. print __doc__
  129. if __name__ == "__main__":
  130. main(sys.argv[1:])