thermistor: Add support for printers with an "inline_resistor"

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-05-29 11:11:07 -04:00
parent e2e4a5d4e6
commit 3e1c42da80
2 changed files with 11 additions and 4 deletions

View File

@ -164,6 +164,11 @@ sensor_pin: analog13
# The resistance (in ohms) of the pullup attached to the # The resistance (in ohms) of the pullup attached to the
# thermistor. This parameter is only valid when the sensor is a # thermistor. This parameter is only valid when the sensor is a
# thermistor. The default is 4700 ohms. # 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 #adc_voltage: 5.0
# The ADC comparison voltage. This parameter is only valid when the # The ADC comparison voltage. This parameter is only valid when the
# sensor is an AD595 or "PT100 INA826". The default is 5 volts. # sensor is an AD595 or "PT100 INA826". The default is 5 volts.

View File

@ -10,8 +10,9 @@ KELVIN_TO_CELCIUS = -273.15
# Analog voltage to temperature converter for thermistors # Analog voltage to temperature converter for thermistors
class Thermistor: class Thermistor:
def __init__(self, pullup): def __init__(self, pullup, inline_resistor):
self.pullup = pullup self.pullup = pullup
self.inline_resistor = inline_resistor
self.c1 = self.c2 = self.c3 = 0. self.c1 = self.c2 = self.c3 = 0.
def setup_coefficients(self, t1, r1, t2, r2, t3, r3, name=""): def setup_coefficients(self, t1, r1, t2, r2, t3, r3, name=""):
# Calculate Steinhart-Hart coefficents from temp measurements. # Calculate Steinhart-Hart coefficents from temp measurements.
@ -48,7 +49,7 @@ class Thermistor:
# Calculate temperature from adc # Calculate temperature from adc
adc = max(.00001, min(.99999, adc)) adc = max(.00001, min(.99999, adc))
r = self.pullup * adc / (1.0 - 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 inv_t = self.c1 + self.c2 * ln_r + self.c3 * ln_r**3
return 1.0/inv_t + KELVIN_TO_CELCIUS return 1.0/inv_t + KELVIN_TO_CELCIUS
def calc_adc(self, temp): 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.) ln_r = math.pow(x - y, 1./3.) - math.pow(x + y, 1./3.)
else: else:
ln_r = (inv_t - self.c1) / self.c2 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) return r / (self.pullup + r)
# Create an ADC converter with a thermistor # Create an ADC converter with a thermistor
def PrinterThermistor(config, params): def PrinterThermistor(config, params):
pullup = config.getfloat('pullup_resistor', 4700., above=0.) 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: if 'beta' in params:
thermistor.setup_coefficients_beta( thermistor.setup_coefficients_beta(
params['t1'], params['r1'], params['beta']) params['t1'], params['r1'], params['beta'])