Skip to content

Commit 11f13ff

Browse files
committed
Drafted new load code
1 parent 4df951a commit 11f13ff

2 files changed

Lines changed: 89 additions & 12 deletions

File tree

include/LoadConfig.hpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,42 @@ namespace LOAD {
189189
static_assert(NUM_COMBINATIONS == (1u << NUM_PINS),
190190
"NUM_COMBINATIONS must be 2^NUM_PINS");
191191

192-
etl::array<uint_fast8_t, 46> RES_INDEX_TABLE = {
193-
0, 1, 2, 4, 3, 8, 5, 6, 9, 12, 16, 11, 7, 14, 13, 15,
194-
19, 24, 21, 22, 25, 23, 28, 29, 32, 31, 33, 35, 36, 40, 37, 41,
195-
39, 44, 48, 45, 46, 50, 47, 51, 56, 53, 54, 57, 55, 60};
192+
constexpr uint_fast8_t VOLT_LIMIT_SIZE = 12; // CONFIG
193+
etl::array<uint_fast16_t, VOLT_LIMIT_SIZE> VOLT_LIMITS_TABLE_mV = {
194+
2000, //
195+
1900, 1800, 1700, 1600, 1400, //
196+
1500, 1400, 1300, 1200, 1100, //
197+
1000};
198+
199+
constexpr uint_fast8_t RES_INDEX_SIZE = 46; // CONFIG
200+
etl::array<uint_fast8_t, RES_INDEX_SIZE> RES_INDEX_TABLE = {
201+
0, 1, 2, 4, 3, //
202+
8, 5, 6, 9, 12, //
203+
16, 11, 7, 14, 13, //
204+
15, 19, 24, 21, 22, //
205+
25, 23, 28, 29, 32, //
206+
31, 33, 35, 36, 40, //
207+
37, 41, 39, 44, 48, //
208+
45, 46, 50, 47, 51, //
209+
56, 53, 54, 57, 55, //
210+
60};
211+
// constexpr uint_fast8_t RES_INDEX_SIZE = 46; // CONFIG
212+
// etl::array<uint_fast8_t, RES_INDEX_SIZE> RES_INDEX_TABLE = {
213+
// 0, 1, 2, 4, 3, 8, 5, 6, 9, 12, 16, 11, 7, 14, 13, 15,
214+
// 19, 24, 21, 22, 25, 23, 28, 29, 32, 31, 33, 35, 36, 40, 37, 41,
215+
// 39, 44, 48, 45, 46, 50, 47, 51, 56, 53, 54, 57, 55, 60};
216+
217+
static_assert(
218+
VOLT_LIMIT_SIZE <= RES_INDEX_SIZE,
219+
"VOLT_LIMIT_SIZE must be less than or equal to RES_INDEX_SIZE");
220+
static_assert(
221+
RES_INDEX_SIZE <= NUM_COMBINATIONS,
222+
"RES_INDEX_SIZE must be less than or equal to NUM_COMBINATIONS");
196223

197224
/**
198-
* @brief Resistor values, in milli‑ohms, corresponding to each pin. Note
199-
* that the resistors will be connected in series, so the total resistance
200-
* is the sum of the selected resistors.
225+
* @brief Resistor values, in milli‑ohms, corresponding to each pin.
226+
* Note that the resistors will be connected in series, so the total
227+
* resistance is the sum of the selected resistors.
201228
*/
202229
constexpr etl::array<uint_fast16_t, NUM_PINS> PIN_VALUES_mOhms = {
203230
500, 1000, 2000, 2200, 5000, 10000}; // in mOhms, in series // TODO

src/main.cpp

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ vTaskUpdateFSM([[maybe_unused]] void *pvParameters) { // NOSONAR
432432
*/
433433
#define LOAD_STRATEGY_MPPT_POM 0
434434
#define LOAD_STRATEGY_VOLTAGE_LIV 1
435-
#define LOAD_STRATEGY LOAD_STRATEGY_VOLTAGE_LIV
435+
#define LOAD_STRATEGY_VOLTAGE_FIX 2
436+
#define LOAD_STRATEGY LOAD_STRATEGY_VOLTAGE_FIX
436437
[[noreturn]] void
437438
vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
438439
while (true) {
@@ -585,29 +586,78 @@ vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
585586

586587
// Note: Could be more efficient (?) but then I'd have to add more flow
587588
// control
588-
if ((INA260::antiBackwards < 1) && (INA260::voltage_mV > V1SP_mV) && (INA260::voltage_mV <= V2SP_mV)) {
589+
if ((INA260::antiBackwards < 1) && (INA260::voltage_mV > V1SP_mV) &&
590+
(INA260::voltage_mV <= V2SP_mV)) {
589591
/* Increase to stage 2: (> 10 V & < 15 V) */
590592
INA260::antiBackwards = 1;
591593
load.setLoadGPIO(LOAD_GPIO_V1SP);
592594
ESP_LOGI(TAG, "Voltage between %d mV and %d mV, setting load to %d",
593595
V1SP_mV, V2SP_mV, LOAD_GPIO_V1SP);
594-
} else if ((INA260::antiBackwards < 2) &&(INA260::voltage_mV > V2SP_mV) &&
596+
} else if ((INA260::antiBackwards < 2) &&
597+
(INA260::voltage_mV > V2SP_mV) &&
595598
(INA260::voltage_mV <= V3SP_mV)) {
596599
INA260::antiBackwards = 2;
597600
/* Increase to stage 3: !(> 10 V & < 15 V) & (> 15 V & <= 18 V) */
598601
load.setLoadGPIO(LOAD_GPIO_V2SP);
599602
ESP_LOGI(TAG, "Voltage above %d mV, setting load to %d", V1SP_mV,
600603
LOAD_GPIO_V2SP);
601-
} else if ((INA260::antiBackwards < 3) && (INA260::voltage_mV > V3SP_mV)) {
604+
} else if ((INA260::antiBackwards < 3) &&
605+
(INA260::voltage_mV > V3SP_mV)) {
602606
/* Increase to stage 4: !(< 10*0.05 V) & !(> 10 V & < 15 V) &
603607
!(< 15*0.05 V) & !(> 15 V) & !(< 18*0.05 V) & (> 18 V) */
604608
INA260::antiBackwards = 3;
605609
load.setLoadGPIO(LOAD_GPIO_V3SP);
606610
ESP_LOGI(TAG, "Voltage above %d mV, setting load to %d", V3SP_mV,
607611
LOAD_GPIO_V3SP);
608612
}
609-
#endif
613+
#elif LOAD_STRATEGY == LOAD_STRATEGY_VOLTAGE_FIX
614+
// Iterate over the parallel array in revers order
615+
static uint_fast8_t voltLimitIndex = LOAD::VOLT_LIMITS_TABLE_mV.size() - 1;
616+
// LCR: Load Control Resistor
617+
static uint_fast8_t lCRIndex = LOAD::RES_INDEX_TABLE.size() - 1;
618+
619+
constexpr int_fast16_t MAX_SAFE_VOLTAGE_mV =
620+
30000; // CONFIG - Actually 32 or 36 V max
621+
if (INA260::voltage_mV > MAX_SAFE_VOLTAGE_mV) {
622+
ESP_LOGW(TAG, "%d mV exceeds max safe of %d mV", INA260::voltage_mV,
623+
MAX_SAFE_VOLTAGE_mV);
624+
// Decrease limit
625+
if (voltLimitIndex > 0) {
626+
// Have not reached lowest value
627+
voltLimitIndex--;
628+
}
629+
// Decrease load cont.
630+
if (lCRIndex > 0) {
631+
// Have not reached lowest value
632+
lCRIndex--;
633+
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[lCRIndex]);
634+
}
635+
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
636+
continue;
637+
}
610638

639+
ESP_LOGI(TAG, "Adjust Load: %d mV, Threshold: %d mV",
640+
INA260::voltage_mV, LOAD::VOLT_LIMITS_TABLE_mV[voltLimitIndex]);
641+
if (INA260::voltage_mV > LOAD::VOLT_LIMITS_TABLE_mV[voltLimitIndex]) {
642+
// Exceeded threshold voltage
643+
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[lCRIndex]);
644+
ESP_LOGI(TAG, "Set: %d", LOAD::RES_INDEX_TABLE[lCRIndex]);
645+
// Decrease limit
646+
if (voltLimitIndex > 0) {
647+
// Have not reached lowest value
648+
voltLimitIndex--;
649+
}
650+
// Decrease load cont.
651+
if (lCRIndex > 0) {
652+
// Have not reached lowest value
653+
lCRIndex--;
654+
load.setLoadGPIO(LOAD::RES_INDEX_TABLE[lCRIndex]);
655+
}
656+
} else {
657+
// Did not exceed threshold voltage
658+
ESP_LOGD(TAG, "No change");
659+
}
660+
#endif
611661
delay(RUN::TASK_INTERVALS::TI_ADJUST_LOAD_mS);
612662
}
613663
}

0 commit comments

Comments
 (0)