Skip to content

Commit eccafaa

Browse files
committed
Update1
1 parent 42873bb commit eccafaa

File tree

5 files changed

+383
-211
lines changed

5 files changed

+383
-211
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/******************************************************************************
2+
AutoTune Filter DIRECT Example
3+
Reference: https://github.com/Dlloydev/QuickPID/wiki/AutoTune
4+
Circuit: https://github.com/Dlloydev/QuickPID/wiki/AutoTune_RC_Filter
5+
******************************************************************************/
6+
7+
#include "QuickPID.h"
8+
9+
const byte inputPin = 0;
10+
const byte outputPin = 3;
11+
12+
const int outputMax = 255;
13+
const int outputMin = 0;
14+
15+
float POn = 1.0; // mix of PonE to PonM (0.0-1.0)
16+
17+
bool printOrPlotter = 0; // on(1) monitor, off(0) plotter
18+
byte tuningRule = 1; // see reference link
19+
byte outputStep = 1;
20+
byte hysteresis = 1;
21+
int setpoint = 341; // 1/3 of 10-bit ADC range for symetrical waveform
22+
int output = 85; // 1/3 of 8-bit PWM range for symetrical waveform
23+
24+
float Input, Output, Setpoint;
25+
float Kp = 0, Ki = 0, Kd = 0;
26+
27+
QuickPID _myPID = QuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, QuickPID::DIRECT);
28+
29+
void setup() {
30+
Serial.begin(115200);
31+
Serial.println();
32+
if (constrain(output, outputMin, outputMax - outputStep - 5) < output) {
33+
Serial.println(F("AutoTune test exceeds outMax limit, check output, hysteresis and outputStep values"));
34+
while (1);
35+
}
36+
_myPID.AutoTune(tuningRule);
37+
_myPID.autoTune->autoTuneConfig(outputStep, hysteresis, setpoint, 85, printOrPlotter);
38+
}
39+
40+
void loop() {
41+
42+
if (_myPID.autoTune->autoTuneLoop() != _myPID.autoTune->RUN_PID) { // running autotune
43+
Input = avg(_myPID.analogReadFast(inputPin)); // filtered input
44+
analogWrite(outputPin, Output);
45+
}
46+
47+
if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->NEW_TUNINGS) { // get new tunings
48+
_myPID.autoTune->setAutoTuneConstants(&Kp, &Ki, &Kd); // set new tunings
49+
_myPID.clearAutoTune(); // releases memory used by AutoTune object
50+
_myPID.SetMode(QuickPID::AUTOMATIC); // setup PID
51+
_myPID.SetTunings(Kp, Ki, Kd, POn); // apply new tunings to PID
52+
Setpoint = 500;
53+
}
54+
55+
if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->RUN_PID) { // running PID
56+
if (printOrPlotter == 0) { // plotter
57+
Serial.print("Setpoint:"); Serial.print(Setpoint); Serial.print(",");
58+
Serial.print("Input:"); Serial.print(Input); Serial.print(",");
59+
Serial.print("Output:"); Serial.print(Output); Serial.println();
60+
}
61+
Input = _myPID.analogReadFast(inputPin);
62+
_myPID.Compute();
63+
analogWrite(outputPin, Output);
64+
}
65+
}
66+
67+
float avg(int inputVal) {
68+
static int arrDat[16];
69+
static int pos;
70+
static long sum;
71+
pos++;
72+
if (pos >= 16) pos = 0;
73+
sum = sum - arrDat[pos] + inputVal;
74+
arrDat[pos] = inputVal;
75+
return (float)sum / 16.0;
76+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/******************************************************************************
2+
AutoTune Filter REVERSE Example
3+
Reference: https://github.com/Dlloydev/QuickPID/wiki/AutoTune
4+
Circuit: https://github.com/Dlloydev/QuickPID/wiki/AutoTune_RC_Filter
5+
******************************************************************************/
6+
7+
#include "QuickPID.h"
8+
9+
const byte inputPin = 0;
10+
const byte outputPin = 3;
11+
12+
const int inputMax = 1023;
13+
const int outputMax = 255;
14+
const int outputMin = 0;
15+
16+
float POn = 1.0; // mix of PonE to PonM (0.0-1.0)
17+
18+
bool printOrPlotter = 0; // on(1) monitor, off(0) plotter
19+
byte tuningRule = 1; // see reference link
20+
byte outputStep = 1;
21+
byte hysteresis = 1;
22+
int setpoint = 341; // 1/3 of 10-bit ADC range for symetrical waveform
23+
int output = 85; // 1/3 of 8-bit PWM range for symetrical waveform
24+
25+
float Input, Output, Setpoint;
26+
float Kp = 0, Ki = 0, Kd = 0;
27+
28+
QuickPID _myPID = QuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, QuickPID::REVERSE);
29+
30+
void setup() {
31+
Serial.begin(115200);
32+
Serial.println();
33+
if (constrain(output, outputMin, outputMax - outputStep - 5) < output) {
34+
Serial.println(F("AutoTune test exceeds outMax limit, check output, hysteresis and outputStep values"));
35+
while (1);
36+
}
37+
_myPID.AutoTune(tuningRule);
38+
_myPID.autoTune->autoTuneConfig(outputStep, hysteresis, inputMax - setpoint, output, QuickPID::REVERSE, printOrPlotter);
39+
}
40+
41+
void loop() {
42+
43+
if (_myPID.autoTune->autoTuneLoop() != _myPID.autoTune->RUN_PID) { // running autotune
44+
Input = inputMax - avg(_myPID.analogReadFast(inputPin)); // filtered input (reverse acting)
45+
analogWrite(outputPin, Output);
46+
}
47+
48+
if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->NEW_TUNINGS) { // get new tunings
49+
_myPID.autoTune->setAutoTuneConstants(&Kp, &Ki, &Kd); // set new tunings
50+
_myPID.clearAutoTune(); // releases memory used by AutoTune object
51+
_myPID.SetMode(QuickPID::AUTOMATIC); // setup PID
52+
_myPID.SetTunings(Kp, Ki, Kd, POn); // apply new tunings to PID
53+
Setpoint = 500;
54+
}
55+
56+
if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->RUN_PID) { // running PID
57+
if (printOrPlotter == 0) { // plotter
58+
Serial.print("Setpoint:"); Serial.print(Setpoint); Serial.print(",");
59+
Serial.print("Input:"); Serial.print(Input); Serial.print(",");
60+
Serial.print("Output:"); Serial.print(Output); Serial.println();
61+
}
62+
Input = inputMax - _myPID.analogReadFast(inputPin); // reverse acting
63+
_myPID.Compute();
64+
analogWrite(outputPin, Output);
65+
}
66+
}
67+
68+
float avg(int inputVal) {
69+
static int arrDat[16];
70+
static int pos;
71+
static long sum;
72+
pos++;
73+
if (pos >= 16) pos = 0;
74+
sum = sum - arrDat[pos] + inputVal;
75+
arrDat[pos] = inputVal;
76+
return (float)sum / 16.0;
77+
}

examples/AutoTune_RC_Filter/AutoTune_RC_Filter.ino

Lines changed: 0 additions & 205 deletions
This file was deleted.

0 commit comments

Comments
 (0)