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.
Dependents: Autonomous_quadcopter
Fork of PID by
Diff: PID.cpp
- Revision:
- 2:b03de6191e60
- Parent:
- 1:a36e49dac330
- Child:
- 3:1a8e62899e55
--- a/PID.cpp Fri Dec 01 11:11:13 2017 +0000
+++ b/PID.cpp Sun Mar 04 15:16:39 2018 +0000
@@ -229,7 +229,7 @@
//Pull in the input and setpoint, and scale them into percent span.
float scaledPV = (processVariable_ - inMin_) / inSpan_;
-
+
if (scaledPV > 1.0) {
scaledPV = 1.0;
} else if (scaledPV < 0.0) {
@@ -237,6 +237,7 @@
}
float scaledSP = (setPoint_ - inMin_) / inSpan_;
+
if (scaledSP > 1.0) {
scaledSP = 1;
} else if (scaledSP < 0.0) {
@@ -244,24 +245,26 @@
}
float error = scaledSP - scaledPV;
+
//Check and see if the output is pegged at a limit and only
//integrate if it is not. This is to prevent reset-windup.
if (!(prevControllerOutput_ >= 1 && error > 0) && !(prevControllerOutput_ <= 0 && error < 0)) {
accError_ += error;
+
}
//Compute the current slope of the input signal.
float dMeas = (scaledPV - prevProcessVariable_) / tSample_;
-
+
float scaledBias = 0.0;
-
if (usingFeedForward) {
scaledBias = (bias_ - outMin_) / outSpan_;
}
//Perform the PID calculation.
controllerOutput_ = scaledBias + Kc_ * (error + (tauR_ * accError_) - (tauD_ * dMeas));
+
//Make sure the computed output is within output constraints.
if (controllerOutput_ < 0.0) {
@@ -272,8 +275,25 @@
//Remember this output for the windup check next time.
prevControllerOutput_ = controllerOutput_;
+
//Remember the input for the derivative calculation next time.
prevProcessVariable_ = scaledPV;
+
+ //printf("processVariable_ %f\n\r", processVariable_);
+ //printf("ScaledPV %f\n\r", scaledPV);
+ //printf("setPoint_ %f\n\r", setPoint_);
+ //printf("scaledSP %f\n\r", scaledSP);
+ //printf("error %f\n\r", error);
+ //printf("accError_ %f\n\r", accError_);
+ //printf("tSample_ %f\n\r", tSample_);
+ //printf("dMeas %f\n\r", dMeas);
+ //printf("controller output %f \n\r", controllerOutput_);
+ //printf("Kc_ output %f \n\r", Kc_);
+ //printf("tauR_ output %f \n\r", tauR_);
+ //printf("tauD_ output %f \n\r", tauD_);
+ //printf("dMeas output %f \n\r", dMeas);
+ //printf("prevControllerOutput_ %f\n\r", prevControllerOutput_);
+ //printf("prevProcessVariable_ %f\n\r", prevProcessVariable_);
//Scale the output from percent span back out to a real world number.
return ((controllerOutput_ * outSpan_) + outMin_);
