Skip to content

Commit 4d1ebee

Browse files
committed
QuickPID 2.0.4
Added QuickPID_AdaptiveTunings.ino, QuickPID_Basic.ino, QuickPID_PonM.ino and QuickPID_RelayOutput.ino to the examples folder. Note thatQuickPID_RelayOutput.ino has the added feature of minWindow setting that sets the minimum on time for the relay.
1 parent eef81ef commit 4d1ebee

File tree

8 files changed

+202
-12
lines changed

8 files changed

+202
-12
lines changed

QuickPID.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************************
2-
QuickPID Library for Arduino - Version 2.0.3
2+
QuickPID Library for Arduino - Version 2.0.4
33
by dlloydev https://github.com/Dlloydev/QuickPID
44
Based on the Arduino PID Library by Brett Beauregard
55
@@ -28,7 +28,6 @@ QuickPID::QuickPID(int16_t* Input, int16_t* Output, int16_t* Setpoint,
2828

2929
QuickPID::SetOutputLimits(0, 255); // default is same as the arduino PWM limit
3030
SampleTimeUs = 100000; // default is 0.1 seconds
31-
3231
QuickPID::SetControllerDirection(ControllerDirection);
3332
QuickPID::SetTunings(Kp, Ki, Kd, POn);
3433

@@ -42,7 +41,7 @@ QuickPID::QuickPID(int16_t* Input, int16_t* Output, int16_t* Setpoint,
4241

4342
QuickPID::QuickPID(int16_t* Input, int16_t* Output, int16_t* Setpoint,
4443
float Kp, float Ki, float Kd, bool ControllerDirection)
45-
: QuickPID::QuickPID(Input, Output, Setpoint, Kp, Ki, Kd, pOn, ControllerDirection)
44+
: QuickPID::QuickPID(Input, Output, Setpoint, Kp, Ki, Kd, POn, ControllerDirection)
4645
{
4746

4847
}
@@ -91,6 +90,7 @@ void QuickPID::SetTunings(float Kp, float Ki, float Kd, float POn)
9190
if (Kp < 0 || Ki < 0 || Kd < 0) return;
9291

9392
pOn = POn;
93+
9494
dispKp = Kp; dispKi = Ki; dispKd = Kd;
9595

9696
float SampleTimeSec = (float)SampleTimeUs / 1000000;
@@ -112,7 +112,7 @@ void QuickPID::SetTunings(float Kp, float Ki, float Kd, float POn)
112112
Set Tunings using the last remembered POn setting.
113113
******************************************************************************/
114114
void QuickPID::SetTunings(float Kp, float Ki, float Kd) {
115-
SetTunings(Kp, Ki, Kd, pOn);
115+
SetTunings(Kp, Ki, Kd, POn);
116116
}
117117

118118
/* SetSampleTime(...) *********************************************************

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Development began with a fork of the Arduino PID Library. Some modifications and
6262
outputSum += (kpi * error) - (kpd * dInput);
6363
```
6464

65-
The new `kpi` and `kpd` parameters are calculated in the `SetTunings()` function. This results in a simple and fast algorithm with only two multiply operations required The POn parameter controls the setpoint weighting of Proportional on Error and Proportional on Measurement. The gains for `error` (`kpi`) and measurement `dInput` (`kpd`) are calculated in the `QuickPID::SetTunings()` function as follows:
65+
The new `kpi` and `kpd` parameters are calculated in the `SetTunings()` function. This results in a simple and fast algorithm with only two multiply operations required The pOn variable controls the setpoint weighting of Proportional on Error and Proportional on Measurement. The gains for `error` (`kpi`) and measurement `dInput` (`kpd`) are calculated as follows:
6666

6767
```c++
6868
kpi = kp * pOn + ki;
@@ -172,11 +172,14 @@ int QuickPID::analogReadFast(int ADCpin)
172172
A faster configuration of `analogRead()`where a preset of 32 is used. Works with the following defines:
173173
174174
`__AVR_ATmega328P__`
175+
175176
`__AVR_ATtiny_Zero_One__`
177+
176178
`__AVR_ATmega_Zero__`
179+
177180
`__AVR_DA__`
178181
179-
Uses Arduino's default settings and returns `analogRead()` if the definition isn't found.
182+
If the definition isn't found, normal `analogRead()`is used to return a value.
180183
181184
### Original README (Arduino PID)
182185
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/********************************************************
2+
PID Adaptive Tuning Example
3+
One of the benefits of the PID library is that you can
4+
change the tuning parameters at any time. this can be
5+
helpful if we want the controller to be agressive at some
6+
times, and conservative at others. in the example below
7+
we set the controller to use Conservative Tuning Parameters
8+
when we're near setpoint and more agressive Tuning
9+
Parameters when we're farther away.
10+
********************************************************/
11+
12+
#include <QuickPID.h>
13+
14+
#define PIN_INPUT 0
15+
#define PIN_OUTPUT 3
16+
17+
//Define Variables we'll be connecting to
18+
int Setpoint, Input, Output;
19+
20+
//Define the aggressive and conservative and POn Tuning Parameters
21+
float aggKp = 4, aggKi = 0.2, aggKd = 1;
22+
float consKp = 1, consKi = 0.05, consKd = 0.25;
23+
float aggPOn = 1.0; // Range is 0.0 to 1.0 (1.0 is 100% P on Error, 0% P on Measurement)
24+
float consPOn = 0.0; // Range is 0.0 to 1.0 (0.0 is 0% P on Error, 100% P on Measurement)
25+
26+
//Specify the links and initial tuning parameters
27+
QuickPID myQuickPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, aggPOn, DIRECT);
28+
29+
void setup()
30+
{
31+
//initialize the variables we're linked to
32+
Input = myQuickPID.analogReadFast(PIN_INPUT);
33+
Setpoint = 100;
34+
35+
//turn the PID on
36+
myQuickPID.SetMode(AUTOMATIC);
37+
}
38+
39+
void loop()
40+
{
41+
Input = myQuickPID.analogReadFast(PIN_INPUT);
42+
43+
float gap = abs(Setpoint - Input); //distance away from setpoint
44+
if (gap < 10) { //we're close to setpoint, use conservative tuning parameters
45+
myQuickPID.SetTunings(consKp, consKi, consKd, consPOn);
46+
} else {
47+
//we're far from setpoint, use aggressive tuning parameters
48+
myQuickPID.SetTunings(aggKp, aggKi, aggKd, aggPOn);
49+
}
50+
myQuickPID.Compute();
51+
analogWrite(PIN_OUTPUT, Output);
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/********************************************************
2+
PID Basic Example
3+
Reading analog input 0 to control analog PWM output 3
4+
********************************************************/
5+
6+
#include <QuickPID.h>
7+
8+
#define PIN_INPUT 0
9+
#define PIN_OUTPUT 3
10+
11+
//Define Variables we'll be connecting to
12+
int Setpoint, Input, Output;
13+
14+
//Specify the links and initial tuning parameters
15+
float Kp = 2, Ki = 5, Kd = 1;
16+
float POn = 1.0; // Range is 0.0 to 1.0 (1.0 is 100% P on Error, 0% P on Measurement)
17+
18+
QuickPID myQuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, DIRECT);
19+
20+
void setup()
21+
{
22+
//initialize the variables we're linked to
23+
Input = myQuickPID.analogReadFast(PIN_INPUT);
24+
Setpoint = 100;
25+
26+
//turn the PID on
27+
myQuickPID.SetMode(AUTOMATIC);
28+
}
29+
30+
void loop()
31+
{
32+
Input = myQuickPID.analogReadFast(PIN_INPUT);
33+
myQuickPID.Compute();
34+
analogWrite(PIN_OUTPUT, Output);
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/********************************************************
2+
PID Proportional on measurement Example
3+
Setting the PID to use Proportional on measurement will
4+
make the output move more smoothly when the setpoint
5+
is changed. In addition, it can eliminate overshoot
6+
in certain processes like sous-vides.
7+
********************************************************/
8+
9+
#include <QuickPID.h>
10+
11+
//Define Variables we'll be connecting to
12+
int Setpoint, Input, Output;
13+
float POn = 0.0; // Range is 0.0 to 1.0 (0.0 is 0% P on Error, 100% P on Measurement)
14+
15+
//Specify the links and initial tuning parameters
16+
QuickPID myQuickPID(&Input, &Output, &Setpoint, 2, 5, 1, POn, DIRECT);
17+
18+
void setup()
19+
{
20+
//initialize the variables we're linked to
21+
Input = myQuickPID.analogReadFast(0);
22+
Setpoint = 100;
23+
24+
//turn the PID on
25+
myQuickPID.SetMode(AUTOMATIC);
26+
}
27+
28+
void loop()
29+
{
30+
Input = myQuickPID.analogReadFast(0);
31+
myQuickPID.Compute();
32+
analogWrite(3, Output);
33+
}

examples/QuickPID_RC_Filter/QuickPID_RC_Filter.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
https://github.com/Dlloydev/QuickPID/wiki/QuickPID_RC_Filter
77
**************************************************************/
88

9-
#include "QuickPID.h"
9+
#include <QuickPID.h>
1010

11-
#define PIN_INPUT A0
11+
#define PIN_INPUT 0
1212
#define PIN_OUTPUT 3
1313

1414
//Define Variables
@@ -20,14 +20,15 @@ unsigned long before, after;
2020
int cnt = 0;
2121

2222
//Specify the initial tuning parameters
23-
float Kp = 2.0, Ki = 15.0, Kd = 0.05, POn = 1.0;
23+
float Kp = 2.0, Ki = 15.0, Kd = 0.05;
24+
float POn = 1.0; // Range is 0.0 to 1.0 (1.0 is 100% P on Error, 0% P on Measurement)
2425

25-
QuickPID myQuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
26+
QuickPID myQuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, DIRECT);
2627

2728
void setup()
2829
{
2930
Serial.begin(115200);
30-
myQuickPID.SetTunings(Kp, Ki, Kd, POn);
31+
myQuickPID.SetTunings(Kp, Ki, Kd);
3132
myQuickPID.SetMode(AUTOMATIC);
3233
analogWrite(PIN_OUTPUT, 0); // discharge capacitor
3334
delay(1000);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/********************************************************
2+
PID RelayOutput Example
3+
Same as basic example, except that this time, the output
4+
is going to a digital pin which (we presume) is controlling
5+
a relay. the pid is designed to Output an analog value,
6+
but the relay can only be On/Off.
7+
8+
to connect them together we use "time proportioning
9+
control" it's essentially a really slow version of PWM.
10+
first we decide on a window size (5000mS say.) we then
11+
set the pid to adjust its output between 0 and that window
12+
size. lastly, we add some logic that translates the PID
13+
output into "Relay On Time" with the remainder of the
14+
window being "Relay Off Time"
15+
The minWindow setting is a floor so that the relay would
16+
be on for a minimum amount of time.
17+
********************************************************/
18+
19+
#include <QuickPID.h>
20+
21+
#define PIN_INPUT 0
22+
#define RELAY_PIN 6
23+
24+
//Define Variables we'll be connecting to
25+
int Setpoint, Input, Output;
26+
27+
//Specify the links and initial tuning parameters
28+
float Kp = 2, Ki = 5, Kd = 1;
29+
float POn = 0.0; // Range is 0.0 to 1.0 (0.0 is 0% P on Error, 100% P on Measurement)
30+
31+
QuickPID myQuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, DIRECT);
32+
33+
unsigned int WindowSize = 5000;
34+
unsigned int minWindow = 500;
35+
unsigned long windowStartTime;
36+
37+
void setup()
38+
{
39+
pinMode(RELAY_PIN, OUTPUT);
40+
windowStartTime = millis();
41+
42+
//initialize the variables we're linked to
43+
Setpoint = 100;
44+
45+
//tell the PID to range between 0 and the full window size
46+
myQuickPID.SetOutputLimits(0, WindowSize);
47+
48+
//turn the PID on
49+
myQuickPID.SetMode(AUTOMATIC);
50+
}
51+
52+
void loop()
53+
{
54+
Input = analogRead(PIN_INPUT);
55+
56+
/************************************************
57+
turn the output pin on/off based on pid output
58+
************************************************/
59+
if (millis() - windowStartTime > WindowSize)
60+
{ //time to shift the Relay Window
61+
windowStartTime += WindowSize;
62+
myQuickPID.Compute();
63+
}
64+
if (((unsigned int)Output > minWindow) && ((unsigned int)Output < (millis() - windowStartTime))) digitalWrite(RELAY_PIN, HIGH);
65+
else digitalWrite(RELAY_PIN, LOW);
66+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=QuickPID
2-
version=2.0.3
2+
version=2.0.4
33
author=dlloydev
44
maintainer=David Lloyd <[email protected]>
55
sentence=QuickPID controller - a faster hybrid fixed/floating point implementation of the Arduino PID Library with more features.

0 commit comments

Comments
 (0)