Skip to content

Commit c05efc2

Browse files
committed
Update Readme
1 parent 5aa4c33 commit c05efc2

File tree

4 files changed

+46
-50
lines changed

4 files changed

+46
-50
lines changed

QuickPID.cpp

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
/**********************************************************************************
22
QuickPID Library for Arduino - Version 2.2.3
33
by dlloydev https://github.com/Dlloydev/QuickPID
4-
Based on the Arduino PID Library by Brett Beauregard
5-
6-
This Library is licensed under the MIT License
4+
Based on the Arduino PID Library, licensed under the MIT License
75
**********************************************************************************/
86

97
#if ARDUINO >= 100
108
#include "Arduino.h"
119
#else
1210
#include "WProgram.h"
1311
#endif
14-
1512
#include "QuickPID.h"
1613

1714
/* Constructor ********************************************************************
1815
The parameters specified here are those for for which we can't set up
1916
reliable defaults, so we need to have the user set them.
2017
**********************************************************************************/
2118
QuickPID::QuickPID(int* Input, int* Output, int* Setpoint,
22-
float Kp, float Ki, float Kd, float POn = 1, uint8_t ControllerDirection = 0)
23-
{
19+
float Kp, float Ki, float Kd, float POn = 1, uint8_t ControllerDirection = 0) {
20+
2421
myOutput = Output;
2522
myInput = Input;
2623
mySetpoint = Setpoint;
2724
inAuto = false;
2825

29-
QuickPID::SetOutputLimits(0, 255); // default is same as the arduino PWM limit
30-
sampleTimeUs = 100000; // default is 0.1 seconds
31-
26+
QuickPID::SetOutputLimits(0, 255); // same default as Arduino PWM limit
27+
sampleTimeUs = 100000; // 0.1 sec default
3228
QuickPID::SetControllerDirection(ControllerDirection);
3329
QuickPID::SetTunings(Kp, Ki, Kd, POn);
3430

@@ -39,19 +35,16 @@ QuickPID::QuickPID(int* Input, int* Output, int* Setpoint,
3935
To allow backwards compatability for v1.1, or for people that just want
4036
to use Proportional on Error without explicitly saying so.
4137
**********************************************************************************/
42-
4338
QuickPID::QuickPID(int* Input, int* Output, int* Setpoint,
4439
float Kp, float Ki, float Kd, uint8_t ControllerDirection)
45-
: QuickPID::QuickPID(Input, Output, Setpoint, Kp, Ki, Kd, pOn = 1, ControllerDirection = 0)
46-
{
40+
: QuickPID::QuickPID(Input, Output, Setpoint, Kp, Ki, Kd, pOn = 1, ControllerDirection = 0) {
4741

4842
}
4943

5044
/* Compute() **********************************************************************
51-
This, as they say, is where the magic happens. This function should be called
52-
every time "void loop()" executes. The function will decide whether a new
53-
PID Output needs to be computed. Returns true when the output is computed,
54-
false when nothing has been done.
45+
This function should be called every time "void loop()" executes. The function
46+
will decide whether a new PID Output needs to be computed. Returns true
47+
when the output is computed, false when nothing has been done.
5548
**********************************************************************************/
5649
bool QuickPID::Compute() {
5750
if (!inAuto) return false;
@@ -63,7 +56,7 @@ bool QuickPID::Compute() {
6356
int dInput = input - lastInput;
6457
error = *mySetpoint - input;
6558

66-
if (kpi < 31 && kpd < 31) outputSum += FX_MUL(FL_FX(kpi) , error) - FX_MUL(FL_FX(kpd), dInput); // fixed-point
59+
if (kpi < 31 && kpd < 31) outputSum += FX_MUL(FL_FX(kpi) , error) - FX_MUL(FL_FX(kpd), dInput); // fixed point
6760
else outputSum += (kpi * error) - (kpd * dInput); // floating-point
6861

6962
outputSum = CONSTRAIN(outputSum, outMin, outMax);
@@ -99,10 +92,11 @@ void QuickPID::AutoTune(int inputPin, int outputPin, int tuningRule, int Print =
9992
{ 333, 667, 111 }, // SOME_OVERSHOOT_PID
10093
{ 200, 400, 67 }, // NO_OVERSHOOT_PID
10194
};
102-
const byte ckp = 0, cki = 1, ckd = 2; // c = column
95+
const byte ckp = 0, cki = 1, ckd = 2; //c = column
10396
peakHigh = atSetpoint;
10497
peakLow = atSetpoint;
10598
timeout *= 1000;
99+
106100
if (Print == 1) Serial.print("Stabilizing (33%) →");
107101
QuickPID::Stabilize(inputPin, outputPin, timeout);
108102
if (Print == 1) Serial.print(" Running AutoTune ↑");
@@ -234,7 +228,7 @@ void QuickPID::Initialize() {
234228

235229
/* SetControllerDirection(...)*************************************************
236230
The PID will either be connected to a DIRECT acting process (+Output leads
237-
to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to
231+
to +Input) or a REVERSE acting process(+Output leads to -Input.) We need to
238232
know which one, because otherwise we may increase the output when we should
239233
be decreasing. This is called from the constructor.
240234
******************************************************************************/
@@ -282,19 +276,19 @@ uint8_t QuickPID::GetDirection() {
282276
int QuickPID::analogReadFast(int ADCpin) {
283277
#if defined(__AVR_ATmega328P__)
284278
byte ADCregOriginal = ADCSRA;
285-
ADCSRA = (ADCSRA & B11111000) | 5; //32 prescaler
279+
ADCSRA = (ADCSRA & B11111000) | 5; // 32 prescaler
286280
int adc = analogRead(ADCpin);
287281
ADCSRA = ADCregOriginal;
288282
return adc;
289283
#elif defined(__AVR_ATtiny_Zero_One__) || defined(__AVR_ATmega_Zero__)
290284
byte ADCregOriginal = ADC0_CTRLC;
291-
ADC0_CTRLC = 0x54; //reduced cap, Vdd ref, 32 prescaler
285+
ADC0_CTRLC = 0x54; // reduced cap, Vdd ref, 32 prescaler
292286
int adc = analogRead(ADCpin);
293287
ADC0_CTRLC = ADCregOriginal;
294288
return adc;
295289
#elif defined(__AVR_DA__)
296290
byte ADCregOriginal = ADC0.CTRLC;
297-
ADC0.CTRLC = ADC_PRESC_DIV32_gc; //32 prescaler
291+
ADC0.CTRLC = ADC_PRESC_DIV32_gc; // 32 prescaler
298292
int adc = analogRead(ADCpin);
299293
ADC0.CTRLC = ADCregOriginal;
300294
return adc;
@@ -325,9 +319,7 @@ void QuickPID::CheckPeak(int inputPin) {
325319

326320
void QuickPID::Stabilize(int inputPin, int outputPin, uint32_t timeout) {
327321
// initial reading
328-
for (int i = 0; i <= 16; i++) {
329-
analogReadAvg(inputPin);
330-
}
322+
for (int i = 0; i <= 16; i++) analogReadAvg(inputPin);
331323
// coarse adjust
332324
analogWrite (outputPin, 0);
333325
while ((analogReadAvg(inputPin) > (atSetpoint - hysteresis)) && (millis() <= timeout));

QuickPID.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#ifndef QuickPID_h
22
#define QuickPID_h
33

4-
class QuickPID
5-
{
4+
class QuickPID {
65

76
public:
87

@@ -12,6 +11,9 @@ class QuickPID
1211
#define DIRECT 0
1312
#define REVERSE 1
1413

14+
static const byte TRY_DIRECT = 0;
15+
static const byte TRY_AUTOMATIC = 1;
16+
1517
#define FL_FX(a) (int32_t)(a*256.0) // float to fixed point
1618
#define FX_MUL(a,b) ((a*b)>>8) // fixed point multiply
1719
#define CONSTRAIN(x,lower,upper) ((x)<(lower)?(lower):((x)>(upper)?(upper):(x)))
@@ -74,7 +76,7 @@ class QuickPID
7476
void StepDown(int, int, uint32_t);
7577
void Stabilize(int, int, uint32_t);
7678

77-
float dispKp; // We'll hold on to the tuning parameters for display purposes.
79+
float dispKp; // tuning parameters for display purposes.
7880
float dispKi;
7981
float dispKd;
8082
float dispKu;
@@ -113,4 +115,4 @@ class QuickPID
113115
#include "utility/analogWrite.h"
114116
#endif
115117

116-
#endif //QuickPID.h
118+
#endif // QuickPID.h

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,36 @@ The new `kpi` and `kpd` parameters are calculated in the `SetTunings()` function
5353

5454
```c++
5555
QuickPID::QuickPID(int* Input, int* Output, int* Setpoint,
56-
float Kp, float Ki, float Kd, float POn, uint8_t ControllerDirection)
56+
float Kp, float Ki, float Kd, float POn, uint8_t ControllerDirection);
5757
```
5858
5959
- `Input`, `Output`, and `Setpoint` are pointers to the variables holding these values.
6060
- `Kp`, `Ki`, and `Kd` are the PID proportional, integral, and derivative gains.
6161
- `POn` is the Proportional on Error weighting value (0.0-1.0). This controls the mix of Proportional on Error (PonE) and Proportional on Measurement (PonM) that's used in the compute algorithm. Note that POn controls the PonE amount, where the remainder (1-PonE) is the PonM amount. Also, the default POn is 1
6262
63-
![POn](https://user-images.githubusercontent.com/63488701/104958919-fe3c4680-599e-11eb-851e-73f26291d3e5.gif)
63+
<img src="https://user-images.githubusercontent.com/63488701/104958919-fe3c4680-599e-11eb-851e-73f26291d3e5.gif" alt="POn" style="zoom: 67%;" />
6464
6565
- `ControllerDirection` Either DIRECT or REVERSE determines which direction the output will move for a given error. DIRECT is most common.
6666
6767
```c++
6868
QuickPID::QuickPID(int* Input, int* Output, int* Setpoint,
69-
float Kp, float Ki, float Kd, uint8_t ControllerDirection)
69+
float Kp, float Ki, float Kd, uint8_t ControllerDirection);
7070
```
7171

7272
This allows you to use Proportional on Error without explicitly saying so.
7373

7474
#### Compute
7575

7676
```c++
77-
bool QuickPID::Compute()
77+
bool QuickPID::Compute();
7878
```
7979

8080
This function contains the PID algorithm and it should be called once every loop(). Most of the time it will just return false without doing anything. However, at a frequency specified by `SetSampleTime` it will calculate a new Output and return true.
8181

8282
#### [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune)
8383

8484
```c++
85-
void QuickPID::AutoTune(int inputPin, int outputPin, int tuningRule, int Print = 0, uint32_t timeout = 30)
85+
void QuickPID::AutoTune(int inputPin, int outputPin, int tuningRule, int Print = 0, uint32_t timeout = 30);
8686
```
8787
8888
The `AutoTune()` function automatically determines and sets `Kp`, `Ki` and `Kd`. It also determines the critical gain `Ku` and critical period `Tu` of the control system.
@@ -98,45 +98,45 @@ Sets the AutoTune timeout where the default is 120 seconds.
9898
#### SetTunings
9999
100100
```c++
101-
void QuickPID::SetTunings(float Kp, float Ki, float Kd, float POn)
101+
void QuickPID::SetTunings(float Kp, float Ki, float Kd, float POn);
102102
```
103103

104104
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. The parameters are as described in the constructor.
105105

106106
```c++
107-
void QuickPID::SetTunings(float Kp, float Ki, float Kd)
107+
void QuickPID::SetTunings(float Kp, float Ki, float Kd);
108108
```
109109
110110
Set Tunings using the last remembered POn setting.
111111
112112
#### SetSampleTime
113113
114114
```c++
115-
void QuickPID::SetSampleTimeUs(uint32_t NewSampleTimeUs)
115+
void QuickPID::SetSampleTimeUs(uint32_t NewSampleTimeUs);
116116
```
117117

118118
Sets the period, in microseconds, at which the calculation is performed. The default is 100ms.
119119

120120
#### SetOutputLimits
121121

122122
```c++
123-
void QuickPID::SetOutputLimits(int Min, int Max)
123+
void QuickPID::SetOutputLimits(int Min, int Max);
124124
```
125125
126126
The PID controller is designed to vary its output within a given range. By default this range is 0-255, the Arduino PWM range.
127127
128128
#### SetMode
129129
130130
```c++
131-
void QuickPID::SetMode(uint8_t Mode)
131+
void QuickPID::SetMode(uint8_t Mode);
132132
```
133133

134134
Allows the controller Mode to be set to `MANUAL` (0) or `AUTOMATIC` (non-zero). when the transition from manual to automatic occurs, the controller is automatically initialized.
135135

136136
#### Initialize
137137

138138
```c++
139-
void QuickPID::Initialize()
139+
void QuickPID::Initialize();
140140
```
141141

142142
Does all the things that need to happen to ensure a bump-less transfer from manual to automatic mode.
@@ -152,14 +152,14 @@ The PID will either be connected to a DIRECT acting process (+Output leads to +I
152152
#### Display_Functions
153153
154154
```c++
155-
float QuickPID::GetKp()
156-
float QuickPID::GetKi()
157-
float QuickPID::GetKd()
158-
float QuickPID::GetKu()
159-
float QuickPID::GetTu()
160-
float QuickPID::GetTd()
161-
uint8_t QuickPID::GetMode()
162-
uint8_t QuickPID::GetDirection()
155+
float QuickPID::GetKp();
156+
float QuickPID::GetKi();
157+
float QuickPID::GetKd();
158+
float QuickPID::GetKu();
159+
float QuickPID::GetTu();
160+
float QuickPID::GetTd();
161+
uint8_t QuickPID::GetMode();
162+
uint8_t QuickPID::GetDirection();
163163
```
164164

165165
These functions query the internal state of the PID. They're here for display purposes.
@@ -174,6 +174,8 @@ A faster configuration of `analogRead()`where a preset of 32 is used. If the ar
174174
175175
#### [AnalogWrite (PWM and DAC) for ESP32](https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite)
176176
177+
Use this link for reference. Note that if you're using QuickPID, there's no need to install the AnalogWrite library as this feature is already included.
178+
177179
#### Change Log
178180
179181
#### [![arduino-library-badge](https://www.ardu-badge.com/badge/QuickPID.svg?)](https://www.ardu-badge.com/QuickPID)

examples/AutoTune_RC_Filter/AutoTune_RC_Filter.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const byte outputPin = 3;
2424

2525
int Print = 0; // on(1) monitor, off(0) plotter
2626
int tuningRule = 1; // see above table
27-
float POn = 1.0; // Mix of PonE to PonM (0.0-1.0)
27+
float POn = 1.0; // mix of PonE to PonM (0.0-1.0)
2828
unsigned long timeout = 120; // AutoTune timeout (sec)
2929

3030
int Input, Output, Setpoint;
@@ -52,7 +52,7 @@ void setup()
5252
Serial.print(" Kp: "); Serial.print(myQuickPID.GetKp());
5353
Serial.print(" Ki: "); Serial.print(myQuickPID.GetKi());
5454
Serial.print(" Kd: "); Serial.println(myQuickPID.GetKd());
55-
delay(6000);
55+
delay(3000);
5656
}
5757
}
5858

0 commit comments

Comments
 (0)