Skip to content

Commit 11d50a7

Browse files
thinkyheadAlexander Yasnogor
authored andcommitted
Per-Hotend Default PIDs (MarlinFirmware#18483)
1 parent e1bf868 commit 11d50a7

6 files changed

Lines changed: 96 additions & 61 deletions

File tree

Marlin/src/gcode/config/M301.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
/**
3131
* M301: Set PID parameters P I D (and optionally C, L)
3232
*
33+
* E[extruder] Default: 0
34+
*
3335
* P[float] Kp term
3436
* I[float] Ki term (unscaled)
3537
* D[float] Kd term (unscaled)
@@ -65,6 +67,7 @@ void GcodeSuite::M301() {
6567
#endif
6668

6769
thermalManager.updatePID();
70+
6871
SERIAL_ECHO_START();
6972
#if ENABLED(PID_PARAMS_PER_HOTEND)
7073
SERIAL_ECHOPAIR(" e:", e); // specify extruder in serial output

Marlin/src/inc/Conditionals_LCD.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@
477477
#define HAS_MULTI_HOTEND 1
478478
#define HAS_HOTEND_OFFSET 1
479479
#endif
480+
#else
481+
#undef PID_PARAMS_PER_HOTEND
480482
#endif
481483

482484
// Helper macros for extruder and hotend arrays

Marlin/src/inc/SanityCheck.h

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,28 +1629,8 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
16291629
#endif
16301630

16311631
/**
1632-
* Test Heater, Temp Sensor, and Extruder Pins; Sensor Type must also be set.
1632+
* A Sensor ID has to be set for each heater
16331633
*/
1634-
#if !HAS_HEATER_0
1635-
#error "HEATER_0_PIN not defined for this board."
1636-
#elif !ANY_PIN(TEMP_0, MAX6675_SS)
1637-
#error "TEMP_0_PIN not defined for this board."
1638-
#elif ((defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)) && !PINS_EXIST(E0_STEP, E0_DIR))
1639-
#error "E0_STEP_PIN or E0_DIR_PIN not defined for this board."
1640-
#elif ( !(defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)) && (!PINS_EXIST(E0_STEP, E0_DIR) || !HAS_E0_ENABLE))
1641-
#error "E0_STEP_PIN, E0_DIR_PIN, or E0_ENABLE_PIN not defined for this board."
1642-
#elif EXTRUDERS && TEMP_SENSOR_0 == 0
1643-
#error "TEMP_SENSOR_0 is required with any extruders."
1644-
#endif
1645-
1646-
// Pins are required for heaters
1647-
#if ENABLED(HEATER_0_USES_MAX6675) && !PIN_EXISTS(MAX6675_SS)
1648-
#error "MAX6675_SS_PIN (required for TEMP_SENSOR_0) not defined for this board."
1649-
#elif HAS_HOTEND && !HAS_TEMP_HOTEND
1650-
#error "TEMP_0_PIN (required for TEMP_SENSOR_0) not defined for this board."
1651-
#elif EITHER(HAS_MULTI_HOTEND, HEATERS_PARALLEL) && !HAS_HEATER_1
1652-
#error "HEATER_1_PIN not defined for this board."
1653-
#endif
16541634

16551635
#if HAS_MULTI_HOTEND
16561636
#if ENABLED(HEATER_1_USES_MAX6675) && !PIN_EXISTS(MAX6675_SS2)
@@ -1792,6 +1772,30 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
17921772
#error "TEMP_SENSOR_1 is required with TEMP_SENSOR_1_AS_REDUNDANT."
17931773
#endif
17941774

1775+
/**
1776+
* Test Heater, Temp Sensor, and Extruder Pins
1777+
*/
1778+
#if !HAS_HEATER_0
1779+
#error "HEATER_0_PIN not defined for this board."
1780+
#elif !ANY_PIN(TEMP_0, MAX6675_SS)
1781+
#error "TEMP_0_PIN not defined for this board."
1782+
#elif ((defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)) && !PINS_EXIST(E0_STEP, E0_DIR))
1783+
#error "E0_STEP_PIN or E0_DIR_PIN not defined for this board."
1784+
#elif ( !(defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)) && (!PINS_EXIST(E0_STEP, E0_DIR) || !HAS_E0_ENABLE))
1785+
#error "E0_STEP_PIN, E0_DIR_PIN, or E0_ENABLE_PIN not defined for this board."
1786+
#elif EXTRUDERS && TEMP_SENSOR_0 == 0
1787+
#error "TEMP_SENSOR_0 is required if there are any extruders."
1788+
#endif
1789+
1790+
// Pins are required for heaters
1791+
#if ENABLED(HEATER_0_USES_MAX6675) && !PIN_EXISTS(MAX6675_SS)
1792+
#error "MAX6675_SS_PIN (required for TEMP_SENSOR_0) not defined for this board."
1793+
#elif HAS_HOTEND && !HAS_TEMP_HOTEND
1794+
#error "TEMP_0_PIN (required for TEMP_SENSOR_0) not defined for this board."
1795+
#elif EITHER(HAS_MULTI_HOTEND, HEATERS_PARALLEL) && !HAS_HEATER_1
1796+
#error "HEATER_1_PIN is not defined. TEMP_SENSOR_1 might not be set, or the board (not EEB / EEF?) doesn't define a pin."
1797+
#endif
1798+
17951799
/**
17961800
* Temperature status LEDs
17971801
*/

Marlin/src/lcd/menu/menu_advanced.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,12 @@ void menu_cancelobject();
205205
// Helpers for editing PID Ki & Kd values
206206
// grab the PID value out of the temp variable; scale it; then update the PID driver
207207
void copy_and_scalePID_i(int16_t e) {
208-
#if DISABLED(PID_PARAMS_PER_HOTEND) || HOTENDS == 1
209-
UNUSED(e);
210-
#endif
208+
TERN(PID_PARAMS_PER_HOTEND,,UNUSED(e));
211209
PID_PARAM(Ki, e) = scalePID_i(raw_Ki);
212210
thermalManager.updatePID();
213211
}
214212
void copy_and_scalePID_d(int16_t e) {
215-
#if DISABLED(PID_PARAMS_PER_HOTEND) || HOTENDS == 1
216-
UNUSED(e);
217-
#endif
213+
TERN(PID_PARAMS_PER_HOTEND,,UNUSED(e));
218214
PID_PARAM(Kd, e) = scalePID_d(raw_Kd);
219215
thermalManager.updatePID();
220216
}
@@ -239,7 +235,7 @@ void menu_cancelobject();
239235

240236
#if HAS_HOTEND
241237
DEFINE_PIDTEMP_FUNCS(0);
242-
#if BOTH(HAS_MULTI_HOTEND, PID_PARAMS_PER_HOTEND)
238+
#if ENABLED(PID_PARAMS_PER_HOTEND)
243239
REPEAT_S(1, HOTENDS, DEFINE_PIDTEMP_FUNCS)
244240
#endif
245241
#endif
@@ -314,7 +310,7 @@ void menu_cancelobject();
314310
#endif
315311

316312
PID_EDIT_MENU_ITEMS(0);
317-
#if BOTH(HAS_MULTI_HOTEND, PID_PARAMS_PER_HOTEND)
313+
#if ENABLED(PID_PARAMS_PER_HOTEND)
318314
REPEAT_S(1, HOTENDS, PID_EDIT_MENU_ITEMS)
319315
#endif
320316

Marlin/src/module/configuration_store.cpp

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,12 +2587,59 @@ void MarlinSettings::reset() {
25872587
//
25882588

25892589
#if ENABLED(PIDTEMP)
2590+
#if ENABLED(PID_PARAMS_PER_HOTEND)
2591+
constexpr float defKp[] =
2592+
#ifdef DEFAULT_Kp_LIST
2593+
DEFAULT_Kp_LIST
2594+
#else
2595+
ARRAY_BY_HOTENDS1(DEFAULT_Kp)
2596+
#endif
2597+
, defKi[] =
2598+
#ifdef DEFAULT_Ki_LIST
2599+
DEFAULT_Ki_LIST
2600+
#else
2601+
ARRAY_BY_HOTENDS1(DEFAULT_Ki)
2602+
#endif
2603+
, defKd[] =
2604+
#ifdef DEFAULT_Kd_LIST
2605+
DEFAULT_Kd_LIST
2606+
#else
2607+
ARRAY_BY_HOTENDS1(DEFAULT_Kd)
2608+
#endif
2609+
;
2610+
static_assert(WITHIN(COUNT(defKp), 1, HOTENDS), "DEFAULT_Kp_LIST must have between 1 and HOTENDS items.");
2611+
static_assert(WITHIN(COUNT(defKi), 1, HOTENDS), "DEFAULT_Ki_LIST must have between 1 and HOTENDS items.");
2612+
static_assert(WITHIN(COUNT(defKd), 1, HOTENDS), "DEFAULT_Kd_LIST must have between 1 and HOTENDS items.");
2613+
#if ENABLED(PID_EXTRUSION_SCALING)
2614+
constexpr float defKc[] =
2615+
#ifdef DEFAULT_Kc_LIST
2616+
DEFAULT_Kc_LIST
2617+
#else
2618+
ARRAY_BY_HOTENDS1(DEFAULT_Kc)
2619+
#endif
2620+
;
2621+
static_assert(WITHIN(COUNT(defKc), 1, HOTENDS), "DEFAULT_Kc_LIST must have between 1 and HOTENDS items.");
2622+
#endif
2623+
#if ENABLED(PID_FAN_SCALING)
2624+
constexpr float defKf[] =
2625+
#ifdef DEFAULT_Kf_LIST
2626+
DEFAULT_Kf_LIST
2627+
#else
2628+
ARRAY_BY_HOTENDS1(DEFAULT_Kf)
2629+
#endif
2630+
;
2631+
static_assert(WITHIN(COUNT(defKf), 1, HOTENDS), "DEFAULT_Kf_LIST must have between 1 and HOTENDS items.");
2632+
#endif
2633+
#define PID_DEFAULT(N,E) def##N[E]
2634+
#else
2635+
#define PID_DEFAULT(N,E) DEFAULT_##N
2636+
#endif
25902637
HOTEND_LOOP() {
2591-
PID_PARAM(Kp, e) = float(DEFAULT_Kp);
2592-
PID_PARAM(Ki, e) = scalePID_i(DEFAULT_Ki);
2593-
PID_PARAM(Kd, e) = scalePID_d(DEFAULT_Kd);
2594-
TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = DEFAULT_Kc);
2595-
TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = DEFAULT_Kf);
2638+
PID_PARAM(Kp, e) = float(PID_DEFAULT(Kp, ALIM(e, defKp)));
2639+
PID_PARAM(Ki, e) = scalePID_i(PID_DEFAULT(Ki, ALIM(e, defKi)));
2640+
PID_PARAM(Kd, e) = scalePID_d(PID_DEFAULT(Kd, ALIM(e, defKd)));
2641+
TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = float(PID_DEFAULT(Kc, ALIM(e, defKc))));
2642+
TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = float(PID_DEFAULT(Kf, ALIM(e, defKf))));
25962643
}
25972644
#endif
25982645

