diff --git a/src/dcdc.cpp b/src/dcdc.cpp index c4477fa..9ddc101 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -15,7 +15,6 @@ */ #include "dcdc.h" -#include "config.h" #include "pcb.h" #include "log.h" @@ -30,11 +29,11 @@ extern LogData log_data; -void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv) +void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv, DcdcOperationMode mode) { dcdc->hv_bus = hv; dcdc->lv_bus = lv; - dcdc->mode = DCDC_MODE_INIT; + dcdc->mode = mode; dcdc->enabled = true; dcdc->state = DCDC_STATE_OFF; dcdc->ls_current_max = DCDC_CURRENT_MAX; @@ -56,7 +55,6 @@ void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv) int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) { float dcdc_power_new = dcdc->lv_bus->voltage * dcdc->lv_bus->current * dcdc_dir; - static int pwm_delta = 1; static uint32_t low_dcdc_power_count = 0; @@ -69,9 +67,9 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) int retval = 0; - //printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, i_max: %.2f, PWM: %.1f, chg_en: %d\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); + //printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, v_chg_target: %.2f, i_chg_max: %.2f, PWM: %.1f, chg_state: %d\n", + // dcdc_power_new, dcdc->power, in->voltage, out->voltage, in->current, out->current, out->chg_voltage_target, + // out->chg_current_max, half_bridge_get_duty_cycle() * 100.0, dcdc->state); 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 @@ -79,11 +77,6 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) // || (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 - 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" retval = 0; } @@ -113,9 +106,9 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) // start MPPT dcdc->state = DCDC_STATE_MPPT; if (dcdc->power > dcdc_power_new) { - pwm_delta = -pwm_delta; + dcdc->pwm_delta = -dcdc->pwm_delta; } - retval = pwm_delta; + retval = dcdc->pwm_delta; } // store the power BEFORE the executing the current change dcdc->power = dcdc_power_new; diff --git a/src/dcdc.h b/src/dcdc.h index 9289970..8b8b473 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -30,7 +30,7 @@ * * Defines which type of device is connected to the high side and low side ports */ -enum dcdc_operation_mode +enum DcdcOperationMode { MODE_MPPT_BUCK, ///< solar panel at high side port, battery / load at low side port (typical MPPT) MODE_MPPT_BOOST, ///< battery at high side port, solar panel at low side (e.g. e-bike charging) @@ -44,7 +44,7 @@ enum dcdc_operation_mode * * Allows to determine the current control state (off, CC, CV and MPPT) */ -enum dcdc_control_state +enum DcdcControlState { DCDC_STATE_OFF, ///< DC/DC switched off (low input power available or actively disabled) DCDC_STATE_MPPT, ///< Maximum Power Point Tracking @@ -59,7 +59,7 @@ enum dcdc_control_state * actual measurements and calibration parameters. */ typedef struct { - dcdc_operation_mode mode; ///< DC/DC mode (buck, boost or nanogrid) + DcdcOperationMode mode; ///< DC/DC mode (buck, boost or nanogrid) bool enabled; ///< Can be used to disable the DC/DC power stage uint16_t state; ///< Control state (off / MPPT / CC / CV) @@ -94,8 +94,9 @@ typedef struct { * @param dcdc DC/DC object * @param hv High voltage terminal (e.g. solar input for MPPT buck) * @param lv Low voltage terminal (e.g. battery output for MPPT buck) + * @param mode Operation mode (buck, boost or nanogrid) */ -void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv); +void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv, DcdcOperationMode mode); /** Check for valid start conditions of the DC/DC converter * diff --git a/src/main.cpp b/src/main.cpp index 08f4619..6ccd9ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -115,7 +115,7 @@ int main() #ifdef CHARGER_TYPE_PWM pwm_switch_init(&pwm_switch); #else // MPPT - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, DCDC_MODE_INIT); half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage #endif diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index 99a55f0..6f99ae8 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -4,6 +4,7 @@ #include "dcdc.h" #include "dc_bus.h" #include "log.h" +#include "half_bridge.h" #include #include @@ -19,25 +20,61 @@ static void init_structs_buck() dc_bus_init_solar(&hv_terminal, 10); hv_terminal.voltage = 20; hv_terminal.dis_voltage_start = 18; + hv_terminal.current = 0; battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100); battery_init_dc_bus(&lv_bus_int, &bat_conf, 1); lv_bus_int.voltage = 14; + lv_bus_int.current = 0; - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); + dcdc.temp_mosfets = 25; + dcdc.off_timestamp = 0; + dcdc.power = 0; + dcdc.pwm_delta = 1; + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BUCK); +} + +static void start_buck() +{ + init_structs_buck(); + half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97); + dcdc_control(&dcdc); + dcdc_control(&dcdc); + dcdc_control(&dcdc); // call multiple times because of startup delay + TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state); } static void init_structs_boost() { + half_bridge_stop(); + dc_bus_init_solar(&lv_bus_int, 10); lv_bus_int.voltage = 20; lv_bus_int.dis_voltage_start = 18; + lv_bus_int.current = 0; + lv_bus_int.power = 0; battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); battery_init_dc_bus(&hv_terminal, &bat_conf, 1); hv_terminal.voltage = 3.7 * 10; + hv_terminal.current = 0; + hv_terminal.power = 0; - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); + dcdc.temp_mosfets = 25; + dcdc.off_timestamp = 0; + dcdc.power = 0; + dcdc.pwm_delta = -1; + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BOOST); +} + +static void start_boost() +{ + init_structs_boost(); + half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97); + dcdc_control(&dcdc); + dcdc_control(&dcdc); + dcdc_control(&dcdc); // call multiple times because of startup delay + TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state); } void start_valid_mppt_buck() @@ -149,6 +186,207 @@ void no_boost_start_if_solar_voltage_low() TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } +// buck operation + +void buck_increasing_power() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); +} + +void buck_derating_output_voltage_too_high() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + lv_bus_int.voltage = lv_bus_int.chg_voltage_target + 0.1; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage + TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state); +} + +void buck_derating_output_current_too_high() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + lv_bus_int.current = lv_bus_int.chg_current_max + 0.1; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); + TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state); +} + +void buck_derating_input_voltage_too_low() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + hv_terminal.voltage = hv_terminal.dis_voltage_stop - 0.1; + lv_bus_int.current = 0.2; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); + TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state); +} + +void buck_derating_input_current_too_high() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + hv_terminal.current = hv_terminal.dis_current_max - 0.1; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); + TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state); +} + +void buck_derating_temperature_limits_exceeded() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + dcdc.temp_mosfets = 81; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); +} + +void buck_stop_input_power_too_low() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + for (int i = 0; i < 100; i++) { + dcdc_control(&dcdc); + } + TEST_ASSERT(half_bridge_enabled() == false); +} + +void buck_stop_high_voltage_emergency() +{ + start_buck(); + float pwm_before = half_bridge_get_duty_cycle(); + lv_bus_int.voltage = dcdc.ls_voltage_max + 0.1; + dcdc_control(&dcdc); + TEST_ASSERT(half_bridge_enabled() == false); +} + +void buck_correct_mppt_operation() +{ + start_buck(); + + lv_bus_int.current = 0.5; + dcdc_control(&dcdc); + float pwm1 = half_bridge_get_duty_cycle(); + lv_bus_int.current = 0.7; + dcdc_control(&dcdc); + float pwm2 = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm2 > pwm1); + + lv_bus_int.current = 0.6; // decrease power to make the direction turn around + dcdc_control(&dcdc); + float pwm3 = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm3 < pwm2); +} + +// boost operation + +void boost_increasing_power() +{ + // make sure that without changing anything, dcdc_control goes in direction of increasing power + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after < pwm_before); +} + +void boost_derating_output_voltage_too_high() +{ + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + hv_terminal.voltage = hv_terminal.chg_voltage_target + 0.5; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power +} + +void boost_derating_output_current_too_high() +{ + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + hv_terminal.current = hv_terminal.chg_current_max + 0.1; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); +} + +void boost_derating_input_voltage_too_low() +{ + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + lv_bus_int.voltage = lv_bus_int.dis_voltage_stop - 0.1; + lv_bus_int.current = 0.2; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); +} + +void boost_derating_input_current_too_high() +{ + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + lv_bus_int.current = lv_bus_int.dis_current_max - 0.1; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); +} + +void boost_derating_temperature_limits_exceeded() +{ + start_boost(); + float pwm_before = half_bridge_get_duty_cycle(); + dcdc.temp_mosfets = 81; + dcdc_control(&dcdc); + float pwm_after = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm_after > pwm_before); +} + +void boost_stop_input_power_too_low() +{ + start_boost(); + for (int i = 0; i < 100; i++) { + dcdc_control(&dcdc); + } + TEST_ASSERT(half_bridge_enabled() == false); +} + +void boost_stop_high_voltage_emergency() +{ + start_boost(); + hv_terminal.voltage = dcdc.hs_voltage_max + 0.1; + dcdc_control(&dcdc); + TEST_ASSERT(half_bridge_enabled() == false); +} + +void boost_correct_mppt_operation() +{ + start_boost(); + + lv_bus_int.current = -0.5; + dcdc_control(&dcdc); + float pwm1 = half_bridge_get_duty_cycle(); + lv_bus_int.current = -0.7; + dcdc_control(&dcdc); + float pwm2 = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm2 < pwm1); + + lv_bus_int.current = -0.6; // decrease power to make the direction turn around + dcdc_control(&dcdc); + float pwm3 = half_bridge_get_duty_cycle(); + TEST_ASSERT(pwm3 > pwm2); +} + void dcdc_tests() { UNITY_BEGIN(); @@ -185,7 +423,27 @@ void dcdc_tests() // 5. Check DC/DC control after being started - // TODO + // buck mode + RUN_TEST(buck_increasing_power); + RUN_TEST(buck_derating_output_voltage_too_high); + RUN_TEST(buck_derating_output_current_too_high); + RUN_TEST(buck_derating_input_voltage_too_low); + RUN_TEST(buck_derating_input_current_too_high); + RUN_TEST(buck_derating_temperature_limits_exceeded); + RUN_TEST(buck_stop_input_power_too_low); + RUN_TEST(buck_stop_high_voltage_emergency); + RUN_TEST(buck_correct_mppt_operation); + + // boost mode + RUN_TEST(boost_increasing_power); + RUN_TEST(boost_derating_output_voltage_too_high); + RUN_TEST(boost_derating_output_current_too_high); + RUN_TEST(boost_derating_input_voltage_too_low); + RUN_TEST(boost_derating_input_current_too_high); + RUN_TEST(boost_derating_temperature_limits_exceeded); + RUN_TEST(boost_stop_input_power_too_low); + RUN_TEST(boost_stop_high_voltage_emergency); + RUN_TEST(boost_correct_mppt_operation); UNITY_END(); } \ No newline at end of file