Allow to set the resolution to either microseconds or microseconds
This commit is contained in:
parent
d21d7e3d09
commit
9f59efabfc
@ -34,7 +34,7 @@ PID::PID(double* Input, double* Output, double* Setpoint,
|
|||||||
PID::SetControllerDirection(ControllerDirection);
|
PID::SetControllerDirection(ControllerDirection);
|
||||||
PID::SetTunings(Kp, Ki, Kd);
|
PID::SetTunings(Kp, Ki, Kd);
|
||||||
|
|
||||||
lastTime = millis()-SampleTime;
|
PID::setResolution(MILLIS); // Use a resolution of milliseconds by default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ PID::PID(double* Input, double* Output, double* Setpoint,
|
|||||||
bool PID::Compute()
|
bool PID::Compute()
|
||||||
{
|
{
|
||||||
if(!inAuto) return false;
|
if(!inAuto) return false;
|
||||||
unsigned long now = millis();
|
unsigned long now = PID::getTime();
|
||||||
unsigned long timeChange = (now - lastTime);
|
unsigned long timeChange = (now - lastTime);
|
||||||
if(timeChange>=SampleTime)
|
if(timeChange>=SampleTime)
|
||||||
{
|
{
|
||||||
@ -86,7 +86,7 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
|
|||||||
|
|
||||||
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
||||||
|
|
||||||
double SampleTimeInSec = ((double)SampleTime)/1000;
|
double SampleTimeInSec = ((double)SampleTime)/secondsDivider;
|
||||||
kp = Kp;
|
kp = Kp;
|
||||||
ki = Ki * SampleTimeInSec;
|
ki = Ki * SampleTimeInSec;
|
||||||
kd = Kd / SampleTimeInSec;
|
kd = Kd / SampleTimeInSec;
|
||||||
@ -182,6 +182,29 @@ void PID::SetControllerDirection(int Direction)
|
|||||||
controllerDirection = Direction;
|
controllerDirection = Direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* getTime()*******************************************************************
|
||||||
|
* Will get the current time either by using millis() or micros()
|
||||||
|
******************************************************************************/
|
||||||
|
unsigned long PID::getTime()
|
||||||
|
{
|
||||||
|
if (secondsDivider == 1000.0) return millis();
|
||||||
|
return micros();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* setResolution(...)**********************************************************
|
||||||
|
* Will set the resolution of getTime().
|
||||||
|
* MILLIS will set the resolution to milliseconds while
|
||||||
|
* MICROS will set the resolution to microseconds.
|
||||||
|
******************************************************************************/
|
||||||
|
void PID::setResolution(int resolution)
|
||||||
|
{
|
||||||
|
if (resolution == MILLIS)
|
||||||
|
secondsDivider = 1000.0;
|
||||||
|
else
|
||||||
|
secondsDivider = 1000000.0;
|
||||||
|
lastTime = PID::getTime()-SampleTime; // Update last time variable
|
||||||
|
}
|
||||||
|
|
||||||
/* Status Funcions*************************************************************
|
/* Status Funcions*************************************************************
|
||||||
* Just because you set the Kp=-1 doesn't mean it actually happened. these
|
* Just because you set the Kp=-1 doesn't mean it actually happened. these
|
||||||
* functions query the internal state of the PID. they're here for display
|
* functions query the internal state of the PID. they're here for display
|
||||||
|
@ -13,6 +13,8 @@ class PID
|
|||||||
#define MANUAL 0
|
#define MANUAL 0
|
||||||
#define DIRECT 0
|
#define DIRECT 0
|
||||||
#define REVERSE 1
|
#define REVERSE 1
|
||||||
|
#define MILLIS 0
|
||||||
|
#define MICROS 1
|
||||||
|
|
||||||
//commonly used functions **************************************************************************
|
//commonly used functions **************************************************************************
|
||||||
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
|
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
|
||||||
@ -41,6 +43,9 @@ class PID
|
|||||||
// once it is set in the constructor.
|
// once it is set in the constructor.
|
||||||
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
|
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
|
||||||
// the PID calculation is performed. default is 100
|
// the PID calculation is performed. default is 100
|
||||||
|
void setResolution(int); // * Set the resolution of the getTime() function.
|
||||||
|
// MILLIS sets the resolution to milliseconds.
|
||||||
|
// MICROS sets the resolution to microseconds.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -53,6 +58,8 @@ class PID
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
unsigned long getTime(); // * This will call either millis() or micros()
|
||||||
|
// depending on the used resolution.
|
||||||
|
|
||||||
double dispKp; // * we'll hold on to the tuning parameters in user-entered
|
double dispKp; // * we'll hold on to the tuning parameters in user-entered
|
||||||
double dispKi; // format for display purposes
|
double dispKi; // format for display purposes
|
||||||
@ -73,6 +80,7 @@ class PID
|
|||||||
double ITerm, lastInput;
|
double ITerm, lastInput;
|
||||||
|
|
||||||
unsigned long SampleTime;
|
unsigned long SampleTime;
|
||||||
|
double secondsDivider;
|
||||||
double outMin, outMax;
|
double outMin, outMax;
|
||||||
bool inAuto;
|
bool inAuto;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user