Skip to content

Commit e4490e1

Browse files
feldi12EvilGremlin
authored andcommitted
⚡️ Optimize PID, increase PID range (MarlinFirmware#27740)
1 parent 4ed5797 commit e4490e1

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

Marlin/Configuration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@
937937
#if ANY(PIDTEMP, PIDTEMPBED, PIDTEMPCHAMBER)
938938
//#define PID_OPENLOOP // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX
939939
//#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
940-
#define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature
940+
#define PID_FUNCTIONAL_RANGE 20 // If the temperature difference between the target temperature and the actual temperature
941941
// is more than PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.
942942

943943
//#define PID_EDIT_MENU // Add PID editing to the "Advanced Settings" menu. (~700 bytes of flash)

Marlin/src/lcd/sovol_rts/sovol_rts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,9 @@ void RTS::handleData() {
13121312
#endif
13131313

13141314
#if ENABLED(PIDTEMPBED)
1315-
case Hot_Bed_P: thermalManager.temp_bed.pid.Kp = float(recdat.data[0]) / 100.0f; break;
1316-
case Hot_Bed_I: thermalManager.temp_bed.pid.Ki = float(recdat.data[0]) * 8.0f / 10000.0f; break;
1317-
case Hot_Bed_D: thermalManager.temp_bed.pid.Kd = float(recdat.data[0]) / 0.8f; break;
1315+
case Hot_Bed_P: thermalManager.temp_bed.pid.set_Kp(float(recdat.data[0]) / 100.0f); break;
1316+
case Hot_Bed_I: thermalManager.temp_bed.pid.set_Ki(float(recdat.data[0]) * 8.0f / 10000.0f); break;
1317+
case Hot_Bed_D: thermalManager.temp_bed.pid.set_Kd(float(recdat.data[0]) / 0.8f); break;
13181318
#endif
13191319

13201320
#if HAS_X_AXIS

Marlin/src/module/temperature.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ typedef struct { float p, i, d, c, f; } raw_pidcf_t;
173173
struct PID_t {
174174
protected:
175175
bool pid_reset = true;
176-
float temp_iState = 0.0f, temp_dState = 0.0f;
176+
float temp_dState = 0;
177177
float work_p = 0, work_i = 0, work_d = 0;
178178

179179
public:
@@ -217,17 +217,14 @@ typedef struct { float p, i, d, c, f; } raw_pidcf_t;
217217
}
218218
else {
219219
if (pid_reset) {
220+
work_i = 0;
221+
work_d = 0;
220222
pid_reset = false;
221-
temp_iState = 0.0;
222-
work_d = 0.0;
223223
}
224224

225-
const float max_power_over_i_gain = float(MAX_POW) / Ki - float(MIN_POW);
226-
temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
227-
228225
work_p = Kp * pid_error;
229-
work_i = Ki * temp_iState;
230-
work_d = work_d + PID_K2 * (Kd * (temp_dState - current) - work_d);
226+
work_i = constrain(work_i + Ki * pid_error, 0, float(MAX_POW - MIN_POW));
227+
work_d += (Kd * (temp_dState - current) - work_d) * PID_K2;
231228

232229
output_pow = constrain(work_p + work_i + work_d + float(MIN_POW), 0, MAX_POW);
233230
}

0 commit comments

Comments
 (0)