Skip to content

Commit f846c61

Browse files
committed
Update
1 parent f5c92c5 commit f846c61

File tree

6 files changed

+30
-47
lines changed

6 files changed

+30
-47
lines changed

README.md

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
# QuickPID [![arduino-library-badge](https://www.ardu-badge.com/badge/QuickPID.svg?)](https://www.ardu-badge.com/QuickPID)
22

3-
QuickPID is a fast fixed/floating point implementation of the Arduino PID library with built-in [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune) function. This controller can automatically determine and set parameters (Kp, Ki, Kd). Additionally the Ultimate Gain `Ku`, Ultimate Period `Tu`, Dead Time `td` and controllability of the process are determined. There are 9 tuning rules available to choose from. Also available is a POn setting that controls the mix of Proportional on Error to Proportional on Measurement.
3+
QuickPID is a fast fixed/floating point implementation of the Arduino PID library with built-in [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune) class as a dynamic object to reduce memory if not used. This controller can automatically determine and set parameters `Kp, Ki, Kd`. Additionally the Ultimate Gain `Ku`, Ultimate Period `Tu`, Dead Time `td` and determine how easy the process is to control. There are 10 tuning rules available to choose from. Also available is a POn setting that controls the mix of Proportional on Error to Proportional on Measurement.
44

55
### About
66

7-
This PID controller provides a faster *read-compute-write* cycle than alternatives as it has a more efficient and reduced algorithm that avoids time consuming partial calculations, it takes advantage of fixed point math and has a faster analog read function. The `Ki` and `Kd` are scaled by time (µs) and 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 in `Compute()`.
7+
This PID controller provides a shortened *read-compute-write* cycle by using a reduced PID algorithm, taking advantage of fixed point math and using a fast analog read function. The `Ki` and `Kd` are scaled by time (µs) and the new `kpi` and `kpd` parameters are calculated in the `SetTunings()` function resulting in only two multiply operations required in `Compute()`.
88

99
### Features
1010

1111
Development began with a fork of the Arduino PID Library. Modifications and new features have been added as described in the change log and below:
1212

13-
- Quicker hybrid fixed/floating point math in compute function
14-
- Built-in `AutoTune()` function automatically determines and sets `Kp`, `Ki` and `Kd`. and also ultimate gain `Ku` and ultimate period `Tu` of the control system. There are 9 tuning rules to choose from
15-
- [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune) uses a precise and low control effort sequence that gets results quickly. It also determines how difficult the process is to control.
16-
- `POn` parameter controls the setpoint weighting and mix of Proportional on Error to Proportional on Measurement
17-
- Reorganized and more efficient PID algorithm, faster analog read function, micros() timing resolution
18-
- Runs a complete PID cycle (*read-compute-write*) faster than just an `analogRead()` command in Arduino
19-
- Includes a complete [analogWrite function for ESP32](https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite) boards. This controls up to 8 independent PWM pins and 2 DAC pins.
13+
- Quicker hybrid fixed/floating point math, efficient PID algorithm and micros() timing resolution.
14+
- Built-in `AutoTunePID` class as a dynamic object. [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune) automatically determines and applies `Kp`, `Ki` and `Kd` tuning parameters and has 10 tuning rules to choose from.
15+
- Variable `POn` parameter controls the setpoint weighting and mix of Proportional on Error to Proportional on Measurement.
16+
- Includes [analogWrite](https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite) for ESP32 boards.
2017

21-
### Performance
18+
### Performance (16MHz ATmega328P)
2219

23-
| `Compute()` | Kp | Ki | Kd | Step Time (µS) |
24-
| :----------------------------------------------------------- | ---- | ---- | ---- | -------------- |
25-
| QuickPID | 2.0 | 15.0 | 0.05 | 72 |
26-
| Arduino PID | 2.0 | 15.0 | 0.05 | 104 |
27-
| **Full PID cycle:** **`analogRead(), Compute(), analogWrite()`** | | | | |
28-
| QuickPID using `analogReadFast()` | 2.0 | 5.0 | 0.2 | 96 |
29-
| Arduino PID using `analogRead()` | 2.0 | 5.0 | 0.2 | 224 |
20+
| `Compute()` | Kp | Ki | Kd | Step Time (µS) |
21+
| :---------- | ---- | ---- | ---- | -------------- |
22+
| QuickPID | 2.0 | 5.0 | 1.0 | 72 |
23+
| Arduino PID | 2.0 | 5.0 | 1.0 | 104 |
3024

3125
### [AutoTune RC Filter](https://github.com/Dlloydev/QuickPID/wiki/AutoTune_RC_Filter)
3226

