Browse Source

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.
Yuri D'Elia 2 years ago
parent
commit
6832ec7648

+ 13 - 0
Firmware/ConfigurationStore.cpp

@@ -75,6 +75,9 @@ void Config_StoreSettings()
   
   if (EEPROM_writeData(reinterpret_cast<uint8_t*>(EEPROM_M500_base),reinterpret_cast<uint8_t*>(&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<uint8_t*>(EEPROM_M500_base->version), reinterpret_cast<uint8_t*>(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();
 

+ 6 - 3
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)

+ 9 - 1
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 !!!!!
 // !!!!!

+ 0 - 1
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
 

+ 54 - 23
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<<OCF5A);
     ENABLE_TEMP_MGR_INTERRUPT();
@@ -2391,21 +2387,6 @@ void setup()
     data.flag_bits.uninitialized = true;
 }
 
-void init()
-{
-    // TODO: initialize the model with eeprom values
-    data.P = TEMP_MODEL_P;
-    data.C = TEMP_MODEL_C;
-    for(uint8_t i = 0; i != TEMP_MODEL_R_SIZE; ++i)
-        data.R[i] = TEMP_MODEL_R;
-    data.Ta_corr = TEMP_MODEL_Ta_corr;
-    data.warn = TEMP_MODEL_W;
-    data.err = TEMP_MODEL_E;
-
-    enabled = true;
-    setup();
-}
-
 bool calibrated()
 {
     if(!(data.P >= 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)

+ 5 - 0
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