From f0e99cd80f51ac18f886e1d35bc430d047eaff04 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Mon, 3 Feb 2020 18:02:44 +0100 Subject: [PATCH] Voltage replaced by DcBus ptr in PowerPort class Preparation to separate current and voltage measurements and control parameters into DcBus and PowerPort classes. This will help to manage current from different sources (grid, solar) going towards one bus and control the battery voltage. It will also help to align multiple charge controllers in parallel, communicating via CAN bus. --- src/bat_charger.cpp | 20 ++++++++-------- src/daq.cpp | 34 +++++++++++++------------- src/data_objects.cpp | 4 ++-- src/dcdc.cpp | 38 ++++++++++++++--------------- src/device_status.cpp | 10 ++++---- src/ext/oled.cpp | 6 ++--- src/load.cpp | 8 +++---- src/main.h | 3 ++- src/main_mbed.cpp | 21 ++++++++-------- src/main_zephyr.cpp | 19 ++++++++------- src/power_port.cpp | 4 ++-- src/power_port.h | 26 +++++++++++++++++--- src/pwm_switch.cpp | 29 ++++++++++------------- src/pwm_switch.h | 7 +++--- test/main.cpp | 15 ++++++------ test/tests_bat_charger.cpp | 42 ++++++++++++++++---------------- test/tests_daq.cpp | 8 +++---- test/tests_dcdc.cpp | 38 ++++++++++++++--------------- test/tests_device_status.cpp | 8 +++---- test/tests_load.cpp | 46 +++++++++++++++++++++++------------- test/tests_power_port.cpp | 8 +++---- 21 files changed, 214 insertions(+), 180 deletions(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index ea81eda..2328631 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -211,8 +211,8 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char void Charger::detect_num_batteries(BatConf *bat) { - if (port->voltage > bat->voltage_absolute_min * 2 && - port->voltage < bat->voltage_absolute_max * 2) { + if (port->bus->voltage > bat->voltage_absolute_min * 2 && + port->bus->voltage < bat->voltage_absolute_max * 2) { num_batteries = 2; printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } @@ -226,7 +226,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)((port->voltage - bat_conf->ocv_empty) / + int soc_new = (int)((port->bus->voltage - bat_conf->ocv_empty) / (bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0); if (soc_new > 500 && soc_filtered == 0) { @@ -262,7 +262,7 @@ void Charger::discharge_control(BatConf *bat_conf) // load output state is defined by battery negative current limit if (port->neg_current_limit < 0) { // discharging currently allowed. see if that's still valid: - if (port->voltage < num_batteries * + if (port->bus->voltage < num_batteries * (bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res)) { // low state of charge @@ -297,7 +297,7 @@ void Charger::discharge_control(BatConf *bat_conf) } else { // discharging currently not allowed. should we allow it? - if (port->voltage >= num_batteries * + if (port->bus->voltage >= num_batteries * (bat_conf->voltage_load_reconnect - port->current * port->neg_droop_res) && bat_temperature < bat_conf->discharge_temp_max - 1 && bat_temperature > bat_conf->discharge_temp_min + 1) @@ -329,7 +329,7 @@ void Charger::charge_control(BatConf *bat_conf) } if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) && - port->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries) + port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries) { dev_stat.clear_error(ERR_BAT_OVERVOLTAGE); } @@ -337,7 +337,7 @@ void Charger::charge_control(BatConf *bat_conf) // state machine switch (state) { case CHG_STATE_IDLE: { - if (port->voltage < bat_conf->voltage_recharge * num_batteries + if (port->bus->voltage < bat_conf->voltage_recharge * num_batteries && (uptime() - time_state_changed) > bat_conf->time_limit_recharge && bat_temperature < bat_conf->charge_temp_max - 1 && bat_temperature > bat_conf->charge_temp_min + 1) @@ -358,7 +358,7 @@ void Charger::charge_control(BatConf *bat_conf) port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (port->voltage > num_batteries * + if (port->bus->voltage > num_batteries * (port->sink_voltage_max - port->current * port->pos_droop_res)) { enter_state(CHG_STATE_TOPPING); @@ -370,7 +370,7 @@ void Charger::charge_control(BatConf *bat_conf) port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (port->voltage >= num_batteries * + if (port->bus->voltage >= num_batteries * (port->sink_voltage_max - port->current * port->pos_droop_res)) { time_voltage_limit_reached = uptime(); @@ -414,7 +414,7 @@ void Charger::charge_control(BatConf *bat_conf) port->sink_voltage_max = num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (port->voltage >= num_batteries * + if (port->bus->voltage >= num_batteries * (port->sink_voltage_max - port->current * port->pos_droop_res)) { time_voltage_limit_reached = uptime(); diff --git a/src/daq.cpp b/src/daq.cpp index b5360a4..5087236 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -154,17 +154,15 @@ void daq_update() //int vcc = 3300; // calculate lower voltage first, as it is needed for PWM terminal voltage calculation - lv_terminal.voltage = adc_scaled(ADC_POS_V_BAT, vcc, ADC_GAIN_V_BAT); - load_terminal.voltage = lv_terminal.voltage; + lv_bus.voltage = adc_scaled(ADC_POS_V_BAT, vcc, ADC_GAIN_V_BAT); #if CONFIG_HAS_DCDC_CONVERTER - hv_terminal.voltage = adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR); - dcdc_lv_port.voltage = lv_terminal.voltage; + hv_bus.voltage = adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR); #endif + #if CONFIG_HAS_PWM_SWITCH - pwm_terminal.voltage = lv_terminal.voltage - vcc * (ADC_OFFSET_V_SOLAR / 1000.0) - + pwm_switch.ext_voltage = lv_bus.voltage - vcc * (ADC_OFFSET_V_SOLAR / 1000.0) - adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR); - pwm_port_int.voltage = lv_terminal.voltage; #endif load_terminal.current = @@ -172,25 +170,27 @@ void daq_update() #if CONFIG_HAS_PWM_SWITCH // current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation - pwm_port_int.current = pwm_switch.get_duty_cycle() * ( + pwm_terminal.current = -pwm_switch.get_duty_cycle() * ( adc_scaled(ADC_POS_I_SOLAR, vcc, ADC_GAIN_I_SOLAR) + solar_current_offset); - pwm_terminal.current = -pwm_port_int.current; - lv_terminal.current = pwm_port_int.current - load_terminal.current; - pwm_port_int.power = pwm_port_int.voltage * pwm_port_int.current; - pwm_terminal.power = pwm_terminal.voltage * pwm_terminal.current; + lv_terminal.current = -pwm_terminal.current - load_terminal.current; + + pwm_terminal.power = pwm_terminal.bus->voltage * pwm_terminal.current; #endif + #if CONFIG_HAS_DCDC_CONVERTER dcdc_lv_port.current = adc_scaled(ADC_POS_I_DCDC, vcc, ADC_GAIN_I_DCDC) + solar_current_offset; - lv_terminal.current = dcdc_lv_port.current - load_terminal.current; - hv_terminal.current = -dcdc_lv_port.current * lv_terminal.voltage / hv_terminal.voltage; - dcdc_lv_port.power = dcdc_lv_port.voltage * dcdc_lv_port.current; - hv_terminal.power = hv_terminal.voltage * hv_terminal.current; + lv_terminal.current = dcdc_lv_port.current - load_terminal.current; + + hv_terminal.current = -dcdc_lv_port.current * lv_terminal.bus->voltage / hv_terminal.bus->voltage; + + dcdc_lv_port.power = dcdc_lv_port.bus->voltage * dcdc_lv_port.current; + hv_terminal.power = hv_terminal.bus->voltage * hv_terminal.current; #endif - lv_terminal.power = lv_terminal.voltage * lv_terminal.current; - load_terminal.power = load_terminal.voltage * load_terminal.current; + lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current; + load_terminal.power = load_terminal.bus->voltage * load_terminal.current; #ifdef PIN_ADC_TEMP_BAT // battery temperature calculation diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 532f575..7120ba9 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -207,11 +207,11 @@ const data_object_t data_objects[] = { TS_OUTPUT, TS_READ_ALL), // battery related data objects - TS_DATA_OBJ_FLOAT(0x70, "Bat_V", &bat_terminal.voltage, 2, + TS_DATA_OBJ_FLOAT(0x70, "Bat_V", &bat_terminal.bus->voltage, 2, TS_OUTPUT, TS_READ_ALL), #if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR - TS_DATA_OBJ_FLOAT(0x71, "Solar_V", &solar_terminal.voltage, 2, + TS_DATA_OBJ_FLOAT(0x71, "Solar_V", &solar_terminal.bus->voltage, 2, TS_OUTPUT, TS_READ_ALL), #endif diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 7f2b111..4279282 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -74,7 +74,7 @@ int Dcdc::duty_cycle_delta() print_test("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->voltage, out->voltage, in->current, out->current, + out->power, power_prev, in->bus->voltage, out->bus->voltage, in->current, out->current, out->sink_voltage_max, out->pos_current_limit, half_bridge_get_duty_cycle() * 100.0, state); @@ -82,7 +82,7 @@ int Dcdc::duty_cycle_delta() // swith off after 10s low power or negative power (if not in nanogrid mode) pwr_inc_goal = 0; } - else if (out->voltage > (out->sink_voltage_max - out->pos_droop_res * out->current)) { + else if (out->bus->voltage > (out->sink_voltage_max - out->pos_droop_res * out->current)) { // output voltage target reached state = DCDC_STATE_CV; pwr_inc_goal = -1; // decrease output power @@ -94,14 +94,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->voltage < (in->src_voltage_stop - in->neg_droop_res * in->current) + || (in->bus->voltage < (in->src_voltage_stop - in->neg_droop_res * in->current) && out->current > 0.1) // input voltage below limit || in->current < in->neg_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->voltage < out->src_voltage_start) { + else if (out->power < output_power_min && out->bus->voltage < out->src_voltage_start) { // no load condition (e.g. start-up of nanogrid) --> raise voltage pwr_inc_goal = 1; // increase output power } @@ -121,9 +121,9 @@ int Dcdc::duty_cycle_delta() int Dcdc::check_start_conditions() { if (enabled == false || - hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing - lvs->voltage > ls_voltage_max || - lvs->voltage < ls_voltage_min || + 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 || dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) || (int)uptime() < (off_timestamp + (int)restart_interval)) { @@ -131,19 +131,19 @@ int Dcdc::check_start_conditions() } if (lvs->pos_current_limit > 0 && - lvs->voltage < lvs->sink_voltage_max && - lvs->voltage > lvs->sink_voltage_min && + lvs->bus->voltage < lvs->sink_voltage_max && + lvs->bus->voltage > lvs->sink_voltage_min && hvs->neg_current_limit < 0 && - hvs->voltage > hvs->src_voltage_start) + hvs->bus->voltage > hvs->src_voltage_start) { return 1; // start in buck mode allowed } if (hvs->pos_current_limit > 0 && - hvs->voltage < hvs->sink_voltage_max && - hvs->voltage > hvs->sink_voltage_min && + hvs->bus->voltage < hvs->sink_voltage_max && + hvs->bus->voltage > hvs->sink_voltage_min && lvs->neg_current_limit < 0 && - lvs->voltage > lvs->src_voltage_start) + lvs->bus->voltage > lvs->src_voltage_start) { return -1; // start in boost mode allowed } @@ -155,7 +155,7 @@ void Dcdc::control() { if (half_bridge_enabled()) { const char *stop_reason = NULL; - if (lvs->voltage > ls_voltage_max || hvs->voltage > hs_voltage_max) { + if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) { stop_reason = "emergency (voltage limits exceeded)"; } else if (enabled == false) { @@ -212,17 +212,17 @@ void Dcdc::control() mode_name = "buck"; // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush // currents and stress on MOSFETs - half_bridge_start(lvs->voltage / (hvs->voltage - 1)); + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1)); } else { mode_name = "boost"; // Will automatically start with max. duty (0.97) if connected to a // nanogrid not yet started up (zero voltage) - half_bridge_start(lvs->voltage / (hvs->voltage + 1)); + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1)); } power_good_timestamp = uptime(); print_info("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name, - hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); startup_delay_counter = 0; // ensure we will have a delay before next start } } @@ -246,9 +246,9 @@ void Dcdc::test() if (startup_delay_counter > num_wait_calls) { if (check_start_conditions() != 0) { - half_bridge_start(lvs->voltage / hvs->voltage); + half_bridge_start(lvs->bus->voltage / hvs->bus->voltage); print_info("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", - hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); } } else { diff --git a/src/device_status.cpp b/src/device_status.cpp index 098c3bf..8243cf2 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -39,7 +39,7 @@ void DeviceStatus::update_energy() } #if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR - if (solar_terminal.voltage < bat_terminal.voltage) { + if (solar_terminal.bus->voltage < bat_terminal.bus->voltage) { seconds_zero_solar += 1; } else { @@ -75,13 +75,13 @@ void DeviceStatus::update_energy() void DeviceStatus::update_min_max_values() { - if (bat_terminal.voltage > battery_voltage_max) { - battery_voltage_max = bat_terminal.voltage; + if (bat_terminal.bus->voltage > battery_voltage_max) { + battery_voltage_max = bat_terminal.bus->voltage; } #if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR - if (solar_terminal.voltage > solar_voltage_max) { - solar_voltage_max = solar_terminal.voltage; + if (solar_terminal.bus->voltage > solar_voltage_max) { + solar_voltage_max = solar_terminal.bus->voltage; } #endif diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index b5d0658..1f5c7b7 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -150,11 +150,11 @@ void ExtOled::process_1s() oled.writeString(buf, len); } #ifndef CHARGER_TYPE_PWM - if (in_terminal.voltage > bat_terminal.voltage) + if (in_terminal.bus->voltage > bat_terminal.bus->voltage) #endif { oled.setTextCursor(0, 26); - len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.voltage); + len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.bus->voltage); oled.writeString(buf, len); } @@ -164,7 +164,7 @@ void ExtOled::process_1s() (abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros oled.writeString(buf, len); oled.setTextCursor(42, 26); - len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.voltage); + len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.bus->voltage); oled.writeString(buf, len); // load data diff --git a/src/load.cpp b/src/load.cpp index 1922d0a..f7a0672 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -140,8 +140,8 @@ void LoadOutput::control() // long-term overvoltage (overvoltage transients are detected as an ADC alert and switch // off the solar input instead of the load output) static int debounce_counter = 0; - if (port->voltage > port->sink_voltage_max || - port->voltage > LOW_SIDE_VOLTAGE_MAX) + if (port->bus->voltage > port->sink_voltage_max || + port->bus->voltage > LOW_SIDE_VOLTAGE_MAX) { debounce_counter++; if (debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag @@ -178,8 +178,8 @@ void LoadOutput::control() } if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) && - port->voltage < (port->sink_voltage_max - ov_hysteresis) && - port->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis)) + port->bus->voltage < (port->sink_voltage_max - ov_hysteresis) && + port->bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis)) { dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE); } diff --git a/src/main.h b/src/main.h index 2c84928..9c35305 100644 --- a/src/main.h +++ b/src/main.h @@ -16,10 +16,12 @@ #include "load.h" #include "pcb.h" +extern DcBus lv_bus; extern PowerPort lv_terminal; extern PowerPort load_terminal; #if CONFIG_HAS_DCDC_CONVERTER +extern DcBus hv_bus; extern PowerPort hv_terminal; extern PowerPort dcdc_lv_port; extern Dcdc dcdc; @@ -27,7 +29,6 @@ extern Dcdc dcdc; #if CONFIG_HAS_PWM_SWITCH extern PowerPort pwm_terminal; -extern PowerPort pwm_port_int; extern PwmSwitch pwm_switch; #endif diff --git a/src/main_mbed.cpp b/src/main_mbed.cpp index f159d50..882d6b8 100644 --- a/src/main_mbed.cpp +++ b/src/main_mbed.cpp @@ -31,11 +31,13 @@ #include "data_objects.h" // for access to internal data via ThingSet #include "bl_support.h" // Bootloader support from the application side -PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) +DcBus lv_bus; +PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) #if CONFIG_HAS_DCDC_CONVERTER -PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter #if CONFIG_HV_TERMINAL_NANOGRID Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); #elif CONFIG_HV_TERMINAL_BATTERY @@ -46,13 +48,12 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); #endif #if CONFIG_HAS_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 pwm_terminal(&lv_bus); // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal); #endif #if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal; // load terminal (also connected to lv_bus) +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) LoadOutput load(&load_terminal); #endif @@ -152,7 +153,7 @@ int main() #endif #if CONFIG_HAS_PWM_SWITCH - bat_terminal.pass_voltage_targets(&pwm_port_int); + bat_terminal.pass_voltage_targets(&pwm_terminal); #endif eeprom_update(); @@ -187,10 +188,10 @@ void system_control() daq_update(); // alerts should trigger only for transients, so update based on actual voltage - daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8); + daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8); #if CONFIG_HAS_PWM_SWITCH - ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal); + ports_update_current_limits(&pwm_terminal, &bat_terminal, &load_terminal); pwm_switch.control(); leds_set_charging(pwm_switch.active()); #endif diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index 1a9b997..aec5875 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -28,11 +28,13 @@ #include "device_status.h" // log data (error memory, min/max measurements, etc.) #include "data_objects.h" // for access to internal data via ThingSet -PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) +DcBus lv_bus; +PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) #if CONFIG_HAS_DCDC_CONVERTER -PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter #if CONFIG_HV_TERMINAL_NANOGRID Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); #elif CONFIG_HV_TERMINAL_BATTERY @@ -43,13 +45,12 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); #endif #if CONFIG_HAS_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 pwm_terminal(&lv_bus); // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal); #endif #if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal; // load terminal (also connected to lv_bus) +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) LoadOutput load(&load_terminal); #endif @@ -141,10 +142,10 @@ void control_thread() daq_update(); // alerts should trigger only for transients, so update based on actual voltage - daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8); + daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8); #if CONFIG_HAS_PWM_SWITCH - ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal); + ports_update_current_limits(&pwm_port, &bat_terminal, &load_terminal); //pwm_switch.control(); leds_set_charging(pwm_switch.active()); #endif diff --git a/src/power_port.cpp b/src/power_port.cpp index be7aac4..8cc29ac 100644 --- a/src/power_port.cpp +++ b/src/power_port.cpp @@ -45,10 +45,10 @@ void PowerPort::energy_balance() { // remark: timespan = 1s, so no multiplication with time necessary for energy calculation if (current >= 0.0F) { - pos_energy_Wh += voltage * current / 3600.0; + pos_energy_Wh += bus->voltage * current / 3600.0; } else { - neg_energy_Wh -= voltage * current / 3600.0; + neg_energy_Wh -= bus->voltage * current / 3600.0; } } diff --git a/src/power_port.h b/src/power_port.h index ae62a02..4645692 100644 --- a/src/power_port.h +++ b/src/power_port.h @@ -15,6 +15,22 @@ #include +/** + * These values may be calculated based on external data, if multiple devices are connected to the + * bus. + */ +class DcBus +{ +public: + float voltage; ///< Measured bus voltage + + float voltage_max; ///< Maximum voltage of a DC bus, equivalent to battery + ///< charging target voltage if hard-wired to a battery + + float voltage_min; ///< Absolute minimum for both chg and discharging direction +}; + + // ----------------- // | | >> positive current/power direction // | o---->---- @@ -48,7 +64,7 @@ class PowerPort { public: - float voltage; ///< Measured port voltage + DcBus *bus; ///< Pointer to DC bus containing voltage measurement float sink_voltage_max; ///< Maximum voltage if connected device acts as a sink ///< (equivalent to battery charging target voltage) @@ -59,9 +75,9 @@ public: ///< (equal to load reconnect voltage) float src_voltage_stop; ///< Absolute minimum = load disconnect for batteries - float current; ///< Measured current through this port (positive sign = + float current = 0; ///< Measured current through this port (positive sign = ///< sinking current into the named external device) - float power; ///< Product of current & voltage + float power = 0; ///< Product of port current and bus voltage float pos_current_limit; ///< Maximum positive current (valid values >= 0.0) float pos_droop_res; ///< control voltage = nominal voltage - droop_res * current @@ -72,6 +88,10 @@ public: float pos_energy_Wh; ///< Cumulated sunk energy since last counter reset (Wh) float neg_energy_Wh; ///< Cumulated sourced energy since last counter reset (Wh) + PowerPort(DcBus *dc_bus) : + bus(dc_bus) + {} + /** Initialize power port for solar panel connection * * @param max_abs_current Maximum input current allowed by PCB (as a positive value) diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index d449ca6..282b3c4 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -152,10 +152,9 @@ bool PwmSwitch::signal_high() return pwm_signal_high(); } -PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int) +PwmSwitch::PwmSwitch(PowerPort *pwm_port) { - terminal = pwm_terminal; - port_int = pwm_port_int; + port = pwm_port; off_timestamp = -10000; // start immediately @@ -172,20 +171,19 @@ PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int) void PwmSwitch::control() { if (_pwm_active) { - if (port_int->pos_current_limit == 0 || terminal->neg_current_limit == 0 - || terminal->current > 0 // discharging battery into solar panel --> stop - || port_int->voltage < 9.0 // not enough voltage for MOSFET drivers anymore + if (port->neg_current_limit == 0 + || port->current > 0 // discharging battery into solar panel --> stop + || port->bus->voltage < 9.0 // not enough voltage for MOSFET drivers anymore || enabled == false) { pwm_signal_stop(); off_timestamp = uptime(); print_info("PWM charger stop.\n"); } - else if (port_int->voltage > (port_int->sink_voltage_max - - port_int->pos_droop_res * port_int->current) // voltage above target - || port_int->current > port_int->pos_current_limit // port current limit exceeded - || port_int->current > PWM_CURRENT_MAX // PCB current limit exceeded - || terminal->current < terminal->neg_current_limit) // input current (negative signs) above limit + else if (port->bus->voltage > (port->sink_voltage_max - + port->pos_droop_res * port->current) // voltage above target + || port->current < port->neg_current_limit // port current limit exceeded + || port->current < -PWM_CURRENT_MAX) // PCB current limit exceeded { // decrease power, as limits were reached @@ -218,11 +216,10 @@ void PwmSwitch::control() } } else { - if (port_int->pos_current_limit > 0 // charging allowed - && port_int->voltage < port_int->sink_voltage_max - && port_int->voltage > port_int->sink_voltage_min - && terminal->neg_current_limit < 0 // discharging allowed - && terminal->voltage > port_int->voltage + offset_voltage_start + if (port->pos_current_limit > 0 // charging allowed + && port->bus->voltage < port->bus->voltage_max + && port->bus->voltage > port->bus->voltage_min + && ext_voltage > port->bus->voltage + offset_voltage_start && uptime() > (off_timestamp + restart_interval) && enabled == true) { diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 5691fa0..845bb08 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -26,7 +26,7 @@ class PwmSwitch public: /** Initialization of PWM switch struct */ - PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int); + PwmSwitch(PowerPort *pwm_port); /** Main control function for the PWM switching algorithm */ @@ -56,8 +56,9 @@ public: */ float get_duty_cycle(); - PowerPort *terminal; ///< Pointer to external power port (terminal) - PowerPort *port_int; ///< Pointer to internal power port (junction with load and battery) + PowerPort *port; ///< Pointer to internal power port (junction with load and battery) + + float ext_voltage; ///< Voltage measurement at terminal (external) bool enabled; ///< Can be used to disable the PWM power stage float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V) diff --git a/test/main.cpp b/test/main.cpp index 5e41c12..ece9fe2 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -22,11 +22,13 @@ #include "tests.h" -PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) +DcBus lv_bus; +PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) #if CONFIG_HAS_DCDC_CONVERTER -PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter #if CONFIG_HV_TERMINAL_NANOGRID Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); #elif CONFIG_HV_TERMINAL_BATTERY @@ -37,13 +39,12 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); #endif #if CONFIG_HAS_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 pwm_terminal(&lv_bus); // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal); #endif #if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal; // load terminal (also connected to lv_bus) +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) LoadOutput load(&load_terminal); #endif diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index a17f973..99b8ba5 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -18,14 +18,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.voltage = 14.0; + bat_terminal.bus->voltage = 14.0; bat_terminal.current = 0; } void no_start_at_high_voltage() { init_structs(); - bat_terminal.voltage = bat_conf.voltage_recharge + 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_recharge + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -34,7 +34,7 @@ void no_start_after_short_rest() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1; - bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -43,7 +43,7 @@ void no_start_outside_temperature_limits() { init_structs(); charger.bat_temperature = bat_conf.charge_temp_max + 1; - bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); @@ -56,7 +56,7 @@ void start_if_everything_just_fine() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); } @@ -65,10 +65,10 @@ void enter_topping_at_voltage_setpoint() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal.voltage = bat_conf.voltage_recharge - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_recharge - 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); - bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -78,7 +78,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal.current = bat_conf.topping_current_cutoff + 0.1; charger.charge_control(&bat_conf); @@ -94,7 +94,7 @@ void stop_topping_at_cutoff_current() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 1; - bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -106,7 +106,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -118,7 +118,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.voltage = bat_conf.topping_voltage - 0.1; + bat_terminal.bus->voltage = bat_conf.topping_voltage - 0.1; bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; charger.charge_control(&bat_conf); @@ -141,7 +141,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -159,7 +159,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -173,7 +173,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -188,7 +188,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.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.bus->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); @@ -200,7 +200,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.voltage = bat_conf.equalization_voltage + 0.1; + bat_terminal.bus->voltage = bat_conf.equalization_voltage + 0.1; charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); @@ -219,11 +219,11 @@ void stop_discharge_at_low_voltage() { init_structs(); - bat_terminal.voltage = 14; + bat_terminal.bus->voltage = 14; charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); - bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect - 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); } @@ -253,17 +253,17 @@ void restart_discharge_if_allowed() init_structs(); // stop because of undervoltage - bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect - 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); // increase voltage slightly above DISconnect voltage - bat_terminal.voltage = bat_conf.voltage_load_disconnect + 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_load_disconnect + 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); // increase voltage above REconnect voltage - bat_terminal.voltage = bat_conf.voltage_load_reconnect + 0.1; + bat_terminal.bus->voltage = bat_conf.voltage_load_reconnect + 0.1; charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); } diff --git a/test/tests_daq.cpp b/test/tests_daq.cpp index ac628a5..7ed9a6b 100644 --- a/test/tests_daq.cpp +++ b/test/tests_daq.cpp @@ -40,27 +40,27 @@ void check_filtering() void check_solar_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.bus->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.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.bus->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.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.bus->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_lv_port.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_lv_port.bus->voltage * 10) / 10); TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_lv_port.current * 10) / 10); } diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index f2cbe93..08c60b2 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -17,13 +17,13 @@ static void init_structs_buck() { hv_terminal.init_solar(); - hv_terminal.voltage = 20; + hv_terminal.bus->voltage = 20; hv_terminal.src_voltage_start = 18; hv_terminal.current = 0; battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100); battery_init_dc_bus(&dcdc_lv_port, &bat_conf, 1); - dcdc_lv_port.voltage = 14; + dcdc_lv_port.bus->voltage = 14; dcdc_lv_port.current = 0; dcdc.mode = MODE_MPPT_BUCK; @@ -49,14 +49,14 @@ static void init_structs_boost() half_bridge_stop(); hv_terminal.init_solar(); - dcdc_lv_port.voltage = 20; + dcdc_lv_port.bus->voltage = 20; dcdc_lv_port.src_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(&hv_terminal, &bat_conf, 1); - hv_terminal.voltage = 3.7 * 10; + hv_terminal.bus->voltage = 3.7 * 10; hv_terminal.current = 0; hv_terminal.power = 0; @@ -109,7 +109,7 @@ void no_start_if_dcdc_disabled() void no_start_if_dcdc_lv_voltage_low() { init_structs_buck(); - dcdc_lv_port.voltage = dcdc.ls_voltage_min - 0.5; + dcdc_lv_port.bus->voltage = dcdc.ls_voltage_min - 0.5; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -118,14 +118,14 @@ void no_start_if_dcdc_lv_voltage_low() void no_buck_start_if_bat_voltage_low() { init_structs_buck(); - dcdc_lv_port.voltage = bat_conf.voltage_absolute_min - 0.1; + dcdc_lv_port.bus->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_lv_port.voltage = bat_conf.topping_voltage + 0.1; + dcdc_lv_port.bus->voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -139,14 +139,14 @@ void no_buck_start_if_bat_chg_not_allowed() void no_buck_start_if_solar_voltage_high() { init_structs_buck(); - hv_terminal.voltage = dcdc.hs_voltage_max + 1; + hv_terminal.bus->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(); - hv_terminal.voltage = 17; + hv_terminal.bus->voltage = 17; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -155,14 +155,14 @@ void no_buck_start_if_solar_voltage_low() void no_boost_start_if_bat_voltage_low() { init_structs_boost(); - hv_terminal.voltage = bat_conf.voltage_absolute_min - 0.1; + hv_terminal.bus->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(); - hv_terminal.voltage = bat_conf.topping_voltage + 0.1; + hv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -176,14 +176,14 @@ void no_boost_start_if_bat_chg_not_allowed() void no_boost_start_if_solar_voltage_high() { init_structs_boost(); - dcdc_lv_port.voltage = dcdc.ls_voltage_max + 1; + dcdc_lv_port.bus->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_lv_port.voltage = 17; + dcdc_lv_port.bus->voltage = 17; TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } @@ -202,7 +202,7 @@ void buck_derating_output_voltage_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_lv_port.voltage = dcdc_lv_port.sink_voltage_max + 0.1; + dcdc_lv_port.bus->voltage = dcdc_lv_port.sink_voltage_max + 0.1; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage @@ -224,7 +224,7 @@ void buck_derating_input_voltage_too_low() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.voltage = hv_terminal.src_voltage_stop - 0.1; + hv_terminal.bus->voltage = hv_terminal.src_voltage_stop - 0.1; dcdc_lv_port.current = 0.2; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); @@ -264,7 +264,7 @@ void buck_stop_input_power_too_low() void buck_stop_high_voltage_emergency() { start_buck(); - dcdc_lv_port.voltage = dcdc.ls_voltage_max + 0.1; + dcdc_lv_port.bus->voltage = dcdc.ls_voltage_max + 0.1; dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -303,7 +303,7 @@ void boost_derating_output_voltage_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.voltage = hv_terminal.sink_voltage_max + 0.5; + hv_terminal.bus->voltage = hv_terminal.sink_voltage_max + 0.5; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power @@ -323,7 +323,7 @@ void boost_derating_input_voltage_too_low() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_lv_port.voltage = dcdc_lv_port.src_voltage_stop - 0.1; + dcdc_lv_port.bus->voltage = dcdc_lv_port.src_voltage_stop - 0.1; hv_terminal.current = 0.2; dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); @@ -361,7 +361,7 @@ void boost_stop_input_power_too_low() void boost_stop_high_voltage_emergency() { start_boost(); - hv_terminal.voltage = dcdc.hs_voltage_max + 0.1; + hv_terminal.bus->voltage = dcdc.hs_voltage_max + 0.1; dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } diff --git a/test/tests_device_status.cpp b/test/tests_device_status.cpp index d6b0d96..0f8b36d 100644 --- a/test/tests_device_status.cpp +++ b/test/tests_device_status.cpp @@ -14,7 +14,7 @@ void reset_counters_at_start_of_day() { - solar_terminal.voltage = bat_terminal.voltage - 1; + solar_terminal.bus->voltage = bat_terminal.bus->voltage - 1; dev_stat.day_counter = 0; @@ -42,7 +42,7 @@ void reset_counters_at_start_of_day() TEST_ASSERT_EQUAL(0, dev_stat.day_counter); // now solar power comes back - solar_terminal.voltage = bat_terminal.voltage + 1; + solar_terminal.bus->voltage = bat_terminal.bus->voltage + 1; dev_stat.update_energy(); // day counter should be increased and daily energy counters reset @@ -55,14 +55,14 @@ void reset_counters_at_start_of_day() void dev_stat_new_solar_voltage_max() { - solar_terminal.voltage = 40; + solar_terminal.bus->voltage = 40; dev_stat.update_min_max_values(); TEST_ASSERT_EQUAL(40, dev_stat.solar_voltage_max); } void dev_stat_new_bat_voltage_max() { - bat_terminal.voltage = 31; + bat_terminal.bus->voltage = 31; dev_stat.update_min_max_values(); TEST_ASSERT_EQUAL(31, dev_stat.battery_voltage_max); } diff --git a/test/tests_load.cpp b/test/tests_load.cpp index c1d83c4..6ea56ca 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -18,13 +18,14 @@ void load_init(LoadOutput *l) { l->port->init_load(14.6); l->port->pos_current_limit = 10; - l->port->voltage = 14; + l->port->bus->voltage = 14; l->junction_temperature = 25; } void control_off_to_pgood_if_everything_fine() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); dev_stat.clear_error(ERR_ANY_ERROR); @@ -37,12 +38,13 @@ void control_off_to_pgood_if_everything_fine() void control_pgood_to_off_overvoltage() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_ON; dev_stat.clear_error(ERR_ANY_ERROR); - port.voltage = port.sink_voltage_max + 0.6; + port.bus->voltage = port.sink_voltage_max + 0.6; // increase debounce counter to 1 before limit for (int i = 0; i < CONTROL_FREQUENCY; i++) { @@ -60,7 +62,8 @@ void control_pgood_to_off_overvoltage() void control_pgood_to_off_overcurrent() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); port.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately @@ -84,7 +87,8 @@ void control_pgood_to_off_overcurrent() void control_pgood_to_off_voltage_dip() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_ON; @@ -104,7 +108,8 @@ void control_pgood_to_off_voltage_dip() void control_pgood_to_off_int_temp() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_ON; @@ -121,7 +126,8 @@ void control_pgood_to_off_int_temp() void control_pgood_to_off_bat_temp() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); @@ -150,7 +156,8 @@ void control_pgood_to_off_bat_temp() void control_pgood_to_off_low_soc() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); @@ -168,7 +175,8 @@ void control_pgood_to_off_low_soc() void control_pgood_to_off_if_enable_false() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); dev_stat.clear_error(ERR_ANY_ERROR); @@ -194,7 +202,8 @@ void control_pgood_to_off_if_enable_false() void control_off_low_soc_to_on_after_delay() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_OFF_LOW_SOC; @@ -218,7 +227,8 @@ void control_off_low_soc_to_on_after_delay() void control_off_overcurrent_to_on_after_delay() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_OFF_OVERCURRENT; @@ -240,12 +250,13 @@ void control_off_overcurrent_to_on_after_delay() void control_off_overvoltage_to_on_at_lower_voltage() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_OFF_OVERVOLTAGE; load.usb_state = LOAD_STATE_ON; - port.voltage = port.sink_voltage_max + 0.1; + port.bus->voltage = port.sink_voltage_max + 0.1; dev_stat.error_flags = ERR_LOAD_OVERVOLTAGE; load.control(); @@ -253,12 +264,12 @@ void control_off_overvoltage_to_on_at_lower_voltage() TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage - port.voltage = port.sink_voltage_max - 0.1; // test hysteresis + port.bus->voltage = port.sink_voltage_max - 0.1; // test hysteresis load.control(); TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); - port.voltage = port.sink_voltage_max - load.ov_hysteresis - 0.1; + port.bus->voltage = port.sink_voltage_max - load.ov_hysteresis - 0.1; load.control(); TEST_ASSERT_EQUAL(0, dev_stat.error_flags); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated @@ -267,7 +278,8 @@ void control_off_overvoltage_to_on_at_lower_voltage() void control_off_short_circuit_flag_reset() { - PowerPort port = {}; + DcBus bus = {}; + PowerPort port(&bus); LoadOutput load(&port); load_init(&load); load.state = LOAD_STATE_OFF_SHORT_CIRCUIT; diff --git a/test/tests_power_port.cpp b/test/tests_power_port.cpp index b2eff3b..59b21e6 100644 --- a/test/tests_power_port.cpp +++ b/test/tests_power_port.cpp @@ -69,26 +69,26 @@ void charging_energy_calculation_valid() { energy_calculation_init(); // charging only during sun hours - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * (dcdc_current_sun - load_current)), round(lv_terminal.pos_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * (dcdc_current_sun - load_current)), round(lv_terminal.pos_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.voltage * adcval.load_current), round(lv_terminal.neg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.bus->voltage * adcval.load_current), round(lv_terminal.neg_energy_Wh)); } void solar_input_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * dcdc_current_sun), round(hv_terminal.neg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * dcdc_current_sun), round(hv_terminal.neg_energy_Wh)); } void load_output_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.voltage * adcval.load_current), round(load_terminal.pos_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load_terminal.pos_energy_Wh)); } void pass_voltage_targets_to_adjacent_bus()