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
Diff: PID.h
- Revision:
- 5:f1613df66ceb
- Parent:
- 4:208f5279143a
- Child:
- 6:477382219bcf
--- a/PID.h Sat Mar 23 19:46:09 2019 +0000
+++ b/PID.h Mon Mar 25 22:42:31 2019 +0000
@@ -3,8 +3,8 @@
private:
float Kp_, Ki_, Kd_, Ts, PrevErr, PrevPrevErr, prevControlAction, setPoint;
- float inMin_;
- float inMax_;
+ float* inMin_;
+ float* inMax_;
float outMin_;
float outMax_;
@@ -17,8 +17,6 @@
prevControlAction = 0;
setPoint = 0;
- inMin_ = -3.3f;
- inMax_ = 3.3f;
outMin_ = -1.0f;
outMax_ = 1.0f;
@@ -30,23 +28,24 @@
float compute (float currVal)
{
- if (currVal >
float currErr = setPoint - (currVal);
float controlAction;
- if (currErr > inMax_) {currErr = inMax_;}
- if (currErr < inMin_) {currErr = inMin_;}
+ 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_;}
+ 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_;
+ return ((((controlAction)- *inMin_)/(*inMax_ - *inMin_)) * (outMax_ - outMin_)) + outMin_;
}
void setSetPoint(float _sP)
@@ -62,16 +61,15 @@
outMax_ = outMax;
}
- void setInputLimits(float inMin,float inMax)
+ void assignLimitAddress(float *inMax, float *inMin)
{
- if (inMin > inMax) {return;}
- //scales the previous errors and control action to correct input limits
- PrevPrevErr = scaler(inMin_,inMax_,inMin,inMax,PrevPrevErr);
- PrevErr = scaler(inMin_,inMax_,inMin,inMax,PrevErr);
- prevControlAction = scaler(inMin_,inMax_,inMin,inMax,prevControlAction);
-
+ inMax_ = inMax;
inMin_ = inMin;
- inMax_ = inMax;
+ }
+
+ float returnInMax()
+ {
+ return *inMax_;
}
float scaler(float prevMin, float prevMax, float newMin, float newMax, float var)
@@ -86,11 +84,6 @@
return prevControlAction;
}
- float returnInMax()
- {
- return inMax_;
- }
-
};
//(((temp-inMin)/(inMax - inMin))*(outMax-outMin))+outMin
\ No newline at end of file