Buggy bois / Mbed 2 deprecated UDITTEST

Dependencies:   mbed

Committer:
mazdo25
Date:
Mon Mar 25 22:42:31 2019 +0000
Revision:
5:f1613df66ceb
Child:
6:477382219bcf
LATEST WORKING VERSION;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazdo25 5:f1613df66ceb 1 class PID2
mazdo25 5:f1613df66ceb 2 {
mazdo25 5:f1613df66ceb 3 private:
mazdo25 5:f1613df66ceb 4 float Kp_, Ki_, Kd_, Ts, PrevErr, PrevPrevErr, prevControlAction, setPoint;
mazdo25 5:f1613df66ceb 5
mazdo25 5:f1613df66ceb 6 float inMin_;
mazdo25 5:f1613df66ceb 7 float inMax_;
mazdo25 5:f1613df66ceb 8
mazdo25 5:f1613df66ceb 9 float* outMin_;
mazdo25 5:f1613df66ceb 10 float* outMax_;
mazdo25 5:f1613df66ceb 11
mazdo25 5:f1613df66ceb 12 public:
mazdo25 5:f1613df66ceb 13 PID2 (float Kp, float Ki, float Kd, float sampleTime)
mazdo25 5:f1613df66ceb 14 {
mazdo25 5:f1613df66ceb 15 PrevErr = 0;
mazdo25 5:f1613df66ceb 16 PrevPrevErr = 0;
mazdo25 5:f1613df66ceb 17 prevControlAction = 0;
mazdo25 5:f1613df66ceb 18 setPoint = 0;
mazdo25 5:f1613df66ceb 19
mazdo25 5:f1613df66ceb 20 inMin_ = -1.0f;
mazdo25 5:f1613df66ceb 21 inMax_ = 1.0f;
mazdo25 5:f1613df66ceb 22
mazdo25 5:f1613df66ceb 23 Ts = sampleTime;
mazdo25 5:f1613df66ceb 24 Kp_ = Kp;
mazdo25 5:f1613df66ceb 25 Ki_ = Ki;
mazdo25 5:f1613df66ceb 26 Kd_ = Kd;
mazdo25 5:f1613df66ceb 27 }
mazdo25 5:f1613df66ceb 28
mazdo25 5:f1613df66ceb 29 float compute (float currVal)
mazdo25 5:f1613df66ceb 30 {
mazdo25 5:f1613df66ceb 31 float currErr = setPoint - currVal;
mazdo25 5:f1613df66ceb 32 float controlAction;
mazdo25 5:f1613df66ceb 33
mazdo25 5:f1613df66ceb 34 if (currVal > inMax_) {inMax_ = currVal;}
mazdo25 5:f1613df66ceb 35 if (currVal < inMin_) {inMin_ = currVal;}
mazdo25 5:f1613df66ceb 36
mazdo25 5:f1613df66ceb 37 controlAction = prevControlAction - (PrevErr*Kp_) + (Kp_*currErr)+ (Ki_*Ts*currErr) + ((Kd_/Ts)*(currErr - PrevErr - PrevErr + PrevPrevErr));
mazdo25 5:f1613df66ceb 38
mazdo25 5:f1613df66ceb 39 if (controlAction > inMax_) {controlAction = inMax_;}
mazdo25 5:f1613df66ceb 40 if (controlAction < inMin_) {controlAction = inMin_;}
mazdo25 5:f1613df66ceb 41
mazdo25 5:f1613df66ceb 42 prevControlAction = controlAction;
mazdo25 5:f1613df66ceb 43 PrevPrevErr = PrevErr;
mazdo25 5:f1613df66ceb 44 PrevErr = currErr;
mazdo25 5:f1613df66ceb 45 //scale the control Action to the correct output limits and output
mazdo25 5:f1613df66ceb 46 return ((((controlAction)- inMin_)/(inMax_ - inMin_)) * (*outMax_ - *outMin_)) + *outMin_;
mazdo25 5:f1613df66ceb 47 }
mazdo25 5:f1613df66ceb 48
mazdo25 5:f1613df66ceb 49 void setSetPoint(float _sP)
mazdo25 5:f1613df66ceb 50 {
mazdo25 5:f1613df66ceb 51 setPoint = _sP;
mazdo25 5:f1613df66ceb 52 }
mazdo25 5:f1613df66ceb 53
mazdo25 5:f1613df66ceb 54 void setInputLimits(float inMin,float inMax)
mazdo25 5:f1613df66ceb 55 {
mazdo25 5:f1613df66ceb 56 if (inMin > inMax) {return;}
mazdo25 5:f1613df66ceb 57
mazdo25 5:f1613df66ceb 58 inMin_ = inMin;
mazdo25 5:f1613df66ceb 59 inMax_ = inMax;
mazdo25 5:f1613df66ceb 60 }
mazdo25 5:f1613df66ceb 61
mazdo25 5:f1613df66ceb 62 void assignLimitAddress(float *outMax, float *outMin)
mazdo25 5:f1613df66ceb 63 {
mazdo25 5:f1613df66ceb 64 outMin_ = outMax;
mazdo25 5:f1613df66ceb 65 outMax_ = outMin;
mazdo25 5:f1613df66ceb 66 }
mazdo25 5:f1613df66ceb 67
mazdo25 5:f1613df66ceb 68 float scaler(float prevMin, float prevMax, float newMin, float newMax, float var)
mazdo25 5:f1613df66ceb 69 {
mazdo25 5:f1613df66ceb 70 if (var > prevMax) {var = prevMax;}
mazdo25 5:f1613df66ceb 71 if (var < prevMin) {var = prevMin;}
mazdo25 5:f1613df66ceb 72 return (((var-prevMin)/(prevMax - prevMin))*(newMax-newMin))+newMin;
mazdo25 5:f1613df66ceb 73 }
mazdo25 5:f1613df66ceb 74
mazdo25 5:f1613df66ceb 75 float returnPrevCA()
mazdo25 5:f1613df66ceb 76 {
mazdo25 5:f1613df66ceb 77 return prevControlAction;
mazdo25 5:f1613df66ceb 78 }
mazdo25 5:f1613df66ceb 79
mazdo25 5:f1613df66ceb 80 };
mazdo25 5:f1613df66ceb 81
mazdo25 5:f1613df66ceb 82 //(((temp-inMin)/(inMax - inMin))*(outMax-outMin))+outMin