From 3e1c42da801fb87c01369cb95f4cbd7766abb948 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 29 May 2019 11:11:07 -0400 Subject: [PATCH] thermistor: Add support for printers with an "inline_resistor" Signed-off-by: Kevin O'Connor --- config/example.cfg | 5 +++++ klippy/extras/thermistor.py | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config/example.cfg b/config/example.cfg index fb5a716e..56f60f29 100644 --- a/config/example.cfg +++ b/config/example.cfg @@ -164,6 +164,11 @@ sensor_pin: analog13 # The resistance (in ohms) of the pullup attached to the # thermistor. This parameter is only valid when the sensor is a # thermistor. The default is 4700 ohms. +#inline_resistor: 0 +# The resistance (in ohms) of an extra (not heat varying) resistor +# that is placed inline with the thermistor. It is rare to set this. +# This parameter is only valid when the sensor is a thermistor. The +# default is 0 ohms. #adc_voltage: 5.0 # The ADC comparison voltage. This parameter is only valid when the # sensor is an AD595 or "PT100 INA826". The default is 5 volts. diff --git a/klippy/extras/thermistor.py b/klippy/extras/thermistor.py index 7a1ca534..e805a70b 100644 --- a/klippy/extras/thermistor.py +++ b/klippy/extras/thermistor.py @@ -10,8 +10,9 @@ KELVIN_TO_CELCIUS = -273.15 # Analog voltage to temperature converter for thermistors class Thermistor: - def __init__(self, pullup): + def __init__(self, pullup, inline_resistor): self.pullup = pullup + self.inline_resistor = inline_resistor self.c1 = self.c2 = self.c3 = 0. def setup_coefficients(self, t1, r1, t2, r2, t3, r3, name=""): # Calculate Steinhart-Hart coefficents from temp measurements. @@ -48,7 +49,7 @@ class Thermistor: # Calculate temperature from adc adc = max(.00001, min(.99999, adc)) r = self.pullup * adc / (1.0 - adc) - ln_r = math.log(r) + ln_r = math.log(r - self.inline_resistor) inv_t = self.c1 + self.c2 * ln_r + self.c3 * ln_r**3 return 1.0/inv_t + KELVIN_TO_CELCIUS def calc_adc(self, temp): @@ -63,13 +64,14 @@ class Thermistor: ln_r = math.pow(x - y, 1./3.) - math.pow(x + y, 1./3.) else: ln_r = (inv_t - self.c1) / self.c2 - r = math.exp(ln_r) + r = math.exp(ln_r) + self.inline_resistor return r / (self.pullup + r) # Create an ADC converter with a thermistor def PrinterThermistor(config, params): pullup = config.getfloat('pullup_resistor', 4700., above=0.) - thermistor = Thermistor(pullup) + inline_resistor = config.getfloat('inline_resistor', 0., minval=0.) + thermistor = Thermistor(pullup, inline_resistor) if 'beta' in params: thermistor.setup_coefficients_beta( params['t1'], params['r1'], params['beta'])