From 9765798944190c713ec71945be773b9e570aaac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 25 Feb 2021 08:47:29 +0100 Subject: [PATCH] Charger: Fix state machine startup behavior If the device starts up with the battery voltage > bat_conf->voltage_recharge, the charger state machine did not leave CHG_STATE_IDLE. However, the PWM solar input still started charging the battery and stayed at bulk charging voltage all the time. Now we don't check the recharge limits if the CHG_STATE_IDLE was reached for the first time, so that the state machine goes into CHG_STATE_BULK properly. Also fixing some formatting and moving the state enum before Charger class. --- src/bat_charger.cpp | 13 ++- src/bat_charger.h | 253 ++++++++++++++++++++++---------------------- 2 files changed, 136 insertions(+), 130 deletions(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index aa60f94..9824514 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -453,7 +453,8 @@ void Charger::charge_control(BatConf *bat_conf) } if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) && - port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5F) * port->bus->series_multiplier) + port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5F) * + port->bus->series_multiplier) { dev_stat.clear_error(ERR_BAT_OVERVOLTAGE); } @@ -461,9 +462,13 @@ void Charger::charge_control(BatConf *bat_conf) // state machine switch (state) { case CHG_STATE_IDLE: { - 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 + if ((time_state_changed == CHARGER_TIME_NEVER || + ((uptime() - time_state_changed) > bat_conf->time_limit_recharge && + 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 && bat_temperature < bat_conf->charge_temp_max - 1 && bat_temperature > bat_conf->charge_temp_min + 1) { diff --git a/src/bat_charger.h b/src/bat_charger.h index 96eeda9..f9f9172 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -19,6 +19,8 @@ #include "power_port.h" +#define CHARGER_TIME_NEVER INT32_MIN + /** * Battery cell types */ @@ -267,132 +269,6 @@ typedef struct } BatConf; -/** - * Charger configuration and battery state - */ -class Charger -{ -public: - Charger(PowerPort *pwr_port): - port(pwr_port) {}; - - PowerPort *port; - - /** - * Current charger state (see enum ChargerState) - */ - uint32_t state; - - /** - * Battery temperature (°C) from ext. temperature sensor (if existing) - */ - float bat_temperature = 25; - - /** - * Flag to indicate if external temperature sensor was detected - */ - bool ext_temp_sensor; - - /** - * Estimated usable capacity (Ah) based on coulomb counting - */ - float usable_capacity; - - /** - * Coulomb counter for SOH calculation - */ - float discharged_Ah; - - /** - * Number of full charge cycles - */ - uint16_t num_full_charges; - - /** - * Number of deep-discharge cycles - */ - uint16_t num_deep_discharges; - - /** - * State of Charge (%) - */ - uint16_t soc = 100; - - /** - * State of Health (%) - */ - uint16_t soh = 100; - - /** - * Timestamp of last state change - */ - time_t time_state_changed; - - /** - * Last time the CV limit was reached - */ - time_t time_target_voltage_reached; - - /** - * Counts the number of seconds during which the target voltage of current charging phase was - * reached. - */ - int target_voltage_timer; - - /** - * Timestamp after finish of last equalization charge - */ - time_t time_last_equalization; - - /** - * Deep discharge counter value after last equalization - */ - int deep_dis_last_equalization; - - /** - * Flag to indicate if battery was fully charged - */ - bool full; - - /** - * Flag to indicate if battery was completely discharged - */ - bool empty; - - /** - * Detect if two batteries are connected in series (12V/24V auto-detection) - */ - void detect_num_batteries(BatConf *bat) const; - - /** - * Discharging control update (for load output), should be called once per second - */ - void discharge_control(BatConf *bat_conf); - - /** - * Charger state machine update, should be called once per second - */ - void charge_control(BatConf *bat_conf); - - /** - * SOC estimation - * - * Must be called exactly once per second, otherwise SOC calculation gets wrong. - */ - void update_soc(BatConf *bat_conf); - - /** - * Initialize terminal and dc bus for battery connection - * - * @param bat Configuration to be used for terminal setpoints - */ - void init_terminal(BatConf *bat) const; - -private: - void enter_state(int next_state); -}; - - /** * Possible charger states * @@ -449,6 +325,131 @@ enum ChargerState { CHG_STATE_EQUALIZATION }; +/** + * Charger configuration and battery state + */ +class Charger +{ +public: + Charger(PowerPort *pwr_port): + port(pwr_port) {}; + + PowerPort *port; + + /** + * Current charger state (see enum ChargerState) + */ + uint32_t state = CHG_STATE_IDLE; + + /** + * Battery temperature (°C) from ext. temperature sensor (if existing) + */ + float bat_temperature = 25; + + /** + * Flag to indicate if external temperature sensor was detected + */ + bool ext_temp_sensor; + + /** + * Estimated usable capacity (Ah) based on coulomb counting + */ + float usable_capacity; + + /** + * Coulomb counter for SOH calculation + */ + float discharged_Ah; + + /** + * Number of full charge cycles + */ + uint16_t num_full_charges; + + /** + * Number of deep-discharge cycles + */ + uint16_t num_deep_discharges; + + /** + * State of Charge (%) + */ + uint16_t soc = 100; + + /** + * State of Health (%) + */ + uint16_t soh = 100; + + /** + * Timestamp of last state change + */ + time_t time_state_changed = CHARGER_TIME_NEVER; + + /** + * Last time the CV limit was reached + */ + time_t time_target_voltage_reached = CHARGER_TIME_NEVER; + + /** + * Counts the number of seconds during which the target voltage of current charging phase was + * reached. + */ + int target_voltage_timer; + + /** + * Timestamp after finish of last equalization charge + */ + time_t time_last_equalization = CHARGER_TIME_NEVER; + + /** + * Deep discharge counter value after last equalization + */ + int deep_dis_last_equalization; + + /** + * Flag to indicate if battery was fully charged + */ + bool full; + + /** + * Flag to indicate if battery was completely discharged + */ + bool empty; + + /** + * Detect if two batteries are connected in series (12V/24V auto-detection) + */ + void detect_num_batteries(BatConf *bat) const; + + /** + * Discharging control update (for load output), should be called once per second + */ + void discharge_control(BatConf *bat_conf); + + /** + * Charger state machine update, should be called once per second + */ + void charge_control(BatConf *bat_conf); + + /** + * SOC estimation + * + * Must be called exactly once per second, otherwise SOC calculation gets wrong. + */ + void update_soc(BatConf *bat_conf); + + /** + * Initialize terminal and dc bus for battery connection + * + * @param bat Configuration to be used for terminal setpoints + */ + void init_terminal(BatConf *bat) const; + +private: + void enter_state(int next_state); +}; + /** * Basic initialization of battery configuration *