33-
This example allows you to experiment with the AutoTune, various tuning rules and the POn control on an RC filter. It automatically determines and sets the tuning parameters.
27+
This example allows you to experiment with the AutoTunePID class, various tuning rules and the POn control using ADC and PWM with RC filter. It automatically determines and sets the tuning parameters and works with both DIRECT and REVERSE acting controllers.
3428

3529
#### [QuickPID WiKi ...](https://github.com/Dlloydev/QuickPID/wiki)
3630

@@ -40,16 +34,16 @@ This example allows you to experiment with the AutoTune, various tuning rules an
4034
outputSum += (kpi * error) - (kpd * dInput);
4135
```
4236

43-
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:
37+
The `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 gains for `error` (`kpi`) and measurement `dInput` (`kpd`) are calculated as follows:
4438

4539
```c++
4640
kpi = kp * pOn + ki;
4741
kpd = kp * (1 - pOn) + kd;
4842
```
4943

50-
### Controller Action
44+
### Direct and Reverse Controller Action
5145

52-
If a positive error increases the controller's output, the controller is said to be direct acting (i.e. heating process). When a positive error decreases the controller's output, the controller is said to be reverse acting (i.e. cooling process). Since the PWM and ADC peripherals on microcontrollers only operate with positive values, QuickPID only uses positive values for Input, Output and Setpoint. When the controller is set to `REVERSE` acting, the sign of the `error` and `dInput` (derivative of Input) is internally changed. All operating ranges and limits remain the same. To simulate a `REVERSE` acting process from a process that's `DIRECT` acting, the Input value needs to be "flipped". That is, if your reading from a 10-bit ADC with 0-1023 range, the input value used is (1023 - reading). See the example [AutoTune_RC_Filter.ino](https://github.com/Dlloydev/QuickPID/blob/master/examples/AutoTune_RC_Filter/AutoTune_RC_Filter.ino) for details.
46+
If a positive error increases the controller's output, the controller is said to be direct acting (i.e. heating process). When a positive error decreases the controller's output, the controller is said to be reverse acting (i.e. cooling process). When the controller is set to `REVERSE` acting, the sign of the `error` and `dInput` (derivative of Input) is internally changed. All operating ranges and limits remain the same. To simulate a `REVERSE` acting process from a process that's `DIRECT` acting, the Input value needs to be "flipped". That is, if your reading from a 10-bit ADC with 0-1023 range, the input value used is (1023 - reading). See the examples `AutoTune_Filter_DIRECT.ino` and `AutoTune_Filter_REVERSE.ino` for details.
5347

5448
### Functions
5549

@@ -64,7 +58,7 @@ QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
6458
- `Kp`, `Ki`, and `Kd` are the PID proportional, integral, and derivative gains.
6559
- `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
6660
67-
<img src="https://user-images.githubusercontent.com/63488701/104958919-fe3c4680-599e-11eb-851e-73f26291d3e5.gif" alt="POn" style="zoom: 67%;" />
61+
![image](https://user-images.githubusercontent.com/63488701/118383726-986b6e80-b5ce-11eb-94b8-fdbddd4c914e.png)
6862
6963
- `ControllerDirection` Either DIRECT or REVERSE determines which direction the output will move for a given error. DIRECT is most common.
7064
@@ -85,19 +79,7 @@ This function contains the PID algorithm and it should be called once every loop
8579

8680
#### [AutoTune](https://github.com/Dlloydev/QuickPID/wiki/AutoTune)
8781

88-
```c++
89-
void QuickPID::AutoTune(int inputPin, int outputPin, int tuningRule, int Print = 0, uint32_t timeout = 30);
90-
```
91-
92-
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.
93-
94-
`int Print = 0; // on(1), off(0)`
95-
96-
When using Serial Monitor, turn on serial print output to view the AutoTune status and results. When using the Serial Plotter, turn off the AutoTune print output to prevent plot labels from being overwritten.
97-
98-
`uint32_t timeout = 120`
99-
100-
Sets the AutoTune timeout where the default is 120 seconds.
82+
For use of AutoTune, refer to the examples `AutoTune_Filter_DIRECT.ino` and `AutoTune_Filter_REVERSE.ino`
10183

10284
#### SetTunings
10385

