From 9b9ce1733cd88384971919a97fd6220cd21ec65a Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 30 Aug 2022 12:32:47 +0200 Subject: [PATCH] TM: Allow to keep model checking enabled during autotuning Allow running the model checking during autotuning, with the only exception being the parameter estimation stage where we alter the same value which is used by the checker (done to conserve memory/code). With previous changes the model checker will disable/enable itself when passing through an unavailable R vector entry, allowing to start the calibration by checking only the stages where the part fan is disabled. The C/R0 values should be stable enough to provide a fail-safe mechanism for printers of the same variant right from the factory. --- Firmware/temperature.cpp | 27 +++++++++++++++++++++------ Firmware/temperature.h | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index 5c3f3cb55..465130da2 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -2502,6 +2502,13 @@ void log_isr() } // namespace temp_model +static void temp_model_reset_enabled(bool enabled) +{ + TempMgrGuard temp_mgr_guard; + temp_model::enabled = enabled; + temp_model::data.flag_bits.uninitialized = true; +} + void temp_model_set_enabled(bool enabled) { // set the enabled flag @@ -2566,8 +2573,9 @@ void temp_model_reset_settings() TempMgrGuard temp_mgr_guard; temp_model::data.P = TEMP_MODEL_P; - temp_model::data.C = NAN; - for(uint8_t i = 0; i != TEMP_MODEL_R_SIZE; ++i) + temp_model::data.C = TEMP_MODEL_C; + temp_model::data.R[0] = TEMP_MODEL_R; + for(uint8_t i = 1; i != TEMP_MODEL_R_SIZE; ++i) temp_model::data.R[i] = NAN; temp_model::data.Ta_corr = TEMP_MODEL_Ta_corr; temp_model::data.warn = TEMP_MODEL_W; @@ -2729,6 +2737,11 @@ static float estimate(uint16_t samples, float thr, uint16_t max_itr, uint8_t fan_pwm, float ambient) { + // during estimation we alter the model values without an extra copy to conserve memory + // so we cannot keep the main checker active until a value has been found + bool was_enabled = temp_model::enabled; + temp_model_reset_enabled(false); + float orig = *var; float e = NAN; float points[2]; @@ -2752,12 +2765,14 @@ static float estimate(uint16_t samples, } *var = x; + temp_model_reset_enabled(was_enabled); return e; } } SERIAL_ECHOLNPGM("TM estimation did not converge"); *var = orig; + temp_model_reset_enabled(was_enabled); return NAN; } @@ -2857,7 +2872,7 @@ static bool autotune(int16_t cal_temp) } // namespace temp_model_cal -void temp_model_autotune(int16_t temp) +void temp_model_autotune(int16_t temp, bool selftest) { if(moves_planned() || printer_active()) { SERIAL_ECHOLNPGM("TM: printer needs to be idle for calibration"); @@ -2870,15 +2885,15 @@ void temp_model_autotune(int16_t temp) lcd_setstatuspgm(_i("Temp. model autotune")); lcd_return_to_status(); - // disable the model checking during self-calibration + // set the model checking state during self-calibration bool was_enabled = temp_model::enabled; - temp_model_set_enabled(false); + temp_model_reset_enabled(selftest); SERIAL_ECHOLNPGM("TM: autotune start"); bool err = temp_model_cal::autotune(temp > 0 ? temp : TEMP_MODEL_CAL_Th); // always reset temperature - target_temperature[0] = 0; + disable_heater(); if(err) { SERIAL_ECHOLNPGM("TM: autotune failed"); diff --git a/Firmware/temperature.h b/Firmware/temperature.h index 05251d85f..85f4d91d8 100755 --- a/Firmware/temperature.h +++ b/Firmware/temperature.h @@ -227,7 +227,7 @@ void temp_model_reset_settings(); void temp_model_load_settings(); void temp_model_save_settings(); -void temp_model_autotune(int16_t temp = 0); +void temp_model_autotune(int16_t temp = 0, bool selftest = false); #ifdef TEMP_MODEL_DEBUG void temp_model_log_enable(bool enable);