By setting SetSampleTime to 0. Compute will calculate the output every time it's called
This commit is contained in:
parent
9f59efabfc
commit
6231da4cf9
@ -48,16 +48,24 @@ bool PID::Compute()
|
|||||||
{
|
{
|
||||||
if(!inAuto) return false;
|
if(!inAuto) return false;
|
||||||
unsigned long now = PID::getTime();
|
unsigned long now = PID::getTime();
|
||||||
unsigned long timeChange = (now - lastTime);
|
timeChange = (now - lastTime);
|
||||||
if(timeChange>=SampleTime)
|
if(SampleTime == 0 || timeChange>=SampleTime)
|
||||||
{
|
{
|
||||||
/*Compute all the working error variables*/
|
/*Compute all the working error variables*/
|
||||||
double input = *myInput;
|
double input = *myInput;
|
||||||
double error = *mySetpoint - input;
|
double error = *mySetpoint - input;
|
||||||
ITerm+= (ki * error);
|
|
||||||
|
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;
|
if(ITerm > outMax) ITerm= outMax;
|
||||||
else if(ITerm < outMin) ITerm= outMin;
|
else if(ITerm < outMin) ITerm= outMin;
|
||||||
double dInput = (input - lastInput);
|
|
||||||
|
|
||||||
/*Compute PID Output*/
|
/*Compute PID Output*/
|
||||||
double output = kp * error + ITerm- kd * dInput;
|
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;
|
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
||||||
|
|
||||||
|
if (SampleTime > 0) {
|
||||||
double SampleTimeInSec = ((double)SampleTime)/secondsDivider;
|
double SampleTimeInSec = ((double)SampleTime)/secondsDivider;
|
||||||
kp = Kp;
|
kp = Kp;
|
||||||
ki = Ki * SampleTimeInSec;
|
ki = Ki * SampleTimeInSec;
|
||||||
kd = Kd / SampleTimeInSec;
|
kd = Kd / SampleTimeInSec;
|
||||||
|
} else {
|
||||||
|
kp = Kp;
|
||||||
|
ki = Ki;
|
||||||
|
kd = Kd;
|
||||||
|
}
|
||||||
|
|
||||||
if(controllerDirection ==REVERSE)
|
if(controllerDirection ==REVERSE)
|
||||||
{
|
{
|
||||||
@ -100,18 +114,25 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* SetSampleTime(...) *********************************************************
|
/* SetSampleTime(...) *********************************************************
|
||||||
* sets the period, in Milliseconds, at which the calculation is performed
|
* 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)
|
void PID::SetSampleTime(int NewSampleTime)
|
||||||
{
|
{
|
||||||
if (NewSampleTime > 0)
|
if (NewSampleTime > 0)
|
||||||
{
|
{
|
||||||
double ratio = (double)NewSampleTime
|
double ratio;
|
||||||
/ (double)SampleTime;
|
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;
|
ki *= ratio;
|
||||||
kd /= ratio;
|
kd /= ratio;
|
||||||
SampleTime = (unsigned long)NewSampleTime;
|
SampleTime = (unsigned long)NewSampleTime;
|
||||||
}
|
} else
|
||||||
|
SampleTime = 0; // We will compute every time the function is called
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SetOutputLimits(...)****************************************************
|
/* SetOutputLimits(...)****************************************************
|
||||||
|
@ -78,6 +78,7 @@ class PID
|
|||||||
|
|
||||||
unsigned long lastTime;
|
unsigned long lastTime;
|
||||||
double ITerm, lastInput;
|
double ITerm, lastInput;
|
||||||
|
unsigned long timeChange;
|
||||||
|
|
||||||
unsigned long SampleTime;
|
unsigned long SampleTime;
|
||||||
double secondsDivider;
|
double secondsDivider;
|
||||||
|
Loading…
Reference in New Issue
Block a user