Skip to content

Fixes and improvements to PID. #27740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 14, 2025
Merged
2 changes: 1 addition & 1 deletion Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@
#if ANY(PIDTEMP, PIDTEMPBED, PIDTEMPCHAMBER)
//#define PID_OPENLOOP // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX
//#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
#define PID_FUNCTIONAL_RANGE 20 // 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 flash)
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/sovol_rts/sovol_rts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1312,9 +1312,9 @@ void RTS::handleData() {
#endif

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

#if HAS_X_AXIS
Expand Down
13 changes: 5 additions & 8 deletions Marlin/src/module/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ typedef struct { float p, i, d, c, f; } raw_pidcf_t;
struct PID_t {
protected:
bool pid_reset = true;
float temp_iState = 0.0f, temp_dState = 0.0f;
float temp_dState = 0;
float work_p = 0, work_i = 0, work_d = 0;

public:
Expand Down Expand Up @@ -217,17 +217,14 @@ typedef struct { float p, i, d, c, f; } raw_pidcf_t;
}
else {
if (pid_reset) {
work_i = 0;
work_d = 0;
pid_reset = false;
temp_iState = 0.0;
work_d = 0.0;
}

const float max_power_over_i_gain = float(MAX_POW) / Ki - float(MIN_POW);
temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);

work_p = Kp * pid_error;
work_i = Ki * temp_iState;
work_d = work_d + PID_K2 * (Kd * (temp_dState - current) - work_d);
work_i = constrain(work_i + Ki * pid_error, 0, float(MAX_POW - MIN_POW));
work_d += (Kd * (temp_dState - current) - work_d) * PID_K2;

output_pow = constrain(work_p + work_i + work_d + float(MIN_POW), 0, MAX_POW);
}
Expand Down
Loading