aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/adc_temperature.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-04-04 14:53:47 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-04-04 23:14:55 -0400
commit06d73207e73f751eca9cf19ad96251da5c245955 (patch)
tree5efb9566978d8e48ef6ee4a7b672523f4a846a27 /klippy/extras/adc_temperature.py
parent0fc4f0946ed6801de1a3403311f5fc2836559dd9 (diff)
downloadkutter-06d73207e73f751eca9cf19ad96251da5c245955.tar.gz
kutter-06d73207e73f751eca9cf19ad96251da5c245955.tar.xz
kutter-06d73207e73f751eca9cf19ad96251da5c245955.zip
heater: Move Thermistor and Linear to their own files in extras/
Move the Thermistor code to a new thermistor.py module. Move the Linear code to a new adc_temperature.py module. This simplifies the heater.py code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras/adc_temperature.py')
-rw-r--r--klippy/extras/adc_temperature.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/klippy/extras/adc_temperature.py b/klippy/extras/adc_temperature.py
new file mode 100644
index 00000000..1006944d
--- /dev/null
+++ b/klippy/extras/adc_temperature.py
@@ -0,0 +1,45 @@
+# Obtain temperature using linear interpolation of ADC values
+#
+# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+
+SAMPLE_TIME = 0.001
+SAMPLE_COUNT = 8
+REPORT_TIME = 0.300
+
+# Linear style conversion chips calibrated with two temp measurements
+class Linear:
+ def __init__(self, config, params):
+ adc_voltage = config.getfloat('adc_voltage', 5., above=0.)
+ ppins = config.get_printer().lookup_object('pins')
+ self.mcu_adc = ppins.setup_pin('adc', config.get('sensor_pin'))
+ self.mcu_adc.setup_adc_callback(REPORT_TIME, self.adc_callback)
+ self.temperature_callback = None
+ slope = (params['t2'] - params['t1']) / (params['v2'] - params['v1'])
+ self.gain = adc_voltage * slope
+ self.offset = params['t1'] - params['v1'] * slope
+ def setup_minmax(self, min_temp, max_temp):
+ adc_range = [self.calc_adc(min_temp), self.calc_adc(max_temp)]
+ self.mcu_adc.setup_minmax(SAMPLE_TIME, SAMPLE_COUNT,
+ minval=min(adc_range), maxval=max(adc_range))
+ def setup_callback(self, temperature_callback):
+ self.temperature_callback = temperature_callback
+ def get_report_time_delta(self):
+ return REPORT_TIME
+ def adc_callback(self, read_time, read_value):
+ temp = read_value * self.gain + self.offset
+ self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
+ def calc_adc(self, temp):
+ return (temp - self.offset) / self.gain
+
+Sensors = {
+ "AD595": { 't1': 25., 'v1': .25, 't2': 300., 'v2': 3.022 },
+}
+
+def load_config(config):
+ # Register default sensors
+ pheater = config.get_printer().lookup_object("heater")
+ for sensor_type, params in Sensors.items():
+ func = (lambda config, params=params: Linear(config, params))
+ pheater.add_sensor(sensor_type, func)