diff --git a/examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino b/examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino index f5ebde7..04cbcb9 100644 --- a/examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino +++ b/examples/AutoTune_Filter_DIRECT/AutoTune_Filter_DIRECT.ino @@ -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) { diff --git a/src/QuickPID.cpp b/src/QuickPID.cpp index 2dd5257..b675257 100644 --- a/src/QuickPID.cpp +++ b/src/QuickPID.cpp @@ -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)