From 5dc0d5f7fa6181d0550b4b087c73ae51b8b77674 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Thu, 7 Jul 2022 00:27:57 +0200 Subject: [PATCH] TM autotune: fail if value is outside of the boundaries Ensure we never fall into the boundary values provided by the min/max limits. Save/restore the initial guess value, so that a convergence failure restores the initial model state. --- Firmware/temperature.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index 484ac588f..6a8b0798e 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -2695,6 +2695,7 @@ float estimate(uint16_t samples, float thr, uint16_t max_itr, uint8_t fan_pwm, float ambient) { + float orig = *var; float e = NAN; float points[2]; float bounds[2] = {min, max}; @@ -2710,10 +2711,19 @@ float estimate(uint16_t samples, e = (1-GOLDEN_RATIO) * fabsf((bounds[0]-bounds[1]) / x); printf_P(PSTR("TM iter:%u v:%.2f e:%.3f\n"), it, x, e); - if(e < thr) return e; + if(e < thr) { + if(x == min || x == max) { + // real value likely outside of the search boundaries + break; + } + + *var = x; + return e; + } } SERIAL_ECHOLNPGM("TM estimation did not converge"); + *var = orig; return NAN; }