Skip to content

Commit

Permalink
🐛 Fix Bed/Chamber PID Autotune with MPCTEMP (MarlinFirmware#23983)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMagician authored and LCh-77 committed Apr 19, 2022
1 parent 86d0b90 commit 59c2ece
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
5 changes: 3 additions & 2 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,6 @@
#define PID_K1 0.95 // Smoothing factor within any PID loop

#if ENABLED(PIDTEMP)
//#define PID_EDIT_MENU // Add PID editing to the "Advanced Settings" menu. (~700 bytes of PROGMEM)
//#define PID_AUTOTUNE_MENU // Add PID auto-tuning to the "Advanced Settings" menu. (~250 bytes of PROGMEM)
//#define PID_PARAMS_PER_HOTEND // Uses separate PID parameters for each extruder (useful for mismatched extruders)
// Set/get with gcode: M301 E[extruder number, 0-2]

Expand Down Expand Up @@ -767,6 +765,9 @@
//#define SLOW_PWM_HEATERS // PWM with very low frequency (roughly 0.125Hz=8s) and minimum state time of approximately 1s useful for heaters driven by a relay
#define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature
// is more than PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.

//#define PID_EDIT_MENU // Add PID editing to the "Advanced Settings" menu. (~700 bytes of PROGMEM)
//#define PID_AUTOTUNE_MENU // Add PID auto-tuning to the "Advanced Settings" menu. (~250 bytes of PROGMEM)
#endif

// @section extruder
Expand Down
66 changes: 23 additions & 43 deletions Marlin/src/lcd/menu/menu_advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void menu_backlash();

// Helpers for editing PID Ki & Kd values
// grab the PID value out of the temp variable; scale it; then update the PID driver
void copy_and_scalePID_i(int16_t e) {
void copy_and_scalePID_i(const uint8_t e) {
switch (e) {
#if ENABLED(PIDTEMPBED)
case H_BED: thermalManager.temp_bed.pid.Ki = scalePID_i(raw_Ki); break;
Expand All @@ -226,7 +226,7 @@ void menu_backlash();
break;
}
}
void copy_and_scalePID_d(int16_t e) {
void copy_and_scalePID_d(const uint8_t e) {
switch (e) {
#if ENABLED(PIDTEMPBED)
case H_BED: thermalManager.temp_bed.pid.Kd = scalePID_d(raw_Kd); break;
Expand All @@ -242,30 +242,6 @@ void menu_backlash();
break;
}
}

#define _DEFINE_PIDTEMP_BASE_FUNCS(N) \
void copy_and_scalePID_i_E##N() { copy_and_scalePID_i(N); } \
void copy_and_scalePID_d_E##N() { copy_and_scalePID_d(N); }

#else

#define _DEFINE_PIDTEMP_BASE_FUNCS(N) //

#endif

#if ENABLED(PID_AUTOTUNE_MENU)
#define DEFINE_PIDTEMP_FUNCS(N) \
_DEFINE_PIDTEMP_BASE_FUNCS(N); \
void lcd_autotune_callback_E##N() { _lcd_autotune(heater_id_t(N)); }
#else
#define DEFINE_PIDTEMP_FUNCS(N) _DEFINE_PIDTEMP_BASE_FUNCS(N);
#endif

#if HAS_HOTEND
DEFINE_PIDTEMP_FUNCS(0);
#if ENABLED(PID_PARAMS_PER_HOTEND)
REPEAT_S(1, HOTENDS, DEFINE_PIDTEMP_FUNCS)
#endif
#endif

#if BOTH(AUTOTEMP, HAS_TEMP_HOTEND) || EITHER(PID_AUTOTUNE_MENU, PID_EDIT_MENU)
Expand Down Expand Up @@ -299,14 +275,7 @@ void menu_backlash();
// PID-P E5, PID-I E5, PID-D E5, PID-C E5, PID Autotune E5
//

#if ENABLED(PID_EDIT_MENU)
#define _PID_EDIT_ITEMS_TMPL(N,T) \
raw_Ki = unscalePID_i(T.pid.Ki); \
raw_Kd = unscalePID_d(T.pid.Kd); \
EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &T.pid.Kp, 1, 9990); \
EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ copy_and_scalePID_i(N); }); \
EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ copy_and_scalePID_d(N); })

#if BOTH(PIDTEMP, PID_EDIT_MENU)
#define __PID_HOTEND_MENU_ITEMS(N) \
raw_Ki = unscalePID_i(PID_PARAM(Ki, N)); \
raw_Kd = unscalePID_d(PID_PARAM(Kd, N)); \
Expand Down Expand Up @@ -336,17 +305,28 @@ void menu_backlash();

#endif

#if ENABLED(PID_AUTOTUNE_MENU)
#define HOTEND_PID_EDIT_MENU_ITEMS(N) \
_HOTEND_PID_EDIT_MENU_ITEMS(N); \
EDIT_ITEM_FAST_N(int3, N, MSG_PID_AUTOTUNE_E, &autotune_temp[N], 150, thermalManager.hotend_max_target(N), []{ _lcd_autotune(heater_id_t(MenuItemBase::itemIndex)); });
#else
#define HOTEND_PID_EDIT_MENU_ITEMS(N) _HOTEND_PID_EDIT_MENU_ITEMS(N);
#if ENABLED(PID_EDIT_MENU) && EITHER(PIDTEMPBED, PIDTEMPCHAMBER)
#define _PID_EDIT_ITEMS_TMPL(N,T) \
raw_Ki = unscalePID_i(T.pid.Ki); \
raw_Kd = unscalePID_d(T.pid.Kd); \
EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &T.pid.Kp, 1, 9990); \
EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ copy_and_scalePID_i(N); }); \
EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ copy_and_scalePID_d(N); })
#endif

HOTEND_PID_EDIT_MENU_ITEMS(0);
#if ENABLED(PID_PARAMS_PER_HOTEND)
REPEAT_S(1, HOTENDS, HOTEND_PID_EDIT_MENU_ITEMS)
#if ENABLED(PIDTEMP)
#if ENABLED(PID_AUTOTUNE_MENU)
#define HOTEND_PID_EDIT_MENU_ITEMS(N) \
_HOTEND_PID_EDIT_MENU_ITEMS(N); \
EDIT_ITEM_FAST_N(int3, N, MSG_PID_AUTOTUNE_E, &autotune_temp[N], 150, thermalManager.hotend_max_target(N), []{ _lcd_autotune(heater_id_t(MenuItemBase::itemIndex)); });
#else
#define HOTEND_PID_EDIT_MENU_ITEMS(N) _HOTEND_PID_EDIT_MENU_ITEMS(N);
#endif

HOTEND_PID_EDIT_MENU_ITEMS(0);
#if ENABLED(PID_PARAMS_PER_HOTEND)
REPEAT_S(1, HOTENDS, HOTEND_PID_EDIT_MENU_ITEMS)
#endif
#endif

#if ENABLED(PIDTEMPBED)
Expand Down

0 comments on commit 59c2ece

Please sign in to comment.