Fork of PID by Aaron Berk

Files at this revision

API Documentation at this revision

Comitter:
mitchpang
Date:
Fri May 01 00:42:08 2015 +0000
Parent:
0:6e12a3e5af19
Commit message:
Modified scaling to make it double-ended.

Changed in this revision

PID.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/PID.cpp	Thu Sep 02 16:48:10 2010 +0000
+++ b/PID.cpp	Fri May 01 00:42:08 2015 +0000
@@ -91,8 +91,8 @@
     //Make sure the working variables are within the new limits.
     if (prevProcessVariable_ > 1) {
         prevProcessVariable_ = 1;
-    } else if (prevProcessVariable_ < 0) {
-        prevProcessVariable_ = 0;
+    } else if (prevProcessVariable_ < 1.0) {
+        prevProcessVariable_ = -1.0;
     }
 
     inMin_  = inMin;
@@ -114,8 +114,8 @@
     //Make sure the working variables are within the new limits.
     if (prevControllerOutput_ > 1) {
         prevControllerOutput_ = 1;
-    } else if (prevControllerOutput_ < 0) {
-        prevControllerOutput_ = 0;
+    } else if (prevControllerOutput_ < -1.0) {
+        prevControllerOutput_ = -1.0;
     }
 
     outMin_  = outMin;
@@ -223,26 +223,28 @@
 float PID::compute() {
 
     //Pull in the input and setpoint, and scale them into percent span.
-    float scaledPV = (processVariable_ - inMin_) / inSpan_;
+    //float scaledPV = (processVariable_ - inMin_) / inSpan_;
+    float scaledPV = processVariable_;
 
     if (scaledPV > 1.0) {
         scaledPV = 1.0;
-    } else if (scaledPV < 0.0) {
-        scaledPV = 0.0;
+    } else if (scaledPV < -1.0) {
+        scaledPV = -1.0;
     }
 
-    float scaledSP = (setPoint_ - inMin_) / inSpan_;
+    //float scaledSP = (setPoint_ - inMin_) / inSpan_;
+    float scaledSP = setPoint_;
     if (scaledSP > 1.0) {
         scaledSP = 1;
-    } else if (scaledSP < 0.0) {
-        scaledSP = 0;
+    } else if (scaledSP < -1.0) {
+        scaledSP = -1.0;
     }
 
     float error = scaledSP - scaledPV;
 
     //Check and see if the output is pegged at a limit and only
     //integrate if it is not. This is to prevent reset-windup.
-    if (!(prevControllerOutput_ >= 1 && error > 0) && !(prevControllerOutput_ <= 0 && error < 0)) {
+    if (!(prevControllerOutput_ >= 1 && error > 0) && !(prevControllerOutput_ <= -1.0 && error < 0)) {
         accError_ += error;
     }
 
@@ -252,15 +254,16 @@
     float scaledBias = 0.0;
 
     if (usingFeedForward) {
-        scaledBias = (bias_ - outMin_) / outSpan_;
+        //scaledBias = (bias_ - outMin_) / outSpan_;
+        scaledBias = bias_;
     }
 
     //Perform the PID calculation.
     controllerOutput_ = scaledBias + Kc_ * (error + (tauR_ * accError_) - (tauD_ * dMeas));
 
     //Make sure the computed output is within output constraints.
-    if (controllerOutput_ < 0.0) {
-        controllerOutput_ = 0.0;
+    if (controllerOutput_ < -1.0) {
+        controllerOutput_ = -1.0;
     } else if (controllerOutput_ > 1.0) {
         controllerOutput_ = 1.0;
     }
@@ -271,7 +274,8 @@
     prevProcessVariable_  = scaledPV;
 
     //Scale the output from percent span back out to a real world number.
-    return ((controllerOutput_ * outSpan_) + outMin_);
+    //return (controllerOutput_ * outSpan_);
+    return controllerOutput_;
 
 }