From 8c8261ba8009b5c16815cb5166d50f7269d786cc Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 9 Apr 2018 12:14:20 -0400 Subject: [PATCH] thermistor: Add support for defining custom thermistors Add the ability to define a new thermistor type in the config file. Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 25 +++++++++++++++++++++++++ config/example.cfg | 5 +++-- klippy/extras/thermistor.py | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index a28654f7..e67f7165 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -394,6 +394,31 @@ # provided when using an st7920 display. +# Custom thermistors (one may define any number of sections with a +# "thermistor" prefix). A custom thermistor may be used in the +# sensor_type field of a heater config section. (For example, if one +# defines a "[thermistor my_thermistor]" section then one may use a +# "sensor_type: my_thermistor" when defining a heater.) Be sure to +# place the thermistor section in the config file above its first use +# in a heater section. +#[thermistor my_thermistor] +#temperature1: +#resistance1: +#temperature2: +#resistance2: +#temperature3: +#resistance3: +# Three resistance measurements (in Ohms) at the given temperatures +# (in Celsius). The three measurements will be used to calculate the +# Steinhart-Hart coefficients for the thermistor. These parameters +# must be provided when using Steinhart-Hart to define the +# thermistor. +#beta: +# Alternatively, one may define temperature1, resistance1, and beta +# to define the thermistor parameters. This parameter must be +# provided when using "beta" to define the thermistor. + + # Replicape support - see the generic-replicape.cfg file for further # details. #[replicape] diff --git a/config/example.cfg b/config/example.cfg index ae8c3b9a..9d860a58 100644 --- a/config/example.cfg +++ b/config/example.cfg @@ -146,8 +146,9 @@ heater_pin: ar10 # periods) to the heater. The default is 1.0. sensor_type: EPCOS 100K B57560G104F # Type of sensor - this may be "EPCOS 100K B57560G104F", "ATC -# Semitec 104GT-2", "NTC 100K beta 3950", or "AD595". This parameter -# must be provided. +# Semitec 104GT-2", "NTC 100K beta 3950", or "AD595". Additional +# sensor types may be available - see the example-extras.cfg file +# for details. This parameter must be provided. sensor_pin: analog13 # Analog input pin connected to the sensor. This parameter must be # provided. diff --git a/klippy/extras/thermistor.py b/klippy/extras/thermistor.py index 22836379..cc2d64fd 100644 --- a/klippy/extras/thermistor.py +++ b/klippy/extras/thermistor.py @@ -77,6 +77,27 @@ class Thermistor: r = math.exp(ln_r) return r / (self.pullup + r) +# Custom defined thermistors from the config file +class CustomThermistor: + def __init__(self, config): + self.name = " ".join(config.get_name().split()[1:]) + t1 = config.getfloat("temperature1", minval=KELVIN_TO_CELCIUS) + r1 = config.getfloat("resistance1", minval=0.) + beta = config.getfloat("beta", None, above=0.) + if beta is not None: + self.params = {'t1': t1, 'r1': r1, 'beta': beta} + return + t2 = config.getfloat("temperature2", minval=KELVIN_TO_CELCIUS) + r2 = config.getfloat("resistance2", minval=0.) + t3 = config.getfloat("temperature3", minval=KELVIN_TO_CELCIUS) + r3 = config.getfloat("resistance3", minval=0.) + (t1, r1), (t2, r2), (t3, r3) = sorted([(t1, r1), (t2, r2), (t3, r3)]) + self.params = {'t1': t1, 'r1': r1, 't2': t2, 'r2': r2, + 't3': t3, 'r3': r3} + def create(self, config): + return Thermistor(config, self.params) + +# Default sensors Sensors = { "EPCOS 100K B57560G104F": { 't1': 25., 'r1': 100000., 't2': 150., 'r2': 1641.9, @@ -93,3 +114,8 @@ def load_config(config): for sensor_type, params in Sensors.items(): func = (lambda config, params=params: Thermistor(config, params)) pheater.add_sensor(sensor_type, func) + +def load_config_prefix(config): + thermistor = CustomThermistor(config) + pheater = config.get_printer().lookup_object("heater") + pheater.add_sensor(thermistor.name, thermistor.create)