Skip to content

Commit

Permalink
🐛 Fix PID upon entering PID_FUNCTIONAL_RANGE (MarlinFirmware#26926)
Browse files Browse the repository at this point in the history
The PID algorithm did not cache the last seen temperature until it entered the PID_FUNCTIONAL_RANGE. This caused an incorrect output power to be calculated temporarily while the algorithm caught up.

This has likely always been a problem for bed and chamber PID. For the hotend this error was introduced in refactoring in commit 54e7b93.
  • Loading branch information
Triodes authored and RPGFabi committed May 5, 2024
1 parent 2820874 commit 537cd9f
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions Marlin/src/module/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,35 @@ typedef struct { float p, i, d, c, f; } raw_pidcf_t;

float get_pid_output(const float target, const float current) {
const float pid_error = target - current;
float output_pow;
if (!target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
pid_reset = true;
return 0;
output_pow = 0;
}
else if (pid_error > PID_FUNCTIONAL_RANGE) {
pid_reset = true;
return MAX_POW;
output_pow = MAX_POW;
}
else {
if (pid_reset) {
pid_reset = false;
temp_iState = 0.0;
work_d = 0.0;
}

if (pid_reset) {
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);

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_p = Kp * pid_error;
work_i = Ki * temp_iState;
work_d = work_d + PID_K2 * (Kd * (temp_dState - current) - work_d);
output_pow = constrain(work_p + work_i + work_d + float(MIN_POW), 0, MAX_POW);
}

temp_dState = current;

return constrain(work_p + work_i + work_d + float(MIN_POW), 0, MAX_POW);
return output_pow;
}

};
Expand Down

0 comments on commit 537cd9f

Please sign in to comment.