Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
PID2.h
- Committer:
- mazdo25
- Date:
- 2019-03-25
- Revision:
- 5:f1613df66ceb
- Child:
- 6:477382219bcf
File content as of revision 5:f1613df66ceb:
class PID2
{
private:
float Kp_, Ki_, Kd_, Ts, PrevErr, PrevPrevErr, prevControlAction, setPoint;
float inMin_;
float inMax_;
float* outMin_;
float* outMax_;
public:
PID2 (float Kp, float Ki, float Kd, float sampleTime)
{
PrevErr = 0;
PrevPrevErr = 0;
prevControlAction = 0;
setPoint = 0;
inMin_ = -1.0f;
inMax_ = 1.0f;
Ts = sampleTime;
Kp_ = Kp;
Ki_ = Ki;
Kd_ = Kd;
}
float compute (float currVal)
{
float currErr = setPoint - currVal;
float controlAction;
if (currVal > inMax_) {inMax_ = currVal;}
if (currVal < inMin_) {inMin_ = currVal;}
controlAction = prevControlAction - (PrevErr*Kp_) + (Kp_*currErr)+ (Ki_*Ts*currErr) + ((Kd_/Ts)*(currErr - PrevErr - PrevErr + PrevPrevErr));
if (controlAction > inMax_) {controlAction = inMax_;}
if (controlAction < inMin_) {controlAction = inMin_;}
prevControlAction = controlAction;
PrevPrevErr = PrevErr;
PrevErr = currErr;
//scale the control Action to the correct output limits and output
return ((((controlAction)- inMin_)/(inMax_ - inMin_)) * (*outMax_ - *outMin_)) + *outMin_;
}
void setSetPoint(float _sP)
{
setPoint = _sP;
}
void setInputLimits(float inMin,float inMax)
{
if (inMin > inMax) {return;}
inMin_ = inMin;
inMax_ = inMax;
}
void assignLimitAddress(float *outMax, float *outMin)
{
outMin_ = outMax;
outMax_ = outMin;
}
float scaler(float prevMin, float prevMax, float newMin, float newMax, float var)
{
if (var > prevMax) {var = prevMax;}
if (var < prevMin) {var = prevMin;}
return (((var-prevMin)/(prevMax - prevMin))*(newMax-newMin))+newMin;
}
float returnPrevCA()
{
return prevControlAction;
}
};
//(((temp-inMin)/(inMax - inMin))*(outMax-outMin))+outMin