|
10 | 10 | // Library Includes |
11 | 11 | #include <Adafruit_NeoPixel.h> |
12 | 12 | #include <Arduino.h> |
| 13 | +#include <etl/circular_buffer.h> |
13 | 14 |
|
14 | 15 | // Include LoadConfig first |
15 | 16 | #include "LoadConfig.hpp" |
@@ -435,42 +436,66 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR |
435 | 436 | // i += 20; |
436 | 437 | static int_fast32_t lastPower = INT_FAST32_MIN; |
437 | 438 | 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 | + |
438 | 445 | // if (INA260::current_mA > 0) { |
439 | 446 | /** @deprecated first check */ |
440 | 447 | if (loadFSM.getCurrentState() != FSMCommon::States::sRunLoad) { |
441 | 448 | // Don't run at startup |
442 | 449 | delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS); |
443 | 450 | 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 |
444 | 457 | } else if (abs(INA260::dPower_mWPS) > (abs(INA260::power_mW) * 0.05)) { |
| 458 | + // TODO: Maybe wait for a bigger change? |
445 | 459 | // Wait for power to stabilize |
446 | 460 | } else if ((lastPower == INT_FAST32_MIN) && (powerIndex > 0)) { |
447 | 461 | powerIndex--; |
448 | 462 | load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]); |
| 463 | + adjustmentHistory.push(LoadAdjustment::DECREASE); |
449 | 464 | ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW", |
450 | 465 | static_cast<int>(lastPower), |
451 | 466 | static_cast<int>(INA260::power_mW)); |
452 | 467 | ESP_LOGI(TAG, "Initial load adjustment, setpoint: %d", |
453 | 468 | 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; |
455 | 474 | powerIndex--; |
456 | 475 | load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]); |
| 476 | + // lastAdjustment = LoadAdjustment::DECREASE; |
| 477 | + adjustmentHistory.push(LoadAdjustment::DECREASE); |
457 | 478 | ESP_LOGD(TAG, "Last power: %d mW, current power: %d mW", |
458 | 479 | static_cast<int>(lastPower), |
459 | 480 | static_cast<int>(INA260::power_mW)); |
460 | 481 | ESP_LOGI(TAG, "Decreasing load to %d", |
461 | | - LOAD::RES_INDEX_TABLE[powerIndex]); |
| 482 | + LOAD::RES_INDEX_TABLE[powerIndex]); // TODO: Try by twos |
462 | 483 | } else if ((INA260::power_mW < lastPower) && (powerIndex < 46)) { |
| 484 | + // TODO: Not the best? |
463 | 485 | powerIndex++; |
464 | 486 | load.setLoadGPIO(LOAD::RES_INDEX_TABLE[powerIndex]); |
| 487 | + // lastAdjustment = LoadAdjustment::INCREASE; |
| 488 | + adjustmentHistory.push(LoadAdjustment::INCREASE); |
465 | 489 | ESP_LOGI(TAG, "Last power: %d mW, current power: %d mW", |
466 | 490 | static_cast<int>(lastPower), |
467 | 491 | static_cast<int>(INA260::power_mW)); |
468 | 492 | ESP_LOGI(TAG, "Increasing load to %d", |
469 | 493 | 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); |
472 | 497 | } else { // todo: change trigger to when rpm changes or when power |
473 | | - // changes |
| 498 | + // changes |
474 | 499 | // No change |
475 | 500 | } |
476 | 501 | lastPower = INA260::power_mW; |
|
0 commit comments