Pat McC / Mbed 2 deprecated AUV_PIDusna

Dependencies:   mbed BNO055_fusion_AUV

Revision:
4:e89234ca9d4e
Parent:
0:37123f30e8b2
--- a/PIDusna/PID.cpp	Mon Feb 03 18:37:11 2020 +0000
+++ b/PIDusna/PID.cpp	Thu Nov 12 20:25:07 2020 +0000
@@ -14,11 +14,12 @@
 // K values as floats.
 // dt should be set to same as Ticker that uses this class.
 // deadzone represents the acceptable error, defaults to 0.
-PID::PID (float Kp, float Ki, float Kd, int dt, float deadzone)
+PID::PID (float Kp, float Ki, float Kd, float dt, float deadzone)
 {
     set_dt(dt);
     set_K(Kp, Ki, Kd);
     set_deadzone(deadzone);
+    clear();
 }
 
 /*
@@ -76,7 +77,7 @@
 // }
 
 // Change dt.
-void PID::set_dt(int dt) 
+void PID::set_dt(float dt) 
 {
     _dt=dt;
 }
@@ -158,26 +159,30 @@
 // Overloaded version to give function the error directly.
 float PID::process(float error)
 {
+    float k_term=0;
+    float i_term=0;
+    float d_term=0;
+    float out_PID=0;
     // If abs value of error is smaller than the deadzone,
     //  cause all the PID gains to zeroize.
-    if (abs(error)<_deadzone) {
+    if ((abs(error))<_deadzone) {
         clear_integral();
         clear_error_previous();
         error=0;
     }
 
     // Proportional = Kp * e
-    float k_term = (_Kp*error);
+    k_term = (_Kp*error);
 
     // Integral = Ki * e dt
     _integral+=(error*_dt);
-    float i_term = (_Ki*_integral);
+    i_term = (_Ki*_integral);
 
     // Derivative = Kd * (de/dt)
-    float d_term = (_Kd* ((error-_error_previous)/_dt) );
+    d_term = (_Kd* ((error-_error_previous)/_dt) );
 
     // PID = P + I + D
-    float out_PID = k_term+i_term+d_term;
+    out_PID = k_term+i_term+d_term;
 
     // Update last error for next Derivative calculation.
     _error_previous=error;