Chun Feng Huang / PID

Fork of PID_modified by Chun Feng Huang

Files at this revision

API Documentation at this revision

Comitter:
adam_z
Date:
Fri Apr 22 09:38:38 2016 +0000
Parent:
0:7f9b4ca968ae
Child:
2:b9610a2d2ea0
Commit message:
add a anti-windup function

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	Thu Feb 18 04:25:52 2016 +0000
+++ b/PID.cpp	Fri Apr 22 09:38:38 2016 +0000
@@ -13,6 +13,7 @@
     sampletime = 0.0;
     Outputlimit_bool = false;
     Inputlimit_bool = false;
+    AntiWindUp_bool = false;
     outputLimits_H = 0.0;
     outputLimits_L = 0.0;
     inputLimits_H = 0.0;
@@ -20,8 +21,8 @@
     feedbackvalue = 0.0;   
     
     Kp = setKp;
-    Ki = setKi/setSampletime;
-    Kd = setKd*setSampletime;
+    Ki = setKi*setSampletime;
+    Kd = setKd/setSampletime;
     sampletime = setSampletime;   
 }
 
@@ -37,6 +38,12 @@
     inputLimits_L = setinputLimits_L;     
 }
 
+void PID::EnableAntiWindUp(float Ka_)
+{
+    AntiWindUp_bool = true;
+    Ka = Ka_;
+}
+
 void PID::Compute(float setreference, float setfeedbackvalue){
 
     if(Inputlimit_bool == true){
@@ -56,14 +63,18 @@
     error[2] = error[1];
     error[1] = error[0];    
     
-    if(Outputlimit_bool == true){
+    if(Outputlimit_bool == true && AntiWindUp_bool == true){
         if( output >= outputLimits_H){
-            output = outputLimits_H;
+            output = output - (output - outputLimits_H)*Ka;
+            //output = outputLimits_H;
         }else if( output <= outputLimits_L){
-            output = outputLimits_L;
+            output =output - (output - outputLimits_L)*Ka;
+            //output = outputLimits_L;
+            
         }
     }else{
         output = output;
     }
             
-}
\ No newline at end of file
+}
+
--- a/PID.h	Thu Feb 18 04:25:52 2016 +0000
+++ b/PID.h	Fri Apr 22 09:38:38 2016 +0000
@@ -9,11 +9,13 @@
         PID(float setKp, float setKi, float setKd, float setSampletime);       
         void SetOutputLimits(float setoutputLimits_H, float setoutputLimits_L);
         void SetInputLimits(float setinputLimits_H, float setinputLimits_L);
-        void Compute(float setreference, float setfeedbackvalue);  
+        void Compute(float setreference, float setfeedbackvalue); 
+        void PID::EnableAntiWindUp(float Ka_); 
         
         float Kp;
         float Ki;
         float Kd;
+        float Ka;
  
         float error[3];
         float output;
@@ -30,6 +32,7 @@
     
         bool Outputlimit_bool;
         bool Inputlimit_bool;
+        bool AntiWindUp_bool;
         
         float outputLimits_H;
         float outputLimits_L;