aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/heater.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-08-18 13:28:04 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-08-18 13:28:04 -0400
commitf25cb33367739abb6a66ff96f0bc6cdb1de3bb22 (patch)
treeb62660cd943475f9efd90e109728ead6fed14bbb /klippy/heater.py
parent4b5f3bec4b0685cd73436117c3ec0f08c9aaa8d6 (diff)
downloadkutter-f25cb33367739abb6a66ff96f0bc6cdb1de3bb22.tar.gz
kutter-f25cb33367739abb6a66ff96f0bc6cdb1de3bb22.tar.xz
kutter-f25cb33367739abb6a66ff96f0bc6cdb1de3bb22.zip
heater: Minor cleanup to Steinhart-Hart math
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/heater.py')
-rw-r--r--klippy/heater.py23
1 files changed, 12 insertions, 11 deletions
diff --git a/klippy/heater.py b/klippy/heater.py
index cb915d8a..1873104a 100644
--- a/klippy/heater.py
+++ b/klippy/heater.py
@@ -23,30 +23,31 @@ class Thermistor:
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.
+ 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
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.c3 * ln3_r1 - self.c2 * ln_r1
+ self.c1 = inv_t1 - self.c2 * ln_r1 - self.c3 * ln3_r1
def calc_temp(self, adc):
r = self.pullup * adc / (1.0 - adc)
ln_r = math.log(r)
- temp_inv = self.c1 + self.c2*ln_r + self.c3*math.pow(ln_r, 3.)
- return 1.0/temp_inv + KELVIN_TO_CELCIUS
+ inv_t = self.c1 + self.c2 * ln_r + self.c3 * ln_r**3
+ return 1.0/inv_t + KELVIN_TO_CELCIUS
def calc_adc(self, temp):
- temp -= KELVIN_TO_CELCIUS
- temp_inv = 1./temp
+ inv_t = 1. / (temp - KELVIN_TO_CELCIUS)
if self.c3:
- y = (self.c1 - temp_inv) / (2. * self.c3)
- x = math.sqrt(math.pow(self.c2 / (3.*self.c3), 3.) + math.pow(y, 2.))
- r = math.exp(math.pow(x-y, 1./3.) - math.pow(x+y, 1./3.))
+ 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.)
+ r = math.exp(ln_r)
else:
- r = math.exp((temp_inv - self.c1) / self.c2)
+ ln_r = (inv_t - self.c1) / self.c2
+ r = math.exp(ln_r)
return r / (self.pullup + r)
# Thermistor calibrated from one temp measurement and its beta