New PID library with digital anti-windup and process control
Fork of PID_modified by
Diff: PID.cpp
- Revision:
- 3:d8646d8c994f
- Parent:
- 2:b9610a2d2ea0
- Child:
- 4:e3c9cb64be44
--- a/PID.cpp Tue Oct 25 13:04:58 2016 +0000 +++ b/PID.cpp Tue Oct 25 14:19:13 2016 +0000 @@ -3,11 +3,11 @@ PID::PID(float Kp_in, float Ki_in, float Kd_in, float Sampletime_in){ // Parameters - Outputlimit_bool = false; + Outputlimit_bool = true; Inputlimit_bool = false; - AntiWindUp_bool = false; - outputLimits_H = 0.0; - outputLimits_L = 0.0; + AntiWindUp_bool = true; + outputLimits_H = 7.4; + outputLimits_L = -7.4; inputLimits_H = 0.0; inputLimits_L = 0.0; feedbackvalue = 0.0; @@ -16,6 +16,7 @@ Kp = Kp_in; Ki = Ki_in; Kd = Kd_in; + Ka = 0.1; // Sampling time Ts = Sampletime_in; @@ -44,10 +45,10 @@ inputLimits_L = setinputLimits_L; } -void PID::EnableAntiWindUp(float Ka_) +void PID::EnableAntiWindUp(float Ka_in) { AntiWindUp_bool = true; - Ka = Ka_; + Ka = Ka_in; } void PID::Compute(float reference_in, float feedbackvalue_in){ @@ -81,11 +82,13 @@ // Output satuation if(Outputlimit_bool && AntiWindUp_bool){ if( output >= outputLimits_H){ - output = output - (output - outputLimits_H)*Ka; - //output = outputLimits_H; + // output = output - (output - outputLimits_H)*Ka; + error_I -= Ka*(output - outputLimits_H); // Anti-windup + output = outputLimits_H; }else if( output <= outputLimits_L){ - output = output - (output - outputLimits_L)*Ka; - //output = outputLimits_L; + // output = output - (output - outputLimits_L)*Ka; + error_I -= Ka*(output - outputLimits_L); // Anti-windup + output = outputLimits_L; } }else{ // output = output;