From 1a9c290485ec6cb1a9e3c690e0eef275711c2716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 1 Mar 2020 09:48:31 +0100 Subject: [PATCH] Charger.num_batteries moved to DcBus.series_multiplier The reason is that the voltage disconnect thresholds used in the load outputs need to be multiplied with number of batteries aswell. As the DcBus class should contain all voltage-relevant information, it is the right place for the multiplier. --- src/bat_charger.cpp | 73 ++++++++++++++++++++++++-------------------- src/bat_charger.h | 4 --- src/data_objects.cpp | 9 +++--- src/dcdc.cpp | 4 +-- src/load.cpp | 4 +-- src/main_mbed.cpp | 6 ++-- src/main_zephyr.cpp | 6 ++-- src/power_port.h | 26 +++++++++++----- src/pwm_switch.cpp | 2 +- 9 files changed, 72 insertions(+), 62 deletions(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 5a9502a..e759e8e 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -243,7 +243,7 @@ void Charger::detect_num_batteries(BatConf *bat) { if (port->bus->voltage > bat->voltage_absolute_min * 2 && port->bus->voltage < bat->voltage_absolute_max * 2) { - num_batteries = 2; + port->bus->series_multiplier = 2; printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } else { @@ -292,8 +292,8 @@ 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->bus->voltage < num_batteries * - port->bus->src_droop_voltage(bat_conf->voltage_load_disconnect)) + if (port->bus->voltage < + port->bus->src_control_voltage(bat_conf->voltage_load_disconnect)) { uv_debounce_counter++; if (uv_debounce_counter >= 3) { // 3s in undervoltage --> switch off @@ -333,8 +333,7 @@ void Charger::discharge_control(BatConf *bat_conf) } else { // discharging currently not allowed. should we allow it? - if (port->bus->voltage >= num_batteries * - port->bus->src_droop_voltage(bat_conf->voltage_load_reconnect) + if (port->bus->voltage >= port->bus->src_control_voltage(bat_conf->voltage_load_reconnect) && bat_temperature < bat_conf->discharge_temp_max - 1 && bat_temperature > bat_conf->discharge_temp_min + 1) { @@ -365,7 +364,7 @@ void Charger::charge_control(BatConf *bat_conf) } if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) && - port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries) + port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5) * port->bus->series_multiplier) { dev_stat.clear_error(ERR_BAT_OVERVOLTAGE); } @@ -373,14 +372,14 @@ void Charger::charge_control(BatConf *bat_conf) // state machine switch (state) { case CHG_STATE_IDLE: { - if (port->bus->voltage < bat_conf->voltage_recharge * num_batteries - && port->bus->voltage > bat_conf->voltage_absolute_min * num_batteries // ToDo set error flag otherwise + if (port->bus->voltage < port->bus->sink_control_voltage(bat_conf->voltage_recharge) + && port->bus->voltage > port->bus->sink_control_voltage(bat_conf->voltage_absolute_min) // ToDo set error flag otherwise && (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) { - port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); port->pos_current_limit = bat_conf->charge_current_max; full = false; dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP); @@ -392,10 +391,10 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_BULK: { // continuously adjust voltage setting for temperature compensation - port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); - if (port->bus->voltage > port->bus->sink_droop_voltage()) { + if (port->bus->voltage > port->bus->sink_control_voltage()) { target_voltage_timer = 0; enter_state(CHG_STATE_TOPPING); } @@ -403,10 +402,10 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_TOPPING: { // continuously adjust voltage setting for temperature compensation - port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); - if (port->bus->voltage >= port->bus->sink_droop_voltage() - 0.05F) { + if (port->bus->voltage >= port->bus->sink_control_voltage() - 0.05F) { // battery is full if topping target voltage is still reached (i.e. sufficient // solar power available) and time limit or cut-off current reached if (port->current < bat_conf->topping_current_cutoff || @@ -432,13 +431,13 @@ void Charger::charge_control(BatConf *bat_conf) num_deep_discharges - deep_dis_last_equalization >= bat_conf->equalization_trigger_deep_cycles)) { - port->bus->sink_voltage_bound = num_batteries * bat_conf->equalization_voltage; + port->bus->sink_voltage_bound = bat_conf->equalization_voltage; port->pos_current_limit = bat_conf->equalization_current_limit; enter_state(CHG_STATE_EQUALIZATION); } else if (bat_conf->trickle_enabled) { - port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); enter_state(CHG_STATE_TRICKLE); } else { @@ -450,10 +449,10 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_TRICKLE: { // continuously adjust voltage setting for temperature compensation - port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); - if (port->bus->voltage >= port->bus->sink_droop_voltage()) { + if (port->bus->voltage >= port->bus->sink_control_voltage()) { time_target_voltage_reached = uptime(); } @@ -470,8 +469,8 @@ void Charger::charge_control(BatConf *bat_conf) } case CHG_STATE_EQUALIZATION: { // continuously adjust voltage setting for temperature compensation - port->bus->sink_voltage_bound = num_batteries * (bat_conf->equalization_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->equalization_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); // current or time limit for equalization reached if (uptime() - time_state_changed > bat_conf->equalization_duration) @@ -483,8 +482,8 @@ void Charger::charge_control(BatConf *bat_conf) discharged_Ah = 0; // reset coulomb counter again if (bat_conf->trickle_enabled) { - port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (bat_temperature - 25)); + port->bus->sink_voltage_bound = bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25); enter_state(CHG_STATE_TRICKLE); } else { @@ -499,16 +498,24 @@ void Charger::charge_control(BatConf *bat_conf) void Charger::init_terminal(BatConf *bat) { - port->bus->sink_voltage_bound = bat->topping_voltage * num_batteries; - port->bus->src_voltage_bound = bat->voltage_load_disconnect * num_batteries; + port->bus->sink_voltage_bound = bat->topping_voltage; + port->bus->src_voltage_bound = bat->voltage_load_disconnect; port->neg_current_limit = -bat->discharge_current_max; port->pos_current_limit = bat->charge_current_max; - // negative sign for compensation of actual resistance - port->bus->sink_droop_res = -bat->wire_resistance; + /* + * Negative sign for compensation of actual resistance + * + * droop_res is multiplied with number of series connected batteries to calculate control + * voltage, so we need to divide by number of batteries here for correction + */ + port->bus->sink_droop_res = -bat->wire_resistance / port->bus->series_multiplier; - // in discharging direction also include battery internal resistance for current-compensation - // of voltage setpoints - port->bus->src_droop_res = -bat->wire_resistance - bat->internal_resistance * num_batteries; + /* + * In discharging direction also include battery internal resistance for current-compensation + * of voltage setpoints + */ + port->bus->src_droop_res = -bat->wire_resistance / port->bus->series_multiplier + - bat->internal_resistance; } diff --git a/src/bat_charger.h b/src/bat_charger.h index 40290de..b339ad2 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -18,7 +18,6 @@ #include #include "power_port.h" -#include "load.h" /** Battery cell types */ @@ -234,9 +233,6 @@ public: uint32_t state; ///< Current charger state (see enum ChargerState) - int32_t num_batteries = 1; ///< Used for automatic 12V/24V battery detection at - ///< start-up (can be 1 or 2 only) - float bat_temperature = 25; ///< Battery temperature in °C from ext. temperature sensor ///< (if existing) bool ext_temp_sensor; ///< True if external temperature sensor was detected diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 69f0c76..6e93ce9 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -277,6 +277,9 @@ const data_object_t data_objects[] = { TS_OUTPUT, TS_READ_ALL), #endif + TS_DATA_OBJ_INT16(0x8F, "NumBatteries", &lv_bus.series_multiplier, + TS_OUTPUT, TS_READ_ALL), + TS_DATA_OBJ_UINT32(0x90, "ErrorFlags", &dev_stat.error_flags, TS_OUTPUT, TS_READ_ALL), @@ -447,10 +450,8 @@ void data_objects_update_conf() if (battery_conf_check(&bat_conf_user)) { printf("New config valid and activated.\n"); battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger); - load.set_voltage_limits( - bat_conf.voltage_load_disconnect * charger.num_batteries, - bat_conf.voltage_load_reconnect * charger.num_batteries, - bat_conf.voltage_absolute_max * charger.num_batteries); + load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, + bat_conf.voltage_absolute_max); changed = true; } else { diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 1a18ac4..e8061e0 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -78,7 +78,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->bus->voltage > out->bus->sink_droop_voltage()) { + else if (out->bus->voltage > out->bus->sink_control_voltage()) { // output voltage target reached state = DCDC_STATE_CV; pwr_inc_goal = -1; // decrease output power @@ -90,7 +90,7 @@ 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->src_droop_voltage() + || (in->bus->voltage < in->bus->src_control_voltage() && out->current > 0.1) // input voltage below limit || in->current < in->neg_current_limit) // input current (negative signs) limit exceeded { diff --git a/src/load.cpp b/src/load.cpp index 260ca91..373c9dd 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -56,7 +56,7 @@ void LoadOutput::control() flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT); } - if (bus->voltage < bus->src_droop_voltage(disconnect_voltage)) { + if (bus->voltage < bus->src_control_voltage(disconnect_voltage)) { flags_set(&error_flags, ERR_LOAD_SHEDDING); lvd_timestamp = uptime(); } @@ -86,7 +86,7 @@ void LoadOutput::control() // load is off: check if errors are resolved and if load can be switched on if (flags_check(&error_flags, ERR_LOAD_SHEDDING) && - bus->voltage > bus->src_droop_voltage(reconnect_voltage) && + bus->voltage > bus->src_control_voltage(reconnect_voltage) && uptime() - lvd_timestamp > lvd_recovery_delay) { flags_clear(&error_flags, ERR_LOAD_SHEDDING); diff --git a/src/main_mbed.cpp b/src/main_mbed.cpp index b72f691..8bc6584 100644 --- a/src/main_mbed.cpp +++ b/src/main_mbed.cpp @@ -72,10 +72,8 @@ int main() charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system charger.init_terminal(&bat_conf); - load.set_voltage_limits( - bat_conf.voltage_load_disconnect * charger.num_batteries, - bat_conf.voltage_load_reconnect * charger.num_batteries, - bat_conf.voltage_absolute_max * charger.num_batteries); + load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, + bat_conf.voltage_absolute_max); wait(2); // safety feature: be able to re-flash before starting control_timer_start(CONTROL_FREQUENCY); diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index 28ff6ef..797fba5 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -64,10 +64,8 @@ void main(void) charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system charger.init_terminal(&bat_conf); - load.set_voltage_limits( - bat_conf.voltage_load_disconnect * charger.num_batteries, - bat_conf.voltage_load_reconnect * charger.num_batteries, - bat_conf.voltage_absolute_max * charger.num_batteries); + load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, + bat_conf.voltage_absolute_max); k_sleep(2000); // safety feature: be able to re-flash before starting diff --git a/src/power_port.h b/src/power_port.h index a4e3a4f..a022f39 100644 --- a/src/power_port.h +++ b/src/power_port.h @@ -12,6 +12,7 @@ * @brief Definition of charge controller terminals and internal DC buses */ +#include #include class PowerPort; // forward-declaration @@ -29,6 +30,15 @@ public: */ float voltage; + /** + * Multiplier for series connection of batteries + * + * Used for automatic 12V/24V battery detection at start-up (can be 1 or 2 only) + * + * This factur must be applied to all voltage setpoints + */ + int16_t series_multiplier = 1; + /** * Upper voltage boundary where this bus may be used to sink current. * @@ -78,36 +88,36 @@ public: float src_current_margin; /** - * Calculate current-compensated voltage based on bus reference current + * Calculate current-compensated src control voltage, considering droop and series multiplier * * @param voltage_zero_current Voltage at zero current (without droop). If this parameter is * left empty, the src_voltage_bound is considered. */ - inline float src_droop_voltage(float voltage_zero_current = 0) + inline float src_control_voltage(float voltage_zero_current = 0) { float v0 = (voltage_zero_current == 0) ? src_voltage_bound : voltage_zero_current; if (ref_current != nullptr) { - return v0 - src_droop_res * (*ref_current); + return (v0 - src_droop_res * (*ref_current)) * series_multiplier; } else { - return v0; + return v0 * series_multiplier; } } /** - * Calculate current-compensated voltage based on bus reference current + * Calculate current-compensated sink control voltage, considering droop and series multiplier * * @param voltage_zero_current Voltage at zero current (without droop). If this parameter is * left empty, the sink_voltage_bound is considered. */ - inline float sink_droop_voltage(float voltage_zero_current = 0) + inline float sink_control_voltage(float voltage_zero_current = 0) { float v0 = (voltage_zero_current == 0) ? sink_voltage_bound : voltage_zero_current; if (ref_current != nullptr) { - return v0 - sink_droop_res * (*ref_current); + return (v0 - sink_droop_res * (*ref_current)) * series_multiplier; } else { - return v0; + return v0 * series_multiplier; } } }; diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 2361cb5..e49812f 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -179,7 +179,7 @@ void PwmSwitch::control() off_timestamp = uptime(); print_info("PWM charger stop.\n"); } - else if (bus->voltage > bus->sink_droop_voltage() // bus voltage above target + else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target || current < neg_current_limit // port current limit exceeded || current < -PWM_CURRENT_MAX) // PCB current limit exceeded {