Proportional, integral, derivative controller library. Ported from the Arduino PID library by Brett Beauregard.
Fork of PID by
Diff: PID.cpp
- Revision:
- 3:0f1bc3b0d050
- Parent:
- 1:709ed452cc4c
--- a/PID.cpp Thu May 14 12:28:08 2015 +0000 +++ b/PID.cpp Tue May 19 16:39:18 2015 +0000 @@ -24,7 +24,7 @@ * THE SOFTWARE. * * @section DESCRIPTION - * + * * A PID controller is a widely used feedback controller commonly found in * industry. * @@ -49,7 +49,8 @@ */ #include "PID.h" -PID::PID(float Kc, float tauI, float tauD, float interval) { +PID::PID(float Kc, float tauI, float tauD, float interval) +{ usingFeedForward = false; inAuto = false; @@ -72,12 +73,13 @@ accError_ = 0.0; bias_ = 0.0; - + realOutput_ = 0.0; } -void PID::setInputLimits(float inMin, float inMax) { +void PID::setInputLimits(float inMin, float inMax) +{ //Make sure we haven't been given impossible values. if (inMin >= inMax) { @@ -101,7 +103,8 @@ } -void PID::setOutputLimits(float outMin, float outMax) { +void PID::setOutputLimits(float outMin, float outMax) +{ //Make sure we haven't been given impossible values. if (outMin >= outMax) { @@ -124,7 +127,8 @@ } -void PID::setTunings(float Kc, float tauI, float tauD) { +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) { @@ -156,10 +160,11 @@ Kc_ = Kc; tauR_ = tempTauR; tauD_ = tauD / tSample_; - + } -void PID::reset(void) { +void PID::reset(void) +{ float scaledBias = 0.0; @@ -177,7 +182,8 @@ } -void PID::setMode(int mode) { +void PID::setMode(int mode) +{ //We were in manual, and we just got set to auto. //Reset the controller internals. @@ -189,7 +195,8 @@ } -void PID::setInterval(float interval) { +void PID::setInterval(float interval) +{ if (interval > 0) { //Convert the time-based tunings to reflect this change. @@ -201,26 +208,30 @@ } -void PID::setSetPoint(float sp) { +void PID::setSetPoint(float sp) +{ setPoint_ = sp; } -void PID::setProcessValue(float pv) { +void PID::setProcessValue(float pv) +{ processVariable_ = pv; } -void PID::setBias(float bias){ +void PID::setBias(float bias) +{ bias_ = bias; usingFeedForward = 1; } -float PID::compute() { +float PID::compute() +{ //Pull in the input and setpoint, and scale them into percent span. scaledPV = (processVariable_ - inMin_) / inSpan_; @@ -275,50 +286,63 @@ } -float PID::getInMin() { +float PID::getInMin() +{ return inMin_; } -float PID::getInMax() { +float PID::getInMax() +{ return inMax_; } -float PID::getOutMin() { +float PID::getOutMin() +{ return outMin_; } -float PID::getOutMax() { +float PID::getOutMax() +{ return outMax_; } -float PID::getInterval() { +float PID::getInterval() +{ return tSample_; } -float PID::getPParam() { +float PID::getPParam() +{ return pParam_; } -float PID::getIParam() { +float PID::getIParam() +{ return iParam_; } -float PID::getDParam() { +float PID::getDParam() +{ return dParam_; } + +void PID::resetAccError() +{ + accError_ = 0; +} \ No newline at end of file