From fda988889b7cee2b95b35d06e15b5ecb3ad92bbb Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 13 Nov 2017 11:07:20 -0500 Subject: [PATCH] heater: Avoid math errors on extreme ADC readings Avoid log(0) and divide by zero errors in the thermistor calc_temp() method. Signed-off-by: Kevin O'Connor --- klippy/heater.py | 1 + 1 file changed, 1 insertion(+) diff --git a/klippy/heater.py b/klippy/heater.py index 445f868b..c666ddec 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -35,6 +35,7 @@ class Thermistor: self.c2 = (inv_t12 - self.c3 * ln3_r12) / ln_r12 self.c1 = inv_t1 - self.c2 * ln_r1 - self.c3 * ln3_r1 def calc_temp(self, adc): + adc = max(.00001, min(.99999, adc)) 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