From fabf511b97cfb41c882462cadc887f0a0483b28a Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 14 Jun 2022 12:11:58 +0200 Subject: [PATCH] Add the ability to log continuously TEMP_MODEL_LOGGING enables D70 to record precisely each cycle of the temperature regulation for offline model simulation --- Firmware/Marlin_main.cpp | 8 ++ Firmware/temperature.cpp | 85 +++++++++++++++++++ Firmware/temperature.h | 3 + .../variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h | 1 + 4 files changed, 97 insertions(+) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 098435980..e8027aa3e 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -9144,6 +9144,14 @@ Sigma_Exit: }; #endif +#ifdef TEMP_MODEL_LOGGING + case 70: { + if(code_seen('I')) + temp_model_log_enable(code_value_short()); + break; + } +#endif + #ifdef HEATBED_ANALYSIS /*! diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index f8ea4012e..d9353d2ea 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -500,6 +500,11 @@ void set_temp_error(TempErrorSource source, uint8_t index, TempErrorType type) void handle_temp_error(); +#ifdef TEMP_MODEL_LOGGING +static void temp_model_log_usr(); +static void temp_model_log_isr(); +#endif + void manage_heater() { #ifdef WATCHDOG @@ -520,6 +525,10 @@ void manage_heater() // periodically check fans checkFans(); + +#ifdef TEMP_MODEL_LOGGING + temp_model_log_usr(); +#endif } #define PGM_RD_W(x) (short)pgm_read_word(&x) @@ -2200,6 +2209,10 @@ static void temp_mgr_isr() check_temp_model(); // model-based heater check #endif +#ifdef TEMP_MODEL_LOGGING + temp_model_log_isr(); +#endif + // PID regulation if (pid_tuning_finished) temp_mgr_pid(); @@ -2390,4 +2403,76 @@ static void check_temp_model() set_temp_error(TempErrorSource::hotend, 0, TempErrorType::model); } } + +#ifdef TEMP_MODEL_LOGGING +static struct +{ + volatile struct + { + uint32_t stamp; + int8_t delta_ms; + uint8_t counter; + uint8_t cur_pwm; + float cur_temp; + float cur_amb; + } entry; + + uint8_t serial; + bool enabled; +} temp_model_log_buf; + +static void temp_model_log_usr() +{ + if(!temp_model_log_buf.enabled) + return; + + uint8_t counter = temp_model_log_buf.entry.counter; + if (counter == temp_model_log_buf.serial) + return; + + int8_t delta_ms; + uint8_t cur_pwm; + float cur_temp; + float cur_amb; + { + TempMgrGuard temp_mgr_guard; + delta_ms = temp_model_log_buf.entry.delta_ms; + counter = temp_model_log_buf.entry.counter; + cur_pwm = temp_model_log_buf.entry.cur_pwm; + cur_temp = temp_model_log_buf.entry.cur_temp; + cur_amb = temp_model_log_buf.entry.cur_amb; + } + + uint8_t d = counter - temp_model_log_buf.serial; + temp_model_log_buf.serial = counter; + + printf_P(PSTR("TML %d %d %x %lx %lx\n"), (unsigned)d - 1, (int)delta_ms + 1, (int)cur_pwm, + *(unsigned long*)&cur_temp, *(unsigned long*)&cur_amb); +} + +static void temp_model_log_isr() +{ + if(!temp_model_log_buf.enabled) + return; + + uint32_t stamp = _millis(); + uint8_t delta_ms = stamp - temp_model_log_buf.entry.stamp - (TEMP_MGR_INTV * 1000); + temp_model_log_buf.entry.stamp = stamp; + + ++temp_model_log_buf.entry.counter; + temp_model_log_buf.entry.delta_ms = delta_ms; + temp_model_log_buf.entry.cur_pwm = soft_pwm[0]; + temp_model_log_buf.entry.cur_temp = current_temperature_isr[0]; + temp_model_log_buf.entry.cur_amb = current_temperature_ambient_isr; +} + +void temp_model_log_enable(bool enable) +{ + if(enable) { + TempMgrGuard temp_mgr_guard; + temp_model_log_buf.entry.stamp = _millis(); + } + temp_model_log_buf.enabled = enable; +} +#endif #endif diff --git a/Firmware/temperature.h b/Firmware/temperature.h index c62d388ae..558c1fe27 100755 --- a/Firmware/temperature.h +++ b/Firmware/temperature.h @@ -228,6 +228,9 @@ FORCE_INLINE void autotempShutdown(){ } void PID_autotune(float temp, int extruder, int ncycles); +#ifdef TEMP_MODEL_LOGGING +void temp_model_log_enable(bool enable); +#endif #ifdef FAN_SOFT_PWM extern unsigned char fanSpeedSoftPwm; diff --git a/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h index 461d65448..205e43f0e 100644 --- a/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h @@ -420,6 +420,7 @@ // model-based temperature check #define TEMP_MODEL_CHECK 1 #define TEMP_MODEL_CHECK_WARN_ONLY 1 +#define TEMP_MODEL_LOGGING 1 /*------------------------------------ MOTOR CURRENT SETTINGS