️ Optimize PID, increase PID range (#27740)

This commit is contained in:
feldi12 2025-04-15 00:08:40 +02:00 committed by GitHub
parent b5d7b4aee6
commit 5e8a5230a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 12 deletions

View file

@ -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)

View file

@ -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

View file

@ -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:
@ -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);
}