From 96d26b5c34c9ad51bd670e8ec0ce76caa7b83b6b Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Sun, 16 Jun 2019 14:46:33 +0200 Subject: [PATCH] Fixing stalled MCU if solar is connected before battery (the change in dcdc.cpp fixes the failed unit-test in previous commit, which didn't find min/max functions) --- src/dcdc.cpp | 8 ++++---- src/main.cpp | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dcdc.cpp b/src/dcdc.cpp index f45bd56..5335f25 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -23,6 +23,7 @@ #include // for time(NULL) function #include // for fabs function +#include // for min/max function #include extern log_data_t log_data; @@ -153,9 +154,9 @@ void dcdc_control(dcdc_t *dcdc, dc_bus_t *hs, dc_bus_t *ls) } static int startup_delay_counter = 0; + static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; // wait at least 100 ms for voltages to settle if (_dcdc_check_start_conditions(dcdc, ls, hs) && ls->voltage < dcdc->ls_voltage_max) { - // wait at least 100 ms for voltages to settle - if (startup_delay_counter >= max(CONTROL_FREQUENCY / 10, 1)) { + if (startup_delay_counter >= num_wait_calls) { // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents and stress on MOSFETs half_bridge_start(ls->voltage / (hs->voltage - 1)); printf("DC/DC buck mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100); @@ -165,8 +166,7 @@ void dcdc_control(dcdc_t *dcdc, dc_bus_t *hs, dc_bus_t *ls) } } else if (_dcdc_check_start_conditions(dcdc, hs, ls) && hs->voltage < dcdc->hs_voltage_max) { - // wait at least 100 ms for voltages to settle - if (startup_delay_counter >= max(CONTROL_FREQUENCY / 10, 1)) { + if (startup_delay_counter >= num_wait_calls) { // will automatically start with max. duty (0.97) if connected to a nanogrid not yet started up (zero voltage) half_bridge_start(ls->voltage / (hs->voltage + 1)); printf("DC/DC boost mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100); diff --git a/src/main.cpp b/src/main.cpp index 5d0d7a9..0410ae4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,6 +156,7 @@ int main() wait(2); // safety feature: be able to re-flash before starting control_timer_start(CONTROL_FREQUENCY); + wait(0.1); // necessary to prevent MCU from randomly getting stuck here if PV panel is connected before battery // the main loop is suitable for slow tasks like communication (even blocking wait allowed) time_t last_call = timestamp;