Version 1.2

-Added capability for Proportional On Measurement
-Changed license from GPLv3 to MIT
This commit is contained in:
Brett 2017-06-19 16:29:36 -07:00
parent 5adeed52b0
commit 5cefbae8e5
6 changed files with 135 additions and 58 deletions

View File

@ -18,9 +18,8 @@
* reliable defaults, so we need to have the user set them.
***************************************************************************/
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;
mySetpoint = Setpoint;
@ -32,11 +31,23 @@ PID::PID(double* Input, double* Output, double* Setpoint,
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
PID::SetControllerDirection(ControllerDirection);
PID::SetTunings(Kp, Ki, Kd);
PID::SetTunings(Kp, Ki, Kd, POn);
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() **********************************************************************
* This, as they say, is where the magic happens. this function should be called
@ -54,13 +65,22 @@ bool PID::Compute()
/*Compute all the working error variables*/
double input = *myInput;
double error = *mySetpoint - input;
ITerm+= (ki * error);
if(ITerm > outMax) ITerm= outMax;
else if(ITerm < outMin) ITerm= outMin;
double dInput = (input - lastInput);
outputSum+= (ki * error);
/*Compute PID Output*/
double output = kp * error + ITerm- kd * dInput;
/*Add Proportional on Measurement, if P_ON_M is specified*/
if(!pOnE) outputSum-= kp * dInput;
if(outputSum > outMax) outputSum= outMax;
else if(outputSum < outMin) outputSum= outMin;
/*Add Proportional on Error, if P_ON_E is specified*/
double output;
if(pOnE) output = kp * error;
else output = 0;
/*Compute Rest of PID Output*/
output += outputSum - kd * dInput;
if(output > outMax) output = outMax;
else if(output < outMin) output = outMin;
@ -74,16 +94,18 @@ bool PID::Compute()
else return false;
}
/* 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
******************************************************************************/
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;
pOn = POn;
pOnE = POn == P_ON_E;
dispKp = Kp; dispKi = Ki; dispKd = Kd;
double SampleTimeInSec = ((double)SampleTime)/1000;
@ -99,6 +121,13 @@ void PID::SetTunings(double Kp, double Ki, double 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(...) *********************************************************
* sets the period, in Milliseconds, at which the calculation is performed
******************************************************************************/
@ -133,8 +162,8 @@ void PID::SetOutputLimits(double Min, double Max)
if(*myOutput > outMax) *myOutput = outMax;
else if(*myOutput < outMin) *myOutput = outMin;
if(ITerm > outMax) ITerm= outMax;
else if(ITerm < outMin) ITerm= outMin;
if(outputSum > outMax) outputSum= outMax;
else if(outputSum < outMin) outputSum= outMin;
}
}
@ -159,10 +188,10 @@ void PID::SetMode(int Mode)
******************************************************************************/
void PID::Initialize()
{
ITerm = *myOutput;
outputSum = *myOutput;
lastInput = *myInput;
if(ITerm > outMax) ITerm = outMax;
else if(ITerm < outMin) ITerm = outMin;
if(outputSum > outMax) outputSum = outMax;
else if(outputSum < outMin) outputSum = outMin;
}
/* SetControllerDirection(...)*************************************************

View File

@ -13,8 +13,14 @@ class PID
#define MANUAL 0
#define DIRECT 0
#define REVERSE 1
#define P_ON_M 0
#define P_ON_E 1
//commonly used functions **************************************************************************
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
double, double, double, int, int);// Setpoint. Initial tuning parameters are also set here.
// (overload for specifying proportional mode)
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
@ -25,7 +31,7 @@ class PID
// calculation frequency can be set using SetMode
// SetSampleTime respectively
void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
void SetOutputLimits(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
// the application
@ -35,6 +41,9 @@ class PID
void SetTunings(double, double, // * While most users will set the tunings once in the
double); // constructor, this function gives the user the option
// of changing tunings during runtime for Adaptive control
void SetTunings(double, double, // * overload for specifying proportional mode
double, int);
void SetControllerDirection(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
@ -63,6 +72,7 @@ class PID
double kd; // * (D)erivative Tuning Parameter
int controllerDirection;
int pOn;
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
double *myOutput; // This creates a hard link between the variables and the
@ -70,11 +80,11 @@ class PID
// what these values are. with pointers we'll just know.
unsigned long lastTime;
double ITerm, lastInput;
double outputSum, lastInput;
unsigned long SampleTime;
double outMin, outMax;
bool inAuto;
bool inAuto, pOnE;
};
#endif

View File

@ -1,8 +1,8 @@
***************************************************************
* Arduino PID Library - Version 1.1.1
* Arduino PID Library - Version 1.2.0
* 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:

View 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);
}

View File

@ -32,3 +32,5 @@ AUTOMATIC LITERAL1
MANUAL LITERAL1
DIRECT LITERAL1
REVERSE LITERAL1
P_ON_E LITERAL1
P_ON_M LITERAL1

View File

@ -1,5 +1,5 @@
name=PID
version=1.1.1
version=1.2.0
author=Brett Beauregard
maintainer=Brett Beauregard
sentence=PID controller