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.
Fork of PID by
Revision 7:52f66036f628, committed 2016-12-24
- Comitter:
- benson516
- Date:
- Sat Dec 24 09:51:00 2016 +0000
- Parent:
- 4:e3c9cb64be44
- Commit message:
- Add the reset function
Changed in this revision
| PID.cpp | Show annotated file Show diff for this revision Revisions of this file |
| PID.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/PID.cpp Wed Oct 26 17:56:01 2016 +0000
+++ b/PID.cpp Sat Dec 24 09:51:00 2016 +0000
@@ -1,27 +1,27 @@
#include "PID.h"
PID::PID(float Kp_in, float Ki_in, float Kd_in, float Sampletime_in){
-
+
// Parameters
- Outputlimit_bool = true;
+ Outputlimit_bool = false; // true
Inputlimit_bool = false;
- AntiWindUp_bool = true;
+ AntiWindUp_bool = false; // true
outputLimits_H = 7.4;
outputLimits_L = -7.4;
inputLimits_H = 0.0;
- inputLimits_L = 0.0;
- feedbackvalue = 0.0;
-
- // Feedback gain
+ inputLimits_L = 0.0;
+ feedbackvalue = 0.0;
+
+ // Feedback gain
Kp = Kp_in;
Ki = Ki_in;
Kd = Kd_in;
// Ka = 0.1; // Volt.-sec./deg.
Ka = 0.0017;// Volt.-sec./rad
-
+
// Sampling time
- Ts = Sampletime_in;
-
+ Ts = Sampletime_in;
+
// Variables
error[0] = 0.0;
error[1] = 0.0;
@@ -29,21 +29,21 @@
//
reference = 0.0;
output = 0.0;
-
+
-
+
}
void PID::SetOutputLimits(float setoutputLimits_H, float setoutputLimits_L){
Outputlimit_bool = true;
outputLimits_H = setoutputLimits_H;
- outputLimits_L = setoutputLimits_L;
+ outputLimits_L = setoutputLimits_L;
}
void PID::SetInputLimits(float setinputLimits_H, float setinputLimits_L){
Inputlimit_bool = true;
inputLimits_H = setinputLimits_H;
- inputLimits_L = setinputLimits_L;
+ inputLimits_L = setinputLimits_L;
}
void PID::EnableAntiWindUp(float Ka_in)
@@ -51,7 +51,12 @@
AntiWindUp_bool = true;
Ka = Ka_in;
}
-
+void PID::Reset(void){
+ error[0] = 0.0;
+ error[1] = 0.0;
+ error_I = 0.0;
+ output = 0.0;
+}
void PID::Compute(float reference_in, float feedbackvalue_in){
if(Inputlimit_bool == true){
@@ -62,24 +67,24 @@
}else{
reference = reference_in;
}
-
+
}else{
reference = reference_in;
}
-
+
// bypass
feedbackvalue = feedbackvalue_in;
-
+
error[0] = reference - feedbackvalue;
// output = output + ( Kp + Ki + Kd )*error[0] + ( -Kp - 2.0*Kd )*error[1] + Kd*error[2];
output = Kp*error[0] + Ki*error_I;
-
+
// Delay 1
- error[1] = error[0];
-
+ error[1] = error[0];
+
// Integration
error_I += Ts*error[0];
-
+
// Output satuation
if(Outputlimit_bool && AntiWindUp_bool){
if( output >= outputLimits_H){
@@ -94,7 +99,7 @@
}else{
// output = output;
}
-
+
}
void PID::Compute_noWindUP(float reference_in, float feedbackvalue_in){
@@ -106,24 +111,24 @@
}else{
reference = reference_in;
}
-
+
}else{
reference = reference_in;
}
-
+
// bypass
feedbackvalue = feedbackvalue_in;
-
+
error[0] = reference - feedbackvalue;
// output = output + ( Kp + Ki + Kd )*error[0] + ( -Kp - 2.0*Kd )*error[1] + Kd*error[2];
output = Kp*error[0] + Ki*error_I;
-
+
// Delay 1
- error[1] = error[0];
-
+ error[1] = error[0];
+
// Integration
error_I += Ts*error[0];
-
+
// Output satuation
/*
if(Outputlimit_bool && AntiWindUp_bool){
@@ -140,8 +145,12 @@
// output = output;
}
*/
-
+
}
+
+// Use separately
+// Compute_noWindUP() -- [ Saturation_output(): optional, can be replaced by customized saturation function ] -- Anti_windup(delta) -- output
+//////////////////////////////////////////////////
void PID::Saturation_output(){
if( output >= outputLimits_H){
delta_output = outputLimits_H - output;
@@ -156,4 +165,16 @@
void PID::Anti_windup(float delta){ // delta_V = Vs - V
// Anti-windup compensation
error_I += Ka*delta; // Anti-windup
-}
\ No newline at end of file
+}
+////////////////////////////////////////////////// end Use separately
+
+// Use alone
+// Compute_noWindUP() -- Anti_windup() -- output
+//////////////////////////////////////////////////
+void PID::Anti_windup(){ // delta_V = Vs - V
+
+ PID::Saturation_output();
+ // Anti-windup compensation
+ error_I += Ka*PID::delta_output; // Anti-windup
+}
+////////////////////////////////////////////////// end Use alone
--- a/PID.h Wed Oct 26 17:56:01 2016 +0000
+++ b/PID.h Sat Dec 24 09:51:00 2016 +0000
@@ -6,47 +6,50 @@
class PID{
public:
- PID(float Kp_in, float Ki_in, float Kd_in, float Sampletime_in);
+ PID(float Kp_in, float Ki_in, float Kd_in, float Sampletime_in);
void Compute(float reference_in, float feedbackvalue_in);
-
- //
+ void Reset(void);
+ //
void Compute_noWindUP(float reference_in, float feedbackvalue_in);
+ // Method 1: Separated operation for anti-windup
void Saturation_output();
void Anti_windup(float delta); // delta_V = Vs - V
+ // Method 2: Single anti-windup operation
+ void Anti_windup();
//
-
+
void SetOutputLimits(float setoutputLimits_H, float setoutputLimits_L);
- void SetInputLimits(float setinputLimits_H, float setinputLimits_L);
- void EnableAntiWindUp(float Ka_in);
-
+ void SetInputLimits(float setinputLimits_H, float setinputLimits_L);
+ void EnableAntiWindUp(float Ka_in);
+
float Kp;
float Ki;
float Kd;
float Ka;
-
+
float error[2];
double error_I;
-
+
float output;
float reference;
float delta_output; // Error by saturating
-
+
float Ts;
private:
-
+
bool Outputlimit_bool;
bool Inputlimit_bool;
bool AntiWindUp_bool;
-
+
float outputLimits_H;
float outputLimits_L;
float inputLimits_H;
float inputLimits_L;
-
+
float feedbackvalue;
// Ticker PID_timer;
};
-#endif /* PID_H*/
\ No newline at end of file
+#endif /* PID_H*/
