8000 Changes to PID library to allow it to be configured to not be time ba… · mikebroome/Arduino-PID-Library@c4f3f31 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4f3f31

Browse files
author
Mike Broome
committed
Changes to PID library to allow it to be configured to not be time based.
- Allow SampleTime to be set to 0 to force Compute() to run every time it's called. In this case, the PID algorithm is not time based, and the user should not call it every time through loop(), but rather only based on some event (e.g. timer, button press, external input). - Add GetITerm() and GetLastInput() functions to return the current ITerm and LastInput, respectively. Being able to get and store these values allows the user of the PID library to reinitialize the PID library and continue using the same PID curve and calculation across resets. (Pass the saved ITerm as output and lastInput as input to the PID constructor to reinitialize and pick up where you left off.)
1 parent d21d7e3 commit c4f3f31

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

PID_v1/PID_v1.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**********************************************************************************************
2-
* Arduino PID Library - Version 1.0.1
2+
* Arduino PID Library - Version 1.0.2
33
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
4+
* modified by Mike Broome - http://github.com/mikebroome/
45
*
56
* This Library is licensed under a GPLv3 License
67
**********************************************************************************************/
@@ -43,6 +44,11 @@ PID::PID(double* Input, double* Output, double* Setpoint,
4344
* every time "void loop()" executes. the function will decide for itself whether a new
4445
* pid Output needs to be computed. returns true when the output is computed,
4546
* false when nothing has been done.
47+
*
48+
* Alternatively, if SampleTime is set to 0, Compute() will run every time it is
49+
* called. In this case, it would not be time based, and the user should not call
50+
* it every time through loop(), but rather only based on some event (e.g. timer,
51+
* button press, external input).
4652
**********************************************************************************/
4753
bool PID::Compute()
4854
{
@@ -87,6 +93,10 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
8793
dispKp = Kp; dispKi = Ki; dispKd = Kd;
8894

8995
double SampleTimeInSec = ((double)SampleTime)/1000;
96+
if (SampleTime == 0) {
97+
SampleTimeInSec = 1;
98+
}
99+
90100
kp = Kp;
91101
ki = Ki * SampleTimeInSec;
92102
kd = Kd / SampleTimeInSec;
@@ -101,13 +111,19 @@ void PID::SetTunings(double Kp, double Ki, double Kd)
101111

102112
/* SetSampleTime(...) *********************************************************
103113
* sets the period, in Milliseconds, at which the calculation is performed
114+
*
115+
* Allow SampleTime to be set to 0 to force Compute() to run every time it's
116+
* called.
104117
******************************************************************************/
105118
void PID::SetSampleTime(int NewSampleTime)
106119
{
107-
if (NewSampleTime > 0)
120+
if (NewSampleTime >= 0)
108121
{
109-
double ratio = (double)NewSampleTime
110-
/ (double)SampleTime;
122+
double ratio = 1;
123+
if ((SampleTime != 0) && (NewSampleTime != 0)) {
124+
ratio = (double)NewSampleTime
125+
/ (double)SampleTime;
126+
}
111127
ki *= ratio;
112128
kd /= ratio;
113129
SampleTime = (unsigned long)NewSampleTime;
@@ -192,4 +208,5 @@ double PID::GetKi(){ return dispKi;}
192208
double PID::GetKd(){ return dispKd;}
193209
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
194210
int PID::GetDirection(){ return controllerDirection;}
195-
211+
double PID::GetITerm(){ return ITerm;}
212+
double PID::GetLastInput(){ return lastInput;}

PID_v1/PID_v1.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef PID_v1_h
22
#define PID_v1_h
3-
#define LIBRARY_VERSION 1.0.0
3+
#define LIBRARY_VERSION 1.0.2
44

55
class PID
66
{
@@ -50,6 +50,8 @@ class PID
5050
double GetKd(); // where it's important to know what is actually
5151
int GetMode(); // inside the PID.
5252
int GetDirection(); //
53+
double GetITerm(); //
54+
double GetLastInput(); //
5355

5456
private:
5557
void Initialize();

PID_v1/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ GetKi KEYWORD2
2323
GetKd KEYWORD2
2424
GetMode KEYWORD2
2525
GetDirection KEYWORD2
26+
GetITerm KEYWORD2
27+
GetLastInput KEYWORD2
2628

2729
#######################################
2830
# Constants (LITERAL1)

0 commit comments

Comments
 (0)
0