Chun Feng Huang / PID

Fork of PID_modified by Chun Feng Huang

Files at this revision

API Documentation at this revision

Comitter:
benson516
Date:
Tue Oct 25 14:19:13 2016 +0000
Parent:
2:b9610a2d2ea0
Child:
4:e3c9cb64be44
Commit message:
Fix anti-windup

Changed in this revision

PID.cpp Show annotated file Show diff for this revision Revisions of this file
PID.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
--- a/PID.h	Tue Oct 25 13:04:58 2016 +0000
+++ b/PID.h	Tue Oct 25 14:19:13 2016 +0000
@@ -11,7 +11,7 @@
            
         void SetOutputLimits(float setoutputLimits_H, float setoutputLimits_L);
         void SetInputLimits(float setinputLimits_H, float setinputLimits_L); 
-        void EnableAntiWindUp(float Ka_); 
+        void EnableAntiWindUp(float Ka_in); 
         
         float Kp;
         float Ki;