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 Boboobooov5 by
Diff: controller.cpp
- Revision:
- 10:03d5aa2511c4
- Parent:
- 9:33b99cb45e99
diff -r 33b99cb45e99 -r 03d5aa2511c4 controller.cpp
--- a/controller.cpp Tue Jun 24 10:06:54 2014 +0000
+++ b/controller.cpp Thu Jun 26 09:15:35 2014 +0000
@@ -2,14 +2,10 @@
#include "controller.h"
+
+
PID::PID(float in_min,float in_max,float out_min,float out_max,float Kc, float tauI, float tauD, float interval) {
- usingFeedForward = false;
- //inAuto = false;
-
- //Default the limits to the full range of I/O.
- //Make sure to set these to more appropriate limits for your application.
-
//BX tune
setInputLimits(in_min,in_max);
setOutputLimits(out_min,out_max);
@@ -24,10 +20,7 @@
controllerOutput_ = 0.0;
prevControllerOutput_ = 0.0;
- accError_ = 0.0;
- bias_ = 0.0;
-
- realOutput_ = 0.0;
+
}
@@ -38,21 +31,16 @@
return;
}
- //Rescale the working variables to reflect the changes.
- prevProcessVariable_ *= (inMax - inMin) / inSpan_;
- accError_ *= (inMax - inMin) / inSpan_;
+
- //Make sure the working variables are within the new limits.
- if (prevProcessVariable_ > 1) {
- prevProcessVariable_ = 1;
- }
- else if (prevProcessVariable_ < 0) {
- prevProcessVariable_ = 0;
- }
+
inMin_ = inMin;
inMax_ = inMax;
- inSpan_ = inMax - inMin;
+ inSpan_ = (inMax - inMin);
+
+
+
}
@@ -63,20 +51,20 @@
return;
}
- //Rescale the working variables to reflect the changes.
- prevControllerOutput_ *= (outMax - outMin) / outSpan_;
+
- //Make sure the working variables are within the new limits.
- if (prevControllerOutput_ > 1) {
- prevControllerOutput_ = 1;
- }
- else if (prevControllerOutput_ < 0) {
- prevControllerOutput_ = 0;
- }
+ //ppp
outMin_ = outMin;
outMax_ = outMax;
- outSpan_ = outMax - outMin;
+ outMid_ = (outMin+outMax)/2;
+
+ outSpan_ = (outMax - outMin);
+
+
+
+
+
}
@@ -98,99 +86,28 @@
iParam_ = tauI;
dParam_ = tauD;
- float tempTauR;
-
- if (tauI == 0.0) {
- tempTauR = 0.0;
- }
- else {
- tempTauR = (1.0 / tauI) * tSample_;
- }
+
- //For "bumpless transfer" we need to rescale the accumulated error.
- //if (inAuto) {
- //if (tempTauR == 0.0) {
- //accError_ = 0.0;
- //}
- //else {
- accError_ *= (Kc_ * tauR_) / (Kc * tempTauR);
- //}
- //}
+
Kc_ = Kc;
- tauR_ = tempTauR;
- tauD_ = tauD / tSample_;
-
-}
-
-void PID::reset(void) {
-
- float scaledBias = 0.0;
-
- if (usingFeedForward) {
- scaledBias = (bias_ - outMin_) / outSpan_;
- }
- else {
- scaledBias = (realOutput_ - outMin_) / outSpan_;
- }
-
- prevControllerOutput_ = scaledBias;
- prevProcessVariable_ = (processVariable_ - inMin_) / inSpan_;
-
- //Clear any error in the integral.
- accError_ = 0;
+ tauI_=tauI;
+ tauD_=tauD;
}
-/*
-void PID::setMode(int mode) {
-
- //We were in manual, and we just got set to auto.
- //Reset the controller internals.
- if (mode != 0 && !inAuto) {
- reset();
- }
-
- inAuto = (mode != 0);
-
-}*/
-
-void PID::setInterval(float interval) {
-
- if (interval > 0) {
- //Convert the time-based tunings to reflect this change.
- tauR_ *= (interval / tSample_);
- accError_ *= (tSample_ / interval);
- tauD_ *= (interval / tSample_);
- tSample_ = interval;
- }
-
-}
-/*
-void PID::setSetPoint(float sp) {
-
- setPoint_ = sp;
-
-}
-
-void PID::setProcessValue(float pv) {
-
- processVariable_ = pv;
-
-}
-*/
-void PID::setBias(float bias){
-
- bias_ = bias;
- usingFeedForward = 1;
-
-}
-
+
+
float PID::compute(float pv, float sp) {
- //enregistrer variables dans var interne
+
processVariable_ = pv; //ce que l'on mesure
setPoint_ = sp; // ce que l'on veut atteindre
+
+
+
+
+
//Pull in the input and setpoint, and scale them into percent span.
float scaledPV = (processVariable_ - inMin_) / inSpan_;
@@ -209,44 +126,43 @@
scaledSP = 0;
}
- float error = scaledSP - scaledPV;
+ float error = (scaledSP - scaledPV)*2;
+// 100~ -100%
+
+
- //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;
+
+// if error add
+ if ( (-1<error && error <1) ) {
+ accError_ = (accError_*0.66)+ error;
}
-
- //Compute the current slope of the input signal.
- float dMeas = (scaledPV - prevProcessVariable_) / tSample_;
- //float dMeas = (scaledPV - prevProcessVariable_);
-
- float scaledBias = 0.0;
-
- if (usingFeedForward) {
- scaledBias = (bias_ - outMin_) / outSpan_;
- }
-
- //Perform the PID calculation.
- controllerOutput_ = scaledBias + Kc_ * (error + (tauR_ * accError_) - (tauD_ * dMeas));
- //controllerOutput_ = Kc_ * error + tauR_ * accError_ + tauD_ * dMeas;
-
- //Make sure the computed output is within output constraints.
- if (controllerOutput_ < outMin_) {
+
+ float dMeas = (scaledPV - prevProcessVariable_) / tSample_;
+
+ controllerOutput_= Kc_* error+(tauI_*accError_)+tauD_*dMeas+outMid_;
+ de_ip=accError_;
+ de_dp=dMeas;
+ de_kp=error;
+
+
+
+
+
+
+ if (controllerOutput_ < outMin_) {
controllerOutput_ = outMin_;
}
- else if (controllerOutput_ >outMax_ ) {
+ else if (controllerOutput_ > outMax_) {
controllerOutput_ = outMax_;
}
-
- //Remember this output for the windup check next time.
- prevControllerOutput_ = controllerOutput_;
- //Remember the input for the derivative calculation next time.
- prevProcessVariable_ = scaledPV;
+
+
+
+
+ prevProcessVariable_ = scaledPV;
- //Scale the output from percent span back out to a real world number.
- return (controllerOutput_ );
+ return (controllerOutput_);
}
