By setting SetSampleTime to 0. Compute will calculate the output every time it's called

This commit is contained in:
Kristian Sloth Lauszus 2013-07-19 16:30:18 +02:00
parent 9f59efabfc
commit 6231da4cf9
2 changed files with 30 additions and 8 deletions

View File

@ -48,16 +48,24 @@ bool PID::Compute()
{
if(!inAuto) return false;
unsigned long now = PID::getTime();
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime)
timeChange = (now - lastTime);
if(SampleTime == 0 || timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double input = *myInput;
double error = *mySetpoint - input;
double dInput;
if (SampleTime > 0) {
ITerm += (ki * error);
dInput = (input - lastInput);
} else {
ITerm += (ki * error)*(((double)timeChange)/secondsDivider);
dInput = (input - lastInput)/(((double)timeChange)/secondsDivider);
}
if(ITerm > outMax) ITerm= outMax;
else if(ITerm < outMin) ITerm= outMin;
double dInput = (input - lastInput);
/*Compute PID Output*/
double output = kp * error + ITerm- kd * dInput;
@ -86,10 +94,16 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
dispKp = Kp; dispKi = Ki; dispKd = Kd;
if (SampleTime > 0) {
double SampleTimeInSec = ((double)SampleTime)/secondsDivider;
kp = Kp;
ki = Ki * SampleTimeInSec;
kd = Kd / SampleTimeInSec;
} else {
kp = Kp;
ki = Ki;
kd = Kd;
}
if(controllerDirection ==REVERSE)
{
@ -101,17 +115,24 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
/* SetSampleTime(...) *********************************************************
* sets the period, in Milliseconds, at which the calculation is performed
* If it's set to 0 or a negative value it will computer every time the
* function is called
******************************************************************************/
void PID::SetSampleTime(int NewSampleTime)
{
if (NewSampleTime > 0)
{
double ratio = (double)NewSampleTime
/ (double)SampleTime;
double ratio;
if (SampleTime > 0)
ratio = (double)NewSampleTime/(double)SampleTime;
else
ratio = (double)NewSampleTime/(double)timeChange; // We will assume the user is calling Compute at a regular interval
ki *= ratio;
kd /= ratio;
SampleTime = (unsigned long)NewSampleTime;
}
} else
SampleTime = 0; // We will compute every time the function is called
}
/* SetOutputLimits(...)****************************************************

View File

@ -78,6 +78,7 @@ class PID
unsigned long lastTime;
double ITerm, lastInput;
unsigned long timeChange;
unsigned long SampleTime;
double secondsDivider;