Initial changes, merging PID and Autotune.
This commit is contained in:
parent
7787498eda
commit
b3255d94d4
@ -1,196 +0,0 @@
|
|||||||
#if ARDUINO >= 100
|
|
||||||
#include "Arduino.h"
|
|
||||||
#else
|
|
||||||
#include "WProgram.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "PID_AutoTune_v0.h"
|
|
||||||
|
|
||||||
|
|
||||||
PID_ATune::PID_ATune(double* Input, double* Output)
|
|
||||||
{
|
|
||||||
input = Input;
|
|
||||||
output = Output;
|
|
||||||
controlType =0 ; //default to PI
|
|
||||||
noiseBand = 0.5;
|
|
||||||
running = false;
|
|
||||||
oStep = 30;
|
|
||||||
SetLookbackSec(10);
|
|
||||||
lastTime = millis();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void PID_ATune::Cancel()
|
|
||||||
{
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int PID_ATune::Runtime()
|
|
||||||
{
|
|
||||||
//~ justevaled=false;
|
|
||||||
if(peakCount>9 && running)
|
|
||||||
{
|
|
||||||
running = false;
|
|
||||||
FinishUp();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
unsigned long now = millis();
|
|
||||||
|
|
||||||
if((now-lastTime)<sampleTime) return false;
|
|
||||||
lastTime = now;
|
|
||||||
double refVal = *input;
|
|
||||||
//~ justevaled=true;
|
|
||||||
if(!running)
|
|
||||||
{ //initialize working variables the first time around
|
|
||||||
peakType = 0;
|
|
||||||
peakCount=0;
|
|
||||||
atune_peak_change=false;
|
|
||||||
absMax=refVal;
|
|
||||||
absMin=refVal;
|
|
||||||
setpoint = refVal;
|
|
||||||
running = true;
|
|
||||||
outputStart = *output;
|
|
||||||
*output = outputStart+oStep;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(refVal>absMax)absMax=refVal;
|
|
||||||
if(refVal<absMin)absMin=refVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
//oscillate the output base on the input's relation to the setpoint
|
|
||||||
|
|
||||||
if(refVal>setpoint+noiseBand) *output = outputStart-oStep;
|
|
||||||
else if (refVal<setpoint-noiseBand) *output = outputStart+oStep;
|
|
||||||
|
|
||||||
|
|
||||||
//bool isMax=true, isMin=true;
|
|
||||||
isMax=true;isMin=true;
|
|
||||||
//id peaks
|
|
||||||
for(int i=nLookBack-1;i>=0;i--)
|
|
||||||
{
|
|
||||||
double val = lastInputs[i];
|
|
||||||
if(isMax) isMax = refVal>val;
|
|
||||||
if(isMin) isMin = refVal<val;
|
|
||||||
lastInputs[i+1] = lastInputs[i];
|
|
||||||
}
|
|
||||||
lastInputs[0] = refVal;
|
|
||||||
if(nLookBack<9)
|
|
||||||
{ //we don't want to trust the maxes or mins until the inputs array has been filled
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isMax)
|
|
||||||
{
|
|
||||||
if(peakType==0)peakType=1;
|
|
||||||
if(peakType==-1)
|
|
||||||
{
|
|
||||||
peakType = 1;
|
|
||||||
atune_peak_change=true;
|
|
||||||
peak2 = peak1;
|
|
||||||
}
|
|
||||||
peak1 = now;
|
|
||||||
peaks[peakCount] = refVal;
|
|
||||||
|
|
||||||
}
|
|
||||||
else if(isMin)
|
|
||||||
{
|
|
||||||
if(peakType==0)peakType=-1;
|
|
||||||
if(peakType==1)
|
|
||||||
{
|
|
||||||
peakType=-1;
|
|
||||||
peakCount++;
|
|
||||||
atune_peak_change=true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(peakCount<10)peaks[peakCount] = refVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(atune_peak_change && peakCount>2)
|
|
||||||
{ //we've transitioned. check if we can autotune based on the last peaks
|
|
||||||
double avgSeparation = (abs(peaks[peakCount-1]-peaks[peakCount-2])+abs(peaks[peakCount-2]-peaks[peakCount-3]))/2;
|
|
||||||
if( avgSeparation < 0.05*(absMax-absMin))
|
|
||||||
{
|
|
||||||
FinishUp();
|
|
||||||
running = false;
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
atune_peak_change=false;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
void PID_ATune::FinishUp()
|
|
||||||
{
|
|
||||||
*output = outputStart;
|
|
||||||
//we can generate tuning parameters!
|
|
||||||
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
|
||||||
Pu = (double)(peak1-peak2) / 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID_ATune::GetKp()
|
|
||||||
{
|
|
||||||
return controlType==1 ? 0.6 * Ku : 0.4 * Ku;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID_ATune::GetKi()
|
|
||||||
{
|
|
||||||
return controlType==1? 1.2*Ku / Pu : 0.48 * Ku / Pu; // Ki = Kc/Ti
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID_ATune::GetKd()
|
|
||||||
{
|
|
||||||
return controlType==1? 0.075 * Ku * Pu : 0; //Kd = Kc * Td
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID_ATune::SetOutputStep(double Step)
|
|
||||||
{
|
|
||||||
oStep = Step;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID_ATune::GetOutputStep()
|
|
||||||
{
|
|
||||||
return oStep;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID_ATune::SetControlType(int Type) //0=PI, 1=PID
|
|
||||||
{
|
|
||||||
controlType = Type;
|
|
||||||
}
|
|
||||||
int PID_ATune::GetControlType()
|
|
||||||
{
|
|
||||||
return controlType;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID_ATune::SetNoiseBand(double Band)
|
|
||||||
{
|
|
||||||
noiseBand = Band;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID_ATune::GetNoiseBand()
|
|
||||||
{
|
|
||||||
return noiseBand;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID_ATune::SetLookbackSec(int value)
|
|
||||||
{
|
|
||||||
if (value<1) value = 1;
|
|
||||||
|
|
||||||
if(value<25)
|
|
||||||
{
|
|
||||||
nLookBack = value * 4;
|
|
||||||
sampleTime = 250;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nLookBack = 100;
|
|
||||||
sampleTime = value*10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int PID_ATune::GetLookbackSec()
|
|
||||||
{
|
|
||||||
return nLookBack * sampleTime / 1000;
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
#ifndef PID_AutoTune_v0
|
|
||||||
#define PID_AutoTune_v0
|
|
||||||
#define LIBRARY_VERSION 0.0.1
|
|
||||||
|
|
||||||
class PID_ATune
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
|
||||||
//commonly used functions **************************************************************************
|
|
||||||
PID_ATune(double*, double*); // * Constructor. links the Autotune to a given PID
|
|
||||||
int Runtime(); // * Similar to the PID Compue function, returns non 0 when done
|
|
||||||
void Cancel(); // * Stops the AutoTune
|
|
||||||
|
|
||||||
void SetOutputStep(double); // * how far above and below the starting value will the output step?
|
|
||||||
double GetOutputStep(); //
|
|
||||||
|
|
||||||
void SetControlType(int); // * Determies if the tuning parameters returned will be PI (D=0)
|
|
||||||
int GetControlType(); // or PID. (0=PI, 1=PID)
|
|
||||||
|
|
||||||
void SetLookbackSec(int); // * how far back are we looking to identify peaks
|
|
||||||
int GetLookbackSec(); //
|
|
||||||
|
|
||||||
void SetNoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
|
||||||
double GetNoiseBand(); // this should be acurately set
|
|
||||||
|
|
||||||
double GetKp(); // * once autotune is complete, these functions contain the
|
|
||||||
double GetKi(); // computed tuning parameters.
|
|
||||||
double GetKd(); //
|
|
||||||
|
|
||||||
private:
|
|
||||||
void FinishUp();
|
|
||||||
bool isMax, isMin;
|
|
||||||
double *input, *output;
|
|
||||||
double setpoint;
|
|
||||||
double noiseBand;
|
|
||||||
int controlType;
|
|
||||||
bool running;
|
|
||||||
unsigned long peak1, peak2, lastTime;
|
|
||||||
int sampleTime;
|
|
||||||
int nLookBack;
|
|
||||||
int peakType;
|
|
||||||
double lastInputs[101];
|
|
||||||
double peaks[10];
|
|
||||||
int peakCount;
|
|
||||||
bool atune_peak_change;
|
|
||||||
//~ bool justevaled;
|
|
||||||
double absMax, absMin;
|
|
||||||
double oStep;
|
|
||||||
double outputStart;
|
|
||||||
double Ku, Pu;
|
|
||||||
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
291
PID_v1.cpp
291
PID_v1.cpp
@ -1,66 +1,66 @@
|
|||||||
/**********************************************************************************************
|
/**********************************************************************************************
|
||||||
* Arduino PID Library - Version 1.2.1
|
* Arduino PID Library - Version 1.2.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
|
||||||
**********************************************************************************************/
|
**********************************************************************************************/
|
||||||
|
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#else
|
#else
|
||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#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 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,
|
PID::PID(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;
|
||||||
myInput = Input;
|
myInput = Input;
|
||||||
mySetpoint = Setpoint;
|
mySetpoint = Setpoint;
|
||||||
inAuto = false;
|
inAuto = false;
|
||||||
//Tuner = new PID_ATune(Input, Output);
|
//Tuner = new PID_ATune(Input, Output);
|
||||||
PID::SetOutputLimits(0, 100); //Arduino PWM limits 0-255
|
PID::OutputLimits(0, 100);
|
||||||
|
PID::Direction(ControllerDirection);
|
||||||
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
|
||||||
|
|
||||||
PID::SetControllerDirection(ControllerDirection);
|
|
||||||
PID::SetTunings(Kp, Ki, Kd, POn);
|
PID::SetTunings(Kp, Ki, Kd, POn);
|
||||||
|
|
||||||
|
// Autotune defaults
|
||||||
|
noise_band = 0.5;
|
||||||
|
autotune_running = false;
|
||||||
|
oStep = 30;
|
||||||
|
LookbackSec(10);
|
||||||
|
|
||||||
|
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
||||||
lastTime = millis()-SampleTime;
|
lastTime = millis()-SampleTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Constructor (...)*********************************************************
|
/*Constructor (...)*********************************************************
|
||||||
* To allow backwards compatability for v1.1, or for people that just want
|
* To allow backwards compatability for v1.1, or for people that just want
|
||||||
* 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,
|
PID::PID(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)
|
:PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E, ControllerDirection) {
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Compute() **********************************************************************
|
/* Compute() **********************************************************************
|
||||||
* This, as they say, is where the magic happens. this function should be called
|
* 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
|
* 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,
|
* 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 PID::Compute() {
|
||||||
{
|
|
||||||
if(!inAuto) return false;
|
if(!inAuto) return false;
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
unsigned long timeChange = (now - lastTime);
|
unsigned long timeChange = (now - lastTime);
|
||||||
if(timeChange>=SampleTime)
|
if(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;
|
||||||
@ -95,9 +95,8 @@ bool PID::Compute()
|
|||||||
|
|
||||||
int PID::ComputeTune() {
|
int PID::ComputeTune() {
|
||||||
//~ justevaled=false;
|
//~ justevaled=false;
|
||||||
if(peakCount>9 && running)
|
if(peakCount>9 && autotune_running) {
|
||||||
{
|
autotune_running = false;
|
||||||
running = false;
|
|
||||||
FinishUp();
|
FinishUp();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -108,51 +107,49 @@ int PID::ComputeTune() {
|
|||||||
lastTime = now;
|
lastTime = now;
|
||||||
double refVal = *myInput;
|
double refVal = *myInput;
|
||||||
//~ justevaled=true;
|
//~ justevaled=true;
|
||||||
if(!running)
|
if(!autotune_running) {
|
||||||
{ //initialize working variables the first time around
|
//initialize working variables the first time around
|
||||||
peakType = 0;
|
peakType = 0;
|
||||||
peakCount=0;
|
peakCount=0;
|
||||||
atune_peak_change=false;
|
atune_peak_change=false;
|
||||||
absMax=refVal;
|
absMax=refVal;
|
||||||
absMin=refVal;
|
absMin=refVal;
|
||||||
*mySetpoint = refVal;
|
*mySetpoint = refVal;
|
||||||
running = true;
|
autotune_running = true;
|
||||||
outputStart = *myOutput;
|
outputStart = *myOutput;
|
||||||
*myOutput = outputStart+oStep;
|
*myOutput = outputStart+oStep;
|
||||||
}
|
} else {
|
||||||
else
|
if(refVal>absMax)absMax=refVal;
|
||||||
{
|
if(refVal<absMin)absMin=refVal;
|
||||||
|
} else {
|
||||||
if(refVal>absMax)absMax=refVal;
|
if(refVal>absMax)absMax=refVal;
|
||||||
if(refVal<absMin)absMin=refVal;
|
if(refVal<absMin)absMin=refVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
//oscillate the output base on the input's relation to the setpoint
|
//oscillate the output base on the input's relation to the setpoint
|
||||||
|
|
||||||
if(refVal>*mySetpoint+noiseBand) *myOutput = outputStart-oStep;
|
if(refVal>*mySetpoint+noise_band) *myOutput = outputStart-oStep;
|
||||||
else if (refVal<*mySetpoint-noiseBand) *myOutput = outputStart+oStep;
|
else if (refVal<*mySetpoint-noise_band) *myOutput = outputStart+oStep;
|
||||||
|
|
||||||
|
|
||||||
//bool isMax=true, isMin=true;
|
//bool isMax=true, isMin=true;
|
||||||
isMax=true;isMin=true;
|
isMax=true;isMin=true;
|
||||||
//id peaks
|
//id peaks
|
||||||
for(int i=nLookBack-1;i>=0;i--)
|
for(int i=nLookBack-1;i>=0;i--) {
|
||||||
{
|
|
||||||
double val = lastInputs[i];
|
double val = lastInputs[i];
|
||||||
if(isMax) isMax = refVal>val;
|
if(isMax) isMax = refVal>val;
|
||||||
if(isMin) isMin = refVal<val;
|
if(isMin) isMin = refVal<val;
|
||||||
lastInputs[i+1] = lastInputs[i];
|
lastInputs[i+1] = lastInputs[i];
|
||||||
}
|
}
|
||||||
lastInputs[0] = refVal;
|
lastInputs[0] = refVal;
|
||||||
if(nLookBack<9)
|
if(nLookBack<9){
|
||||||
{ //we don't want to trust the maxes or mins until the inputs array has been filled
|
//we don't want to trust the maxes or mins until the inputs array has been filled
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isMax)
|
if(isMax) {
|
||||||
{
|
|
||||||
if(peakType==0)peakType=1;
|
if(peakType==0)peakType=1;
|
||||||
if(peakType==-1)
|
if(peakType==-1) {
|
||||||
{
|
|
||||||
peakType = 1;
|
peakType = 1;
|
||||||
atune_peak_change=true;
|
atune_peak_change=true;
|
||||||
peak2 = peak1;
|
peak2 = peak1;
|
||||||
@ -160,12 +157,9 @@ int PID::ComputeTune() {
|
|||||||
peak1 = now;
|
peak1 = now;
|
||||||
peaks[peakCount] = refVal;
|
peaks[peakCount] = refVal;
|
||||||
|
|
||||||
}
|
} else if(isMin) {
|
||||||
else if(isMin)
|
|
||||||
{
|
|
||||||
if(peakType==0)peakType=-1;
|
if(peakType==0)peakType=-1;
|
||||||
if(peakType==1)
|
if(peakType==1){
|
||||||
{
|
|
||||||
peakType=-1;
|
peakType=-1;
|
||||||
peakCount++;
|
peakCount++;
|
||||||
atune_peak_change=true;
|
atune_peak_change=true;
|
||||||
@ -174,13 +168,12 @@ int PID::ComputeTune() {
|
|||||||
if(peakCount<10)peaks[peakCount] = refVal;
|
if(peakCount<10)peaks[peakCount] = refVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(atune_peak_change && peakCount>2)
|
if(atune_peak_change && peakCount>2) {
|
||||||
{ //we've transitioned. check if we can autotune based on the last peaks
|
//we've transitioned. check if we can autotune based on the last peaks
|
||||||
double avgSeparation = (abs(peaks[peakCount-1]-peaks[peakCount-2])+abs(peaks[peakCount-2]-peaks[peakCount-3]))/2;
|
double avgSeparation = (abs(peaks[peakCount-1]-peaks[peakCount-2])+abs(peaks[peakCount-2]-peaks[peakCount-3]))/2;
|
||||||
if( avgSeparation < 0.05*(absMax-absMin))
|
if( avgSeparation < 0.05*(absMax-absMin)){
|
||||||
{
|
|
||||||
FinishUp();
|
FinishUp();
|
||||||
running = false;
|
autotune_running = false;
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -189,8 +182,7 @@ int PID::ComputeTune() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PID::FinishUp()
|
void PID::FinishUp(){
|
||||||
{
|
|
||||||
*myOutput = outputStart;
|
*myOutput = outputStart;
|
||||||
//we can generate tuning parameters!
|
//we can generate tuning parameters!
|
||||||
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
||||||
@ -198,26 +190,22 @@ void PID::FinishUp()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* SetTunings(...)*************************************************************
|
/* SetTunings(...)*************************************************************
|
||||||
* This function allows the controller's dynamic performance to be adjusted.
|
* This function allows the controller's dynamic performance to be adjusted.
|
||||||
* 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 PID::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;
|
||||||
|
|
||||||
pOn = POn;
|
pOn = POn;
|
||||||
pOnE = POn == P_ON_E;
|
pOnE = POn == P_ON_E;
|
||||||
|
|
||||||
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
|
||||||
|
|
||||||
double SampleTimeInSec = ((double)SampleTime)/1000;
|
double SampleTimeInSec = ((double)SampleTime)/1000;
|
||||||
kp = Kp;
|
kp = Kp;
|
||||||
ki = Ki * SampleTimeInSec;
|
ki = Ki * SampleTimeInSec;
|
||||||
kd = Kd / SampleTimeInSec;
|
kd = Kd / SampleTimeInSec;
|
||||||
|
|
||||||
if(controllerDirection ==REVERSE)
|
if(controllerDirection ==REVERSE){
|
||||||
{
|
|
||||||
kp = (0 - kp);
|
kp = (0 - kp);
|
||||||
ki = (0 - ki);
|
ki = (0 - ki);
|
||||||
kd = (0 - kd);
|
kd = (0 - kd);
|
||||||
@ -225,19 +213,17 @@ 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 PID::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 PID::SampleTime(int NewSampleTime){
|
||||||
{
|
if (NewSampleTime > 0){
|
||||||
if (NewSampleTime > 0)
|
|
||||||
{
|
|
||||||
double ratio = (double)NewSampleTime
|
double ratio = (double)NewSampleTime
|
||||||
/ (double)SampleTime;
|
/ (double)SampleTime;
|
||||||
ki *= ratio;
|
ki *= ratio;
|
||||||
@ -246,22 +232,20 @@ void PID::SetSampleTime(int NewSampleTime)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SetOutputLimits(...)****************************************************
|
/* OutputLimits(...)*******************************************************
|
||||||
* This function will be used far more often than SetInputLimits. while
|
* 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 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
|
* 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
|
* 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
|
* 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 PID::OutputLimits(double Min, double Max){
|
||||||
{
|
|
||||||
if(Min >= Max) return;
|
if(Min >= Max) return;
|
||||||
outMin = Min;
|
outMin = Min;
|
||||||
outMax = Max;
|
outMax = Max;
|
||||||
|
|
||||||
if(inAuto)
|
if(inAuto){
|
||||||
{
|
|
||||||
if(*myOutput > outMax) *myOutput = outMax;
|
if(*myOutput > outMax) *myOutput = outMax;
|
||||||
else if(*myOutput < outMin) *myOutput = outMin;
|
else if(*myOutput < outMin) *myOutput = outMin;
|
||||||
|
|
||||||
@ -270,16 +254,14 @@ void PID::SetOutputLimits(double Min, double Max)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SetMode(...)****************************************************************
|
/* Mode(...)*******************************************************************
|
||||||
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
|
* 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
|
* when the transition from manual to auto occurs, the controller is
|
||||||
* automatically initialized
|
* automatically initialized
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
void PID::Mode(int Mode)
|
void PID::Mode(int Mode){
|
||||||
{
|
|
||||||
bool newAuto = (Mode == 1);
|
bool newAuto = (Mode == 1);
|
||||||
if(newAuto && !inAuto)
|
if(newAuto && !inAuto){ /*we just went from manual to auto*/
|
||||||
{ /*we just went from manual to auto*/
|
|
||||||
PID::Initialize();
|
PID::Initialize();
|
||||||
}
|
}
|
||||||
inAuto = newAuto;
|
inAuto = newAuto;
|
||||||
@ -297,27 +279,24 @@ int PID::CycleMode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize()****************************************************************
|
/* Initialize()****************************************************************
|
||||||
* 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 PID::Initialize(){
|
||||||
{
|
|
||||||
outputSum = *myOutput;
|
outputSum = *myOutput;
|
||||||
lastInput = *myInput;
|
lastInput = *myInput;
|
||||||
if(outputSum > outMax) outputSum = outMax;
|
if(outputSum > outMax) outputSum = outMax;
|
||||||
else if(outputSum < outMin) outputSum = outMin;
|
else if(outputSum < outMin) outputSum = outMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SetControllerDirection(...)*************************************************
|
/* Direction(...)**************************************************************
|
||||||
* The PID will either be connected to a DIRECT acting process (+Output leads
|
* 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
|
* 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
|
* 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 PID::Direction(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);
|
||||||
@ -326,72 +305,34 @@ void PID::SetControllerDirection(int Direction)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 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
|
||||||
* 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 PID::Kp(){ return Kp; }
|
||||||
double PID::GetKi(){ return dispKi;}
|
double PID::Ki(){ return Ki;}
|
||||||
double PID::GetKd(){ return dispKd;}
|
double PID::Kd(){ return Kd;}
|
||||||
int PID::Mode(){ return OpMode;}
|
int PID::Mode(){ return OpMode;}
|
||||||
int PID::GetDirection(){ return controllerDirection;}
|
int PID::Direction(){ return controllerDirection;}
|
||||||
void PID::Cancel()
|
void PID::Cancel(){ autotune_running = false;}
|
||||||
{
|
double PID::TunedKp(){ return 0.6 * Ku;}
|
||||||
running = false;
|
double PID::TunedKi(){ return 1.2*Ku / Pu ; // Ki = Kc/Ti}
|
||||||
}
|
double PID::TunedKd(){ return 0.075 * Ku * Pu; //Kd = Kc * Td}
|
||||||
|
void PID::SetOutputStep(double Step){ oStep = Step;}
|
||||||
|
double PID::GetOutputStep(){ return oStep;}
|
||||||
|
void PID::SetNoiseBand(double Band){ noise_band = Band;}
|
||||||
|
double PID::GetNoiseBand(){ return noise_band;}
|
||||||
|
|
||||||
double PID::TunedKp()
|
void PID::LookbackSec(int value){
|
||||||
{
|
|
||||||
return 0.6 * Ku;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID::TunedKi()
|
|
||||||
{
|
|
||||||
return 1.2*Ku / Pu ; // Ki = Kc/Ti
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID::TunedKd()
|
|
||||||
{
|
|
||||||
return 0.075 * Ku * Pu; //Kd = Kc * Td
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID::SetOutputStep(double Step)
|
|
||||||
{
|
|
||||||
oStep = Step;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID::GetOutputStep()
|
|
||||||
{
|
|
||||||
return oStep;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID::SetNoiseBand(double Band)
|
|
||||||
{
|
|
||||||
noiseBand = Band;
|
|
||||||
}
|
|
||||||
|
|
||||||
double PID::GetNoiseBand()
|
|
||||||
{
|
|
||||||
return noiseBand;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PID::SetLookbackSec(int value)
|
|
||||||
{
|
|
||||||
if (value<1) value = 1;
|
if (value<1) value = 1;
|
||||||
|
|
||||||
if(value<25)
|
if(value<25) {
|
||||||
{
|
|
||||||
nLookBack = value * 4;
|
nLookBack = value * 4;
|
||||||
sampleTime = 250;
|
sampleTime = 250;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
nLookBack = 100;
|
nLookBack = 100;
|
||||||
sampleTime = value*10;
|
sampleTime = value*10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int PID::GetLookbackSec()
|
int PID::LookbackSec(){ return nLookBack * sampleTime / 1000;}
|
||||||
{
|
|
||||||
return nLookBack * sampleTime / 1000;
|
|
||||||
}
|
|
||||||
|
33
PID_v1.h
33
PID_v1.h
@ -2,6 +2,7 @@
|
|||||||
#define PID_v1_h
|
#define PID_v1_h
|
||||||
#define LIBRARY_VERSION 1.2.1
|
#define LIBRARY_VERSION 1.2.1
|
||||||
|
|
||||||
|
enum modes : uint8_t {OFF, AUTOMATIC, MANUAL, OVERFLOW};
|
||||||
|
|
||||||
class PID
|
class PID
|
||||||
{
|
{
|
||||||
@ -28,7 +29,7 @@ class PID
|
|||||||
// calculation frequency can be set using SetMode
|
// calculation frequency can be set using SetMode
|
||||||
// SetSampleTime respectively
|
// SetSampleTime respectively
|
||||||
int ComputeTune();
|
int ComputeTune();
|
||||||
void SetOutputLimits(double, double); // * clamps the output to a specific range. 0-255 by default, but
|
void OutputLimits(double, double); // * clamps the output to a specific range. 0-255 by default, but
|
||||||
// it's likely the user will want to change this depending on
|
// it's likely the user will want to change this depending on
|
||||||
// the application
|
// the application
|
||||||
|
|
||||||
@ -41,21 +42,19 @@ class PID
|
|||||||
void SetTunings(double, double, // * overload for specifying proportional mode
|
void SetTunings(double, double, // * overload for specifying proportional mode
|
||||||
double, int);
|
double, int);
|
||||||
|
|
||||||
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
|
void Direction(int); // * Sets the Direction, or "Action" of the controller. DIRECT
|
||||||
// means the output will increase when error is positive. REVERSE
|
// means the output will increase when error is positive. REVERSE
|
||||||
// means the opposite. it's very unlikely that this will be needed
|
// means the opposite. it's very unlikely that this will be needed
|
||||||
// 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 SampleTime(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 AutoTune();
|
|
||||||
|
|
||||||
|
|
||||||
//Display functions ****************************************************************
|
//Display functions ****************************************************************
|
||||||
double GetKp(); // These functions query the pid for interal values.
|
double Kp(); // These functions query the pid for interal values.
|
||||||
double GetKi(); // they were created mainly for the pid front-end,
|
double Ki(); // they were created mainly for the pid front-end,
|
||||||
double GetKd(); // where it's important to know what is actually
|
double Kd(); // where it's important to know what is actually
|
||||||
int Mode(); // inside the PID.
|
modes Mode(); // inside the PID.
|
||||||
int GetDirection(); //
|
int Direction(); //
|
||||||
|
|
||||||
// Auto Tune Public
|
// Auto Tune Public
|
||||||
void Cancel(); // * Stops the AutoTune
|
void Cancel(); // * Stops the AutoTune
|
||||||
@ -63,8 +62,8 @@ class PID
|
|||||||
void SetOutputStep(double); // * how far above and below the starting value will the output step?
|
void SetOutputStep(double); // * how far above and below the starting value will the output step?
|
||||||
double GetOutputStep(); //
|
double GetOutputStep(); //
|
||||||
|
|
||||||
void SetLookbackSec(int); // * how far back are we looking to identify peaks
|
void LookbackSec(int); // * how far back are we looking to identify peaks
|
||||||
int GetLookbackSec(); //
|
int LookbackSec(); //
|
||||||
|
|
||||||
void SetNoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
void SetNoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
||||||
double GetNoiseBand(); // this should be acurately set
|
double GetNoiseBand(); // this should be acurately set
|
||||||
@ -76,10 +75,6 @@ class PID
|
|||||||
private:
|
private:
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
|
||||||
double dispKp; // * we'll hold on to the tuning parameters in user-entered
|
|
||||||
double dispKi; // format for display purposes
|
|
||||||
double dispKd; //
|
|
||||||
|
|
||||||
double kp; // * (P)roportional Tuning Parameter
|
double kp; // * (P)roportional Tuning Parameter
|
||||||
double ki; // * (I)ntegral Tuning Parameter
|
double ki; // * (I)ntegral Tuning Parameter
|
||||||
double kd; // * (D)erivative Tuning Parameter
|
double kd; // * (D)erivative Tuning Parameter
|
||||||
@ -98,13 +93,13 @@ class PID
|
|||||||
unsigned long SampleTime;
|
unsigned long SampleTime;
|
||||||
double outMin, outMax;
|
double outMin, outMax;
|
||||||
bool inAuto, pOnE;
|
bool inAuto, pOnE;
|
||||||
int OpMode;
|
modes OpMode;
|
||||||
|
|
||||||
// Autotune stuff
|
// Autotune stuff
|
||||||
void FinishUp();
|
void FinishUp();
|
||||||
bool isMax, isMin;
|
bool isMax, isMin;
|
||||||
double noiseBand;
|
double noise_band;
|
||||||
bool running;
|
bool autotune_running;
|
||||||
unsigned long peak1, peak2;
|
unsigned long peak1, peak2;
|
||||||
int sampleTime;
|
int sampleTime;
|
||||||
int nLookBack;
|
int nLookBack;
|
||||||
|
Loading…
Reference in New Issue
Block a user