@@ -460,7 +460,13 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
460460 delay (RUN ::TASK_INTERVALS ::TI_ADJUST_LOAD_mS);
461461 continue ;
462462 }
463- if (abs (powerHistory.front ()) * 1.20 < abs (powerHistory.back ())) {
463+ if (powerHistory.empty ()) {
464+ ESP_LOGW (TAG , " Insufficient data" );
465+ ESP_LOGI (TAG , " Last power: %d mW, current power: %d mW" ,
466+ static_cast <int >(powerHistory.back ()),
467+ static_cast <int >(INA260 ::power_mW));
468+ // Don't do anything until one reading has been made
469+ } else if (abs (powerHistory.front ()) * 1.20 < abs (powerHistory.back ())) {
464470 // DONE: Consider waiting for a bigger change over 5 seconds instead
465471 // of checking dPower_mWPS
466472 // Run algo again if power increases by more than 30% in five
@@ -478,16 +484,10 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
478484 static_cast <int >(powerHistory.back ()),
479485 static_cast <int >(INA260 ::power_mW));
480486 // Wait for power to stabilize
481- } else if (powerHistory.empty ()) {
482- ESP_LOGW (TAG , " Insufficient data" );
483- ESP_LOGI (TAG , " Last power: %d mW, current power: %d mW" ,
484- static_cast <int >(powerHistory.back ()),
485- static_cast <int >(INA260 ::power_mW));
486- // Don't do anything until one reading has been made
487487 } else if ((powerHistory.size () == 1 ) && (powerIndex > 1 )) {
488488 // Make initial adjustment when one value filled
489489 // powerIndex--;
490- powerIndex -= 2 ;
490+ powerIndex > 2 ? powerIndex -= 2 : powerIndex = 0 ; // FIXED, avoiding out of bounds condition
491491 load.setLoadGPIO (LOAD ::RES_INDEX_TABLE [powerIndex]);
492492 adjustmentHistory.push (LoadAdjustment::DECREASE );
493493 ESP_LOGD (TAG , " Last power: %d mW, current power: %d mW" ,
@@ -500,7 +500,7 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
500500 (powerIndex > 1 )) {
501501 // Decreasing + got better -> decrease again
502502 // powerIndex--;
503- powerIndex -= 2 ;
503+ powerIndex > 2 ? powerIndex -= 2 : powerIndex = 0 ; // FIXED, avoiding out of bounds condition
504504 load.setLoadGPIO (LOAD ::RES_INDEX_TABLE [powerIndex]);
505505 adjustmentHistory.push (LoadAdjustment::DECREASE );
506506 ESP_LOGD (TAG , " Last power: %d mW, current power: %d mW" ,
@@ -526,7 +526,8 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
526526 LOAD ::RES_INDEX_TABLE [powerIndex]);
527527 // Delay an extra 1/2 second
528528 delay (RUN ::TASK_INTERVALS ::TI_ADJUST_LOAD_mS);
529- } else if ((INA260 ::power_mW < powerHistory[powerHistory.size () - 3 ]) &&
529+ } else if ((powerHistory.size () >= 3 ) &&
530+ (INA260 ::power_mW < powerHistory[powerHistory.size () - 3 ]) &&
530531 (INA260 ::power_mW < powerHistory[powerHistory.size () - 2 ]) &&
531532 (INA260 ::power_mW < powerHistory[powerHistory.size () - 1 ]) &&
532533 (adjustmentHistory.back () == LoadAdjustment::DECREASE ) &&
@@ -549,7 +550,7 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
549550 (powerIndex > 0 )) { // MARK: BOLD LOAD
550551 // increasing + got worse a bunch -> decrease
551552 // powerIndex--;
552- powerIndex -= 2 ;
553+ powerIndex > 2 ? powerIndex -= 2 : powerIndex = 0 ; // FIXED, avoiding out of bounds condition
553554 load.setLoadGPIO (LOAD ::RES_INDEX_TABLE [powerIndex]);
554555 adjustmentHistory.push (LoadAdjustment::DECREASE );
555556 ESP_LOGI (TAG , " Last power: %d mW, current power: %d mW" ,
0 commit comments