diff --git a/src/bat_charger.h b/src/bat_charger.h index cbdb96a..9fd4923 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -35,87 +35,102 @@ enum BatType { BAT_TYPE_NMC_HV ///< NMC/Graphite High Voltage Li-ion batteries (3.7V nominal, 4.35 max) }; -/** Battery configuration data +/** + * Battery configuration data * * Data will be initialized in battery_init depending on configured cell type in config.h. */ typedef struct { - /** Nominal battery capacity or sum of parallel cells capacity (Ah) + /** + * Nominal battery capacity or sum of parallel cells capacity (Ah) * * The nominal capacity is used for SOC calculation and definition of current limits. */ float nominal_capacity; - /** Recharge voltage (V) + /** + * Recharge voltage (V) * * Start charging again if voltage of fully charged battery drops below this threshold. * - * Remark: Setting the value too close to the max voltage will cause more charging stress on lithium based batteries. + * Remark: Setting the value too close to the max voltage will cause more charging stress on + * lithium based batteries. */ float voltage_recharge; - /** Recharge time limit (sec) + /** + * Recharge time limit (sec) * * Start charging of previously fully charged battery earliest after this period of time. */ int32_t time_limit_recharge; - /** Absolute maximum voltage (V) + /** + * Absolute maximum voltage (V) * * Above this voltage the battery or loads might get damaged. */ float voltage_absolute_max; - /** Absolute minimum voltage (V) + /** + * Absolute minimum voltage (V) * * Below this voltage the battery is considered damaged. */ float voltage_absolute_min; - /** Maximum charge current in *CC/bulk* phase (A) + /** + * Maximum charge current in *CC/bulk* phase (A) * * Limits the current if the PCB allows more than the battery. (positive value) */ float charge_current_max; - /** Maximum discharge current via load port (A) + /** + * Maximum discharge current via load port (A) * * Limits the current if the PCB allows more than the battery. (positive value) */ float discharge_current_max; - /** Maximum voltage in CV/absorption phase (V) + /** + * Maximum voltage in CV/absorption phase (V) * * Charger target voltage, switching from CC to CV at this voltage threshold. */ float topping_voltage; - /** CV phase cut-off current limit (A) + /** + * CV phase cut-off current limit (A) * * Constant voltage charging phase stopped if current is below this value. */ float topping_current_cutoff; - /** CV phase cut-off time limit (s) + /** + * CV phase cut-off time limit (s) * * After this time, CV charging is stopped independent of current. */ int32_t topping_duration; - /** Enable float/trickle charging + /** + * Enable float/trickle charging * * Caution: Do not enable trickle charging for lithium-ion batteries */ bool trickle_enabled; - /** Trickle voltage (V) + /** + * Trickle voltage (V) * * Charger target voltage for trickle charging of lead-acid batteries */ float trickle_voltage; - /** Trickle recharge time (s) + /** + * Trickle recharge time (s) * * If the trickle voltage is not reached anymore (e.g. because of lack of solar * input power) for this period of time, the charger state machine goes back @@ -123,43 +138,50 @@ typedef struct */ int32_t trickle_recharge_time; - /** Enable equalization charging + /** + * Enable equalization charging * * Caution: Do not enable trickle charging for lithium-ion batteries */ bool equalization_enabled; - /** Equalization voltage (V) + /** + * Equalization voltage (V) * * Charger target voltage for equalization charging of lead-acid batteries */ float equalization_voltage; - /** Equalization cut-off time limit (s) + /** + * Equalization cut-off time limit (s) * * After this time, equalization charging is stopped. */ int32_t equalization_duration; - /** Equalization phase maximum current (A) + /** + * Equalization phase maximum current (A) * * The current of the charger is controlled to stay below this limit during equalization. */ float equalization_current_limit; - /** Equalization trigger time interval (days) + /** + * Equalization trigger time interval (days) * * After passing specified amount of days, an equalization charge is triggered. */ int32_t equalization_trigger_days; - /** Equalization trigger deep-discharge cycles + /** + * Equalization trigger deep-discharge cycles * * After specified number of deep discharges, an equalization charge is triggered. */ int32_t equalization_trigger_deep_cycles; - /** Load disconnect open circuit voltage (V) + /** + * Load disconnect open circuit voltage (V) * * Load output is disabled if battery voltage is below this threshold. * @@ -170,7 +192,8 @@ typedef struct */ float voltage_load_disconnect; - /** Load reconnect open circuit voltage (V) + /** + * Load reconnect open circuit voltage (V) * * Re-enable the load only after charged beyond this value. * @@ -181,43 +204,62 @@ typedef struct */ float voltage_load_reconnect; - /** Battery internal resistance (Ohm) + /** + * Battery internal resistance (Ohm) * * Resistance value for current-compensation of load switch voltage thresholds. * - * The value must have a positive sign. Only resistance values leading to less than 10% voltage drop at max. current are allowed. + * The value must have a positive sign. Only resistance values leading to less than 10% voltage + * drop at max. current are allowed. */ float internal_resistance; - /** Resistance of wire between charge controller and battery (Ohm) + /** + * Resistance of wire between charge controller and battery (Ohm) * * Resistance value for current-compensation of charging voltages. * - * The value must have a positive sign. Only resistance values leading to less than 3% voltage loss at max. current are allowed. + * The value must have a positive sign. Only resistance values leading to less than 3% voltage + * loss at max. current are allowed. */ float wire_resistance; - // used to calculate state of charge information + /** + * Open circuit voltage of full battery (V) + * + * Used for simple state of charge (SOC) algorithm. + */ float ocv_full; + + /** + * Open circuit voltage of empty battery (V) + * + * Used for simple state of charge (SOC) algorithm. + */ float ocv_empty; - /** Maximum allowed charging temperature of the battery (°C) + /** + * Maximum allowed charging temperature of the battery (°C) */ float charge_temp_max; - /** Minimum allowed charging temperature of the battery (°C) + /** + * Minimum allowed charging temperature of the battery (°C) */ float charge_temp_min; - /** Maximum allowed discharging temperature of the battery (°C) + /** + * Maximum allowed discharging temperature of the battery (°C) */ float discharge_temp_max; - /** Minimum allowed discharging temperature of the battery (°C) + /** + * Minimum allowed discharging temperature of the battery (°C) */ float discharge_temp_min; - /** Voltage compensation based on battery temperature (mV/K/cell) + /** + * Voltage compensation based on battery temperature (mV/K/cell) * * Suggested value: -3 mV/K/cell */ @@ -225,7 +267,8 @@ typedef struct } BatConf; -/** Charger configuration and battery state +/** + * Charger configuration and battery state */ class Charger { @@ -235,53 +278,111 @@ public: PowerPort *port; - uint32_t state; ///< Current charger state (see enum ChargerState) + /** + * Current charger state (see enum ChargerState) + */ + uint32_t state; - 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 + /** + * Battery temperature (°C) from ext. temperature sensor (if existing) + */ + float bat_temperature = 25; - float usable_capacity; ///< Estimated usable capacity (Ah) based on coulomb counting + /** + * Flag to indicate if external temperature sensor was detected + */ + bool ext_temp_sensor; - float discharged_Ah; ///< Coulomb counter for SOH calculation + /** + * Estimated usable capacity (Ah) based on coulomb counting + */ + float usable_capacity; - uint16_t num_full_charges; ///< Number of full charge cycles - uint16_t num_deep_discharges; ///< Number of deep-discharge cycles + /** + * Coulomb counter for SOH calculation + */ + float discharged_Ah; - uint16_t soc = 100; ///< State of Charge (%) - uint16_t soh = 100; ///< State of Health (%) + /** + * Number of full charge cycles + */ + uint16_t num_full_charges; - time_t time_state_changed; ///< Timestamp of last state change - time_t time_target_voltage_reached; ///< Last time the CV limit was reached + /** + * Number of deep-discharge cycles + */ + uint16_t num_deep_discharges; - int target_voltage_timer; ///< Counts the number of seconds during which the target - ///< voltage of current charging phase was reached. + /** + * State of Charge (%) + */ + uint16_t soc = 100; - time_t time_last_equalization; ///< Timestamp after finish of last equalization charge - int deep_dis_last_equalization; ///< Deep discharge counter value after last equalization + /** + * State of Health (%) + */ + uint16_t soh = 100; - bool full; ///< Flag to indicate if battery was fully charged - bool empty; ///< Flag to indicate if battery was completely discharged + /** + * Timestamp of last state change + */ + time_t time_state_changed; - /** Detect if two batteries are connected in series (12V/24V auto-detection) + /** + * 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); - /** Discharging control update (for load output), should be called once per second + /** + * 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 + /** + * Charger state machine update, should be called once per second */ void charge_control(BatConf *bat_conf); - /** SOC estimation + /** + * 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 + /** + * Initialize terminal and dc bus for battery connection * * @param bat Configuration to be used for terminal setpoints */ @@ -292,7 +393,8 @@ private: }; -/** Possible charger states +/** + * Possible charger states * * Further information: * - https://en.wikipedia.org/wiki/IUoU_battery_charging @@ -300,21 +402,24 @@ private: */ enum ChargerState { - /** Idle + /** + * Idle * * Initial state of the charge controller. If the solar voltage is high enough * and the battery is not full, bulk charging mode is started. */ CHG_STATE_IDLE, - /** Bulk / CC / MPPT charging + /** + * Bulk / CC / MPPT charging * * The battery is charged with maximum possible current (MPPT algorithm is * active) until the CV voltage limit is reached. */ CHG_STATE_BULK, - /** Topping / CV / absorption charging + /** + * Topping / CV / absorption charging * * Lead-acid batteries are charged for some time using a slightly higher charge * voltage. After a current cutoff limit or a time limit is reached, the charger @@ -323,7 +428,8 @@ enum ChargerState { */ CHG_STATE_TOPPING, - /** Trickle charging + /** + * Trickle charging * * This mode is kept forever for a lead-acid battery and keeps the battery at * full state of charge. If too much power is drawn from the battery, the @@ -331,7 +437,8 @@ enum ChargerState { */ CHG_STATE_TRICKLE, - /** Equalization charging + /** + * Equalization charging * * This mode is only used for lead-acid batteries after several deep-discharge * cycles or a very long period of time with no equalization. Voltage is @@ -342,7 +449,8 @@ enum ChargerState { CHG_STATE_EQUALIZATION }; -/** Basic initialization of battery configuration +/** + * Basic initialization of battery configuration * * Configures battery based on settings defined in config.h and initializes * bat_user with same values @@ -355,18 +463,21 @@ enum ChargerState { */ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capacity); -/** Checks battery user settings for plausibility +/** + * Checks battery user settings for plausibility */ bool battery_conf_check(BatConf *bat); -/** Overwrites battery settings (config should be checked first) +/** + * Overwrites battery settings (config should be checked first) * * Settings specified in bat_user will be copied to actual battery_t, * if suggested updates are valid (includes plausibility check!) */ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *charger = NULL); -/** Checks if incoming configuration is different to current configuration +/** + * Checks if incoming configuration is different to current configuration * * Returns true if changed */