from greg

Fork of PID by Greg Abdo

Files at this revision

API Documentation at this revision

Comitter:
oprospero
Date:
Sat Feb 15 19:55:04 2014 +0000
Parent:
8:a020a225bc65
Child:
10:3296a2150efe
Commit message:
Update windup guard logic

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 Dec 31 05:28:01 2013 +0000
+++ b/PID.cpp	Sat Feb 15 19:55:04 2014 +0000
@@ -9,18 +9,22 @@
  */
 
 #include "PID.h"
+//
+//#include "mbed.h"
+//Serial pc(USBTX, USBRX);
 
 PID::PID(const float proportionalGain,
-         const float integralGain,
-         const float derivativeGain,
-         const float windupGainGuard,
-         const int timeDifferential) :
-    proportionalGain(proportionalGain),
-    integralGain(integralGain),
-    derivativeGain(derivativeGain),
-    windupGainGuard(windupGainGuard),
-    dt(timeDifferential)
+    const float integralGain,
+    const float derivativeGain,
+    const float windupGainGuard,
+    const float timeDifferential ) :
+        proportionalGain(proportionalGain),
+        integralGain(integralGain),
+        derivativeGain(derivativeGain),
+        windupGainGuard(windupGainGuard),
+        dt(timeDifferential)
 {
+//    pc.baud(38400);
     integralError = 0.0f;
     previousError = 0.0f;
     currentError = 0.0f;
@@ -36,7 +40,8 @@
 //    currentError = targetPosition - currentPosition;
 
     // Sum of the errors the controller has incurred over time
-    integralError += currentError;
+    integralError += currentError * dt / 1000;
+
     // Cap the sum of errors to prevent excessive correction
     if (integralError < -windupGainGuard) {
         integralError = -windupGainGuard;
@@ -50,6 +55,7 @@
     integralControl = integralError * integralGain;
     derivativeControl = derivativeGain * (currentError - previousError) / dt;
 
+
     // Save current error for next call to correction
     previousError = currentError;
 
--- a/PID.h	Tue Dec 31 05:28:01 2013 +0000
+++ b/PID.h	Sat Feb 15 19:55:04 2014 +0000
@@ -31,7 +31,7 @@
      *                          such as momentum.
      * @param windupGainGuard Cap for the maximum error value.
      */
-    PID(const float, const float, const float, const float, const int);
+    PID(const float, const float, const float, const float, const float);
 
     /**
      * Determine how to correct the system to the desired position.
@@ -53,7 +53,7 @@
     const float integralGain;
     float derivativeGain;
     const float windupGainGuard;
-    const int dt;
+    const float dt;
     float proportionalControl, integralControl, derivativeControl;
 
     float currentError;