Skip to content

Commit d4ccf4e

Browse files
committed
Potentially improved load adjustment algo.
1 parent 90051b3 commit d4ccf4e

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/main.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Library Includes
1111
#include <Adafruit_NeoPixel.h>
1212
#include <Arduino.h>
13+
#include <etl/circular_buffer.h>
1314

1415
// Include LoadConfig first
1516
#include "LoadConfig.hpp"
@@ -435,42 +436,66 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
435436
// i += 20;
436437
static int_fast32_t lastPower = INT_FAST32_MIN;
437438
static int_fast8_t powerIndex = 46;
439+
// static bool searchDone = false;
440+
enum class LoadAdjustment : uint_fast8_t { INCREASE, DECREASE, NONE };
441+
// static LoadAdjustment adjustment = LoadAdjustment::NONE;
442+
static etl::circular_buffer<LoadAdjustment, 2> adjustmentHistory = {
443+
LoadAdjustment::NONE, LoadAdjustment::NONE};
444+
438445
// if (INA260::current_mA > 0) {
439446
/** @deprecated first check */
440447
if (loadFSM.getCurrentState() != FSMCommon::States::sRunLoad) {
441448
// Don't run at startup
442449
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
443450
continue;
451+
}
452+
if (abs(INA260::dPower_mWPS) > (abs(INA260::power_mW) * 0.10)) {
453+
// TODO: Maybe wait for a bigger change?, maybe over a longer time
454+
adjustmentHistory[0] = LoadAdjustment::NONE;
455+
adjustmentHistory[1] = LoadAdjustment::NONE;
456+
// Wait for power to stabilize
444457
} else if (abs(INA260::dPower_mWPS) > (abs(INA260::power_mW) * 0.05)) {
458+
// TODO: Maybe wait for a bigger change?
445459
// Wait for power to stabilize
446460
} else if ((lastPower == INT_FAST32_MIN) && (powerIndex > 0)) {
447461
powerIndex--;
448462
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
463+
adjustmentHistory.push(LoadAdjustment::DECREASE);
449464
ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW",
450465
static_cast<int>(lastPower),
451466
static_cast<int>(INA260::power_mW));
452467
ESP_LOGI(TAG, "Initial load adjustment, setpoint: %d",
453468
LOAD::RES_INDEX_TABLE[powerIndex]);
454-
} else if ((INA260::power_mW > lastPower) && (powerIndex > 0)) {
469+
470+
} else if ((adjustmentHistory[0] != LoadAdjustment::DECREASE) &&
471+
(adjustmentHistory[1] != LoadAdjustment::INCREASE) &&
472+
(INA260::power_mW > lastPower) && (powerIndex > 0)) {
473+
// searchDone = false;
455474
powerIndex--;
456475
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
476+
// lastAdjustment = LoadAdjustment::DECREASE;
477+
adjustmentHistory.push(LoadAdjustment::DECREASE);
457478
ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW",
458479
static_cast<int>(lastPower),
459480
static_cast<int>(INA260::power_mW));
460481
ESP_LOGI(TAG, "Decreasing load to %d",
461-
LOAD::RES_INDEX_TABLE[powerIndex]);
482+
LOAD::RES_INDEX_TABLE[powerIndex]); // TODO: Try by twos
462483
} else if ((INA260::power_mW < lastPower) && (powerIndex < 46)) {
484+
// TODO: Not the best?
463485
powerIndex++;
464486
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
487+
// lastAdjustment = LoadAdjustment::INCREASE;
488+
adjustmentHistory.push(LoadAdjustment::INCREASE);
465489
ESP_LOGI(TAG, "Last power: %d mW, current power: %d mW",
466490
static_cast<int>(lastPower),
467491
static_cast<int>(INA260::power_mW));
468492
ESP_LOGI(TAG, "Increasing load to %d",
469493
LOAD::RES_INDEX_TABLE[powerIndex]);
470-
delay(20 *
471-
1000); // todo RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS * 100
494+
// todo RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS * 100
495+
// Delay an extra 1/2 second
496+
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
472497
} else { // todo: change trigger to when rpm changes or when power
473-
// changes
498+
// changes
474499
// No change
475500
}
476501
lastPower = INA260::power_mW;

0 commit comments

Comments
 (0)