From a1fb94baf71fc8bf5ea8598bc6de48936d8a48ba Mon Sep 17 00:00:00 2001 From: "github@matthewlloyd.net" Date: Mon, 28 Jun 2021 18:04:55 -0400 Subject: [PATCH] Automatically save PID settings to EEPROM after M301, M304, and M303 U1 --- README.md | 21 +++++++++++++++++++++ src/common/marlin_server.cpp | 14 ++++++++++++++ src/common/marlin_server.h | 2 ++ src/marlin_stubs/gcode.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/README.md b/README.md index 002c7042c..0b0be35e6 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ a few goodies: * **OctoPrint screen**: Adds support for `M73` (print progress) and `M117` (LCD messages). * **Sound**: Adds support for `M300` (play a sound). +* **PID tuning**: Automatically writes PID settings to EEPROM after `M303 U1` (autotune), + `M301` (set hotend PID), and `M304` (set bed PID). All settings are automatically saved to EEPROM and loaded on boot. @@ -137,6 +139,25 @@ mesh bed leveling. A skew factor of e.g. 0.01 equates to `0.01 * 180mm = 1.8mm` of movement at the far end of the bed, so your usable print area will be reduced accordingly. +### Configuring PID Parameters + +The stock firmware allows you to run an `M303` PID autotune, but the new +settings are lost on reset. In Llama, PID settings are *automatically* written +to EEPROM after any command that updates Marlin's PID values, which could be +an `M301` (set hotend PID), `M304` (set bed PID), or an `M303 U1` (autotune and +use the PID result). These values will then be restored on reset, too. You do not +need to use `M500`. + +If you need to restore the default PID values, they can be reset by running +the following commands: + +* Hotend: `M301 P7.00 I0.50 D45.00` +* Bed: `M304 P120.00 I1.50 D600.0` + +Note that if you run `M303` (autotune) without the `U1` parameter, Marlin +will just print out the suggested PID values without changing the settings, +and they won't get written to EEPROM. + --- ### Print Progress diff --git a/src/common/marlin_server.cpp b/src/common/marlin_server.cpp index ceb4be3ca..f8b191b7a 100644 --- a/src/common/marlin_server.cpp +++ b/src/common/marlin_server.cpp @@ -414,6 +414,20 @@ void marlin_server_settings_save(void) { #endif } +void marlin_server_settings_save_bed_pid(void) { + eeprom_set_var(EEVAR_PID_BED_P, variant8_flt(Temperature::temp_bed.pid.Kp)); + eeprom_set_var(EEVAR_PID_BED_I, variant8_flt(Temperature::temp_bed.pid.Ki)); + eeprom_set_var(EEVAR_PID_BED_D, variant8_flt(Temperature::temp_bed.pid.Kd)); +} + +void marlin_server_settings_save_noz_pid(void) { +#if ENABLED(PIDTEMP) + eeprom_set_var(EEVAR_PID_NOZ_P, variant8_flt(Temperature::temp_hotend[0].pid.Kp)); + eeprom_set_var(EEVAR_PID_NOZ_I, variant8_flt(Temperature::temp_hotend[0].pid.Ki)); + eeprom_set_var(EEVAR_PID_NOZ_D, variant8_flt(Temperature::temp_hotend[0].pid.Kd)); +#endif +} + void marlin_server_settings_load(void) { (void)settings.reset(); #if HAS_BED_PROBE diff --git a/src/common/marlin_server.h b/src/common/marlin_server.h index 59d64c73e..8ae0682c4 100644 --- a/src/common/marlin_server.h +++ b/src/common/marlin_server.h @@ -55,6 +55,8 @@ extern int marlin_server_inject_gcode(const char *gcode); // direct call of settings.save() extern void marlin_server_settings_save(void); +extern void marlin_server_settings_save_bed_pid(void); +extern void marlin_server_settings_save_noz_pid(void); // direct call of settings.load() extern void marlin_server_settings_load(void); diff --git a/src/marlin_stubs/gcode.cpp b/src/marlin_stubs/gcode.cpp index 589c3719d..1a182f7fa 100644 --- a/src/marlin_stubs/gcode.cpp +++ b/src/marlin_stubs/gcode.cpp @@ -3,6 +3,7 @@ #include "../../lib/Marlin/Marlin/src/gcode/queue.h" #include "PrusaGcodeSuite.hpp" +#include "../common/marlin_server.h" #include "M330.h" #include "M50.hpp" @@ -22,6 +23,30 @@ bool GcodeSuite::process_parsed_command_custom(bool no_ok) { PrusaGcodeSuite::M300(); if (!no_ok) queue.ok_to_send(); return true; + case 301: + M301(); + marlin_server_settings_save_noz_pid(); + if (!no_ok) queue.ok_to_send(); + return true; + case 303: { + M303(); + const int16_t e = parser.intval('E'); + const bool u = parser.boolval('U'); + if (u) { + if (e == -1) + marlin_server_settings_save_bed_pid(); + else if (e == 0) + marlin_server_settings_save_noz_pid(); + } + if (!no_ok) + queue.ok_to_send(); + return true; + } + case 304: + M304(); + marlin_server_settings_save_bed_pid(); + if (!no_ok) queue.ok_to_send(); + return true; #if defined(_DEBUG) case 330: PrusaGcodeSuite::M330();