aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/heater.py
diff options
context:
space:
mode:
Diffstat (limited to 'klippy/heater.py')
-rw-r--r--klippy/heater.py136
1 files changed, 13 insertions, 123 deletions
diff --git a/klippy/heater.py b/klippy/heater.py
index b8f2a276..7c0def43 100644
--- a/klippy/heater.py
+++ b/klippy/heater.py
@@ -3,128 +3,17 @@
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
-import math, logging, threading
-
-
-######################################################################
-# Sensors
-######################################################################
-
-KELVIN_TO_CELCIUS = -273.15
-SAMPLE_TIME = 0.001
-SAMPLE_COUNT = 8
-REPORT_TIME = 0.300
-
-# Analog voltage to temperature converter for thermistors
-class Thermistor:
- def __init__(self, config, params):
- 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.
- if 'beta' in params:
- self.calc_coefficients_beta(params)
- else:
- self.calc_coefficients(params)
- def calc_coefficients(self, params):
- # Calculate Steinhart-Hart coefficents from temp measurements.
- # Arrange samples as 3 linear equations and solve for c1, c2, and c3.
- inv_t1 = 1. / (params['t1'] - KELVIN_TO_CELCIUS)
- inv_t2 = 1. / (params['t2'] - KELVIN_TO_CELCIUS)
- inv_t3 = 1. / (params['t3'] - KELVIN_TO_CELCIUS)
- ln_r1 = math.log(params['r1'])
- ln_r2 = math.log(params['r2'])
- ln_r3 = math.log(params['r3'])
- 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
- ln_r12, ln_r13 = ln_r1 - ln_r2, ln_r1 - ln_r3
- ln3_r12, ln3_r13 = ln3_r1 - ln3_r2, ln3_r1 - ln3_r3
-
- self.c3 = ((inv_t12 - inv_t13 * ln_r12 / ln_r13)
- / (ln3_r12 - ln3_r13 * ln_r12 / ln_r13))
- self.c2 = (inv_t12 - self.c3 * ln3_r12) / ln_r12
- self.c1 = inv_t1 - self.c2 * ln_r1 - self.c3 * ln3_r1
- def calc_coefficients_beta(self, params):
- # Calculate equivalent Steinhart-Hart coefficents from beta
- inv_t1 = 1. / (params['t1'] - KELVIN_TO_CELCIUS)
- ln_r1 = math.log(params['r1'])
- self.c3 = 0.
- self.c2 = 1. / params['beta']
- self.c1 = inv_t1 - self.c2 * ln_r1
- 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))
- def setup_callback(self, temperature_callback):
- self.temperature_callback = temperature_callback
- def adc_callback(self, read_time, read_value):
- # Calculate temperature from adc
- adc = max(.00001, min(.99999, read_value))
- r = self.pullup * adc / (1.0 - adc)
- ln_r = math.log(r)
- inv_t = self.c1 + self.c2 * ln_r + self.c3 * ln_r**3
- temp = 1.0/inv_t + KELVIN_TO_CELCIUS
- self.temperature_callback(read_time, temp)
- def calc_adc(self, temp):
- inv_t = 1. / (temp - KELVIN_TO_CELCIUS)
- if self.c3:
- # Solve for ln_r using Cardano's formula
- y = (self.c1 - inv_t) / (2. * self.c3)
- x = math.sqrt((self.c2 / (3. * self.c3))**3 + y**2)
- 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)
- return r / (self.pullup + r)
-
-# Linear style conversion chips calibrated with two temp measurements
-class Linear:
- def __init__(self, config, params):
- adc_voltage = config.getfloat('adc_voltage', 5., 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
- slope = (params['t2'] - params['t1']) / (params['v2'] - params['v1'])
- self.gain = adc_voltage * slope
- self.offset = params['t1'] - params['v1'] * slope
- 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))
- def setup_callback(self, temperature_callback):
- self.temperature_callback = temperature_callback
- def adc_callback(self, read_time, read_value):
- temp = read_value * self.gain + self.offset
- self.temperature_callback(read_time, temp)
- def calc_adc(self, temp):
- return (temp - self.offset) / self.gain
-
-# Available sensors
-Sensors = {
- "EPCOS 100K B57560G104F": {
- 'class': Thermistor, 't1': 25., 'r1': 100000.,
- 't2': 150., 'r2': 1641.9, 't3': 250., 'r3': 226.15 },
- "ATC Semitec 104GT-2": {
- 'class': Thermistor, 't1': 20., 'r1': 126800.,
- 't2': 150., 'r2': 1360., 't3': 300., 'r3': 80.65 },
- "NTC 100K beta 3950": {
- 'class': Thermistor, 't1': 25., 'r1': 100000., 'beta': 3950. },
- "AD595": { 'class': Linear, 't1': 25., 'v1': .25, 't2': 300., 'v2': 3.022 },
-}
+import logging, threading
######################################################################
# Heater
######################################################################
+KELVIN_TO_CELCIUS = -273.15
MAX_HEAT_TIME = 5.0
AMBIENT_TEMP = 25.
PID_PARAM_BASE = 255.
-PWM_DELAY = REPORT_TIME + SAMPLE_TIME*SAMPLE_COUNT
class error(Exception):
pass
@@ -139,6 +28,7 @@ class Heater:
self.max_temp = config.getfloat('max_temp', above=self.min_temp)
self.sensor.setup_minmax(self.min_temp, self.max_temp)
self.sensor.setup_callback(self.temperature_callback)
+ self.pwm_delay = self.sensor.get_report_time_delta()
self.min_extrude_temp = config.getfloat(
'min_extrude_temp', 170., minval=self.min_temp, maxval=self.max_temp)
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
@@ -155,7 +45,7 @@ class Heater:
else:
self.mcu_pwm = ppins.setup_pin('pwm', heater_pin)
pwm_cycle_time = config.getfloat(
- 'pwm_cycle_time', 0.100, above=0., maxval=REPORT_TIME)
+ 'pwm_cycle_time', 0.100, above=0., maxval=self.pwm_delay)
self.mcu_pwm.setup_cycle_time(pwm_cycle_time)
self.mcu_pwm.setup_max_duration(MAX_HEAT_TIME)
is_fileoutput = self.mcu_pwm.get_mcu().is_fileoutput()
@@ -174,7 +64,7 @@ class Heater:
and abs(value - self.last_pwm_value) < 0.05):
# No significant change in value - can suppress update
return
- pwm_time = read_time + PWM_DELAY
+ pwm_time = read_time + self.pwm_delay
self.next_pwm_time = pwm_time + 0.75 * MAX_HEAT_TIME
self.last_pwm_value = value
logging.debug("%s: pwm=%.3f@%.3f (from %.3f@%.3f [%.3f])",
@@ -307,20 +197,23 @@ class PrinterHeaters:
self.printer = printer
self.sensors = {}
self.heaters = {}
- def add_sensor(self, sensor_type, params):
- self.sensors[sensor_type] = params
+ def add_sensor(self, sensor_type, sensor_factory):
+ self.sensors[sensor_type] = sensor_factory
def setup_heater(self, config):
heater_name = config.get_name()
if heater_name == 'extruder':
heater_name = 'extruder0'
if heater_name in self.heaters:
raise config.error("Heater %s already registered" % (heater_name,))
+ # Setup sensor
+ self.printer.try_load_module(config, "thermistor")
+ self.printer.try_load_module(config, "adc_temperature")
sensor_type = config.get('sensor_type')
if sensor_type not in self.sensors:
raise self.printer.config_error("Unknown temperature sensor '%s'" % (
sensor_type,))
- params = self.sensors[sensor_type]
- sensor = params['class'](config, params)
+ sensor = self.sensors[sensor_type](config)
+ # Create heater
self.heaters[heater_name] = heater = Heater(config, sensor)
return heater
def lookup_heater(self, heater_name):
@@ -332,7 +225,4 @@ class PrinterHeaters:
return self.heaters[heater_name]
def add_printer_objects(printer, config):
- ph = PrinterHeaters(printer, config)
- printer.add_object('heater', ph)
- for sensor_type, params in Sensors.items():
- ph.add_sensor(sensor_type, params)
+ printer.add_object('heater', PrinterHeaters(printer, config))