@@ -3140,7 +3187,7 @@ void MarlinSettings::reset() {
31403187
HOTEND_LOOP() {
31413188
CONFIG_ECHO_START();
31423189
SERIAL_ECHOPAIR_P(
3143-
#if BOTH(HAS_MULTI_HOTEND, PID_PARAMS_PER_HOTEND)
3190+
#if ENABLED(PID_PARAMS_PER_HOTEND)
31443191
PSTR(" M301 E"), e,
31453192
SP_P_STR
31463193
#else

Marlin/src/module/temperature.h

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@
3737
#define SOFT_PWM_SCALE 0
3838
#endif
3939

40-
#if HOTENDS <= 1
41-
#define HOTEND_INDEX 0
42-
#define E_NAME
43-
#else
44-
#define HOTEND_INDEX e
45-
#define E_NAME e
46-
#endif
40+
#define HOTEND_INDEX TERN(HAS_MULTI_HOTEND, e, 0)
41+
#define E_NAME TERN_(HAS_MULTI_HOTEND, e)
4742

4843
// Identifiers for other heaters
4944
typedef enum : int8_t {
@@ -74,30 +69,18 @@ hotend_pid_t;
7469
typedef IF<(LPQ_MAX_LEN > 255), uint16_t, uint8_t>::type lpq_ptr_t;
7570
#endif
7671

72+
#define PID_PARAM(F,H) _PID_##F(TERN(PID_PARAMS_PER_HOTEND, H, 0))
73+
#define _PID_Kp(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kp, NAN)
74+
#define _PID_Ki(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Ki, NAN)
75+
#define _PID_Kd(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kd, NAN)
7776
#if ENABLED(PIDTEMP)
78-
#define _PID_Kp(H) Temperature::temp_hotend[H].pid.Kp
79-
#define _PID_Ki(H) Temperature::temp_hotend[H].pid.Ki
80-
#define _PID_Kd(H) Temperature::temp_hotend[H].pid.Kd
81-
#if ENABLED(PID_EXTRUSION_SCALING)
82-
#define _PID_Kc(H) Temperature::temp_hotend[H].pid.Kc
83-
#else
84-
#define _PID_Kc(H) 1
85-
#endif
86-
87-
#if ENABLED(PID_FAN_SCALING)
88-
#define _PID_Kf(H) Temperature::temp_hotend[H].pid.Kf
89-
#else
90-
#define _PID_Kf(H) 0
91-
#endif
77+
#define _PID_Kc(H) TERN(PID_EXTRUSION_SCALING, Temperature::temp_hotend[H].pid.Kc, 1)
78+
#define _PID_Kf(H) TERN(PID_FAN_SCALING, Temperature::temp_hotend[H].pid.Kf, 0)
9279
#else
93-
#define _PID_Kp(H) NAN
94-
#define _PID_Ki(H) NAN
95-
#define _PID_Kd(H) NAN
9680
#define _PID_Kc(H) 1
81+
#define _PID_Kf(H) 0
9782
#endif
9883

99-
#define PID_PARAM(F,H) _PID_##F(H)
100-
10184
/**
10285
* States for ADC reading in the ISR
10386
*/

0 commit comments

Comments
 (0)