adc_temperature: Move PrinterADCtoTemperature to adc_temperature.py
Move the low-level PrinterADCtoTemperature() class from thermistor.py to adc_temperature.py and use it from the Linear() class. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
0c4fc64ef8
commit
99980817c3
|
@ -5,18 +5,44 @@
|
|||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import logging, bisect
|
||||
|
||||
|
||||
######################################################################
|
||||
# Interface between MCU adc and heater temperature callbacks
|
||||
######################################################################
|
||||
|
||||
SAMPLE_TIME = 0.001
|
||||
SAMPLE_COUNT = 8
|
||||
REPORT_TIME = 0.300
|
||||
RANGE_CHECK_COUNT = 4
|
||||
|
||||
# Linear style conversion chips calibrated with two temp measurements
|
||||
class Linear:
|
||||
def __init__(self, config, params):
|
||||
# Interface between ADC and heater temperature callbacks
|
||||
class PrinterADCtoTemperature:
|
||||
def __init__(self, config, adc_convert):
|
||||
self.adc_convert = adc_convert
|
||||
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
|
||||
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.adc_convert.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.adc_convert.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)
|
||||
|
||||
|
||||
######################################################################
|
||||
# Linear voltage sensors
|
||||
######################################################################
|
||||
|
||||
# Linear style conversion chips calibrated with two temp measurements
|
||||
class Linear:
|
||||
def __init__(self, config, params):
|
||||
self.adc_samples = []
|
||||
self.slope_samples = []
|
||||
self.calc_coefficients(config, params)
|
||||
|
@ -48,20 +74,10 @@ class Linear:
|
|||
raise config.error(
|
||||
"adc_temperature needs two volt and temperature measurements")
|
||||
self.adc_samples[-1] = 1.
|
||||
def setup_minmax(self, min_temp, max_temp):
|
||||
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):
|
||||
pos = bisect.bisect(self.adc_samples, read_value)
|
||||
def calc_temp(self, adc):
|
||||
pos = bisect.bisect(self.adc_samples, adc)
|
||||
gain, offset = self.slope_samples[pos]
|
||||
temp = read_value * gain + offset
|
||||
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
|
||||
return read_value * gain + offset
|
||||
def calc_adc(self, temp):
|
||||
temps = [adc * gain + offset for adc, (gain, offset) in zip(
|
||||
self.adc_samples, self.slope_samples)]
|
||||
|
@ -86,7 +102,8 @@ class CustomLinear:
|
|||
v = config.getfloat("voltage%d" % (i,))
|
||||
self.params.append((t, v))
|
||||
def create(self, config):
|
||||
return Linear(config, self.params)
|
||||
lv = Linear(config, self.params)
|
||||
return PrinterADCtoTemperature(config, lv)
|
||||
|
||||
AD595 = [
|
||||
(0., .0027), (10., .101), (20., .200), (25., .250), (30., .300),
|
||||
|
@ -114,7 +131,8 @@ def load_config(config):
|
|||
# Register default sensors
|
||||
pheater = config.get_printer().lookup_object("heater")
|
||||
for sensor_type, params in [("AD595", AD595), ("PT100 INA826", PT100)]:
|
||||
func = (lambda config, params=params: Linear(config, params))
|
||||
func = (lambda config, params=params:
|
||||
PrinterADCtoTemperature(config, Linear(config, params)))
|
||||
pheater.add_sensor(sensor_type, func)
|
||||
|
||||
def load_config_prefix(config):
|
||||
|
|
|
@ -4,12 +4,9 @@
|
|||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import math, logging
|
||||
import adc_temperature
|
||||
|
||||
KELVIN_TO_CELCIUS = -273.15
|
||||
SAMPLE_TIME = 0.001
|
||||
SAMPLE_COUNT = 8
|
||||
REPORT_TIME = 0.300
|
||||
RANGE_CHECK_COUNT = 4
|
||||
|
||||
# Analog voltage to temperature converter for thermistors
|
||||
class Thermistor:
|
||||
|
@ -69,26 +66,6 @@ class Thermistor:
|
|||
r = math.exp(ln_r)
|
||||
return r / (self.pullup + r)
|
||||
|
||||
# Interface between ADC and heater temperature callbacks
|
||||
class PrinterADCtoTemperature:
|
||||
def __init__(self, config, adc_convert):
|
||||
self.adc_convert = adc_convert
|
||||
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)
|
||||
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.adc_convert.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.adc_convert.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)
|
||||
|
||||
# Create an ADC converter with a thermistor
|
||||
def PrinterThermistor(config, params):
|
||||
pullup = config.getfloat('pullup_resistor', 4700., above=0.)
|
||||
|
@ -100,7 +77,7 @@ def PrinterThermistor(config, params):
|
|||
thermistor.setup_coefficients(
|
||||
params['t1'], params['r1'], params['t2'], params['r2'],
|
||||
params['t3'], params['r3'], name=config.get_name())
|
||||
return PrinterADCtoTemperature(config, thermistor)
|
||||
return adc_temperature.PrinterADCtoTemperature(config, thermistor)
|
||||
|
||||
# Custom defined thermistors from the config file
|
||||
class CustomThermistor:
|
||||
|
|
Loading…
Reference in New Issue