Skip to content

Commit 862665a

Browse files
committed
Implemented proposed new load adjustment config
1 parent 6073d05 commit 862665a

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/main.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#else
4141
# error "Invalid COMMS_STRATEGY"
4242
#endif
43+
#include <2026Core/Units.hpp>
4344

4445
/* Config */
4546
static constexpr const char *TAG = "LoMa";
@@ -434,7 +435,13 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
434435
// pitchPIDController.compute(
435436
// i)); // todo - just a quick performances test
436437
// i += 20;
437-
static int_fast32_t lastPower = INT_FAST32_MIN;
438+
// static int_fast32_t lastPower = INT_FAST32_MIN;
439+
constexpr uint32_t powerHistoryLength_s = 5;
440+
constexpr uint32_t powerHistoryLength =
441+
powerHistoryLength_s *
442+
(UNITS::MILLIS_PER_SEC / RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
443+
static etl::circular_buffer<int_fast32_t, powerHistoryLength>
444+
powerHistory;
438445
static int_fast8_t powerIndex = 46;
439446
// static bool searchDone = false;
440447
enum class LoadAdjustment : uint_fast8_t { INCREASE, DECREASE, NONE };
@@ -449,46 +456,49 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
449456
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
450457
continue;
451458
}
452-
if (abs(INA260::dPower_mWPS) > (abs(INA260::power_mW) * 0.10)) {
453-
// TODO: Maybe wait for a bigger change?, maybe over a 5 second
454-
// longer time, maybe record every 5
459+
if (abs(powerHistory.front()) * 1.30 < abs(powerHistory.back())) {
460+
// DONE: Consider waiting for a bigger change over 5 seconds instead
461+
// of checking dPower_mWPS
462+
// Run algo again if power increases by more than 30% in five
463+
// seconds
455464
adjustmentHistory[0] = LoadAdjustment::NONE;
456465
adjustmentHistory[1] = LoadAdjustment::NONE;
457466
// Wait for power to stabilize
458467
} else if (abs(INA260::dPower_mWPS) > (abs(INA260::power_mW) * 0.05)) {
459-
// TODO: Maybe wait for a bigger change?
460468
// Wait for power to stabilize
461-
} else if ((lastPower == INT_FAST32_MIN) && (powerIndex > 0)) {
469+
} else if ((powerHistory.empty()) && (powerIndex > 0)) {
462470
powerIndex--;
463471
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
464472
adjustmentHistory.push(LoadAdjustment::DECREASE);
465473
ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW",
466-
static_cast<int>(lastPower),
474+
static_cast<int>(powerHistory.back()),
467475
static_cast<int>(INA260::power_mW));
468476
ESP_LOGI(TAG, "Initial load adjustment, setpoint: %d",
469477
LOAD::RES_INDEX_TABLE[powerIndex]);
470478

471479
} else if ((adjustmentHistory[0] != LoadAdjustment::DECREASE) &&
472480
(adjustmentHistory[1] != LoadAdjustment::INCREASE) &&
473-
(INA260::power_mW > lastPower) && (powerIndex > 0)) {
481+
(INA260::power_mW > powerHistory.back()) &&
482+
(powerIndex > 0)) {
474483
// searchDone = false;
475484
powerIndex--;
476485
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
477486
// lastAdjustment = LoadAdjustment::DECREASE;
478487
adjustmentHistory.push(LoadAdjustment::DECREASE);
479488
ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW",
480-
static_cast<int>(lastPower),
489+
static_cast<int>(powerHistory.back()),
481490
static_cast<int>(INA260::power_mW));
482491
ESP_LOGI(TAG, "Decreasing load to %d",
483492
LOAD::RES_INDEX_TABLE[powerIndex]); // TODO: Try by twos
484-
} else if ((INA260::power_mW < lastPower) && (powerIndex < 46)) {
493+
} else if ((INA260::power_mW < powerHistory.back()) &&
494+
(powerIndex < 46)) {
485495
// TODO: Not the best?
486496
powerIndex++;
487497
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]);
488498
// lastAdjustment = LoadAdjustment::INCREASE;
489499
adjustmentHistory.push(LoadAdjustment::INCREASE);
490500
ESP_LOGI(TAG, "Last power: %d mW, current power: %d mW",
491-
static_cast<int>(lastPower),
501+
static_cast<int>(powerHistory.back()),
492502
static_cast<int>(INA260::power_mW));
493503
ESP_LOGI(TAG, "Increasing load to %d",
494504
LOAD::RES_INDEX_TABLE[powerIndex]);
@@ -499,7 +509,7 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
499509
// changes
500510
// No change
501511
}
502-
lastPower = INA260::power_mW;
512+
powerHistory.push(INA260::power_mW);
503513

504514
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
505515
}

0 commit comments

Comments
 (0)