adc_temperature: Add support for linear interpolation of resistances

Add support for performing linear interpolation between a set of
measured temperature/resistance pairs.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-01-21 20:14:49 -05:00
parent fc946c796c
commit 7327394c50
3 changed files with 80 additions and 9 deletions

View File

@ -1051,13 +1051,13 @@
# sections with an "adc_temperature" prefix). This allows one to # sections with an "adc_temperature" prefix). This allows one to
# define a custom temperature sensor that measures a voltage on an # define a custom temperature sensor that measures a voltage on an
# Analog to Digital Converter (ADC) pin and uses linear interpolation # Analog to Digital Converter (ADC) pin and uses linear interpolation
# between a set of configured temperature/voltage measurements to # between a set of configured temperature/voltage (or
# determine the temperature. The resulting sensor can be used as a # temperature/resistance) measurements to determine the
# sensor_type in a heater section. (For example, if one defines a # temperature. The resulting sensor can be used as a sensor_type in a
# "[adc_temperature my_sensor]" section then one may use a # heater section. (For example, if one defines a "[adc_temperature
# "sensor_type: my_sensor" when defining a heater.) Be sure to place # my_sensor]" section then one may use a "sensor_type: my_sensor" when
# the sensor section in the config file above its first use in a # defining a heater.) Be sure to place the sensor section in the
# heater section. # config file above its first use in a heater section.
#[adc_temperature my_sensor] #[adc_temperature my_sensor]
#temperature1: #temperature1:
#voltage1: #voltage1:
@ -1067,6 +1067,14 @@
# A set of temperatures (in Celsius) and voltages (in Volts) to use # A set of temperatures (in Celsius) and voltages (in Volts) to use
# as reference when converting a temperature. At least two # as reference when converting a temperature. At least two
# measurements must be provided. # 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 # MAXxxxxx serial peripheral interface (SPI) temperature based

View File

@ -119,6 +119,50 @@ class CustomLinearVoltage:
lv = LinearVoltage(config, self.params) lv = LinearVoltage(config, self.params)
return PrinterADCtoTemperature(config, lv) 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 = [ AD595 = [
(0., .0027), (10., .101), (20., .200), (25., .250), (30., .300), (0., .0027), (10., .101), (20., .200), (25., .250), (30., .300),
(40., .401), (50., .503), (60., .605), (80., .810), (100., 1.015), (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) pheater.add_sensor(sensor_type, func)
def load_config_prefix(config): 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 = config.get_printer().lookup_object("heater")
pheater.add_sensor(linear.name, linear.create) pheater.add_sensor(custom_sensor.name, custom_sensor.create)

View File

@ -117,6 +117,22 @@ control: watermark
sensor_type: my_custom_adc sensor_type: my_custom_adc
sensor_pin: analog4 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] [mcu]
serial: /dev/ttyACM0 serial: /dev/ttyACM0
pin_map: arduino pin_map: arduino