diff --git a/libraries/PID/PID.cpp b/libraries/PID/PID.cpp index a44d5f3e13..f5bc689026 100644 --- a/libraries/PID/PID.cpp +++ b/libraries/PID/PID.cpp @@ -51,6 +51,33 @@ PID::get_pid(int32_t error, uint16_t dt, float scaler) return output; } + +long +PID::get_pi(int32_t error, uint16_t dt, float scaler) +{ + float output = 0; + float delta_time = (float)dt / 1000.0; + + // Compute proportional component + output += error * _kp; + + // scale the P components + output *= scaler; + + // Compute integral component if time has elapsed + if ((fabs(_ki) > 0) && (dt > 0)) { + _integrator += (error * _ki) * scaler * delta_time; + if (_integrator < -_imax) { + _integrator = -_imax; + } else if (_integrator > _imax) { + _integrator = _imax; + } + output += _integrator; + } + + return output; +} + void PID::reset_I() { diff --git a/libraries/PID/PID.h b/libraries/PID/PID.h index 68a3e13238..46b88151c3 100644 --- a/libraries/PID/PID.h +++ b/libraries/PID/PID.h @@ -85,6 +85,9 @@ public: /// long get_pid(int32_t error, uint16_t dt, float scaler = 1.0); + long get_pi(int32_t error, uint16_t dt, float scaler = 1.0); + + /// Reset the PID integrator /// void reset_I();