Allow to set the resolution to either microseconds or microseconds

This commit is contained in:
Kristian Sloth Lauszus 2013-07-19 16:23:42 +02:00
parent d21d7e3d09
commit 9f59efabfc
2 changed files with 38 additions and 7 deletions

View File

@ -34,7 +34,7 @@ PID::PID(double* Input, double* Output, double* Setpoint,
PID::SetControllerDirection(ControllerDirection);
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()
{
if(!inAuto) return false;
unsigned long now = millis();
unsigned long now = PID::getTime();
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
@ -85,11 +85,11 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
if (Kp<0 || Ki<0 || Kd<0) return;
dispKp = Kp; dispKi = Ki; dispKd = Kd;
double SampleTimeInSec = ((double)SampleTime)/1000;
kp = Kp;
ki = Ki * SampleTimeInSec;
kd = Kd / SampleTimeInSec;
double SampleTimeInSec = ((double)SampleTime)/secondsDivider;
kp = Kp;
ki = Ki * SampleTimeInSec;
kd = Kd / SampleTimeInSec;
if(controllerDirection ==REVERSE)
{
@ -182,6 +182,29 @@ void PID::SetControllerDirection(int 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*************************************************************
* 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

View File

@ -13,6 +13,8 @@ class PID
#define MANUAL 0
#define DIRECT 0
#define REVERSE 1
#define MILLIS 0
#define MICROS 1
//commonly used functions **************************************************************************
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.
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
// 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:
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 dispKi; // format for display purposes
@ -73,6 +80,7 @@ class PID
double ITerm, lastInput;
unsigned long SampleTime;
double secondsDivider;
double outMin, outMax;
bool inAuto;
};