From 779ad8ce350d8404ad0dac1f8be9f7efe164f092 Mon Sep 17 00:00:00 2001 From: Gonzalo Date: Tue, 18 May 2021 18:25:13 -0300 Subject: [PATCH 1/2] Changed return value for the current tunning stage Please, check example suggested in the other PR. --- src/QuickPID.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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) From d7e4e31acf79351ac4b1e22b90cdf9d5e117ef0e Mon Sep 17 00:00:00 2001 From: Gonzalo Date: Tue, 18 May 2021 18:27:17 -0300 Subject: [PATCH 2/2] Please consider this aproach Just some safety deference checks and better segmentation of the code. Just to better understand what is running at each loop cycle. --- .../AutoTune_Filter_DIRECT.ino | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) 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) {