Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. # Obtain temperature using linear interpolation of ADC values
  2. #
  3. # Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
  4. #
  5. # This file may be distributed under the terms of the GNU GPLv3 license.
  6. import logging, bisect
  7.  
  8. SAMPLE_TIME = 0.001
  9. SAMPLE_COUNT = 8
  10. REPORT_TIME = 0.300
  11.  
  12. # Linear style conversion chips calibrated with two temp measurements
  13. class Linear:
  14. def __init__(self, config, params):
  15. ppins = config.get_printer().lookup_object('pins')
  16. self.mcu_adc = ppins.setup_pin('adc', config.get('sensor_pin'))
  17. self.mcu_adc.setup_adc_callback(REPORT_TIME, self.adc_callback)
  18. self.temperature_callback = None
  19. self.adc_samples = []
  20. self.slope_samples = []
  21. self.calc_coefficients(config, params)
  22. def calc_coefficients(self, config, params):
  23. adc_voltage = config.getfloat('adc_voltage', 5., above=0.)
  24. last_volt = last_temp = None
  25. for volt, temp in sorted([(v, t) for t, v in params]):
  26. adc = volt / adc_voltage
  27. if adc < 0. or adc > 1.:
  28. logging.warn("Ignoring adc sample %.3f/%.3f in heater %s",
  29. temp, volt, config.get_name())
  30. continue
  31. if last_volt is None:
  32. last_volt = volt
  33. last_temp = temp
  34. continue
  35. if volt <= last_volt:
  36. raise config.error("adc_temperature duplicate voltage")
  37. slope = (temp - last_temp) / (volt - last_volt)
  38. gain = adc_voltage * slope
  39. offset = last_temp - last_volt * slope
  40. if self.slope_samples and self.slope_samples[-1] == (gain, offset):
  41. continue
  42. last_temp = temp
  43. last_volt = volt
  44. self.adc_samples.append(adc)
  45. self.slope_samples.append((gain, offset))
  46. if not self.adc_samples:
  47. raise config.error(
  48. "adc_temperature needs two volt and temperature measurements")
  49. self.adc_samples[-1] = 1.
  50. def setup_minmax(self, min_temp, max_temp):
  51. adc_range = [self.calc_adc(min_temp), self.calc_adc(max_temp)]
  52. self.mcu_adc.setup_minmax(SAMPLE_TIME, SAMPLE_COUNT,
  53. minval=min(adc_range), maxval=max(adc_range))
  54. def setup_callback(self, temperature_callback):
  55. self.temperature_callback = temperature_callback
  56. def get_report_time_delta(self):
  57. return REPORT_TIME
  58. def adc_callback(self, read_time, read_value):
  59. pos = bisect.bisect(self.adc_samples, read_value)
  60. gain, offset = self.slope_samples[pos]
  61. temp = read_value * gain + offset
  62. logging.info("temperature=%.6f from adc=%.6f at %.6f", temp, read_value, read_time)
  63. self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
  64. def calc_adc(self, temp):
  65. temps = [adc * gain + offset for adc, (gain, offset) in zip(
  66. self.adc_samples, self.slope_samples)]
  67. if temps[0] < temps[-1]:
  68. pos = min([i for i in range(len(temps)) if temps[i] >= temp]
  69. + [len(temps) - 1])
  70. else:
  71. pos = min([i for i in range(len(temps)) if temps[i] <= temp]
  72. + [len(temps) - 1])
  73. gain, offset = self.slope_samples[pos]
  74. return (temp - offset) / gain
  75.  
  76. # Custom defined sensors from the config file
  77. class CustomLinear:
  78. def __init__(self, config):
  79. self.name = " ".join(config.get_name().split()[1:])
  80. self.params = []
  81. for i in range(1, 1000):
  82. t = config.getfloat("temperature%d" % (i,), None)
  83. if t is None:
  84. break
  85. v = config.getfloat("voltage%d" % (i,))
  86. self.params.append((t, v))
  87. def create(self, config):
  88. return Linear(config, self.params)
  89.  
  90. AD595 = [
  91. (0., .0027), (10., .101), (20., .200), (25., .250), (30., .300), (40., .401),
  92. (50., .503), (60., .605), (80., .810), (100., 1.015), (120., 1.219),
  93. (140., 1.420), (160., 1.620), (180., 1.817), (200., 2.015), (220., 2.213),
  94. (240., 2.413), (260., 2.614), (280., 2.817), (300., 3.022), (320., 3.227),
  95. (340., 3.434), (360., 3.641), (380., 3.849), (400., 4.057), (420., 4.266),
  96. (440., 4.476), (460., 4.686), (480., 4.896)
  97. ]
  98.  
  99. PT100 = [
  100. (0, 0.00), (1, 1.11), (10, 1.15), (20, 1.20), (30, 1.24), (40, 1.28),
  101. (50, 1.32), (60, 1.36), (70, 1.40), (80, 1.44), (90, 1.48), (100, 1.52),
  102. (110, 1.56), (120, 1.61), (130, 1.65), (140, 1.68), (150, 1.72), (160, 1.76),
  103. (170, 1.80), (180, 1.84), (190, 1.88), (200, 1.92), (210, 1.96), (220, 2.00),
  104. (230, 2.04), (240, 2.07), (250, 2.11), (260, 2.15), (270, 2.18), (280, 2.22),
  105. (290, 2.26), (300, 2.29), (310, 2.33), (320, 2.37), (330, 2.41), (340, 2.44),
  106. (350, 2.48), (360, 2.51), (370, 2.55), (380, 2.58), (390, 2.62), (400, 2.66),
  107. (500, 3.00), (600, 3.33), (700, 3.63), (800, 3.93), (900, 4.21),
  108. (1000, 4.48), (1100, 4.73)
  109. ]
  110.  
  111. def load_config(config):
  112. # Register default sensors
  113. pheater = config.get_printer().lookup_object("heater")
  114. for sensor_type, params in [("AD595", AD595), ("PT100 INA826", PT100)]:
  115. func = (lambda config, params=params: Linear(config, params))
  116. pheater.add_sensor(sensor_type, func)
  117.  
  118. def load_config_prefix(config):
  119. linear = CustomLinear(config)
  120. pheater = config.get_printer().lookup_object("heater")
  121. pheater.add_sensor(linear.name, linear.create)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement