|
|
@ -1,5 +1,5 @@
|
|
|
|
/**********************************************************************************************
|
|
|
|
/**********************************************************************************************
|
|
|
|
* Arduino PID Library - Version 1.2.1
|
|
|
|
* Arduino Makeblock PID Library - Version 0.0.1
|
|
|
|
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
|
|
|
|
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This Library is licensed under the MIT License
|
|
|
|
* This Library is licensed under the MIT License
|
|
|
@ -14,10 +14,10 @@
|
|
|
|
#include <PID_v1.h>
|
|
|
|
#include <PID_v1.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*Constructor (...)*********************************************************
|
|
|
|
/*Constructor (...)*********************************************************
|
|
|
|
* The parameters specified here are those for for which we can't set up
|
|
|
|
* The parameters specified here are those for which we can't set up
|
|
|
|
* reliable defaults, so we need to have the user set them.
|
|
|
|
* reliable defaults, so we need to have the user set them.
|
|
|
|
***************************************************************************/
|
|
|
|
***************************************************************************/
|
|
|
|
PID::PID(double* Input, double* Output, double* Setpoint,
|
|
|
|
PID2::PID2(double* Input, double* Output, double* Setpoint,
|
|
|
|
double Kp, double Ki, double Kd, int POn, int ControllerDirection)
|
|
|
|
double Kp, double Ki, double Kd, int POn, int ControllerDirection)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
myOutput = Output;
|
|
|
|
myOutput = Output;
|
|
|
@ -25,13 +25,13 @@ PID::PID(double* Input, double* Output, double* Setpoint,
|
|
|
|
mySetpoint = Setpoint;
|
|
|
|
mySetpoint = Setpoint;
|
|
|
|
inAuto = false;
|
|
|
|
inAuto = false;
|
|
|
|
|
|
|
|
|
|
|
|
PID::SetOutputLimits(0, 255); //default output limit corresponds to
|
|
|
|
PID2::SetOutputLimits(0, 255); //default output limit corresponds to
|
|
|
|
//the arduino pwm limits
|
|
|
|
//the arduino pwm limits
|
|
|
|
|
|
|
|
|
|
|
|
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
|
|
|
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
|
|
|
|
|
|
|
|
|
|
|
PID::SetControllerDirection(ControllerDirection);
|
|
|
|
PID2::SetControllerDirection(ControllerDirection);
|
|
|
|
PID::SetTunings(Kp, Ki, Kd, POn);
|
|
|
|
PID2::SetTunings(Kp, Ki, Kd, POn);
|
|
|
|
|
|
|
|
|
|
|
|
lastTime = millis()-SampleTime;
|
|
|
|
lastTime = millis()-SampleTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -41,9 +41,9 @@ PID::PID(double* Input, double* Output, double* Setpoint,
|
|
|
|
* to use Proportional on Error without explicitly saying so
|
|
|
|
* to use Proportional on Error without explicitly saying so
|
|
|
|
***************************************************************************/
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
PID::PID(double* Input, double* Output, double* Setpoint,
|
|
|
|
PID2::PID2(double* Input, double* Output, double* Setpoint,
|
|
|
|
double Kp, double Ki, double Kd, int ControllerDirection)
|
|
|
|
double Kp, double Ki, double Kd, int ControllerDirection)
|
|
|
|
:PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E, ControllerDirection)
|
|
|
|
:PID2::PID2(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E, ControllerDirection)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -55,7 +55,7 @@ PID::PID(double* Input, double* Output, double* Setpoint,
|
|
|
|
* pid Output needs to be computed. returns true when the output is computed,
|
|
|
|
* pid Output needs to be computed. returns true when the output is computed,
|
|
|
|
* false when nothing has been done.
|
|
|
|
* false when nothing has been done.
|
|
|
|
**********************************************************************************/
|
|
|
|
**********************************************************************************/
|
|
|
|
bool PID::Compute()
|
|
|
|
bool PID2::Compute()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!inAuto) return false;
|
|
|
|
if(!inAuto) return false;
|
|
|
|
unsigned long now = millis();
|
|
|
|
unsigned long now = millis();
|
|
|
@ -75,7 +75,7 @@ bool PID::Compute()
|
|
|
|
else if(outputSum < outMin) outputSum= outMin;
|
|
|
|
else if(outputSum < outMin) outputSum= outMin;
|
|
|
|
|
|
|
|
|
|
|
|
/*Add Proportional on Error, if P_ON_E is specified*/
|
|
|
|
/*Add Proportional on Error, if P_ON_E is specified*/
|
|
|
|
double output;
|
|
|
|
double output;
|
|
|
|
if(pOnE) output = kp * error;
|
|
|
|
if(pOnE) output = kp * error;
|
|
|
|
else output = 0;
|
|
|
|
else output = 0;
|
|
|
|
|
|
|
|
|
|
|
@ -99,7 +99,7 @@ bool PID::Compute()
|
|
|
|
* it's called automatically from the constructor, but tunings can also
|
|
|
|
* it's called automatically from the constructor, but tunings can also
|
|
|
|
* be adjusted on the fly during normal operation
|
|
|
|
* be adjusted on the fly during normal operation
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetTunings(double Kp, double Ki, double Kd, int POn)
|
|
|
|
void PID2::SetTunings(double Kp, double Ki, double Kd, int POn)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (Kp<0 || Ki<0 || Kd<0) return;
|
|
|
|
if (Kp<0 || Ki<0 || Kd<0) return;
|
|
|
|
|
|
|
|
|
|
|
@ -124,14 +124,14 @@ void PID::SetTunings(double Kp, double Ki, double Kd, int POn)
|
|
|
|
/* SetTunings(...)*************************************************************
|
|
|
|
/* SetTunings(...)*************************************************************
|
|
|
|
* Set Tunings using the last-rembered POn setting
|
|
|
|
* Set Tunings using the last-rembered POn setting
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetTunings(double Kp, double Ki, double Kd){
|
|
|
|
void PID2::SetTunings(double Kp, double Ki, double Kd){
|
|
|
|
SetTunings(Kp, Ki, Kd, pOn);
|
|
|
|
SetTunings(Kp, Ki, Kd, pOn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* SetSampleTime(...) *********************************************************
|
|
|
|
/* SetSampleTime(...) *********************************************************
|
|
|
|
* sets the period, in Milliseconds, at which the calculation is performed
|
|
|
|
* sets the period, in Milliseconds, at which the calculation is performed
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetSampleTime(int NewSampleTime)
|
|
|
|
void PID2::SetSampleTime(int NewSampleTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (NewSampleTime > 0)
|
|
|
|
if (NewSampleTime > 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -151,7 +151,7 @@ void PID::SetSampleTime(int NewSampleTime)
|
|
|
|
* want to clamp it from 0-125. who knows. at any rate, that can all be done
|
|
|
|
* want to clamp it from 0-125. who knows. at any rate, that can all be done
|
|
|
|
* here.
|
|
|
|
* here.
|
|
|
|
**************************************************************************/
|
|
|
|
**************************************************************************/
|
|
|
|
void PID::SetOutputLimits(double Min, double Max)
|
|
|
|
void PID2::SetOutputLimits(double Min, double Max)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(Min >= Max) return;
|
|
|
|
if(Min >= Max) return;
|
|
|
|
outMin = Min;
|
|
|
|
outMin = Min;
|
|
|
@ -172,12 +172,12 @@ void PID::SetOutputLimits(double Min, double Max)
|
|
|
|
* when the transition from manual to auto occurs, the controller is
|
|
|
|
* when the transition from manual to auto occurs, the controller is
|
|
|
|
* automatically initialized
|
|
|
|
* automatically initialized
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetMode(int Mode)
|
|
|
|
void PID2::SetMode(int Mode)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool newAuto = (Mode == AUTOMATIC);
|
|
|
|
bool newAuto = (Mode == AUTOMATIC);
|
|
|
|
if(newAuto && !inAuto)
|
|
|
|
if(newAuto && !inAuto)
|
|
|
|
{ /*we just went from manual to auto*/
|
|
|
|
{ /*we just went from manual to auto*/
|
|
|
|
PID::Initialize();
|
|
|
|
PID2::Initialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inAuto = newAuto;
|
|
|
|
inAuto = newAuto;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -186,7 +186,7 @@ void PID::SetMode(int Mode)
|
|
|
|
* does all the things that need to happen to ensure a bumpless transfer
|
|
|
|
* does all the things that need to happen to ensure a bumpless transfer
|
|
|
|
* from manual to automatic mode.
|
|
|
|
* from manual to automatic mode.
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::Initialize()
|
|
|
|
void PID2::Initialize()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
outputSum = *myOutput;
|
|
|
|
outputSum = *myOutput;
|
|
|
|
lastInput = *myInput;
|
|
|
|
lastInput = *myInput;
|
|
|
@ -200,11 +200,11 @@ void PID::Initialize()
|
|
|
|
* know which one, because otherwise we may increase the output when we should
|
|
|
|
* know which one, because otherwise we may increase the output when we should
|
|
|
|
* be decreasing. This is called from the constructor.
|
|
|
|
* be decreasing. This is called from the constructor.
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetControllerDirection(int Direction)
|
|
|
|
void PID2::SetControllerDirection(int Direction)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(inAuto && Direction !=controllerDirection)
|
|
|
|
if(inAuto && Direction !=controllerDirection)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
kp = (0 - kp);
|
|
|
|
kp = (0 - kp);
|
|
|
|
ki = (0 - ki);
|
|
|
|
ki = (0 - ki);
|
|
|
|
kd = (0 - kd);
|
|
|
|
kd = (0 - kd);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -216,9 +216,9 @@ void PID::SetControllerDirection(int Direction)
|
|
|
|
* 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
|
|
|
|
* purposes. this are the functions the PID Front-end uses for example
|
|
|
|
* purposes. this are the functions the PID Front-end uses for example
|
|
|
|
******************************************************************************/
|
|
|
|
******************************************************************************/
|
|
|
|
double PID::GetKp(){ return dispKp; }
|
|
|
|
double PID2::GetKp(){ return dispKp; }
|
|
|
|
double PID::GetKi(){ return dispKi;}
|
|
|
|
double PID2::GetKi(){ return dispKi;}
|
|
|
|
double PID::GetKd(){ return dispKd;}
|
|
|
|
double PID2::GetKd(){ return dispKd;}
|
|
|
|
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
|
|
|
|
int PID2::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
|
|
|
|
int PID::GetDirection(){ return controllerDirection;}
|
|
|
|
int PID2::GetDirection(){ return controllerDirection;}
|
|
|
|
|
|
|
|
|
|
|
|