2020-02-17 19:14:48 +01:00
|
|
|
/**********************************************************************************************
|
|
|
|
* Arduino PID Library - Version 1.2.1
|
|
|
|
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
|
|
|
|
*
|
|
|
|
* This Library is licensed under the MIT License
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#if ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "PID_v2.h"
|
|
|
|
|
|
|
|
/*Constructor (...)*********************************************************
|
|
|
|
* The parameters specified here are those for for which we can't set up
|
|
|
|
* reliable defaults, so we need to have the user set them.
|
|
|
|
***************************************************************************/
|
2021-05-28 20:53:35 +02:00
|
|
|
PID::PID(double* Input_, double* Output_, double* Setpoint_,
|
|
|
|
double Kp, double Ki, double Kd, int ControllerDirection)
|
2020-02-17 19:14:48 +01:00
|
|
|
{
|
2021-05-28 20:53:35 +02:00
|
|
|
Output = Output_;
|
|
|
|
Input = Input_;
|
|
|
|
Setpoint = Setpoint_;
|
2020-02-17 19:14:48 +01:00
|
|
|
inAuto = false;
|
|
|
|
|
|
|
|
PID::SetOutputLimits(0, 255); //default output limit corresponds to
|
|
|
|
//the arduino pwm limits
|
|
|
|
|
|
|
|
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
|
|
|
|
|
|
|
PID::SetControllerDirection(ControllerDirection);
|
2021-05-28 20:53:35 +02:00
|
|
|
PID::SetTunings(Kp, Ki, Kd);
|
2020-02-17 19:14:48 +01:00
|
|
|
|
|
|
|
lastTime = millis()-SampleTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute() **********************************************************************
|
|
|
|
* This, as they say, is where the magic happens. this function should be called
|
|
|
|
* every time "void loop()" executes. the function will decide for itself whether a new
|
|
|
|
* pid Output needs to be computed. returns true when the output is computed,
|
|
|
|
* false when nothing has been done.
|
|
|
|
**********************************************************************************/
|
|
|
|
bool PID::Compute()
|
|
|
|
{
|
2021-05-28 20:53:35 +02:00
|
|
|
if(!inAuto) return;
|
2020-02-17 19:14:48 +01:00
|
|
|
unsigned long now = millis();
|
2021-05-28 20:53:35 +02:00
|
|
|
int timeChange = (now - lastTime);
|
2020-02-17 19:14:48 +01:00
|
|
|
if(timeChange>=SampleTime)
|
|
|
|
{
|
|
|
|
/*Compute all the working error variables*/
|
2021-05-28 20:53:35 +02:00
|
|
|
double error = *Setpoint - *Input;
|
|
|
|
|
2020-02-19 17:44:31 +01:00
|
|
|
if(angleWrap){
|
2021-05-28 20:53:35 +02:00
|
|
|
if(error > 180) error = error - 360;
|
|
|
|
else if(error < -180) error = error + 360;
|
2020-02-19 17:44:31 +01:00
|
|
|
}
|
2020-02-17 19:14:48 +01:00
|
|
|
|
2021-05-28 20:53:35 +02:00
|
|
|
ITerm+= (ki * error);
|
|
|
|
if(ITerm > outMax) ITerm= outMax;
|
|
|
|
else if(ITerm < outMin) ITerm= outMin;
|
|
|
|
double dInput = (*Input - lastInput);
|
2020-02-17 19:14:48 +01:00
|
|
|
|
2021-05-28 20:53:35 +02:00
|
|
|
if(angleWrap){
|
|
|
|
if(dInput > 180) dInput = dInput - 360;
|
|
|
|
else if(dInput < -180) dInput = dInput + 360;
|
2020-02-17 19:14:48 +01:00
|
|
|
}
|
2021-05-28 20:53:35 +02:00
|
|
|
|
|
|
|
/*Compute PID Output*/
|
|
|
|
*Output = kp * error + ITerm- kd * dInput;
|
|
|
|
if(*Output > outMax) *Output = outMax;
|
|
|
|
else if(*Output < outMin) *Output = outMin;
|
|
|
|
|
2020-02-17 19:14:48 +01:00
|
|
|
/*Remember some variables for next time*/
|
2021-05-28 20:53:35 +02:00
|
|
|
lastInput = *Input;
|
2020-02-17 19:14:48 +01:00
|
|
|
lastTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SetTunings(...)*************************************************************
|
|
|
|
* This function allows the controller's dynamic performance to be adjusted.
|
|
|
|
* it's called automatically from the constructor, but tunings can also
|
|
|
|
* be adjusted on the fly during normal operation
|
|
|
|
******************************************************************************/
|
2021-05-28 20:53:35 +02:00
|
|
|
void PID::SetTunings(double Kp, double Ki, double Kd)
|
2020-02-17 19:14:48 +01:00
|
|
|
{
|
2021-05-28 20:53:35 +02:00
|
|
|
if (Kp<0 || Ki<0|| Kd<0) return;
|
|
|
|
|
|
|
|
double SampleTimeInSec = ((double)SampleTime)/1000;
|
2020-02-17 19:14:48 +01:00
|
|
|
kp = Kp;
|
|
|
|
ki = Ki * SampleTimeInSec;
|
|
|
|
kd = Kd / SampleTimeInSec;
|
2021-05-28 20:53:35 +02:00
|
|
|
|
2020-02-17 19:14:48 +01:00
|
|
|
if(controllerDirection ==REVERSE)
|
|
|
|
{
|
|
|
|
kp = (0 - kp);
|
|
|
|
ki = (0 - ki);
|
|
|
|
kd = (0 - kd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SetSampleTime(...) *********************************************************
|
|
|
|
* sets the period, in Milliseconds, at which the calculation is performed
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetSampleTime(int NewSampleTime)
|
|
|
|
{
|
|
|
|
if (NewSampleTime > 0)
|
|
|
|
{
|
|
|
|
double ratio = (double)NewSampleTime
|
|
|
|
/ (double)SampleTime;
|
|
|
|
ki *= ratio;
|
|
|
|
kd /= ratio;
|
|
|
|
SampleTime = (unsigned long)NewSampleTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SetOutputLimits(...)****************************************************
|
|
|
|
* This function will be used far more often than SetInputLimits. while
|
|
|
|
* the input to the controller will generally be in the 0-1023 range (which is
|
|
|
|
* the default already,) the output will be a little different. maybe they'll
|
|
|
|
* be doing a time window and will need 0-8000 or something. or maybe they'll
|
|
|
|
* want to clamp it from 0-125. who knows. at any rate, that can all be done
|
|
|
|
* here.
|
|
|
|
**************************************************************************/
|
|
|
|
void PID::SetOutputLimits(double Min, double Max)
|
|
|
|
{
|
2021-05-28 20:53:35 +02:00
|
|
|
if(Min > Max) return;
|
2020-02-17 19:14:48 +01:00
|
|
|
outMin = Min;
|
|
|
|
outMax = Max;
|
2021-05-28 20:53:35 +02:00
|
|
|
|
|
|
|
if(*Output > outMax) *Output = outMax;
|
|
|
|
else if(*Output < outMin) *Output = outMin;
|
|
|
|
|
|
|
|
if(ITerm > outMax) ITerm= outMax;
|
|
|
|
else if(ITerm < outMin) ITerm= outMin;
|
2020-02-17 19:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SetMode(...)****************************************************************
|
|
|
|
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
|
|
|
|
* when the transition from manual to auto occurs, the controller is
|
|
|
|
* automatically initialized
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetMode(int Mode)
|
|
|
|
{
|
|
|
|
bool newAuto = (Mode == AUTOMATIC);
|
2021-05-28 20:53:35 +02:00
|
|
|
if(newAuto == !inAuto)
|
2020-02-17 19:14:48 +01:00
|
|
|
{ /*we just went from manual to auto*/
|
2021-05-28 20:53:35 +02:00
|
|
|
Initialize();
|
2020-02-17 19:14:48 +01:00
|
|
|
}
|
|
|
|
inAuto = newAuto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize()****************************************************************
|
|
|
|
* does all the things that need to happen to ensure a bumpless transfer
|
|
|
|
* from manual to automatic mode.
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::Initialize()
|
|
|
|
{
|
2021-05-28 20:53:35 +02:00
|
|
|
lastInput = *Input;
|
|
|
|
ITerm = *Output;
|
|
|
|
if(ITerm > outMax) ITerm= outMax;
|
|
|
|
else if(ITerm < outMin) ITerm= outMin;
|
2020-02-17 19:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SetControllerDirection(...)*************************************************
|
|
|
|
* The PID will either be connected to a DIRECT acting process (+Output leads
|
|
|
|
* to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to
|
|
|
|
* know which one, because otherwise we may increase the output when we should
|
|
|
|
* be decreasing. This is called from the constructor.
|
|
|
|
******************************************************************************/
|
|
|
|
void PID::SetControllerDirection(int Direction)
|
|
|
|
{
|
|
|
|
controllerDirection = Direction;
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:53:35 +02:00
|
|
|
void PID::setAngleWrap(bool val){
|
|
|
|
angleWrap = val;
|
|
|
|
}
|
|
|
|
|
2020-02-17 19:14:48 +01:00
|
|
|
/* 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
|
|
|
|
* purposes. this are the functions the PID Front-end uses for example
|
|
|
|
******************************************************************************/
|
|
|
|
double PID::GetKp(){ return dispKp; }
|
|
|
|
double PID::GetKi(){ return dispKi;}
|
|
|
|
double PID::GetKd(){ return dispKd;}
|
|
|
|
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
|
|
|
|
int PID::GetDirection(){ return controllerDirection;}
|