Skip to content

Commit

Permalink
Merge branch 'work-in-progress' of https://github.com/Dlloydev/QuickPID
Browse files Browse the repository at this point in the history
… into work-in-progress
  • Loading branch information
Dlloydev committed May 19, 2021
2 parents 2ab38f4 + 98cef95 commit c688c8d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
54 changes: 32 additions & 22 deletions examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,40 @@ void setup() {

void loop() {

if (_myPID.autoTune->autoTuneLoop() != _myPID.autoTune->RUN_PID) { // running autotune

Input = avg(_myPID.analogReadFast(inputPin)); // filtered input
analogWrite(outputPin, Output);
}

if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->NEW_TUNINGS) { // get new tunings
_myPID.autoTune->setAutoTuneConstants(&Kp, &Ki, &Kd); // set new tunings
_myPID.clearAutoTune(); // releases memory used by AutoTune object
_myPID.SetMode(QuickPID::AUTOMATIC); // setup PID
_myPID.SetTunings(Kp, Ki, Kd, POn); // apply new tunings to PID
Setpoint = 500;
}

if (_myPID.autoTune->autoTuneLoop() == _myPID.autoTune->RUN_PID) { // running PID
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.println();
}
Input = _myPID.analogReadFast(inputPin);
if (_myPID.autoTune) // Avoid deferencing nullptr after _myPID.clearAutoTune()
{
uint8_t autoTuningCurrentStage = autoTuneLoop();
if(autoTuningCurrentStage < _myPID.autoTune->RUN_PID)
{
Input = avg(_myPID.analogReadFast(inputPin)); // filtered input
analogWrite(outputPin, Output);

if (autoTuningCurrentStage == _myPID.autoTune->NEW_TUNINGS) // get new tunings
{
_myPID.autoTune->setAutoTuneConstants(&Kp, &Ki, &Kd); // set new tunings
_myPID.SetMode(QuickPID::AUTOMATIC); // setup PID
_myPID.SetTunings(Kp, Ki, Kd, POn); // apply new tunings to PID
Setpoint = 500;
}
}
else // RUN_PID stage
{
if (printOrPlotter == 0) // plotter
{
_myPID.clearAutoTune(); // releases memory used by AutoTune object
Serial.print("Setpoint:"); Serial.print(Setpoint); Serial.print(",");
Serial.print("Input:"); Serial.print(Input); Serial.print(",");
Serial.print("Output:"); Serial.print(Output); Serial.println();
}
}

}
else // Autotune already done (or not created)
{
Input = _myPID.analogReadFast(inputPin);
_myPID.Compute();
analogWrite(outputPin, Output);
}
}
}

float avg(int inputVal) {
Expand Down
12 changes: 10 additions & 2 deletions src/QuickPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,18 @@ byte AutoTunePID::autoTuneLoop()
case NEW_TUNINGS: // ready to apply tunings
*_output = 0;
_autoTuneStage++;
return NEW_TUNINGS;
//return NEW_TUNINGS;
break;

case RUN_PID: // ready to apply tunings
return RUN_PID;
break;
}
return RUN_PID;

if(_autoTuneStage < 1) // safety measure to avoid overflow of _autoTuneStage variable if its value is 0, which shouldn't happen never. Nonetheless...
return 0
else
return _autoTuneStage - 1;
}

void AutoTunePID::setAutoTuneConstants(float* kp, float* ki, float* kd)
Expand Down

0 comments on commit c688c8d

Please sign in to comment.