@@ -182,8 +164,6 @@ Use this link for reference. Note that if you're using QuickPID, there's no need
182164
183165
#### Change Log
184166
185-
#### [![arduino-library-badge](https://www.ardu-badge.com/badge/QuickPID.svg?)](https://www.ardu-badge.com/QuickPID)
186-
187167
#### Version 2.2.8
188168
189169
- AutoTune is now independent of the QuickPID library and can be run from a sketch. AutoTune is now non-blocking, no timeouts are required and it uses Input and Output variables directly. See the example [AutoTune_RC_Filter.ino](https://github.com/Dlloydev/QuickPID/blob/master/examples/AutoTune_RC_Filter/AutoTune_RC_Filter.ino) for details.

examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ void setup() {
3030
Serial.begin(115200);
3131
Serial.println();
3232
if (constrain(output, outputMin, outputMax - outputStep - 5) < output) {
33-
Serial.println(F("AutoTune test exceeds outMax limit, check output, hysteresis and outputStep values"));
33+
Serial.println(F("AutoTune test exceeds outMax limit. Check output, hysteresis and outputStep values"));
3434
while (1);
3535
}
3636
_myPID.AutoTune(tuningRule);
37-
_myPID.autoTune->autoTuneConfig(outputStep, hysteresis, setpoint, output, printOrPlotter);
37+
_myPID.autoTune->autoTuneConfig(outputStep, hysteresis, setpoint, output, QuickPID::DIRECT, printOrPlotter);
3838
}
3939

4040
void loop() {
4141

4242
if (_myPID.autoTune->autoTuneLoop() != _myPID.autoTune->RUN_PID) { // running autotune
43+
Serial.println(F("I'm here!"));
44+
4345
Input = avg(_myPID.analogReadFast(inputPin)); // filtered input
4446
analogWrite(outputPin, Output);
4547
}

examples/AutoTune_Filter_REVERSE/AutoTune_Filter_REVERSE.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void setup() {
3131
Serial.begin(115200);
3232
Serial.println();
3333
if (constrain(output, outputMin, outputMax - outputStep - 5) < output) {
34-
Serial.println(F("AutoTune test exceeds outMax limit, check output, hysteresis and outputStep values"));
34+
Serial.println(F("AutoTune test exceeds outMax limit. Check output, hysteresis and outputStep values"));
3535
while (1);
3636
}
3737
_myPID.AutoTune(tuningRule);

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "QuickPID",
33
"keywords": "PID, controller, signal",
4-
"description": "A fast fixed/floating point PID controller with AutoTune and 9 tuning rules to choose from. This controller can automatically determine and set tuning parameters. Compatible with most Arduino and ESP32 boards.",
4+
"description": "A fast fixed/floating point PID controller with AutoTune and 10 tuning rules to choose from. This controller can automatically determine and set tuning parameters. Compatible with most Arduino and ESP32 boards.",
55
"license": "MIT",
6-
"version": "2.2.8",
6+
"version": "2.3.0",
77
"url": "https://github.com/Dlloydev/QuickPID",
88
"include": "QuickPID",
99
"authors":

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name=QuickPID
2-
version=2.2.8
2+
version=2.3.0
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
5-
sentence=A fast fixed/floating point PID controller with AutoTune and 9 tuning rules to choose from.
5+
sentence=A fast fixed/floating point PID controller with AutoTune and 10 tuning rules to choose from.
66
paragraph=This controller can automatically determine and set tuning parameters. Compatible with most Arduino and ESP32 boards.
77
category=Signal Input/Output
88
url=https://github.com/Dlloydev/QuickPID

src/QuickPID.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**********************************************************************************
2-
QuickPID Library for Arduino - Version 2.2.8
2+
QuickPID Library for Arduino - Version 2.3.0
33
by dlloydev https://github.com/Dlloydev/QuickPID
4-
Based on the Arduino PID Library, licensed under the MIT License
4+
Based on the Arduino PID Library and work on AutoTunePID class
5+
by gnalbandian (Gonzalo). Licensed under the MIT License
56
**********************************************************************************/
67

78
#if ARDUINO >= 100
@@ -337,7 +338,7 @@ byte AutoTunePID::autoTuneLoop()
337338
_Tu = (float)(_t3 - _t2) / 1000000.0; // ultimate period (seconds)
338339
_Ku = (float)(4 * _outputStep * 2) / (float)(3.14159 * sqrt (sq (_peakHigh - _peakLow) - sq (_hysteresis))); // ultimate gain
339340
if (_tuningRule == 6) { //AMIGO_PID
340-
(_td < 100) ? _td = 100 : _td = _td; // 100µs minimum
341+
(_td < 0.1) ? _td = 0.1 : _td = _td;
341342
_kp = (0.2 + 0.45 * (_Tu / _td)) / _Ku;
342343
float Ti = (((0.4 * _td) + (0.8 * _Tu)) / (_td + (0.1 * _Tu)) * _td);
343344
float Td = (0.5 * _td * _Tu) / ((0.3 * _td) + _Tu);

0 commit comments

Comments
 (0)