Improved fork of Proportional, integral, derivative controller library. Ported by Aaron Berk from the Arduino PID library by Brett Beauregard.

Fork of PID by Aaron Berk

Revision:
1:125d04fbce1d
Parent:
0:6e12a3e5af19
--- a/PID.cpp	Thu Sep 02 16:48:10 2010 +0000
+++ b/PID.cpp	Wed Mar 28 09:30:47 2018 +0000
@@ -57,23 +57,23 @@
     //Default the limits to the full range of I/O: 3.3V
     //Make sure to set these to more appropriate limits for
     //your application.
-    setInputLimits(0.0, 3.3);
-    setOutputLimits(0.0, 3.3);
+    setInputLimits(0.0f, 3.3f);
+    setOutputLimits(0.0f, 3.3f);
 
     tSample_ = interval;
 
     setTunings(Kc, tauI, tauD);
 
-    setPoint_             = 0.0;
-    processVariable_      = 0.0;
-    prevProcessVariable_  = 0.0;
-    controllerOutput_     = 0.0;
-    prevControllerOutput_ = 0.0;
+    setPoint_             = 0.0f;
+    processVariable_      = 0.0f;
+    prevProcessVariable_  = 0.0f;
+    controllerOutput_     = 0.0f;
+    prevControllerOutput_ = 0.0f;
 
-    accError_ = 0.0;
-    bias_     = 0.0;
+    accError_ = 0.0f;
+    bias_     = 0.0f;
     
-    realOutput_ = 0.0;
+    realOutput_ = 0.0f;
 
 }
 
@@ -89,10 +89,10 @@
     accError_            *= (inMax - inMin) / inSpan_;
 
     //Make sure the working variables are within the new limits.
-    if (prevProcessVariable_ > 1) {
-        prevProcessVariable_ = 1;
-    } else if (prevProcessVariable_ < 0) {
-        prevProcessVariable_ = 0;
+    if (prevProcessVariable_ > 1.0f) {
+        prevProcessVariable_ = 1.0f;
+    } else if (prevProcessVariable_ < 0.0f) {
+        prevProcessVariable_ = 0.0f;
     }
 
     inMin_  = inMin;
@@ -112,10 +112,10 @@
     prevControllerOutput_ *= (outMax - outMin) / outSpan_;
 
     //Make sure the working variables are within the new limits.
-    if (prevControllerOutput_ > 1) {
-        prevControllerOutput_ = 1;
-    } else if (prevControllerOutput_ < 0) {
-        prevControllerOutput_ = 0;
+    if (prevControllerOutput_ > 1.0f) {
+        prevControllerOutput_ = 1.0f;
+    } else if (prevControllerOutput_ < 0.0f) {
+        prevControllerOutput_ = 0.0f;
     }
 
     outMin_  = outMin;
@@ -127,7 +127,7 @@
 void PID::setTunings(float Kc, float tauI, float tauD) {
 
     //Verify that the tunings make sense.
-    if (Kc == 0.0 || tauI < 0.0 || tauD < 0.0) {
+    if (Kc == 0.0f || tauI < 0.0f || tauD < 0.0f) {
         return;
     }
 
@@ -138,16 +138,16 @@
 
     float tempTauR;
 
-    if (tauI == 0.0) {
-        tempTauR = 0.0;
+    if (tauI == 0.0f) {
+        tempTauR = 0.0f;
     } else {
-        tempTauR = (1.0 / tauI) * tSample_;
+        tempTauR = (1.0f / tauI) * tSample_;
     }
 
     //For "bumpless transfer" we need to rescale the accumulated error.
     if (inAuto) {
-        if (tempTauR == 0.0) {
-            accError_ = 0.0;
+        if (tempTauR == 0.0f) {
+            accError_ = 0.0f;
         } else {
             accError_ *= (Kc_ * tauR_) / (Kc * tempTauR);
         }
@@ -161,7 +161,7 @@
 
 void PID::reset(void) {
 
-    float scaledBias = 0.0;
+    float scaledBias = 0.0f;
 
     if (usingFeedForward) {
         scaledBias = (bias_ - outMin_) / outSpan_;
@@ -173,7 +173,7 @@
     prevProcessVariable_  = (processVariable_ - inMin_) / inSpan_;
 
     //Clear any error in the integral.
-    accError_ = 0;
+    accError_ = 0.0f;
 
 }
 
@@ -191,7 +191,7 @@
 
 void PID::setInterval(float interval) {
 
-    if (interval > 0) {
+    if (interval > 0.0f) {
         //Convert the time-based tunings to reflect this change.
         tauR_     *= (interval / tSample_);
         accError_ *= (tSample_ / interval);
@@ -225,31 +225,31 @@
     //Pull in the input and setpoint, and scale them into percent span.
     float scaledPV = (processVariable_ - inMin_) / inSpan_;
 
-    if (scaledPV > 1.0) {
-        scaledPV = 1.0;
-    } else if (scaledPV < 0.0) {
-        scaledPV = 0.0;
+    if (scaledPV > 1.0f) {
+        scaledPV = 1.0f;
+    } else if (scaledPV < 0.0f) {
+        scaledPV = 0.0f;
     }
 
     float scaledSP = (setPoint_ - inMin_) / inSpan_;
-    if (scaledSP > 1.0) {
-        scaledSP = 1;
-    } else if (scaledSP < 0.0) {
-        scaledSP = 0;
+    if (scaledSP > 1.0f) {
+        scaledSP = 1.0f;
+    } else if (scaledSP < 0.0f) {
+        scaledSP = 0.0f;
     }
 
     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.0f && error > 0.0f) && !(prevControllerOutput_ <= 0.0f && error < 0.0f)) {
         accError_ += error;
     }
 
     //Compute the current slope of the input signal.
     float dMeas = (scaledPV - prevProcessVariable_) / tSample_;
 
-    float scaledBias = 0.0;
+    float scaledBias = 0.0f;
 
     if (usingFeedForward) {
         scaledBias = (bias_ - outMin_) / outSpan_;
@@ -259,10 +259,10 @@
     controllerOutput_ = scaledBias + Kc_ * (error + (tauR_ * accError_) - (tauD_ * dMeas));
 
     //Make sure the computed output is within output constraints.
-    if (controllerOutput_ < 0.0) {
-        controllerOutput_ = 0.0;
-    } else if (controllerOutput_ > 1.0) {
-        controllerOutput_ = 1.0;
+    if (controllerOutput_ < 0.0f) {
+        controllerOutput_ = 0.0f;
+    } else if (controllerOutput_ > 1.0f) {
+        controllerOutput_ = 1.0f;
     }
 
     //Remember this output for the windup check next time.