diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 34f1c4e1..a4e52dde 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1051,13 +1051,13 @@ # sections with an "adc_temperature" prefix). This allows one to # define a custom temperature sensor that measures a voltage on an # Analog to Digital Converter (ADC) pin and uses linear interpolation -# between a set of configured temperature/voltage measurements to -# determine the temperature. The resulting sensor can be used as a -# sensor_type in a heater section. (For example, if one defines a -# "[adc_temperature my_sensor]" section then one may use a -# "sensor_type: my_sensor" when defining a heater.) Be sure to place -# the sensor section in the config file above its first use in a -# heater section. +# between a set of configured temperature/voltage (or +# temperature/resistance) measurements to determine the +# temperature. The resulting sensor can be used as a sensor_type in a +# heater section. (For example, if one defines a "[adc_temperature +# my_sensor]" section then one may use a "sensor_type: my_sensor" when +# defining a heater.) Be sure to place the sensor section in the +# config file above its first use in a heater section. #[adc_temperature my_sensor] #temperature1: #voltage1: @@ -1067,6 +1067,14 @@ # A set of temperatures (in Celsius) and voltages (in Volts) to use # as reference when converting a temperature. At least two # measurements must be provided. +#temperature1: +#resistance1: +#temperature2: +#resistance2: +#... +# Alternatively one may specify a set of temperatures (in Celsius) +# and resistance (in Ohms) to use as reference when converting a +# temperature. At least two measurements must be provided. # MAXxxxxx serial peripheral interface (SPI) temperature based diff --git a/klippy/extras/adc_temperature.py b/klippy/extras/adc_temperature.py index cb7b0e28..086f2858 100644 --- a/klippy/extras/adc_temperature.py +++ b/klippy/extras/adc_temperature.py @@ -119,6 +119,50 @@ class CustomLinearVoltage: lv = LinearVoltage(config, self.params) return PrinterADCtoTemperature(config, lv) + +###################################################################### +# Linear resistance to temperature converter +###################################################################### + +# Linear resistance calibrated with two temp measurements +class LinearResistance: + def __init__(self, config, samples): + self.pullup = config.getfloat('pullup_resistor', 4700., above=0.) + try: + self.li = LinearInterpolate(samples) + except ValueError as e: + raise config.error("adc_temperature %s in heater %s" % ( + str(e), config.get_name())) + def calc_temp(self, adc): + # Calculate temperature from adc + adc = max(.00001, min(.99999, adc)) + r = self.pullup * adc / (1.0 - adc) + return self.li.interpolate(r) + def calc_adc(self, temp): + # Calculate adc reading from a temperature + r = self.li.reverse_interpolate(temp) + return r / (self.pullup + r) + +# Custom defined sensors from the config file +class CustomLinearResistance: + def __init__(self, config): + self.name = " ".join(config.get_name().split()[1:]) + self.samples = [] + for i in range(1, 1000): + t = config.getfloat("temperature%d" % (i,), None) + if t is None: + break + r = config.getfloat("resistance%d" % (i,)) + self.samples.append((r, t)) + def create(self, config): + lr = LinearResistance(config, self.samples) + return PrinterADCtoTemperature(config, lr) + + +###################################################################### +# Default sensors +###################################################################### + AD595 = [ (0., .0027), (10., .101), (20., .200), (25., .250), (30., .300), (40., .401), (50., .503), (60., .605), (80., .810), (100., 1.015), @@ -150,6 +194,9 @@ def load_config(config): pheater.add_sensor(sensor_type, func) def load_config_prefix(config): - linear = CustomLinearVoltage(config) + if config.get("resistance1", None) is None: + custom_sensor = CustomLinearVoltage(config) + else: + custom_sensor = CustomLinearResistance(config) pheater = config.get_printer().lookup_object("heater") - pheater.add_sensor(linear.name, linear.create) + pheater.add_sensor(custom_sensor.name, custom_sensor.create) diff --git a/test/klippy/temperature.cfg b/test/klippy/temperature.cfg index fef12161..b4a75e3c 100644 --- a/test/klippy/temperature.cfg +++ b/test/klippy/temperature.cfg @@ -117,6 +117,22 @@ control: watermark sensor_type: my_custom_adc sensor_pin: analog4 +[adc_temperature my_custom_resistance_adc] +temperature1: 25 +resistance1: 1000 +temperature2: 100 +resistance2: 2000 +temperature3: 250 +resistance3: 3000 + +[temperature_fan test_custom_resistance_adc] +pin: ar17 +min_temp: 0 +max_temp: 100 +control: watermark +sensor_type: my_custom_resistance_adc +sensor_pin: analog5 + [mcu] serial: /dev/ttyACM0 pin_map: arduino