forked from br3ttb/Arduino-PID-Library
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#### QuickPID 2.3.3 - Added new `TIMER` mode which is used when the PID compute is called by an external timer function or ISR. In this mode, the timer function and SetSampleTimeUs use the same time period value. The PID compute and timer will always remain in sync because the sample time variable and calculations remain constant. See AutoTune_`TIMER` mode example [AutoTune_Filter_TIMER_Mode.ino](https://github.com/Dlloydev/QuickPID/blob/master/examples/AutoTune_Filter_TIMER_Mode/AutoTune_Filter_TIMER_Mode.ino)
- Loading branch information
Showing
7 changed files
with
146 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
examples/AutoTune_Filter_TIMER_Mode/AutoTune_Filter_TIMER_Mode.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/****************************************************************************** | ||
AutoTune Filter TIMER Mode Example | ||
Circuit: https://github.com/Dlloydev/QuickPID/wiki/AutoTune_RC_Filter | ||
******************************************************************************/ | ||
#include "NoDelay.h" // https://github.com/M-tech-Creations/NoDelay | ||
#include "QuickPID.h" | ||
void runPid(); // declare function before noDelay | ||
noDelay LEDtime(10, runPid); // creates a noDelay varible set to 10ms, will call runPid function | ||
|
||
const byte inputPin = 0; | ||
const byte outputPin = 3; | ||
|
||
const int outputMax = 255; | ||
const int outputMin = 0; | ||
|
||
float POn = 1.0; // mix of PonE to PonM (0.0-1.0) | ||
bool printOrPlotter = 0; // on(1) monitor, off(0) plotter | ||
byte outputStep = 5; | ||
byte hysteresis = 1; | ||
int setpoint = 341; // 1/3 of 10-bit ADC range for symetrical waveform | ||
int output = 85; // 1/3 of 8-bit PWM range for symetrical waveform | ||
|
||
float Input, Output, Setpoint; | ||
float Kp = 0, Ki = 0, Kd = 0; | ||
bool pidLoop = false; | ||
static boolean computeNow = false; | ||
|
||
QuickPID _myPID = QuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, QuickPID::DIRECT); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println(); | ||
if (constrain(output, outputMin, outputMax - outputStep - 5) < output) { | ||
Serial.println(F("AutoTune test exceeds outMax limit. Check output, hysteresis and outputStep values")); | ||
while (1); | ||
} | ||
// Select one, reference: https://github.com/Dlloydev/QuickPID/wiki | ||
//_myPID.AutoTune(tuningMethod::ZIEGLER_NICHOLS_PI); | ||
_myPID.AutoTune(tuningMethod::ZIEGLER_NICHOLS_PID); | ||
//_myPID.AutoTune(tuningMethod::TYREUS_LUYBEN_PI); | ||
//_myPID.AutoTune(tuningMethod::TYREUS_LUYBEN_PID); | ||
//_myPID.AutoTune(tuningMethod::CIANCONE_MARLIN_PI); | ||
//_myPID.AutoTune(tuningMethod::CIANCONE_MARLIN_PID); | ||
//_myPID.AutoTune(tuningMethod::AMIGOF_PID); | ||
//_myPID.AutoTune(tuningMethod::PESSEN_INTEGRAL_PID); | ||
//_myPID.AutoTune(tuningMethod::SOME_OVERSHOOT_PID); | ||
//_myPID.AutoTune(tuningMethod::NO_OVERSHOOT_PID); | ||
|
||
_myPID.autoTune->autoTuneConfig(outputStep, hysteresis, setpoint, output, QuickPID::DIRECT, printOrPlotter); | ||
} | ||
|
||
void loop() { | ||
LEDtime.update();//will check if set time has past and if so will run set function | ||
|
||
if (_myPID.autoTune) // Avoid dereferencing nullptr after _myPID.clearAutoTune() | ||
{ | ||
switch (_myPID.autoTune->autoTuneLoop()) { | ||
case _myPID.autoTune->AUTOTUNE: | ||
Input = avg(_myPID.analogReadFast(inputPin)); | ||
analogWrite(outputPin, Output); | ||
break; | ||
|
||
case _myPID.autoTune->TUNINGS: | ||
_myPID.autoTune->setAutoTuneConstants(&Kp, &Ki, &Kd); // set new tunings | ||
_myPID.SetMode(QuickPID::TIMER); // setup PID | ||
_myPID.SetSampleTimeUs(10000); // 10ms | ||
_myPID.SetTunings(Kp, Ki, Kd, POn); // apply new tunings to PID | ||
Setpoint = 500; | ||
break; | ||
|
||
case _myPID.autoTune->CLR: | ||
if (!pidLoop) { | ||
_myPID.clearAutoTune(); // releases memory used by AutoTune object | ||
pidLoop = true; | ||
} | ||
break; | ||
} | ||
} | ||
if (pidLoop) { | ||
if (printOrPlotter == 0) { // plotter | ||
Serial.print("Setpoint:"); Serial.print(Setpoint); Serial.print(","); | ||
Serial.print("Input:"); Serial.print(Input); Serial.print(","); | ||
Serial.print("Output:"); Serial.print(Output); Serial.print(","); | ||
Serial.print("iTerm:"); Serial.print(_myPID.GetDterm()); Serial.println(); | ||
} | ||
if (computeNow) { | ||
Input = _myPID.analogReadFast(inputPin); | ||
_myPID.Compute(); | ||
analogWrite(outputPin, Output); | ||
computeNow = false; | ||
} | ||
} | ||
delay(1); // adjust loop speed | ||
} | ||
|
||
void runPid() { | ||
computeNow = true; | ||
} | ||
|
||
float avg(int inputVal) { | ||
static int arrDat[16]; | ||
static int pos; | ||
static long sum; | ||
pos++; | ||
if (pos >= 16) pos = 0; | ||
sum = sum - arrDat[pos] + inputVal; | ||
arrDat[pos] = inputVal; | ||
return (float)sum / 16.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters