Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2dde69d575 | ||
|
d565cded53 | ||
|
b3255d94d4 | ||
|
7787498eda | ||
|
9b4ca0e5b6 | ||
|
691f1d7af8 | ||
|
5cefbae8e5 | ||
|
5adeed52b0 | ||
|
21b19e7ab0 | ||
|
fb095d8cfc | ||
|
d7c61d2556 |
470
PID_v1.cpp
470
PID_v1.cpp
@ -1,195 +1,335 @@
|
|||||||
/**********************************************************************************************
|
/**********************************************************************************************
|
||||||
* Arduino PID Library - Version 1.1.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 a GPLv3 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 ControllerDirection)
|
double Kp, double Ki, double Kd, int POn, int ControllerDirection) {
|
||||||
{
|
myOutput = Output;
|
||||||
|
myInput = Input;
|
||||||
myOutput = Output;
|
mySetpoint = Setpoint;
|
||||||
myInput = Input;
|
inAuto = false;
|
||||||
mySetpoint = Setpoint;
|
//Tuner = new PID_ATune(Input, Output);
|
||||||
inAuto = false;
|
PID::OutputLimits(0, 100);
|
||||||
|
PID::Direction(ControllerDirection);
|
||||||
PID::SetOutputLimits(0, 255); //default output limit corresponds to
|
PID::SetTunings(Kp, Ki, Kd, POn);
|
||||||
//the arduino pwm limits
|
|
||||||
|
|
||||||
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
// Autotune defaults
|
||||||
|
noise_band = 0.5;
|
||||||
|
autotune_running = false;
|
||||||
|
oStep = 30;
|
||||||
|
LookbackSec(10);
|
||||||
|
|
||||||
PID::SetControllerDirection(ControllerDirection);
|
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
||||||
PID::SetTunings(Kp, Ki, Kd);
|
lastTime = millis()-SampleTime;
|
||||||
|
|
||||||
lastTime = millis()-SampleTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Constructor (...)*********************************************************
|
||||||
|
* To allow backwards compatability for v1.1, or for people that just want
|
||||||
|
* to use Proportional on Error without explicitly saying so
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
PID::PID(double* Input, double* Output, double* Setpoint,
|
||||||
|
double Kp, double Ki, double Kd, int 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*/
|
||||||
{
|
double input = *myInput;
|
||||||
/*Compute all the working error variables*/
|
double error = *mySetpoint - input;
|
||||||
double input = *myInput;
|
double dInput = (input - lastInput);
|
||||||
double error = *mySetpoint - input;
|
outputSum+= (ki * error);
|
||||||
ITerm+= (ki * error);
|
|
||||||
if(ITerm > outMax) ITerm= outMax;
|
/*Add Proportional on Measurement, if P_ON_M is specified*/
|
||||||
else if(ITerm < outMin) ITerm= outMin;
|
if(!pOnE) outputSum-= kp * dInput;
|
||||||
double dInput = (input - lastInput);
|
|
||||||
|
if(outputSum > outMax) outputSum= outMax;
|
||||||
/*Compute PID Output*/
|
else if(outputSum < outMin) outputSum= outMin;
|
||||||
double output = kp * error + ITerm- kd * dInput;
|
|
||||||
|
/*Add Proportional on Error, if P_ON_E is specified*/
|
||||||
if(output > outMax) output = outMax;
|
double output;
|
||||||
else if(output < outMin) output = outMin;
|
if(pOnE) output = kp * error;
|
||||||
*myOutput = output;
|
else output = 0;
|
||||||
|
|
||||||
/*Remember some variables for next time*/
|
/*Compute Rest of PID Output*/
|
||||||
lastInput = input;
|
output += outputSum - kd * dInput;
|
||||||
lastTime = now;
|
|
||||||
return true;
|
if(output > outMax) output = outMax;
|
||||||
}
|
else if(output < outMin) output = outMin;
|
||||||
else return false;
|
*myOutput = output;
|
||||||
|
|
||||||
|
/*Remember some variables for next time*/
|
||||||
|
lastInput = input;
|
||||||
|
lastTime = now;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int PID::ComputeTune() {
|
||||||
|
//~ justevaled=false;
|
||||||
|
if(peakCount>9 && autotune_running) {
|
||||||
|
autotune_running = false;
|
||||||
|
FinishUp();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
unsigned long now = millis();
|
||||||
|
|
||||||
|
if((now-lastTime)<SampleTime) return false;
|
||||||
|
|
||||||
|
lastTime = now;
|
||||||
|
double refVal = *myInput;
|
||||||
|
//~ justevaled=true;
|
||||||
|
if(!autotune_running) {
|
||||||
|
//initialize working variables the first time around
|
||||||
|
peakType = 0;
|
||||||
|
peakCount=0;
|
||||||
|
atune_peak_change=false;
|
||||||
|
absMax=refVal;
|
||||||
|
absMin=refVal;
|
||||||
|
*mySetpoint = refVal;
|
||||||
|
autotune_running = true;
|
||||||
|
outputStart = *myOutput;
|
||||||
|
*myOutput = 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>*mySetpoint+noise_band) *myOutput = outputStart-oStep;
|
||||||
|
else if (refVal<*mySetpoint-noise_band) *myOutput = 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();
|
||||||
|
autotune_running = false;
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
atune_peak_change=false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PID::FinishUp(){
|
||||||
|
*myOutput = outputStart;
|
||||||
|
//we can generate tuning parameters!
|
||||||
|
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
||||||
|
Pu = (double)(peak1-peak2) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
/* 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)
|
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;
|
||||||
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
pOnE = POn == P_ON_E;
|
||||||
|
|
||||||
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);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* SetTunings(...)*************************************************************
|
||||||
|
* Set Tunings using the last-rembered POn setting
|
||||||
|
******************************************************************************/
|
||||||
|
void PID::SetTunings(double Kp, double Ki, double Kd){
|
||||||
|
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::SetSampleTime(int NewSampleTime){
|
||||||
{
|
if (NewSampleTime > 0){
|
||||||
if (NewSampleTime > 0)
|
double ratio = (double)NewSampleTime
|
||||||
{
|
/ (double)SampleTime;
|
||||||
double ratio = (double)NewSampleTime
|
ki *= ratio;
|
||||||
/ (double)SampleTime;
|
kd /= ratio;
|
||||||
ki *= ratio;
|
SampleTime = (unsigned long)NewSampleTime;
|
||||||
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)
|
|
||||||
{
|
|
||||||
if(Min >= Max) return;
|
|
||||||
outMin = Min;
|
|
||||||
outMax = Max;
|
|
||||||
|
|
||||||
if(inAuto)
|
|
||||||
{
|
|
||||||
if(*myOutput > outMax) *myOutput = outMax;
|
|
||||||
else if(*myOutput < outMin) *myOutput = outMin;
|
|
||||||
|
|
||||||
if(ITerm > outMax) ITerm= outMax;
|
|
||||||
else if(ITerm < outMin) ITerm= outMin;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SetMode(...)****************************************************************
|
/* OutputLimits(...)*******************************************************
|
||||||
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
|
* This function will be used far more often than SetInputLimits. while
|
||||||
* when the transition from manual to auto occurs, the controller is
|
* the input to the controller will generally be in the 0-1023 range (which is
|
||||||
* automatically initialized
|
* 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
|
||||||
void PID::SetMode(int Mode)
|
* want to clamp it from 0-125. who knows. at any rate, that can all be done
|
||||||
{
|
* here.
|
||||||
bool newAuto = (Mode == AUTOMATIC);
|
**************************************************************************/
|
||||||
if(newAuto == !inAuto)
|
void PID::OutputLimits(double Min, double Max){
|
||||||
{ /*we just went from manual to auto*/
|
if(Min >= Max) return;
|
||||||
PID::Initialize();
|
outMin = Min;
|
||||||
}
|
outMax = Max;
|
||||||
inAuto = newAuto;
|
|
||||||
|
if(inAuto){
|
||||||
|
if(*myOutput > outMax) *myOutput = outMax;
|
||||||
|
else if(*myOutput < outMin) *myOutput = outMin;
|
||||||
|
|
||||||
|
if(outputSum > outMax) outputSum= outMax;
|
||||||
|
else if(outputSum < outMin) outputSum= outMin;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Mode(...)*******************************************************************
|
||||||
|
* 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::Mode(int Mode){
|
||||||
|
bool newAuto = (Mode == 1);
|
||||||
|
if(newAuto && !inAuto){ /*we just went from manual to auto*/
|
||||||
|
PID::Initialize();
|
||||||
|
}
|
||||||
|
inAuto = newAuto;
|
||||||
|
OpMode = Mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PID::CycleMode() {
|
||||||
|
if (OpMode + 1 == 3) {
|
||||||
|
PID::Mode(0);
|
||||||
|
} else {
|
||||||
|
PID::Mode(OpMode + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return OpMode;
|
||||||
|
}
|
||||||
|
|
||||||
/* 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;
|
||||||
ITerm = *myOutput;
|
lastInput = *myInput;
|
||||||
lastInput = *myInput;
|
if(outputSum > outMax) outputSum = outMax;
|
||||||
if(ITerm > outMax) ITerm = outMax;
|
else if(outputSum < outMin) outputSum = outMin;
|
||||||
else if(ITerm < outMin) ITerm = 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);
|
||||||
{
|
ki = (0 - ki);
|
||||||
kp = (0 - kp);
|
kd = (0 - kd);
|
||||||
ki = (0 - ki);
|
}
|
||||||
kd = (0 - kd);
|
controllerDirection = Direction;
|
||||||
}
|
|
||||||
controllerDirection = 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::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
|
modes PID::Mode(){ return OpMode;}
|
||||||
int PID::GetDirection(){ return controllerDirection;}
|
int PID::Direction(){ return controllerDirection;}
|
||||||
|
void PID::Cancel(){ autotune_running = false;}
|
||||||
|
double PID::TunedKp(){ 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::OutputStep(double Step){ oStep = Step;}
|
||||||
|
double PID::OutputStep(){ return oStep;}
|
||||||
|
void PID::NoiseBand(double Band){ noise_band = Band;}
|
||||||
|
double PID::NoiseBand(){ return noise_band;}
|
||||||
|
|
||||||
|
void PID::LookbackSec(int value){
|
||||||
|
if (value<1) value = 1;
|
||||||
|
|
||||||
|
if(value<25) {
|
||||||
|
nLookBack = value * 4;
|
||||||
|
SampleTime = 250;
|
||||||
|
} else {
|
||||||
|
nLookBack = 100;
|
||||||
|
SampleTime = value*10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int PID::LookbackSec(){ return nLookBack * SampleTime / 1000;}
|
||||||
|
126
PID_v1.h
126
PID_v1.h
@ -1,80 +1,116 @@
|
|||||||
#ifndef PID_v1_h
|
#ifndef PID_v1_h
|
||||||
#define PID_v1_h
|
#define PID_v1_h
|
||||||
#define LIBRARY_VERSION 1.1.1
|
#define LIBRARY_VERSION 1.2.1
|
||||||
|
|
||||||
|
enum modes : uint8_t {OFF, AUTOMATIC, MANUAL, OVERFLOW};
|
||||||
|
|
||||||
class PID
|
class PID
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//Constants used in some of the functions below
|
//Constants used in some of the functions below
|
||||||
#define AUTOMATIC 1
|
|
||||||
#define MANUAL 0
|
|
||||||
#define DIRECT 0
|
#define DIRECT 0
|
||||||
#define REVERSE 1
|
#define REVERSE 1
|
||||||
|
#define P_ON_M 0
|
||||||
|
#define P_ON_E 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
|
||||||
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
|
double, double, double, int, int);// Setpoint. Initial tuning parameters are also set here.
|
||||||
|
// (overload for specifying proportional mode)
|
||||||
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
|
|
||||||
|
|
||||||
|
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
|
||||||
|
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
|
||||||
|
|
||||||
|
void Mode(int Mode); // * sets PID to either Manual or Auto
|
||||||
|
int CycleMode();
|
||||||
bool Compute(); // * performs the PID calculation. it should be
|
bool Compute(); // * performs the PID calculation. it should be
|
||||||
// called every time loop() cycles. ON/OFF and
|
// called every time loop() cycles. ON/OFF and
|
||||||
// calculation frequency can be set using SetMode
|
// calculation frequency can be set using SetMode
|
||||||
// SetSampleTime respectively
|
// SetSampleTime respectively
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//available but not commonly used functions ********************************************************
|
//available but not commonly used functions ********************************************************
|
||||||
void SetTunings(double, double, // * While most users will set the tunings once in the
|
void SetTunings(double, double, // * While most users will set the tunings once in the
|
||||||
double); // constructor, this function gives the user the option
|
double); // constructor, this function gives the user the option
|
||||||
// of changing tunings during runtime for Adaptive control
|
// of changing tunings during runtime for Adaptive control
|
||||||
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
|
void SetTunings(double, double, // * overload for specifying proportional mode
|
||||||
// means the output will increase when error is positive. REVERSE
|
double, int);
|
||||||
// means the opposite. it's very unlikely that this will be needed
|
|
||||||
// once it is set in the constructor.
|
void Direction(int); // * Sets the Direction, or "Action" of the controller. DIRECT
|
||||||
|
// means the output will increase when error is positive. REVERSE
|
||||||
|
// means the opposite. it's very unlikely that this will be needed
|
||||||
|
// 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//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 GetMode(); // inside the PID.
|
modes Mode(); // inside the PID.
|
||||||
int GetDirection(); //
|
int Direction(); //
|
||||||
|
|
||||||
|
// Auto Tune Public
|
||||||
|
void Cancel(); // * Stops the AutoTune
|
||||||
|
|
||||||
|
void OutputStep(double); // * how far above and below the starting value will the output step?
|
||||||
|
double OutputStep(); //
|
||||||
|
|
||||||
|
void LookbackSec(int); // * how far back are we looking to identify peaks
|
||||||
|
int LookbackSec(); //
|
||||||
|
|
||||||
|
void NoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
||||||
|
double NoiseBand(); // this should be acurately set
|
||||||
|
|
||||||
|
double TunedKp(); // * once autotune is complete, these functions contain the
|
||||||
|
double TunedKi(); // computed tuning parameters.
|
||||||
|
double TunedKd();
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
int controllerDirection;
|
int controllerDirection;
|
||||||
|
int pOn;
|
||||||
|
|
||||||
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
|
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
|
||||||
double *myOutput; // This creates a hard link between the variables and the
|
double *myOutput; // This creates a hard link between the variables and the
|
||||||
double *mySetpoint; // PID, freeing the user from having to constantly tell us
|
double *mySetpoint; // PID, freeing the user from having to constantly tell us
|
||||||
// what these values are. with pointers we'll just know.
|
// what these values are. with pointers we'll just know.
|
||||||
|
|
||||||
unsigned long lastTime;
|
unsigned long lastTime;
|
||||||
double ITerm, lastInput;
|
double outputSum, lastInput;
|
||||||
|
|
||||||
unsigned long SampleTime;
|
double outMin, outMax;
|
||||||
double outMin, outMax;
|
bool inAuto, pOnE;
|
||||||
bool inAuto;
|
modes OpMode;
|
||||||
|
|
||||||
|
// Autotune stuff
|
||||||
|
void FinishUp();
|
||||||
|
bool isMax, isMin;
|
||||||
|
double noise_band;
|
||||||
|
bool autotune_running;
|
||||||
|
unsigned long peak1, peak2;
|
||||||
|
int SampleTime;
|
||||||
|
int nLookBack;
|
||||||
|
int peakType;
|
||||||
|
double lastInputs[101];
|
||||||
|
double peaks[10];
|
||||||
|
int peakCount;
|
||||||
|
bool atune_peak_change;
|
||||||
|
double absMax, absMin;
|
||||||
|
double oStep;
|
||||||
|
double outputStart;
|
||||||
|
double Ku, Pu;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
***************************************************************
|
***************************************************************
|
||||||
* Arduino PID Library - Version 1.1.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 a GPLv3 License
|
* This Library is licensed under the MIT License
|
||||||
***************************************************************
|
***************************************************************
|
||||||
|
|
||||||
- For an ultra-detailed explanation of why the code is the way it is, please visit:
|
- For an ultra-detailed explanation of why the code is the way it is, please visit:
|
||||||
http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/
|
https://web.archive.org/web/20200607033952/http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction
|
||||||
|
|
||||||
- For function documentation see: http://playground.arduino.cc/Code/PIDLibrary
|
- For function documentation see: http://playground.arduino.cc/Code/PIDLibrary
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define PIN_OUTPUT 3
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
@ -24,7 +27,7 @@ PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
|
|||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
//initialize the variables we're linked to
|
//initialize the variables we're linked to
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
Setpoint = 100;
|
Setpoint = 100;
|
||||||
|
|
||||||
//turn the PID on
|
//turn the PID on
|
||||||
@ -33,10 +36,10 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
|
|
||||||
double gap = abs(Setpoint-Input); //distance away from setpoint
|
double gap = abs(Setpoint-Input); //distance away from setpoint
|
||||||
if(gap<10)
|
if (gap < 10)
|
||||||
{ //we're close to setpoint, use conservative tuning parameters
|
{ //we're close to setpoint, use conservative tuning parameters
|
||||||
myPID.SetTunings(consKp, consKi, consKd);
|
myPID.SetTunings(consKp, consKi, consKd);
|
||||||
}
|
}
|
||||||
@ -45,9 +48,9 @@ void loop()
|
|||||||
//we're far from setpoint, use aggressive tuning parameters
|
//we're far from setpoint, use aggressive tuning parameters
|
||||||
myPID.SetTunings(aggKp, aggKi, aggKd);
|
myPID.SetTunings(aggKp, aggKi, aggKd);
|
||||||
}
|
}
|
||||||
|
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
analogWrite(3,Output);
|
analogWrite(PIN_OUTPUT, Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,16 +5,20 @@
|
|||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define PIN_OUTPUT 3
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
//Specify the links and initial tuning parameters
|
//Specify the links and initial tuning parameters
|
||||||
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
|
double Kp=2, Ki=5, Kd=1;
|
||||||
|
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
//initialize the variables we're linked to
|
//initialize the variables we're linked to
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
Setpoint = 100;
|
Setpoint = 100;
|
||||||
|
|
||||||
//turn the PID on
|
//turn the PID on
|
||||||
@ -23,9 +27,9 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
analogWrite(3,Output);
|
analogWrite(PIN_OUTPUT, Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
36
examples/PID_PonM/PID_PonM.ino
Normal file
36
examples/PID_PonM/PID_PonM.ino
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/********************************************************
|
||||||
|
* PID Proportional on measurement Example
|
||||||
|
* Setting the PID to use Proportional on measurement will
|
||||||
|
* make the output move more smoothly when the setpoint
|
||||||
|
* is changed. In addition, it can eliminate overshoot
|
||||||
|
* in certain processes like sous-vides.
|
||||||
|
********************************************************/
|
||||||
|
|
||||||
|
#include <PID_v1.h>
|
||||||
|
|
||||||
|
//Define Variables we'll be connecting to
|
||||||
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
|
//Specify the links and initial tuning parameters
|
||||||
|
PID myPID(&Input, &Output, &Setpoint,2,5,1,P_ON_M, DIRECT); //P_ON_M specifies that Proportional on Measurement be used
|
||||||
|
//P_ON_E (Proportional on Error) is the default behavior
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
//initialize the variables we're linked to
|
||||||
|
Input = analogRead(0);
|
||||||
|
Setpoint = 100;
|
||||||
|
|
||||||
|
//turn the PID on
|
||||||
|
myPID.SetMode(AUTOMATIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
Input = analogRead(0);
|
||||||
|
myPID.Compute();
|
||||||
|
analogWrite(3,Output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -7,28 +7,32 @@
|
|||||||
*
|
*
|
||||||
* to connect them together we use "time proportioning
|
* to connect them together we use "time proportioning
|
||||||
* control" it's essentially a really slow version of PWM.
|
* control" it's essentially a really slow version of PWM.
|
||||||
* first we decide on a window size (5000mS say.) we then
|
* first we decide on a window size (5000mS say.) we then
|
||||||
* set the pid to adjust its output between 0 and that window
|
* set the pid to adjust its output between 0 and that window
|
||||||
* size. lastly, we add some logic that translates the PID
|
* size. lastly, we add some logic that translates the PID
|
||||||
* output into "Relay On Time" with the remainder of the
|
* output into "Relay On Time" with the remainder of the
|
||||||
* window being "Relay Off Time"
|
* window being "Relay Off Time"
|
||||||
********************************************************/
|
********************************************************/
|
||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
#define RelayPin 6
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define RELAY_PIN 6
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
//Specify the links and initial tuning parameters
|
//Specify the links and initial tuning parameters
|
||||||
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
|
double Kp=2, Ki=5, Kd=1;
|
||||||
|
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
|
||||||
|
|
||||||
int WindowSize = 5000;
|
int WindowSize = 5000;
|
||||||
unsigned long windowStartTime;
|
unsigned long windowStartTime;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
windowStartTime = millis();
|
windowStartTime = millis();
|
||||||
|
|
||||||
//initialize the variables we're linked to
|
//initialize the variables we're linked to
|
||||||
Setpoint = 100;
|
Setpoint = 100;
|
||||||
|
|
||||||
@ -41,18 +45,18 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
|
|
||||||
/************************************************
|
/************************************************
|
||||||
* turn the output pin on/off based on pid output
|
* turn the output pin on/off based on pid output
|
||||||
************************************************/
|
************************************************/
|
||||||
if(millis() - windowStartTime>WindowSize)
|
if (millis() - windowStartTime > WindowSize)
|
||||||
{ //time to shift the Relay Window
|
{ //time to shift the Relay Window
|
||||||
windowStartTime += WindowSize;
|
windowStartTime += WindowSize;
|
||||||
}
|
}
|
||||||
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
|
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
|
||||||
else digitalWrite(RelayPin,LOW);
|
else digitalWrite(RELAY_PIN, LOW);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,4 +31,6 @@ GetDirection KEYWORD2
|
|||||||
AUTOMATIC LITERAL1
|
AUTOMATIC LITERAL1
|
||||||
MANUAL LITERAL1
|
MANUAL LITERAL1
|
||||||
DIRECT LITERAL1
|
DIRECT LITERAL1
|
||||||
REVERSE LITERAL1
|
REVERSE LITERAL1
|
||||||
|
P_ON_E LITERAL1
|
||||||
|
P_ON_M LITERAL1
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=PID
|
name=PID
|
||||||
version=1.1.1
|
version=1.2.1
|
||||||
author=Brett Beauregard
|
author=Brett Beauregard
|
||||||
maintainer=Brett Beauregard
|
maintainer=Brett Beauregard
|
||||||
sentence=PID controller
|
sentence=PID controller
|
||||||
|
Loading…
Reference in New Issue
Block a user