From cb206b68644d251a453e144c4dcaef9b72e40a5a Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Wed, 9 Oct 2019 11:24:51 +0200 Subject: [PATCH] DC/DC startup check reworked and unit-tests added --- src/dcdc.cpp | 91 +++++++++++++-------- src/dcdc.h | 26 ++++-- test/main.cpp | 2 + test/tests.h | 2 + test/tests_dcdc.cpp | 191 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 272 insertions(+), 40 deletions(-) create mode 100644 test/tests_dcdc.cpp diff --git a/src/dcdc.cpp b/src/dcdc.cpp index d8fcd0f..6151c69 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -39,6 +39,7 @@ void dcdc_init(Dcdc *dcdc) dcdc->ls_current_min = 0.05; // A if lower, charger is switched off dcdc->hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX; // V dcdc->ls_voltage_max = LOW_SIDE_VOLTAGE_MAX; // V + dcdc->ls_voltage_min = 9.0; //dcdc->offset_voltage_start = 4.0; // V charging switched on if Vsolar > Vbat + offset //dcdc->offset_voltage_stop = 1.0; // V charging switched off if Vsolar < Vbat + offset dcdc->restart_interval = 60; // s --> when should we retry to start charging after low solar power cut-off? @@ -49,7 +50,7 @@ void dcdc_init(Dcdc *dcdc) // returns if output power should be increased (1), decreased (-1) or switched off (0) /** * @param dcdc_dir 1 => current flows from ls to hs through the dcdc, -1 opposite - */ + */ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) { float dcdc_power_new = dcdc->ls_voltage * dcdc->ls_current * dcdc_dir; @@ -72,16 +73,16 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) if ( (out->chg_allowed == false && out->dis_allowed == false) // we are not supposed to transfer any energy into the outputs || low_dcdc_power_count > 100 // the transfered energy drops below a threshold for more than 10s, we make a small averaging here - || in->dis_allowed == false // we are not allowed to take energy from the input + || in->dis_allowed == false // we are not allowed to take energy from the input // || (in->voltage < in->dis_voltage_stop && out->current < 0.1) // input voltage is too low and we have no output consumption (battery or grid) - ) + ) { -#if 0 +#if 0 printf("P: %.2f, P_prev: %.2f, i_v: %.2f, o_v: %.2f, i_cur: %.2f, o_cur: %.2f, o_chg_cur_max: %.2f, PWM: %.1f, o_chg_en: %d o_dis_en: %d, i_dis_en: %d, i_v_dis_volt_stop: %.2f, dc_ls_cur: %.2f\n", dcdc_power_new, dcdc->power, in->voltage, out->voltage, in->current, out->current, out->chg_current_max, half_bridge_get_duty_cycle() * 100.0, out->chg_allowed, out->dis_allowed, in->dis_allowed, in->dis_voltage_stop, dcdc->ls_current); -#endif - low_dcdc_power_count = 0; // reset low dcdc current "timer" +#endif + low_dcdc_power_count = 0; // reset low dcdc current "timer" retval = 0; } else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current) // output voltage above target @@ -119,35 +120,55 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) return retval; } -bool _dcdc_check_start_conditions(Dcdc *dcdc, DcBus *out, DcBus *in) +int dcdc_check_start_conditions(Dcdc *dcdc, DcBus *hv, DcBus *lv) { - return dcdc->enabled == true - && out->chg_allowed == true - && out->voltage < out->chg_voltage_target - && out->voltage > out->chg_voltage_min - && in->dis_allowed == true - && in->voltage > in->dis_voltage_start - //&& dcdc->hs_voltage - dcdc->ls_voltage > dcdc->offset_voltage_start - && time(NULL) > (dcdc->off_timestamp + dcdc->restart_interval); + if (dcdc->enabled == false || + hv->voltage > dcdc->hs_voltage_max || // also critical for buck mode because of ringing + lv->voltage > dcdc->ls_voltage_max || + lv->voltage < dcdc->ls_voltage_min || + time(NULL) < (dcdc->off_timestamp + dcdc->restart_interval)) + { + return 0; // no energy transfer allowed + } + + if (lv->chg_current_max > 0 && + lv->voltage < lv->chg_voltage_target && + lv->voltage > lv->chg_voltage_min && + hv->dis_current_max < 0 && + hv->voltage > hv->dis_voltage_start) + { + return 1; // start in buck mode allowed + } + + if (hv->chg_current_max > 0 && + hv->voltage < hv->chg_voltage_target && + hv->voltage > hv->chg_voltage_min && + lv->dis_current_max < 0 && + lv->voltage > lv->dis_voltage_start) + { + return -1; // start in boost mode allowed + } + + return 0; } -void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls) +void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) { if (half_bridge_enabled()) { int step; - if (dcdc->mode == MODE_MPPT_BUCK || dcdc->ls_current > 0.1 ) { + if (dcdc->mode == MODE_MPPT_BUCK || dcdc->ls_current > 0.1 ) { // always in buck mode or when boost or nano grid are receiving energy from high side enegry producer // ( grid [nanogrid] or battery [boost] ) // for low side energy consumers (battery and/or load). //printf("-"); - step = _dcdc_output_control(dcdc, ls, hs, 1); + step = _dcdc_output_control(dcdc, lv, hv, 1); half_bridge_duty_cycle_step(step); } else { //printf("+"); - step = _dcdc_output_control(dcdc, hs, ls, -1); + step = _dcdc_output_control(dcdc, hv, lv, -1); half_bridge_duty_cycle_step(-step); } @@ -157,7 +178,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls) dcdc->off_timestamp = time(NULL); printf("DC/DC stop.\n"); } - else if (ls->voltage > dcdc->ls_voltage_max || hs->voltage > dcdc->hs_voltage_max) { + else if (lv->voltage > dcdc->ls_voltage_max || hv->voltage > dcdc->hs_voltage_max) { half_bridge_stop(); dcdc->state = DCDC_STATE_OFF; dcdc->off_timestamp = time(NULL); @@ -187,21 +208,23 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *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) { + + int startup_mode = dcdc_check_start_conditions(dcdc, hv, lv); + if (startup_mode == 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); + half_bridge_start(lv->voltage / (hv->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; } } - else if (_dcdc_check_start_conditions(dcdc, hs, ls) && hs->voltage < dcdc->hs_voltage_max) { + else if (startup_mode == -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); + half_bridge_start(lv->voltage / (hv->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -213,7 +236,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls) } } -void dcdc_test(Dcdc *dcdc, DcBus *hs, DcBus *ls) +void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv) { if (half_bridge_enabled()) { if (half_bridge_get_duty_cycle() > 0.5) { @@ -223,21 +246,23 @@ void dcdc_test(Dcdc *dcdc, DcBus *hs, DcBus *ls) else { 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) { + + int startup_mode = dcdc_check_start_conditions(dcdc, hv, lv); + if (startup_mode == 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); + half_bridge_start(lv->voltage / (hv->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; } } - else if (_dcdc_check_start_conditions(dcdc, hs, ls) && hs->voltage < dcdc->hs_voltage_max) { + else if (startup_mode == -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); + half_bridge_start(lv->voltage / (hv->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; diff --git a/src/dcdc.h b/src/dcdc.h index fde8eab..438e3d6 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -78,6 +78,7 @@ typedef struct { float ls_current_min; ///< Minimum low-side current (if lower, charger is switched off) float hs_voltage_max; ///< Maximum high-side voltage float ls_voltage_max; ///< Maximum low-side voltage + float ls_voltage_min; ///< Minimum low-side voltage, e.g. for driver supply // calibration parameters //float offset_voltage_start; // V charging switched on if Vsolar > Vbat + offset @@ -94,21 +95,32 @@ typedef struct { */ void dcdc_init(Dcdc *dcdc); -/** Main control function for the DC/DC converter +/** Check for valid start conditions of the DC/DC converter * * @param dcdc DC/DC type description - * @param high_side High-side power port (e.g. solar input for typical MPPT charge controller application) - * @param low_side Low-side power port (e.g. battery output for typical MPPT charge controller application) + * @param hv High voltage terminal (e.g. solar input for MPPT buck) + * @param lv Low voltage terminal (e.g. battery output for MPPT buck) + * @returns 1 for buck mode, -1 for boost mode and 0 for invalid conditions */ -void dcdc_control(Dcdc *dcdc, DcBus *high_side, DcBus *low_side); +int dcdc_check_start_conditions(Dcdc *dcdc, DcBus *hv, DcBus *lv); + +/** Main control function for the DC/DC converter + * + * If DC/DC is off, this function checks start conditions and starts conversion if possible. + * + * @param dcdc DC/DC type description + * @param hv High voltage side terminal (e.g. solar input for typical MPPT charge controller application) + * @param lv Low voltage side terminal (e.g. battery output for typical MPPT charge controller application) + */ +void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv); /** Test mode for DC/DC, ramping up to 50% duty cycle * * @param dcdc DC/DC type description - * @param high_side High-side power port (e.g. solar input for typical MPPT charge controller application) - * @param low_side Low-side power port (e.g. battery output for typical MPPT charge controller application) + * @param hv High voltage side terminal (e.g. solar input for typical MPPT charge controller application) + * @param lv Low voltage side terminal (e.g. battery output for typical MPPT charge controller application) */ -void dcdc_test(Dcdc *dcdc, DcBus *high_side, DcBus *low_side); +void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv); /** Prevent overcharging of battery in case of shorted HS MOSFET * diff --git a/test/main.cpp b/test/main.cpp index f1e1d8a..54681d7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -38,6 +38,8 @@ int main() bat_charger_tests(); dc_bus_tests(); half_brigde_tests(); + dcdc_tests(); + // TODO //battery_tests(); //load_tests(); diff --git a/test/tests.h b/test/tests.h index a57368e..4e22289 100644 --- a/test/tests.h +++ b/test/tests.h @@ -8,3 +8,5 @@ void adc_tests(); void dc_bus_tests(); void half_brigde_tests(); + +void dcdc_tests(); diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp new file mode 100644 index 0000000..cd547de --- /dev/null +++ b/test/tests_dcdc.cpp @@ -0,0 +1,191 @@ + +#include "tests.h" + +#include "dcdc.h" +#include "dc_bus.h" +#include "log.h" + +#include +#include +#include + +extern Dcdc dcdc; +extern DcBus hv_bus; +extern DcBus lv_bus; +extern BatConf bat_conf; + +static void init_structs_buck() +{ + dc_bus_init_solar(&hv_bus, 10); + hv_bus.voltage = 20; + hv_bus.dis_voltage_start = 18; + + battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100); + battery_init_dc_bus(&lv_bus, &bat_conf, 1); + lv_bus.voltage = 14; + + dcdc_init(&dcdc); +} + +static void init_structs_boost() +{ + dc_bus_init_solar(&lv_bus, 10); + lv_bus.voltage = 20; + lv_bus.dis_voltage_start = 18; + + battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); + battery_init_dc_bus(&hv_bus, &bat_conf, 1); + hv_bus.voltage = 3.7 * 10; + + dcdc_init(&dcdc); +} + +void start_valid_mppt_buck() +{ + init_structs_buck(); + TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void start_valid_mppt_boost() +{ + init_structs_boost(); + TEST_ASSERT_EQUAL(-1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_start_before_restart_delay() +{ + init_structs_buck(); + dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + dcdc.off_timestamp = time(NULL) - dcdc.restart_interval; + TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_start_if_dcdc_disabled() +{ + init_structs_buck(); + dcdc.enabled = false; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_start_if_dcdc_lv_voltage_low() +{ + init_structs_buck(); + lv_bus.voltage = dcdc.ls_voltage_min - 0.5; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +// buck + +void no_buck_start_if_bat_voltage_low() +{ + init_structs_buck(); + lv_bus.voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_buck_start_if_bat_voltage_high() +{ + init_structs_buck(); + lv_bus.voltage = bat_conf.voltage_topping + 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_buck_start_if_bat_chg_not_allowed() +{ + init_structs_buck(); + lv_bus.chg_current_max = 0; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_buck_start_if_solar_voltage_high() +{ + init_structs_buck(); + hv_bus.voltage = dcdc.hs_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_buck_start_if_solar_voltage_low() +{ + init_structs_buck(); + hv_bus.voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +// boost + +void no_boost_start_if_bat_voltage_low() +{ + init_structs_boost(); + hv_bus.voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_boost_start_if_bat_voltage_high() +{ + init_structs_boost(); + hv_bus.voltage = bat_conf.voltage_topping + 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_boost_start_if_bat_chg_not_allowed() +{ + init_structs_boost(); + hv_bus.chg_current_max = 0; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_boost_start_if_solar_voltage_high() +{ + init_structs_boost(); + lv_bus.voltage = dcdc.ls_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void no_boost_start_if_solar_voltage_low() +{ + init_structs_boost(); + lv_bus.voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); +} + +void dcdc_tests() +{ + UNITY_BEGIN(); + + // 1. Check of general (re)start conditions + + // check if initialization of test is correct and we start at all + RUN_TEST(start_valid_mppt_buck); + RUN_TEST(start_valid_mppt_boost); + + RUN_TEST(no_start_before_restart_delay); + RUN_TEST(no_start_if_dcdc_disabled); + RUN_TEST(no_start_if_dcdc_lv_voltage_low); + + // 2. Check startup for MPPT buck converter scenario + + RUN_TEST(no_buck_start_if_bat_voltage_low); + RUN_TEST(no_buck_start_if_bat_voltage_high); + RUN_TEST(no_buck_start_if_bat_chg_not_allowed); + RUN_TEST(no_buck_start_if_solar_voltage_high); + RUN_TEST(no_buck_start_if_solar_voltage_low); + + // 3. Check startup for MPPT boost converter scenario + + RUN_TEST(no_boost_start_if_bat_voltage_low); + RUN_TEST(no_boost_start_if_bat_voltage_high); + RUN_TEST(no_boost_start_if_bat_chg_not_allowed); + RUN_TEST(no_boost_start_if_solar_voltage_high); + RUN_TEST(no_boost_start_if_solar_voltage_low); + + // 4. Check startup for nanogrid scenario + + // TODO + + // 5. Check DC/DC control after being started + + // TODO + + UNITY_END(); +} \ No newline at end of file