Selaa lähdekoodia

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.
Yuri D'Elia 2 vuotta sitten
vanhempi
commit
5dc0d5f7fa
1 muutettua tiedostoa jossa 11 lisäystä ja 1 poistoa
  1. 11 1
      Firmware/temperature.cpp

+ 11 - 1
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;
 }