thermistor: Breakout thermistor math to its own class

Separate the thermistor math from the heater temperature callback
system.  This may make it easier for debugging.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-01-20 14:52:40 -05:00
parent 8c5a94f49f
commit 293366d033
1 changed files with 49 additions and 40 deletions

View File

@ -1,6 +1,6 @@
# Temperature measurements with thermistors # Temperature measurements with thermistors
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging import math, logging
@ -13,27 +13,18 @@ RANGE_CHECK_COUNT = 4
# Analog voltage to temperature converter for thermistors # Analog voltage to temperature converter for thermistors
class Thermistor: class Thermistor:
def __init__(self, config, params): def __init__(self, pullup):
self.name = config.get_name() self.pullup = pullup
self.pullup = config.getfloat('pullup_resistor', 4700., 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
self.c1 = self.c2 = self.c3 = 0. self.c1 = self.c2 = self.c3 = 0.
if 'beta' in params: def setup_coefficients(self, t1, r1, t2, r2, t3, r3, name=""):
self.calc_coefficients_beta(params, params['beta'])
else:
self.calc_coefficients(params)
def calc_coefficients(self, params):
# Calculate Steinhart-Hart coefficents from temp measurements. # Calculate Steinhart-Hart coefficents from temp measurements.
# Arrange samples as 3 linear equations and solve for c1, c2, and c3. # Arrange samples as 3 linear equations and solve for c1, c2, and c3.
inv_t1 = 1. / (params['t1'] - KELVIN_TO_CELCIUS) inv_t1 = 1. / (t1 - KELVIN_TO_CELCIUS)
inv_t2 = 1. / (params['t2'] - KELVIN_TO_CELCIUS) inv_t2 = 1. / (t2 - KELVIN_TO_CELCIUS)
inv_t3 = 1. / (params['t3'] - KELVIN_TO_CELCIUS) inv_t3 = 1. / (t3 - KELVIN_TO_CELCIUS)
ln_r1 = math.log(params['r1']) ln_r1 = math.log(r1)
ln_r2 = math.log(params['r2']) ln_r2 = math.log(r2)
ln_r3 = math.log(params['r3']) ln_r3 = math.log(r3)
ln3_r1, ln3_r2, ln3_r3 = ln_r1**3, ln_r2**3, ln_r3**3 ln3_r1, ln3_r2, ln3_r3 = ln_r1**3, ln_r2**3, ln_r3**3
inv_t12, inv_t13 = inv_t1 - inv_t2, inv_t1 - inv_t3 inv_t12, inv_t13 = inv_t1 - inv_t2, inv_t1 - inv_t3
@ -44,37 +35,27 @@ class Thermistor:
/ (ln3_r12 - ln3_r13 * ln_r12 / ln_r13)) / (ln3_r12 - ln3_r13 * ln_r12 / ln_r13))
if self.c3 <= 0.: if self.c3 <= 0.:
beta = ln_r13 / inv_t13 beta = ln_r13 / inv_t13
logging.warn("Using thermistor beta %.3f in heater %s", logging.warn("Using thermistor beta %.3f in heater %s", beta, name)
beta, self.name) self.setup_coefficients_beta(t1, r1, beta)
self.calc_coefficients_beta(params, beta)
return return
self.c2 = (inv_t12 - self.c3 * ln3_r12) / ln_r12 self.c2 = (inv_t12 - self.c3 * ln3_r12) / ln_r12
self.c1 = inv_t1 - self.c2 * ln_r1 - self.c3 * ln3_r1 self.c1 = inv_t1 - self.c2 * ln_r1 - self.c3 * ln3_r1
def calc_coefficients_beta(self, params, beta): def setup_coefficients_beta(self, t1, r1, beta):
# Calculate equivalent Steinhart-Hart coefficents from beta # Calculate equivalent Steinhart-Hart coefficents from beta
inv_t1 = 1. / (params['t1'] - KELVIN_TO_CELCIUS) inv_t1 = 1. / (t1 - KELVIN_TO_CELCIUS)
ln_r1 = math.log(params['r1']) ln_r1 = math.log(r1)
self.c3 = 0. self.c3 = 0.
self.c2 = 1. / beta self.c2 = 1. / beta
self.c1 = inv_t1 - self.c2 * ln_r1 self.c1 = inv_t1 - self.c2 * ln_r1
def setup_minmax(self, min_temp, max_temp): def calc_temp(self, adc):
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),
range_check_count=RANGE_CHECK_COUNT)
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):
# Calculate temperature from adc # Calculate temperature from adc
adc = max(.00001, min(.99999, read_value)) 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)
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
temp = 1.0/inv_t + KELVIN_TO_CELCIUS return 1.0/inv_t + KELVIN_TO_CELCIUS
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
def calc_adc(self, temp): def calc_adc(self, temp):
# Calculate adc reading from a temperature
if temp <= KELVIN_TO_CELCIUS: if temp <= KELVIN_TO_CELCIUS:
return 1. return 1.
inv_t = 1. / (temp - KELVIN_TO_CELCIUS) inv_t = 1. / (temp - KELVIN_TO_CELCIUS)
@ -88,6 +69,34 @@ class Thermistor:
r = math.exp(ln_r) r = math.exp(ln_r)
return r / (self.pullup + r) return r / (self.pullup + r)
# Thermistor interface for heater temperature callbacks
class PrinterThermistor:
def __init__(self, config, params):
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)
pullup = config.getfloat('pullup_resistor', 4700., above=0.)
self.thermistor = Thermistor(pullup)
if 'beta' in params:
self.thermistor.setup_coefficients_beta(
params['t1'], params['r1'], params['beta'])
else:
self.thermistor.setup_coefficients(
params['t1'], params['r1'], params['t2'], params['r2'],
params['t3'], params['r3'], name=config.get_name())
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 = self.thermistor.calc_temp(read_value)
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
def setup_minmax(self, min_temp, max_temp):
adc_range = [self.thermistor.calc_adc(t) for t in [min_temp, max_temp]]
self.mcu_adc.setup_minmax(SAMPLE_TIME, SAMPLE_COUNT,
minval=min(adc_range), maxval=max(adc_range),
range_check_count=RANGE_CHECK_COUNT)
# Custom defined thermistors from the config file # Custom defined thermistors from the config file
class CustomThermistor: class CustomThermistor:
def __init__(self, config): def __init__(self, config):
@ -106,7 +115,7 @@ class CustomThermistor:
self.params = {'t1': t1, 'r1': r1, 't2': t2, 'r2': r2, self.params = {'t1': t1, 'r1': r1, 't2': t2, 'r2': r2,
't3': t3, 'r3': r3} 't3': t3, 'r3': r3}
def create(self, config): def create(self, config):
return Thermistor(config, self.params) return PrinterThermistor(config, self.params)
# Default sensors # Default sensors
Sensors = { Sensors = {
@ -125,7 +134,7 @@ def load_config(config):
# Register default thermistor types # Register default thermistor types
pheater = config.get_printer().lookup_object("heater") pheater = config.get_printer().lookup_object("heater")
for sensor_type, params in Sensors.items(): for sensor_type, params in Sensors.items():
func = (lambda config, params=params: Thermistor(config, params)) func = (lambda config, params=params: PrinterThermistor(config, params))
pheater.add_sensor(sensor_type, func) pheater.add_sensor(sensor_type, func)
def load_config_prefix(config): def load_config_prefix(config):