From f879dc9c5d7ee024347894888db703f5f466c2bf Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Tue, 22 Oct 2019 18:23:19 +0200 Subject: [PATCH] DC bus integrated into PowerPort class Reason: Previous idea with storing the voltage only once and not in every port doesn't work well for droop settings, e.g. the DC/DC output port doesn't know the actual battery current, which should be used to calculate the droop caued by wire resistance. --- src/adc_dma.cpp | 29 +++--- src/bat_charger.cpp | 48 +++++----- src/bat_charger.h | 3 +- src/config.h_template | 3 - src/data_objects.cpp | 10 +-- src/dcdc.cpp | 46 +++++----- src/load.cpp | 7 +- src/load.h | 2 - src/log.cpp | 10 +-- src/main.cpp | 20 ++--- src/main.h | 6 +- src/power_port.cpp | 17 ++-- src/power_port.h | 28 +----- src/pwm_switch.cpp | 8 +- src/uext/uext_oled.cpp | 6 +- test/main.cpp | 20 ++--- test/tests.h | 2 +- test/tests_adc.cpp | 10 +-- test/tests_bat_charger.cpp | 42 ++++----- test/tests_dcdc.cpp | 90 +++++++++---------- test/tests_log.cpp | 8 +- ...{tests_dc_bus.cpp => tests_power_port.cpp} | 10 +-- 22 files changed, 191 insertions(+), 234 deletions(-) rename test/{tests_dc_bus.cpp => tests_power_port.cpp} (82%) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 73fbcdc..354927c 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -94,20 +94,23 @@ void update_measurements() // rely on LDO accuracy //int vcc = 3300; - // calculate LS voltage first, as it is needed for HS voltage calculation for PWM charge controller - lv_bus.voltage = + // calculate lower voltage first, as it is needed for PWM terminal voltage calculation + lv_terminal.voltage = (float)(((adc_filtered[ADC_POS_V_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_V_BAT / 1000.0; + load_terminal.voltage = lv_terminal.voltage; #if FEATURE_DCDC_CONVERTER - hv_bus.voltage = + hv_terminal.voltage = (float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_V_SOLAR / 1000.0; + dcdc_lv_port.voltage = lv_terminal.voltage; #endif #if FEATURE_PWM_SWITCH - pwm_bus.voltage = lv_bus.voltage - vcc * ADC_OFFSET_V_SOLAR / 1000.0 - + pwm_terminal.voltage = lv_terminal.voltage - vcc * ADC_OFFSET_V_SOLAR / 1000.0 - (float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_V_SOLAR / 1000.0; + pwm_port_int.voltage = lv_terminal.voltage; #endif load_terminal.current = @@ -121,22 +124,20 @@ void update_measurements() ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset); pwm_port_int.current = pwm_terminal.current; lv_terminal.current = -pwm_port_int.current - load_terminal.current; - pwm_terminal.power = pwm_bus.voltage * pwm_terminal.current; + pwm_terminal.power = pwm_terminal.voltage * pwm_terminal.current; #endif #if FEATURE_DCDC_CONVERTER - dcdc_port_lv.current = + dcdc_lv_port.current = (float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_DCDC / 1000.0 + solar_current_offset; - lv_terminal.current = dcdc_port_lv.current - load_terminal.current; - hv_terminal.current = -dcdc_port_lv.current * lv_bus.voltage / hv_bus.voltage; - dcdc_port_hv.current = hv_terminal.current; + lv_terminal.current = dcdc_lv_port.current - load_terminal.current; + hv_terminal.current = -dcdc_lv_port.current * lv_terminal.voltage / hv_terminal.voltage; - dcdc_port_lv.power = lv_bus.voltage * dcdc_port_lv.current; - dcdc_port_hv.power = hv_bus.voltage * dcdc_port_hv.current; - hv_terminal.power = hv_bus.voltage * hv_terminal.current; + dcdc_lv_port.power = dcdc_lv_port.voltage * dcdc_lv_port.current; + hv_terminal.power = hv_terminal.voltage * hv_terminal.current; #endif - lv_terminal.power = lv_bus.voltage * lv_terminal.current; - load_terminal.power = lv_bus.voltage * load_terminal.current; + lv_terminal.power = lv_terminal.voltage * lv_terminal.current; + load_terminal.power = load_terminal.voltage * load_terminal.current; /** \todo Improved (faster) temperature calculation: https://www.embeddedrelated.com/showarticle/91.php diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index c9b4bf4..61b3b66 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -217,8 +217,8 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char void Charger::detect_num_batteries(BatConf *bat) { - if (bus->voltage > bat->voltage_absolute_min * 2 && - bus->voltage < bat->voltage_absolute_max * 2) { + if (port->voltage > bat->voltage_absolute_min * 2 && + port->voltage < bat->voltage_absolute_max * 2) { num_batteries = 2; printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } @@ -232,7 +232,7 @@ void Charger::update_soc(BatConf *bat_conf) static int soc_filtered = 0; // SOC / 100 for better filtering if (fabs(port->current) < 0.2) { - int soc_new = (int)((bus->voltage - bat_conf->ocv_empty) / + int soc_new = (int)((port->voltage - bat_conf->ocv_empty) / (bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0); if (soc_new > 500 && soc_filtered == 0) { @@ -267,7 +267,7 @@ void Charger::discharge_control(BatConf *bat_conf) { // load output state is defined by battery bus dis_allowed flag if (port->dis_current_limit < 0 // discharging allowed - && bus->voltage < num_batteries * + && port->voltage < num_batteries * (bat_conf->voltage_load_disconnect - port->current * port->dis_droop_res)) { // low state of charge @@ -298,7 +298,7 @@ void Charger::discharge_control(BatConf *bat_conf) port->dis_current_limit = 0; } - if (bus->voltage >= num_batteries * + if (port->voltage >= num_batteries * (bat_conf->voltage_load_reconnect - port->current * port->dis_droop_res) && bat_temperature < bat_conf->discharge_temp_max - 1 && bat_temperature > bat_conf->discharge_temp_min + 1) @@ -322,12 +322,12 @@ void Charger::charge_control(BatConf *bat_conf) // state machine switch (state) { case CHG_STATE_IDLE: { - if (bus->voltage < bat_conf->voltage_recharge * num_batteries + if (port->voltage < bat_conf->voltage_recharge * num_batteries && (time(NULL) - time_state_changed) > bat_conf->time_limit_recharge && bat_temperature < bat_conf->charge_temp_max - 1 && bat_temperature > bat_conf->charge_temp_min + 1) { - bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); port->chg_current_limit = bat_conf->charge_current_max; full = false; @@ -337,11 +337,11 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_BULK: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage > num_batteries * - (bus->chg_voltage_target - port->current * port->chg_droop_res)) + if (port->voltage > num_batteries * + (port->chg_voltage_target - port->current * port->chg_droop_res)) { enter_state(CHG_STATE_TOPPING); } @@ -349,11 +349,11 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_TOPPING: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage >= num_batteries * - (bus->chg_voltage_target - port->current * port->chg_droop_res)) + if (port->voltage >= num_batteries * + (port->chg_voltage_target - port->current * port->chg_droop_res)) { time_voltage_limit_reached = time(NULL); } @@ -375,12 +375,12 @@ void Charger::charge_control(BatConf *bat_conf) num_deep_discharges - deep_dis_last_equalization >= bat_conf->equalization_trigger_deep_cycles)) { - bus->chg_voltage_target = bat_conf->equalization_voltage; + port->chg_voltage_target = bat_conf->equalization_voltage; port->chg_current_limit = bat_conf->equalization_current_limit; enter_state(CHG_STATE_EQUALIZATION); } else if (bat_conf->trickle_enabled) { - bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); enter_state(CHG_STATE_TRICKLE); } @@ -393,11 +393,11 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_TRICKLE: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage >= num_batteries * - (bus->chg_voltage_target - port->current * port->chg_droop_res)) + if (port->voltage >= num_batteries * + (port->chg_voltage_target - port->current * port->chg_droop_res)) { time_voltage_limit_reached = time(NULL); } @@ -414,7 +414,7 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_EQUALIZATION: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = num_batteries * (bat_conf->equalization_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->equalization_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); // current or time limit for equalization reached @@ -427,7 +427,7 @@ void Charger::charge_control(BatConf *bat_conf) discharged_Ah = 0; // reset coulomb counter again if (bat_conf->trickle_enabled) { - bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); enter_state(CHG_STATE_TRICKLE); } @@ -445,15 +445,15 @@ void battery_init_dc_bus(PowerPort *port, BatConf *bat, unsigned int num_batteri { unsigned int n = (num_batteries == 2 ? 2 : 1); // only 1 or 2 allowed - port->bus->dis_voltage_start = bat->voltage_load_reconnect * n; - port->bus->dis_voltage_stop = bat->voltage_load_disconnect * n; + port->dis_voltage_start = bat->voltage_load_reconnect * n; + port->dis_voltage_stop = bat->voltage_load_disconnect * n; port->dis_current_limit = -bat->discharge_current_max; // negative sign for compensation of actual resistance port->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n; - port->bus->chg_voltage_target = bat->topping_voltage * n; - port->bus->chg_voltage_min = bat->voltage_absolute_min * n; + port->chg_voltage_target = bat->topping_voltage * n; + port->chg_voltage_min = bat->voltage_absolute_min * n; port->chg_current_limit = bat->charge_current_max; // negative sign for compensation of actual resistance diff --git a/src/bat_charger.h b/src/bat_charger.h index c06d6dc..de4d737 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -236,10 +236,9 @@ class Charger { public: Charger(PowerPort *pwr_port): - port(pwr_port), bus(pwr_port->bus) {}; + port(pwr_port) {}; PowerPort *port; - DcBus *bus; unsigned int state; ///< Current charger state (see enum ChargerState) diff --git a/src/config.h_template b/src/config.h_template index f6f233f..393f96b 100644 --- a/src/config.h_template +++ b/src/config.h_template @@ -26,10 +26,7 @@ #define DCDC_MODE_INIT MODE_MPPT_BUCK //#define DCDC_MODE_INIT MODE_NANOGRID -#define SOLAR_BUS hv_bus #define SOLAR_TERMINAL hv_terminal - -#define BATTERY_BUS lv_bus #define BATTERY_TERMINAL lv_terminal // basic battery configuration diff --git a/src/data_objects.cpp b/src/data_objects.cpp index fd50de3..f2c851f 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -123,12 +123,8 @@ const data_object_t data_objects[] = { {0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t // battery related data objects - {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(BATTERY_BUS.voltage), "Bat_V"}, // TODO: reference to bat_terminal -#if FEATURE_PWM_SWITCH - {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(pwm_bus.voltage), "Solar_V"}, -#else - {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(SOLAR_BUS.voltage), "Solar_V"}, -#endif + {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.voltage), "Bat_V"}, + {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.voltage), "Solar_V"}, {0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.current), "Bat_A"}, {0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.current), "Load_A"}, {0x74, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(charger.bat_temperature), "Bat_degC"}, @@ -142,7 +138,7 @@ const data_object_t data_objects[] = { {0x79, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"}, #endif {0x7A, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.current), "Solar_A"}, - {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(BATTERY_BUS.chg_voltage_target), "BatTarget_V"}, // TODO: reference to bat_terminal + {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.chg_voltage_target), "BatTarget_V"}, {0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.chg_current_limit), "BatTarget_A"}, {0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.power), "Bat_W"}, {0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.power), "Solar_W"}, diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 6048889..0df0b3f 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -83,8 +83,8 @@ int Dcdc::duty_cycle_delta() #if 0 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", - out->power, power_prev, in->bus->voltage, out->bus->voltage, in->current, out->current, - out->bus->chg_voltage_target, out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0, + out->power, power_prev, in->voltage, out->voltage, in->current, out->current, + out->chg_voltage_target, out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0, state); #endif @@ -93,7 +93,7 @@ int Dcdc::duty_cycle_delta() { pwr_inc_goal = 0; // switch off } - else if (out->bus->voltage > (out->bus->chg_voltage_target - out->chg_droop_res * out->current)) + else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current)) { // output voltage target reached state = DCDC_STATE_CV; @@ -107,14 +107,14 @@ int Dcdc::duty_cycle_delta() } else if (fabs(lvs->current) > ls_current_max // current above hardware maximum || temp_mosfets > 80 // temperature limits exceeded - || (in->bus->voltage < (in->bus->dis_voltage_start - in->dis_droop_res * in->current) + || (in->voltage < (in->dis_voltage_start - in->dis_droop_res * in->current) && out->current > 0.1) // input voltage below limit || in->current < in->dis_current_limit) // input current (negative signs) limit exceeded { state = DCDC_STATE_DERATING; pwr_inc_goal = -1; // decrease output power } - else if (out->power < output_power_min && out->bus->voltage < out->bus->dis_voltage_start) + else if (out->power < output_power_min && out->voltage < out->dis_voltage_start) { // no load condition (e.g. start-up of nanogrid) --> raise voltage pwr_inc_goal = 1; // increase output power @@ -135,28 +135,28 @@ int Dcdc::duty_cycle_delta() int Dcdc::check_start_conditions() { if (enabled == false || - hvs->bus->voltage > hs_voltage_max || // also critical for buck mode because of ringing - lvs->bus->voltage > ls_voltage_max || - lvs->bus->voltage < ls_voltage_min || + hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing + lvs->voltage > ls_voltage_max || + lvs->voltage < ls_voltage_min || time(NULL) < (off_timestamp + restart_interval)) { return 0; // no energy transfer allowed } if (lvs->chg_current_limit > 0 && - lvs->bus->voltage < lvs->bus->chg_voltage_target && - lvs->bus->voltage > lvs->bus->chg_voltage_min && + lvs->voltage < lvs->chg_voltage_target && + lvs->voltage > lvs->chg_voltage_min && hvs->dis_current_limit < 0 && - hvs->bus->voltage > hvs->bus->dis_voltage_start) + hvs->voltage > hvs->dis_voltage_start) { return 1; // start in buck mode allowed } if (hvs->chg_current_limit > 0 && - hvs->bus->voltage < hvs->bus->chg_voltage_target && - hvs->bus->voltage > hvs->bus->chg_voltage_min && + hvs->voltage < hvs->chg_voltage_target && + hvs->voltage > hvs->chg_voltage_min && lvs->dis_current_limit < 0 && - lvs->bus->voltage > lvs->bus->dis_voltage_start) + lvs->voltage > lvs->dis_voltage_start) { return -1; // start in boost mode allowed } @@ -176,7 +176,7 @@ void Dcdc::control() off_timestamp = time(NULL); printf("DC/DC stop (low power).\n"); } - else if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) { + else if (lvs->voltage > ls_voltage_max || hvs->voltage > hs_voltage_max) { half_bridge_stop(); state = DCDC_STATE_OFF; off_timestamp = time(NULL); @@ -216,9 +216,9 @@ void Dcdc::control() power_good_timestamp = time(NULL); // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents // and stress on MOSFETs - half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1)); + half_bridge_start(lvs->voltage / (hvs->voltage - 1)); printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", - hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -229,9 +229,9 @@ void Dcdc::control() power_good_timestamp = time(NULL); // will automatically start with max. duty (0.97) if connected to a nanogrid not // yet started up (zero voltage) - half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1)); + half_bridge_start(lvs->voltage / (hvs->voltage + 1)); printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", - hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -261,9 +261,9 @@ void Dcdc::test() 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(lvs->bus->voltage / (hvs->bus->voltage - 1)); + half_bridge_start(lvs->voltage / (hvs->voltage - 1)); printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", - hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -273,9 +273,9 @@ void Dcdc::test() 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(lvs->bus->voltage / (hvs->bus->voltage + 1)); + half_bridge_start(lvs->voltage / (hvs->voltage + 1)); printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", - hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; diff --git a/src/load.cpp b/src/load.cpp index 02887e3..adb5ae5 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -192,7 +192,6 @@ void LoadOutput::usb_set(bool enabled) {;} LoadOutput::LoadOutput(PowerPort *pwr_port) { port = pwr_port; - bus = pwr_port->bus; current_max = LOAD_CURRENT_MAX; enabled_target = true; usb_enabled_target = true; @@ -296,7 +295,7 @@ void LoadOutput::state_machine() } break; case LOAD_STATE_OFF_OVERVOLTAGE: - if (bus->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis? + if (port->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis? switch_state = LOAD_STATE_DISABLED; // switch to normal mode again } break; @@ -335,8 +334,8 @@ void LoadOutput::control(float load_max_voltage) } static int debounce_counter = 0; - if (bus->voltage > load_max_voltage || - bus->voltage > LOW_SIDE_VOLTAGE_MAX) + if (port->voltage > load_max_voltage || + port->voltage > LOW_SIDE_VOLTAGE_MAX) { debounce_counter++; if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag diff --git a/src/load.h b/src/load.h index 441e588..f198784 100644 --- a/src/load.h +++ b/src/load.h @@ -81,8 +81,6 @@ public: uint16_t switch_state; ///< Current state of load output switch uint16_t usb_state; ///< Current state of USB output - DcBus *bus; ///< Pointer to internal DC bus where the load output - ///< current is coming from PowerPort *port; ///< Pointer to DC bus containting actual voltage and current ///< measurement of (external) load output terminal diff --git a/src/log.cpp b/src/log.cpp index 79b3c4a..525a7ed 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -39,7 +39,7 @@ void log_update_energy(LogData *log_data) static uint32_t bat_chg_total_Wh_prev = log_data->bat_chg_total_Wh; static uint32_t bat_dis_total_Wh_prev = log_data->bat_dis_total_Wh; - if (solar_terminal.bus->voltage < bat_terminal.bus->voltage) { + if (solar_terminal.voltage < bat_terminal.voltage) { seconds_zero_solar += 1; } else { @@ -72,12 +72,12 @@ void log_update_energy(LogData *log_data) void log_update_min_max_values(LogData *log_data) { - if (bat_terminal.bus->voltage > log_data->battery_voltage_max) { - log_data->battery_voltage_max = bat_terminal.bus->voltage; + if (bat_terminal.voltage > log_data->battery_voltage_max) { + log_data->battery_voltage_max = bat_terminal.voltage; } - if (solar_terminal.bus->voltage > log_data->solar_voltage_max) { - log_data->solar_voltage_max = solar_terminal.bus->voltage; + if (solar_terminal.voltage > log_data->solar_voltage_max) { + log_data->solar_voltage_max = solar_terminal.voltage; } #if FEATURE_DCDC_CONVERTER diff --git a/src/main.cpp b/src/main.cpp index 797b721..c24866a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,21 +42,17 @@ #include "thingset_serial.h" // UART or USB serial communication #include "thingset_can.h" // CAN bus communication -DcBus lv_bus; -PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) +PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) #if FEATURE_DCDC_CONVERTER -DcBus hv_bus; -PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_port_hv(&hv_bus); // internal high voltage side port of DC/DC converter -PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter -Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT); +PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT); #endif #if FEATURE_PWM_SWITCH -DcBus pwm_bus; -PowerPort pwm_terminal(&pwm_bus); // external terminal of PWM switch port (normally solar) -PowerPort pwm_port_int(&lv_bus); // internal side of PWM switch +PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar) +PowerPort pwm_port_int; // internal side of PWM switch PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int); PowerPort &solar_terminal = pwm_terminal; #else @@ -64,7 +60,7 @@ PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h #endif #if FEATURE_LOAD_OUTPUT -PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) +PowerPort load_terminal; // load terminal (also connected to lv_bus) LoadOutput load(&load_terminal); #endif @@ -152,7 +148,7 @@ int main() charger.charge_control(&bat_conf); #if FEATURE_DCDC_CONVERTER - update_dcdc_current_targets(&dcdc_port_lv, &bat_terminal, &load_terminal); + update_dcdc_current_targets(&dcdc_lv_port, &bat_terminal, &load_terminal); #endif load.state_machine(); diff --git a/src/main.h b/src/main.h index c6d10ad..a7b8558 100644 --- a/src/main.h +++ b/src/main.h @@ -26,20 +26,16 @@ #include "load.h" #include "pcb.h" -extern DcBus lv_bus; extern PowerPort lv_terminal; extern PowerPort load_terminal; #if FEATURE_DCDC_CONVERTER -extern DcBus hv_bus; extern PowerPort hv_terminal; -extern PowerPort dcdc_port_hv; -extern PowerPort dcdc_port_lv; +extern PowerPort dcdc_lv_port; extern Dcdc dcdc; #endif #if FEATURE_PWM_SWITCH -extern DcBus pwm_bus; extern PowerPort pwm_terminal; extern PowerPort pwm_port_int; extern PwmSwitch pwm_switch; diff --git a/src/power_port.cpp b/src/power_port.cpp index d1a1f85..eba3739 100644 --- a/src/power_port.cpp +++ b/src/power_port.cpp @@ -22,8 +22,8 @@ void PowerPort::init_solar() chg_current_limit = 0; // no current towards solar panel allowed // also initialize the connected bus - bus->dis_voltage_start = 16.0; - bus->dis_voltage_stop = 14.0; + dis_voltage_start = 16.0; + dis_voltage_stop = 14.0; } void PowerPort::init_nanogrid() @@ -35,11 +35,11 @@ void PowerPort::init_nanogrid() chg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp // also initialize the connected bus - bus->dis_voltage_start = 30.0; // starting buck mode above this point - bus->dis_voltage_stop = 20.0; // stopping buck mode below this point + dis_voltage_start = 30.0; // starting buck mode above this point + dis_voltage_stop = 20.0; // stopping buck mode below this point - bus->chg_voltage_target = 28.0; // starting idle mode above this point - bus->chg_voltage_min = 10.0; + chg_voltage_target = 28.0; // starting idle mode above this point + chg_voltage_min = 10.0; } // must be called exactly once per second, otherwise energy calculation gets wrong @@ -47,10 +47,9 @@ void PowerPort::energy_balance() { // remark: timespan = 1s, so no multiplication with time necessary for energy calculation if (current >= 0) { - chg_energy_Wh += bus->voltage * current / 3600.0; + chg_energy_Wh += voltage * current / 3600.0; } else { - dis_energy_Wh -= bus->voltage * current / 3600.0; + dis_energy_Wh -= voltage * current / 3600.0; } - } diff --git a/src/power_port.h b/src/power_port.h index dc2a86c..998dbee 100644 --- a/src/power_port.h +++ b/src/power_port.h @@ -19,19 +19,16 @@ /** @file * - * @brief Definition of charge controller terminals and internal DC buses + * @brief Definition of charge controller terminals and internal power port / connections */ #include -/** DC bus type +/** Power Port class * - * Stores the voltage measurement and voltage limits for a DC bus inside the charge controller PCB - * - * A DC bus is considered a connection of two or more electrical circuits in one point, such that - * they have the same electric potential. + * Stores actual measurements and voltage / current limits for external terminals or internal ports */ -class DcBus +class PowerPort { public: float voltage; ///< Measured bus voltage @@ -43,22 +40,6 @@ public: float dis_voltage_start; ///< Minimum voltage to allow discharge current from the bus ///< (equal to load reconnect voltage) float dis_voltage_stop; ///< Absolute minimum = load disconnect for batteries -}; - - -/** Power Port type - * - * Stores the current and power measurements and limits for one connection to a DC bus. One power - * port is assigned to exactly ony DC bus, but one DC bus can have multiple ports. - */ -class PowerPort -{ -public: - PowerPort(DcBus *dc_bus): - bus(dc_bus) {}; - - DcBus *bus; ///< Pointer to the DC bus this port is connected to (also - ///< storing its voltage measurement) float current; ///< Measured current through this port (positive sign = ///< increasing voltage/charge of the named device) @@ -92,5 +73,4 @@ public: void energy_balance(); }; - #endif /* POWER_PORT_H */ diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 94fbb93..518ef64 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -152,7 +152,7 @@ void PwmSwitch::control() off_timestamp = time(NULL); printf("PWM charger stop.\n"); } - else if (port_int->bus->voltage > (port_int->bus->chg_voltage_target - port_int->chg_droop_res * port_int->current) // output voltage above target + else if (port_int->voltage > (port_int->chg_voltage_target - port_int->chg_droop_res * port_int->current) // output voltage above target || port_int->current > port_int->chg_current_limit // output current limit exceeded || terminal->current < terminal->dis_current_limit) // input current (negative signs) above limit { @@ -184,10 +184,10 @@ void PwmSwitch::control() } else { if (port_int->chg_current_limit > 0 // charging allowed - && port_int->bus->voltage < port_int->bus->chg_voltage_target - && port_int->bus->voltage > port_int->bus->chg_voltage_min + && port_int->voltage < port_int->chg_voltage_target + && port_int->voltage > port_int->chg_voltage_min && terminal->dis_current_limit < 0 // dischraging allowed - && terminal->bus->voltage > port_int->bus->voltage + offset_voltage_start + && terminal->voltage > port_int->voltage + offset_voltage_start && time(NULL) > (off_timestamp + restart_interval) && enabled == true) { diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index f65fed6..48980ac 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -100,18 +100,18 @@ void uext_process_1s() oled.printf("n/a"); } #ifndef CHARGER_TYPE_PWM - if (solar_terminal.bus->voltage > bat_terminal.bus->voltage) + if (solar_terminal.voltage > bat_terminal.voltage) #endif { oled.setTextCursor(0, 26); - oled.printf("%4.1fV", solar_terminal.bus->voltage); + oled.printf("%4.1fV", solar_terminal.voltage); } // battery data oled.setTextCursor(42, 18); oled.printf("%5.1fW", (abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros oled.setTextCursor(42, 26); - oled.printf("%5.1fV", bat_terminal.bus->voltage); + oled.printf("%5.1fV", bat_terminal.voltage); // load data oled.setTextCursor(90, 18); diff --git a/test/main.cpp b/test/main.cpp index 66c9073..cf89747 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,21 +17,17 @@ #include "tests.h" -DcBus lv_bus; -PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) +PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) #if FEATURE_DCDC_CONVERTER -DcBus hv_bus; -PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_port_hv(&hv_bus); // internal high voltage side port of DC/DC converter -PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter -Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT); +PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT); #endif #if FEATURE_PWM_SWITCH -DcBus pwm_bus; -PowerPort pwm_terminal(&pwm_bus); // external terminal of PWM switch port (normally solar) -PowerPort pwm_port_int(&lv_bus); // internal side of PWM switch +PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar) +PowerPort pwm_port_int; // internal side of PWM switch PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int); PowerPort &solar_terminal = pwm_terminal; #else @@ -39,7 +35,7 @@ PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h #endif #if FEATURE_LOAD_OUTPUT -PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) +PowerPort load_terminal; // load terminal (also connected to lv_bus) LoadOutput load(&load_terminal); #endif @@ -63,7 +59,7 @@ int main() { adc_tests(); bat_charger_tests(); - dc_bus_tests(); + power_port_tests(); half_brigde_tests(); dcdc_tests(); log_tests(); diff --git a/test/tests.h b/test/tests.h index a52af5a..b4d64b7 100644 --- a/test/tests.h +++ b/test/tests.h @@ -5,7 +5,7 @@ void bat_charger_tests(); void adc_tests(); -void dc_bus_tests(); +void power_port_tests(); void half_brigde_tests(); diff --git a/test/tests_adc.cpp b/test/tests_adc.cpp index 0d7dc84..ea419ac 100644 --- a/test/tests_adc.cpp +++ b/test/tests_adc.cpp @@ -9,28 +9,28 @@ static adc_values_t adcval; void check_solar_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.voltage * 10) / 10); TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage, -round(hv_terminal.current * 10) / 10); } void check_bat_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.voltage * 10) / 10); TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current, round(lv_terminal.current * 10) / 10); } void check_load_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.voltage * 10) / 10); TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10); } void check_lv_bus_int_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_port_lv.bus->voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_port_lv.current * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_lv_port.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_lv_port.current * 10) / 10); } void check_temperature_readings() diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index 8575123..c7af5f4 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -13,14 +13,14 @@ static void init_structs() battery_init_dc_bus(&bat_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; charger.bat_temperature = 25; - bat_terminal.bus->voltage = 14.0; + bat_terminal.voltage = 14.0; bat_terminal.current = 0; } void no_start_at_high_voltage() { init_structs(); - bat_terminal.bus->voltage = bat_conf.voltage_recharge + 0.1; + bat_terminal.voltage = bat_conf.voltage_recharge + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -29,7 +29,7 @@ void no_start_after_short_rest() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1; - bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -38,7 +38,7 @@ void no_start_outside_temperature_limits() { init_structs(); charger.bat_temperature = bat_conf.charge_temp_max + 1; - bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); @@ -51,7 +51,7 @@ void start_if_everything_just_fine() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); } @@ -60,10 +60,10 @@ void enter_topping_at_voltage_setpoint() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -73,7 +73,7 @@ void stop_topping_after_time_limit() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - bat_conf.topping_duration + 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff + 0.1; charger.charge_control(&bat_conf); @@ -89,7 +89,7 @@ void stop_topping_at_cutoff_current() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); @@ -101,7 +101,7 @@ void trickle_to_idle_for_li_ion() battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); @@ -113,7 +113,7 @@ void no_trickle_if_low_current_because_of_low_input() charger.time_state_changed = time(NULL) - 200; charger.time_voltage_limit_reached = time(NULL) - 3; - bat_terminal.bus->voltage = bat_conf.topping_voltage - 0.1; + bat_terminal.voltage = bat_conf.topping_voltage - 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); @@ -136,7 +136,7 @@ void no_equalization_if_disabled() time(NULL) - bat_conf.equalization_trigger_days * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -154,7 +154,7 @@ void no_equalization_if_limits_not_reached() time(NULL) - (bat_conf.equalization_trigger_days - 1) * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -168,7 +168,7 @@ void trickle_to_equalization_if_enabled_and_time_limit_reached() time(NULL) - bat_conf.equalization_trigger_days * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -183,7 +183,7 @@ void trickle_to_equalization_if_enabled_and_deep_dis_limit_reached() charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles; charger.time_state_changed = time(NULL) - 1; - bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -195,7 +195,7 @@ void stop_equalization_after_time_limit() trickle_to_equalization_if_enabled_and_time_limit_reached(); charger.time_state_changed = time(NULL) - bat_conf.equalization_duration + 1; - bat_terminal.bus->voltage = bat_conf.equalization_voltage + 0.1; + bat_terminal.voltage = bat_conf.equalization_voltage + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -214,11 +214,11 @@ void stop_discharge_at_low_voltage() { init_structs(); - bat_terminal.bus->voltage = 14; + bat_terminal.voltage = 14; charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal.dis_current_limit); - bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect - 0.1; + bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.dis_current_limit); } @@ -248,17 +248,17 @@ void restart_discharge_if_allowed() init_structs(); // stop because of undervoltage - bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect - 0.1; + bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.dis_current_limit); // increase voltage slightly above DISconnect voltage - bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect + 0.1; + bat_terminal.voltage = bat_conf.voltage_load_disconnect + 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.dis_current_limit); // increase voltage above REconnect voltage - bat_terminal.bus->voltage = bat_conf.voltage_load_reconnect + 0.1; + bat_terminal.voltage = bat_conf.voltage_load_reconnect + 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal.dis_current_limit); } diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index 77fd571..00a017d 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -11,15 +11,15 @@ static void init_structs_buck() { - dcdc_port_hv.init_solar(); - dcdc_port_hv.bus->voltage = 20; - dcdc_port_hv.bus->dis_voltage_start = 18; - dcdc_port_hv.current = 0; + hv_terminal.init_solar(); + 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(&dcdc_port_lv, &bat_conf, 1); - dcdc_port_lv.bus->voltage = 14; - dcdc_port_lv.current = 0; + battery_init_dc_bus(&dcdc_lv_port, &bat_conf, 1); + dcdc_lv_port.voltage = 14; + dcdc_lv_port.current = 0; dcdc.mode = MODE_MPPT_BUCK; dcdc.temp_mosfets = 25; @@ -43,17 +43,17 @@ static void init_structs_boost() { half_bridge_stop(); - dcdc_port_hv.init_solar(); - dcdc_port_lv.bus->voltage = 20; - dcdc_port_lv.bus->dis_voltage_start = 18; - dcdc_port_lv.current = 0; - dcdc_port_lv.power = 0; + hv_terminal.init_solar(); + dcdc_lv_port.voltage = 20; + dcdc_lv_port.dis_voltage_start = 18; + dcdc_lv_port.current = 0; + dcdc_lv_port.power = 0; battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); - battery_init_dc_bus(&dcdc_port_hv, &bat_conf, 1); - dcdc_port_hv.bus->voltage = 3.7 * 10; - dcdc_port_hv.current = 0; - dcdc_port_hv.power = 0; + battery_init_dc_bus(&hv_terminal, &bat_conf, 1); + hv_terminal.voltage = 3.7 * 10; + hv_terminal.current = 0; + hv_terminal.power = 0; dcdc.mode = MODE_MPPT_BOOST; dcdc.temp_mosfets = 25; @@ -104,7 +104,7 @@ void no_start_if_dcdc_disabled() void no_start_if_dcdc_lv_voltage_low() { init_structs_buck(); - dcdc_port_lv.bus->voltage = dcdc.ls_voltage_min - 0.5; + dcdc_lv_port.voltage = dcdc.ls_voltage_min - 0.5; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -113,35 +113,35 @@ void no_start_if_dcdc_lv_voltage_low() void no_buck_start_if_bat_voltage_low() { init_structs_buck(); - dcdc_port_lv.bus->voltage = bat_conf.voltage_absolute_min - 0.1; + dcdc_lv_port.voltage = bat_conf.voltage_absolute_min - 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_bat_voltage_high() { init_structs_buck(); - dcdc_port_lv.bus->voltage = bat_conf.topping_voltage + 0.1; + dcdc_lv_port.voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_bat_chg_not_allowed() { init_structs_buck(); - dcdc_port_lv.chg_current_limit = 0; + dcdc_lv_port.chg_current_limit = 0; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_solar_voltage_high() { init_structs_buck(); - dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 1; + hv_terminal.voltage = dcdc.hs_voltage_max + 1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_solar_voltage_low() { init_structs_buck(); - dcdc_port_hv.bus->voltage = 17; + hv_terminal.voltage = 17; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -150,35 +150,35 @@ void no_buck_start_if_solar_voltage_low() void no_boost_start_if_bat_voltage_low() { init_structs_boost(); - dcdc_port_hv.bus->voltage = bat_conf.voltage_absolute_min - 0.1; + hv_terminal.voltage = bat_conf.voltage_absolute_min - 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_bat_voltage_high() { init_structs_boost(); - dcdc_port_hv.bus->voltage = bat_conf.topping_voltage + 0.1; + hv_terminal.voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_bat_chg_not_allowed() { init_structs_boost(); - dcdc_port_hv.chg_current_limit = 0; + hv_terminal.chg_current_limit = 0; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_solar_voltage_high() { init_structs_boost(); - dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 1; + dcdc_lv_port.voltage = dcdc.ls_voltage_max + 1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_solar_voltage_low() { init_structs_boost(); - dcdc_port_lv.bus->voltage = 17; + dcdc_lv_port.voltage = 17; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -197,7 +197,7 @@ void buck_derating_output_voltage_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->chg_voltage_target + 0.1; + dcdc_lv_port.voltage = dcdc_lv_port.chg_voltage_target + 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage @@ -208,7 +208,7 @@ void buck_derating_output_current_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_lv.current = dcdc_port_lv.chg_current_limit + 0.1; + dcdc_lv_port.current = dcdc_lv_port.chg_current_limit + 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); @@ -219,8 +219,8 @@ void buck_derating_input_voltage_too_low() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->dis_voltage_stop - 0.1; - dcdc_port_lv.current = 0.2; + hv_terminal.voltage = hv_terminal.dis_voltage_stop - 0.1; + dcdc_lv_port.current = 0.2; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); @@ -231,7 +231,7 @@ void buck_derating_input_current_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_hv.current = dcdc_port_hv.dis_current_limit - 0.1; + hv_terminal.current = hv_terminal.dis_current_limit - 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); @@ -261,7 +261,7 @@ void buck_stop_high_voltage_emergency() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 0.1; + dcdc_lv_port.voltage = dcdc.ls_voltage_max + 0.1; dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -270,15 +270,15 @@ void buck_correct_mppt_operation() { start_buck(); - dcdc_port_lv.power = 5; + dcdc_lv_port.power = 5; dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); - dcdc_port_lv.power = 7; + dcdc_lv_port.power = 7; dcdc.control(); float pwm2 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm2 > pwm1); - dcdc_port_lv.power = 6; // decrease power to make the direction turn around + dcdc_lv_port.power = 6; // decrease power to make the direction turn around dcdc.control(); float pwm3 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm3 < pwm2); @@ -300,7 +300,7 @@ void boost_derating_output_voltage_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->chg_voltage_target + 0.5; + hv_terminal.voltage = hv_terminal.chg_voltage_target + 0.5; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power @@ -310,7 +310,7 @@ void boost_derating_output_current_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_hv.current = dcdc_port_hv.chg_current_limit + 0.1; + hv_terminal.current = hv_terminal.chg_current_limit + 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); @@ -320,8 +320,8 @@ void boost_derating_input_voltage_too_low() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->dis_voltage_stop - 0.1; - dcdc_port_hv.current = 0.2; + dcdc_lv_port.voltage = dcdc_lv_port.dis_voltage_stop - 0.1; + hv_terminal.current = 0.2; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); @@ -331,7 +331,7 @@ void boost_derating_input_current_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_port_lv.current = dcdc_port_lv.dis_current_limit - 0.1; + dcdc_lv_port.current = dcdc_lv_port.dis_current_limit - 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); @@ -358,7 +358,7 @@ void boost_stop_input_power_too_low() void boost_stop_high_voltage_emergency() { start_boost(); - dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 0.1; + hv_terminal.voltage = dcdc.hs_voltage_max + 0.1; dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -367,15 +367,15 @@ void boost_correct_mppt_operation() { start_boost(); - dcdc_port_hv.power = 5; + hv_terminal.power = 5; dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); - dcdc_port_hv.power = 7; + hv_terminal.power = 7; dcdc.control(); float pwm2 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm2 < pwm1); - dcdc_port_hv.power = 6; // decrease power to make the direction turn around + hv_terminal.power = 6; // decrease power to make the direction turn around dcdc.control(); float pwm3 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm3 > pwm2); diff --git a/test/tests_log.cpp b/test/tests_log.cpp index 8065da7..a424a9a 100644 --- a/test/tests_log.cpp +++ b/test/tests_log.cpp @@ -9,7 +9,7 @@ void reset_counters_at_start_of_day() { - solar_terminal.bus->voltage = bat_terminal.bus->voltage - 1; + solar_terminal.voltage = bat_terminal.voltage - 1; log_data.day_counter = 0; @@ -37,7 +37,7 @@ void reset_counters_at_start_of_day() TEST_ASSERT_EQUAL(0, log_data.day_counter); // now solar power comes back - solar_terminal.bus->voltage = bat_terminal.bus->voltage + 1; + solar_terminal.voltage = bat_terminal.voltage + 1; log_update_energy(&log_data); // day counter should be increased and daily energy counters reset @@ -50,14 +50,14 @@ void reset_counters_at_start_of_day() void log_new_solar_voltage_max() { - solar_terminal.bus->voltage = 40; + solar_terminal.voltage = 40; log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(40, log_data.solar_voltage_max); } void log_new_bat_voltage_max() { - bat_terminal.bus->voltage = 31; + bat_terminal.voltage = 31; log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(31, log_data.battery_voltage_max); } diff --git a/test/tests_dc_bus.cpp b/test/tests_power_port.cpp similarity index 82% rename from test/tests_dc_bus.cpp rename to test/tests_power_port.cpp index 919bf07..7368a36 100644 --- a/test/tests_dc_bus.cpp +++ b/test/tests_power_port.cpp @@ -69,29 +69,29 @@ void charging_energy_calculation_valid() { energy_calculation_init(); // charging only during sun hours - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * (dcdc_current_sun - load_current)), round(lv_terminal.chg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * (dcdc_current_sun - load_current)), round(lv_terminal.chg_energy_Wh)); } void discharging_energy_calculation_valid() { energy_calculation_init(); // discharging (sum of current) only during dis hours - TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.bus->voltage * adcval.load_current), round(lv_terminal.dis_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.voltage * adcval.load_current), round(lv_terminal.dis_energy_Wh)); } void solar_input_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * dcdc_current_sun), round(hv_terminal.dis_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * dcdc_current_sun), round(hv_terminal.dis_energy_Wh)); } void load_output_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load_terminal.chg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.voltage * adcval.load_current), round(load_terminal.chg_energy_Wh)); } -void dc_bus_tests() +void power_port_tests() { energy_calculation_init();