diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index b99ec6a29e..07478e372b 100644 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -144,13 +144,13 @@ // Comment the following line to disable PID and enable bang-bang. #define PIDTEMP #define BANG_MAX 255 // limits current to nozzle while in bang-bang mode; 255=full current -#define PID_MAX 255 // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current +#define PID_MAX BANG_MAX // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current #ifdef PIDTEMP //#define PID_DEBUG // Sends debug data to the serial port. //#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX #define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature // is more then PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max. - #define PID_INTEGRAL_DRIVE_MAX 255 //limit for the integral term + #define PID_INTEGRAL_DRIVE_MAX PID_MAX //limit for the integral term #define K1 0.95 //smoothing factor within the PID #define PID_dT ((OVERSAMPLENR * 10.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index a3fedbf986..e7f640953c 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -455,7 +455,14 @@ void manage_heater() //K1 defined in Configuration.h in the PID settings #define K2 (1.0-K1) dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]); - pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX); + pid_output = pTerm[e] + iTerm[e] - dTerm[e]; + if (pid_output > PID_MAX) { + if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration + pid_output=PID_MAX; + } else if (pid_output < 0){ + if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration + pid_output=0; + } } temp_dState[e] = pid_input; #else @@ -558,7 +565,14 @@ void manage_heater() dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed); temp_dState_bed = pid_input; - pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER); + pid_output = pTerm_bed + iTerm_bed - dTerm_bed; + if (pid_output > MAX_BED_PID) { + if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration + pid_output=PID_MAX; + } else if (pid_output < 0){ + if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration + pid_output=0; + } #else pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);