From 6832ec76484c440aaf5687ebc687b692879541d2 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Sun, 26 Jun 2022 13:13:59 +0200 Subject: [PATCH] Allow to save/restore temperature model settings This currently bypasses the ConfigurationStore, which doesn't fit the malin model nicely. temp_model is using it's own private copy directly. But maybe we should change this in the future. --- Firmware/ConfigurationStore.cpp | 13 ++++++ Firmware/Marlin_main.cpp | 9 ++-- Firmware/eeprom.h | 10 ++++- Firmware/temp_model.h | 1 - Firmware/temperature.cpp | 77 +++++++++++++++++++++++---------- Firmware/temperature.h | 5 +++ 6 files changed, 87 insertions(+), 28 deletions(-) diff --git a/Firmware/ConfigurationStore.cpp b/Firmware/ConfigurationStore.cpp index 77ef6df66..0917ee181 100644 --- a/Firmware/ConfigurationStore.cpp +++ b/Firmware/ConfigurationStore.cpp @@ -75,6 +75,9 @@ void Config_StoreSettings() if (EEPROM_writeData(reinterpret_cast(EEPROM_M500_base),reinterpret_cast(&cs),sizeof(cs),0), "cs, invalid version") { +#ifdef TEMP_MODEL + temp_model_save_settings(); +#endif strcpy(cs.version,EEPROM_VERSION); //!< validate data if write succeed EEPROM_writeData(reinterpret_cast(EEPROM_M500_base->version), reinterpret_cast(cs.version), sizeof(cs.version), "cs.version valid"); } @@ -173,6 +176,9 @@ void Config_PrintSettings(uint8_t level) printf_P(PSTR( "%SArc Settings: P:Max length(mm) S:Min length (mm) N:Corrections R:Min segments F:Segments/sec.\n%S M214 P%.2f S%.2f N%d R%d F%d\n"), echomagic, echomagic, cs.mm_per_arc_segment, cs.min_mm_per_arc_segment, cs.n_arc_correction, cs.min_arc_segments, cs.arc_segments_per_sec); +#ifdef TEMP_MODEL + temp_model_report_settings(); +#endif } #endif @@ -321,6 +327,10 @@ bool Config_RetrieveSettings() // Call updatePID (similar to when we have processed M301) updatePID(); +#ifdef TEMP_MODEL + temp_model_load_settings(); +#endif + SERIAL_ECHO_START; SERIAL_ECHOLNPGM("Stored settings retrieved"); } @@ -353,6 +363,9 @@ void Config_ResetDefault() #ifdef PIDTEMP updatePID(); #endif//PIDTEMP +#ifdef TEMP_MODEL + temp_model_reset_settings(); +#endif calculate_extruder_multipliers(); diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index ed71ce352..b3176d639 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -7765,13 +7765,16 @@ Sigma_Exit: ### M310 - Temperature model #### Usage - M310 [ C ] [ P ] [ I R ] [ S ] [ E ] [ W ] [ A ] [ T ] + M310 + M310 [ I ] [ R ] + M310 [ P ] [ C ] [ S ] [ E ] [ W ] [ T ] + M310 [ A ] #### Parameters - - `P` - power - - `C` - capacitance - `I` - resistance index position - `R` - resistance value (requires `I`) + - `P` - power + - `C` - capacitance - `S` - set 0=disable 1=enable (default) - `E` - error threshold (define min/max values in variants) - `W` - warning threshold (define min/max values in variants) diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index abfe33b58..d3ef0a28f 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -550,8 +550,16 @@ static Sheets * const EEPROM_Sheets_base = (Sheets*)(EEPROM_SHEETS_BASE); #define EEPROM_ECOOL_ENABLE (EEPROM_JOB_ID-1) // uint8_t #define EEPROM_FW_CRASH_FLAG (EEPROM_ECOOL_ENABLE-1) // uint8_t +#define EEPROM_TEMP_MODEL_ENABLE (EEPROM_FW_CRASH_FLAG-1) // uint8_t +#define EEPROM_TEMP_MODEL_P (EEPROM_TEMP_MODEL_ENABLE-4) // float +#define EEPROM_TEMP_MODEL_C (EEPROM_TEMP_MODEL_P-4) // float +#define EEPROM_TEMP_MODEL_R (EEPROM_TEMP_MODEL_C-4*16) // float[16] +#define EEPROM_TEMP_MODEL_Ta_corr (EEPROM_TEMP_MODEL_R-4) // float +#define EEPROM_TEMP_MODEL_W (EEPROM_TEMP_MODEL_Ta_corr-4) // float +#define EEPROM_TEMP_MODEL_E (EEPROM_TEMP_MODEL_W-4) // float + //This is supposed to point to last item to allow EEPROM overrun check. Please update when adding new items. -#define EEPROM_LAST_ITEM EEPROM_FW_CRASH_FLAG +#define EEPROM_LAST_ITEM EEPROM_TEMP_MODEL_E // !!!!! // !!!!! this is end of EEPROM section ... all updates MUST BE inserted before this mark !!!!! // !!!!! diff --git a/Firmware/temp_model.h b/Firmware/temp_model.h index 30445f0f9..5fed2be7a 100644 --- a/Firmware/temp_model.h +++ b/Firmware/temp_model.h @@ -72,7 +72,6 @@ struct model_data static bool enabled; // model check enabled static model_data data; // default heater data -static void init(); // initialize and setup the model subsystem static bool calibrated(); // return calibration/model validity status static void check(); // check and trigger errors or warnings based on current state diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index f306618f0..904085680 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -1891,10 +1891,6 @@ void temp_mgr_init() TCNT5 = 0; OCR5A = TIMER5_OCRA_OVF; -#ifdef TEMP_MODEL - temp_model::init(); -#endif - // clear pending interrupts, enable COMPA TIFR5 |= (1<= 0)) return false; @@ -2567,12 +2548,62 @@ void temp_model_set_resistance(uint8_t index, float R) void temp_model_report_settings() { - printf_P(PSTR("%STemperature Model settings:\n" - "%S M310 P%.2f C%.2f S%u E%.2f W%.2f T%.2f\n"), - echomagic, echomagic, (double)temp_model::data.P, (double)temp_model::data.C, (unsigned)temp_model::enabled, - (double)temp_model::data.err, (double)temp_model::data.warn, (double)temp_model::data.Ta_corr); + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("Temperature Model settings:"); for(uint8_t i = 0; i != TEMP_MODEL_R_SIZE; ++i) printf_P(PSTR("%S M310 I%u R%.2f\n"), echomagic, (unsigned)i, (double)temp_model::data.R[i]); + printf_P(PSTR("%S M310 P%.2f C%.2f S%u B%u E%.2f W%.2f T%.2f\n"), + echomagic, (double)temp_model::data.P, (double)temp_model::data.C, + (unsigned)temp_model::enabled, (unsigned)temp_model::warn_beep, + (double)temp_model::data.err, (double)temp_model::data.warn, + (double)temp_model::data.Ta_corr); +} + +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.R[i] = NAN; + temp_model::data.Ta_corr = TEMP_MODEL_Ta_corr; + temp_model::data.warn = TEMP_MODEL_W; + temp_model::data.err = TEMP_MODEL_E; + temp_model::enabled = false; +} + +void temp_model_load_settings() +{ + static_assert(TEMP_MODEL_R_SIZE == 16); // ensure we don't desync with the eeprom table + TempMgrGuard temp_mgr_guard; + + temp_model::enabled = eeprom_read_byte((uint8_t*)EEPROM_TEMP_MODEL_ENABLE); + temp_model::data.P = eeprom_read_float((float*)EEPROM_TEMP_MODEL_P); + temp_model::data.C = eeprom_read_float((float*)EEPROM_TEMP_MODEL_C); + for(uint8_t i = 0; i != TEMP_MODEL_R_SIZE; ++i) + temp_model::data.R[i] = eeprom_read_float((float*)EEPROM_TEMP_MODEL_R + i); + temp_model::data.Ta_corr = eeprom_read_float((float*)EEPROM_TEMP_MODEL_Ta_corr); + temp_model::data.warn = eeprom_read_float((float*)EEPROM_TEMP_MODEL_W); + temp_model::data.err = eeprom_read_float((float*)EEPROM_TEMP_MODEL_E); + + if(!temp_model::calibrated()) { + SERIAL_ECHOLNPGM("TM: stored calibration invalid, resetting"); + temp_model_reset_settings(); + } + temp_model::setup(); +} + +void temp_model_save_settings() +{ + eeprom_update_byte((uint8_t*)EEPROM_TEMP_MODEL_ENABLE, temp_model::enabled); + eeprom_update_float((float*)EEPROM_TEMP_MODEL_P, temp_model::data.P); + eeprom_update_float((float*)EEPROM_TEMP_MODEL_C, temp_model::data.C); + for(uint8_t i = 0; i != TEMP_MODEL_R_SIZE; ++i) + eeprom_update_float((float*)EEPROM_TEMP_MODEL_R + i, temp_model::data.R[i]); + eeprom_update_float((float*)EEPROM_TEMP_MODEL_Ta_corr, temp_model::data.Ta_corr); + eeprom_update_float((float*)EEPROM_TEMP_MODEL_W, temp_model::data.warn); + eeprom_update_float((float*)EEPROM_TEMP_MODEL_E, temp_model::data.err); } void temp_model_autotune(float temp) diff --git a/Firmware/temperature.h b/Firmware/temperature.h index ad170ecf3..7e41cdc2f 100755 --- a/Firmware/temperature.h +++ b/Firmware/temperature.h @@ -233,7 +233,12 @@ void PID_autotune(float temp, int extruder, int ncycles); void temp_model_set_enabled(bool enabled); void temp_model_set_params(float C = NAN, float P = NAN, float Ta_corr = NAN, float warn = NAN, float err = NAN); void temp_model_set_resistance(uint8_t index, float R); + void temp_model_report_settings(); +void temp_model_reset_settings(); +void temp_model_load_settings(); +void temp_model_save_settings(); + void temp_model_autotune(float temp = NAN); #ifdef TEMP_MODEL_DEBUG