From ab63204adbd961b6656af7a27971d2faf1969f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 17 Aug 2020 16:49:46 +0200 Subject: [PATCH] Make battery configurable via Kconfig (fix #94) Also adds crazy IS_ENABLED macro to unit tests. --- src/bat_charger.cpp | 51 ++++++++++- src/bat_charger.h | 2 +- src/main.cpp | 3 +- test/autoconf.h | 26 ++++++ test/zephyr.h | 60 +++++++++++++ zephyr/Kconfig | 210 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 315 insertions(+), 37 deletions(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index a1974ec..a067b2d 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -147,7 +147,56 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa bat->charge_temp_min = 0; break; - case BAT_TYPE_NONE: + case BAT_TYPE_CUSTOM: + bat->voltage_absolute_max = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_ABS_MAX_VOLTAGE_MV); + + bat->topping_voltage = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_TOPPING_VOLTAGE_MV); + + bat->voltage_recharge = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_RECHARGE_VOLTAGE_MV); + + bat->voltage_load_disconnect = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_DISCONNECT_VOLTAGE_MV); + bat->voltage_load_reconnect = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_RECONNECT_VOLTAGE_MV); + + bat->internal_resistance = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_INTERNAL_RESISTANCE_MOHM); + + bat->voltage_absolute_min = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_ABS_MIN_VOLTAGE_MV); + + // Voltages during idle (no charging/discharging current) + bat->ocv_full = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_OCV_FULL_MV); + bat->ocv_empty = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_OCV_EMPTY_MV); + + // https://batteryuniversity.com/learn/article/charging_the_lead_acid_battery + bat->topping_current_cutoff = bat->nominal_capacity * 0.04F; // 3-5 % of C/1 + + bat->trickle_enabled = IS_ENABLED(CONFIG_CELL_TRICKLE); + bat->trickle_recharge_time = CONFIG_CELL_TRICKLE_RECHARGE_TIME; + bat->trickle_voltage = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_TRICKLE_VOLTAGE_MV); + + bat->equalization_enabled = IS_ENABLED(CONFIG_CELL_EQUALIZATION); + bat->equalization_voltage = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_EQUALIZATION_VOLTAGE_MV); + bat->equalization_duration = CONFIG_CELL_EQUALIZATION_DURATION; + bat->equalization_current_limit = (1.0F / 7.0F) * bat->nominal_capacity; + bat->equalization_trigger_days = CONFIG_CELL_EQUALIZATION_TRIGGER_DAYS; + bat->equalization_trigger_deep_cycles = CONFIG_CELL_EQUALIZATION_TRIGGER_DEEP_CYCLES; + + bat->temperature_compensation = 0.001F * + static_cast(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_TEMP_COMPENSATION_MV_K); + + bat->charge_temp_max = CONFIG_BAT_CHARGE_TEMP_MAX; + bat->charge_temp_min = CONFIG_BAT_CHARGE_TEMP_MIN; + bat->discharge_temp_max = CONFIG_BAT_DISCHARGE_TEMP_MAX; + bat->discharge_temp_min = CONFIG_BAT_DISCHARGE_TEMP_MIN; break; } } diff --git a/src/bat_charger.h b/src/bat_charger.h index 5aeccdb..c355156 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -26,7 +26,7 @@ enum BatType { /* * IMPORTANT: make sure to adjust also boards/Kconfig if enum is changed here! */ - BAT_TYPE_NONE = 0, ///< Safe standard settings + BAT_TYPE_CUSTOM = 0, ///< Custom battery type BAT_TYPE_FLOODED, ///< Old flooded (wet) lead-acid batteries BAT_TYPE_GEL, ///< VRLA gel batteries (maintainance-free) BAT_TYPE_AGM, ///< AGM batteries (maintainance-free) diff --git a/src/main.cpp b/src/main.cpp index 69e42b4..3854748 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,7 @@ void main(void) setup(); - battery_conf_init(&bat_conf, CONFIG_BAT_DEFAULT_TYPE, - CONFIG_BAT_DEFAULT_NUM_CELLS, CONFIG_BAT_DEFAULT_CAPACITY_AH); + battery_conf_init(&bat_conf, CONFIG_BAT_TYPE, CONFIG_BAT_NUM_CELLS, CONFIG_BAT_CAPACITY_AH); battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values // read custom configuration from EEPROM diff --git a/test/autoconf.h b/test/autoconf.h index 750c5d8..e05cab3 100644 --- a/test/autoconf.h +++ b/test/autoconf.h @@ -19,3 +19,29 @@ // Values that are otherwise defined by Kconfig #define CONFIG_CONTROL_FREQUENCY 10 // Hz + +#define CONFIG_BAT_TYPE_GEL 1 +#define CONFIG_BAT_TYPE 2 +#define CONFIG_BAT_CAPACITY_AH 50 +#define CONFIG_BAT_NUM_CELLS 6 +#define CONFIG_CELL_ABS_MAX_VOLTAGE_MV 2450 +#define CONFIG_CELL_TOPPING_VOLTAGE_MV 2400 +#define CONFIG_CELL_RECHARGE_VOLTAGE_MV 2300 +#define CONFIG_CELL_RECONNECT_VOLTAGE_MV 2100 +#define CONFIG_CELL_DISCONNECT_VOLTAGE_MV 1950 +#define CONFIG_CELL_ABS_MIN_VOLTAGE_MV 1600 +#define CONFIG_CELL_OCV_FULL_MV 2150 +#define CONFIG_CELL_OCV_EMPTY_MV 1900 +#define CONFIG_CELL_TRICKLE 1 +#define CONFIG_CELL_TRICKLE_VOLTAGE_MV 2300 +#define CONFIG_CELL_TRICKLE_RECHARGE_TIME 1800 +#define CONFIG_CELL_EQUALIZATION_VOLTAGE_MV 2450 +#define CONFIG_CELL_EQUALIZATION_DURATION 3600 +#define CONFIG_CELL_EQUALIZATION_TRIGGER_DAYS 60 +#define CONFIG_CELL_EQUALIZATION_TRIGGER_DEEP_CYCLES 10 +#define CONFIG_CELL_TEMP_COMPENSATION_MV_K -3 +#define CONFIG_CELL_INTERNAL_RESISTANCE_MOHM 5 +#define CONFIG_BAT_CHARGE_TEMP_MAX 50 +#define CONFIG_BAT_CHARGE_TEMP_MIN 0 +#define CONFIG_BAT_DISCHARGE_TEMP_MAX 50 +#define CONFIG_BAT_DISCHARGE_TEMP_MIN -10 diff --git a/test/zephyr.h b/test/zephyr.h index 0a4a884..c1393d6 100644 --- a/test/zephyr.h +++ b/test/zephyr.h @@ -40,3 +40,63 @@ // dummy #define LOG_MODULE_REGISTER(a, b) + + +/* crazy functions from Zephyr's util.h */ + +/** + * @brief Check for macro definition in compiler-visible expressions + * + * This trick was pioneered in Linux as the config_enabled() macro. It + * has the effect of taking a macro value that may be defined to "1" + * or may not be defined at all and turning it into a literal + * expression that can be handled by the C compiler instead of just + * the preprocessor. It is often used with a @p CONFIG_FOO macro which + * may be defined to 1 via Kconfig, or left undefined. + * + * That is, it works similarly to \#if defined(CONFIG_FOO) + * except that its expansion is a C expression. Thus, much \#ifdef + * usage can be replaced with equivalents like: + * + * if (IS_ENABLED(CONFIG_FOO)) { + * do_something_with_foo + * } + * + * This is cleaner since the compiler can generate errors and warnings + * for @p do_something_with_foo even when @p CONFIG_FOO is undefined. + * + * @param config_macro Macro to check + * @return 1 if @p config_macro is defined to 1, 0 otherwise (including + * if @p config_macro is not defined) + */ +#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro) + +/* This is called from IS_ENABLED(), and sticks on a "_XXXX" prefix, + * it will now be "_XXXX1" if config_macro is "1", or just "_XXXX" if it's + * undefined. + * ENABLED: Z_IS_ENABLED2(_XXXX1) + * DISABLED Z_IS_ENABLED2(_XXXX) + */ +#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro) + +/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string + * with a trailing comma), so it has the effect of making this a + * two-argument tuple to the preprocessor only in the case where the + * value is defined to "1" + * ENABLED: _YYYY, <--- note comma! + * DISABLED: _XXXX + */ +#define _XXXX1 _YYYY, + +/* Then we append an extra argument to fool the gcc preprocessor into + * accepting it as a varargs macro. + * arg1 arg2 arg3 + * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0) + * DISABLED Z_IS_ENABLED3(_XXXX 1, 0) + */ +#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args 1, 0) + +/* And our second argument is thus now cooked to be 1 in the case + * where the value is defined to 1, and 0 if not: + */ +#define Z_IS_ENABLED3(ignore_this, val, ...) val diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 369325f..860315a 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -8,13 +8,13 @@ mainmenu "Libre Solar Charge Controller Firmware" # config CONTROL_FREQUENCY - int - default 10 - help - Main control function frequency (Hz) + int + default 10 + help + Main control function frequency (Hz) - Frequencies higher than 10 Hz caused issues with MPPT control during lab test with - PV simulator. Might be different with actual solar panel. + Frequencies higher than 10 Hz caused issues with MPPT control during lab test with + PV simulator. Might be different with actual solar panel. config LIBRE_SOLAR_TYPE_ID int @@ -85,50 +85,63 @@ choice endchoice -menu "Battery" +menu "Battery default settings" choice - prompt "Default battery cell type" - default BAT_DEFAULT_TYPE_GEL + prompt "Battery type" + default BAT_TYPE_GEL help Select the type of the single cells inside the battery to determine voltage set points. Total voltage set points are multiplied with the selected number of cells. - config BAT_DEFAULT_TYPE_FLOODED + config BAT_TYPE_CUSTOM + bool "Custom type" + + config BAT_TYPE_FLOODED bool "Flooded (wet) lead-acid" - config BAT_DEFAULT_TYPE_GEL + config BAT_TYPE_GEL bool "VRLA gel, maintainance-free" - config BAT_DEFAULT_TYPE_AGM + config BAT_TYPE_AGM bool "AGM, maintainance-free" - config BAT_DEFAULT_TYPE_LFP + config BAT_TYPE_LFP bool "LiFePO4 Li-ion, 3.3V nominal" - config BAT_DEFAULT_TYPE_NMC + config BAT_TYPE_NMC bool "NMC/Graphite Li-ion, 3.7V nominal, 4.2V max" - config BAT_DEFAULT_TYPE_NMC_HV + config BAT_TYPE_NMC_HV bool "NMC/Graphite High Voltage Li-ion, 3.7V nominal, 4.35 max" endchoice # values must match enum BatType in bat_charger.h -config BAT_DEFAULT_TYPE +config BAT_TYPE int - default 1 if BAT_DEFAULT_TYPE_FLOODED - default 2 if BAT_DEFAULT_TYPE_GEL - default 3 if BAT_DEFAULT_TYPE_AGM - default 4 if BAT_DEFAULT_TYPE_LFP - default 5 if BAT_DEFAULT_TYPE_NMC - default 6 if BAT_DEFAULT_TYPE_NMC_HV + default 0 if BAT_TYPE_CUSTOM + default 1 if BAT_TYPE_FLOODED + default 2 if BAT_TYPE_GEL + default 3 if BAT_TYPE_AGM + default 4 if BAT_TYPE_LFP + default 5 if BAT_TYPE_NMC + default 6 if BAT_TYPE_NMC_HV -config BAT_DEFAULT_NUM_CELLS - int "Default number of battery cells" - default 6 - range 3 16 - help +config BAT_CAPACITY_AH + int "Battery nominal capacity in Ah" + range 10 200 + default 50 + help + Nominal battery capacity or sum of parallel cells capacity + +config BAT_NUM_CELLS + int "Number of cells for single battery" if BAT_TYPE_CUSTOM + default 6 if BAT_TYPE_FLOODED || BAT_TYPE_GEL || BAT_TYPE_AGM + default 4 if BAT_TYPE_LFP + default 3 if BAT_TYPE_NMC || BAT_TYPE_NMC_HV + range 3 16 + help Typical choices: - 6 for 12V lead-acid battery - 4 for 12V LiFePO4 battery @@ -136,18 +149,149 @@ config BAT_DEFAULT_NUM_CELLS Remarks: - If you choose settings for 12V system, 24V systems - (2 batteries in series) are recognized automatically + (2 batteries in series) are recognized automatically - Make sure the voltage of the used charge controller is not exceeded. -config BAT_DEFAULT_CAPACITY_AH - int "Default battery capacity in Ah" - range 10 200 +menu "Custom cell-level settings" + +config CELL_ABS_MAX_VOLTAGE_MV + int "Absolute maximum voltage (mV)" if BAT_TYPE_CUSTOM + default 2450 + help + Above this voltage the battery or loads might get damaged. + +config CELL_TOPPING_VOLTAGE_MV + int "Topping voltage (mV)" if BAT_TYPE_CUSTOM + default 2400 + help + Charger target voltage, switching from CC to CV at this voltage threshold. + +config CELL_RECHARGE_VOLTAGE_MV + int "Recharge voltage (mV)" if BAT_TYPE_CUSTOM + default 2300 + help + Start charging again if voltage of fully charged battery drops below this threshold. + +config CELL_RECONNECT_VOLTAGE_MV + int "Load reconnect cell voltage (mV)" if BAT_TYPE_CUSTOM + default 2100 + help + Re-enable the load only after charged beyond this value. + +config CELL_DISCONNECT_VOLTAGE_MV + int "Load disconnect cell voltage (mV)" if BAT_TYPE_CUSTOM + default 1950 + help + Load output is disabled if battery voltage is below this threshold. + +config CELL_ABS_MIN_VOLTAGE_MV + int "Topping voltage (mV)" if BAT_TYPE_CUSTOM + default 1600 + help + Below this voltage the battery is considered damaged. + +config CELL_OCV_FULL_MV + int "OCV full cell (mV)" if BAT_TYPE_CUSTOM + default 2150 + help + Voltages during idle (no charging/discharging current) + +config CELL_OCV_EMPTY_MV + int "OCV empty cell (mV)" if BAT_TYPE_CUSTOM + default 1900 + help + Voltages during idle (no charging/discharging current) + +config CELL_TRICKLE + bool "Enable trickle charging phase" if BAT_TYPE_CUSTOM + default y + +config CELL_TRICKLE_VOLTAGE_MV + int "Trickle charging cell voltage (mV)" if BAT_TYPE_CUSTOM && CELL_TRICKLE + default 2300 + help + Charger target voltage for trickle charging of lead-acid batteries + +config CELL_TRICKLE_RECHARGE_TIME + int "Trickle recharge time (s)" if BAT_TYPE_CUSTOM && CELL_TRICKLE + default 1800 + help + 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 + into CC/bulk charging mode. + +config CELL_EQUALIZATION + bool "Enable equalization phase" if BAT_TYPE_CUSTOM + default n + help + Enable for flooded batteries only. + +config CELL_EQUALIZATION_VOLTAGE_MV + int "Equalization phase cell voltage (mV)" if BAT_TYPE_CUSTOM && CELL_EQUALIZATION + default 2450 + help + Charger target voltage for equalization charging of lead-acid batteries + +config CELL_EQUALIZATION_DURATION + int "Equalization duration (seconds)" if BAT_TYPE_CUSTOM && CELL_EQUALIZATION + default 3600 + help + After this time, equalization charging is stopped. + +config CELL_EQUALIZATION_TRIGGER_DAYS + int "Equalization time trigger (days)" if BAT_TYPE_CUSTOM && CELL_EQUALIZATION + default 60 + help + After passing specified amount of days, an equalization charge is triggered. + +config CELL_EQUALIZATION_TRIGGER_DEEP_CYCLES + int "Equalization deep cycle count trigger" if BAT_TYPE_CUSTOM && CELL_EQUALIZATION + default 10 + help + After specified number of deep discharges, an equalization charge is triggered. + +config CELL_TEMP_COMPENSATION_MV_K + int "Cell voltage temperature compenstaion (mV/K)" if BAT_TYPE_CUSTOM + range -5 0 + default -3 + help + Suggested value: -3 mV/K/cell for lead-acid batteries, 0 for Li-ion batteries + +config CELL_INTERNAL_RESISTANCE_MOHM + int "Cell internal resistance (mOhm)" if BAT_TYPE_CUSTOM + range 1 30 + default 5 + +endmenu # Custom cell-level settings + +menu "Custom temperature settings" + +config BAT_CHARGE_TEMP_MAX + int "Maximum charge temperature (°C)" if BAT_TYPE_CUSTOM + range 0 60 default 50 -endmenu +config BAT_CHARGE_TEMP_MIN + int "Minimum charge temperature (°C)" if BAT_TYPE_CUSTOM + range -10 20 + default 0 -endmenu # Terminal configuration +config BAT_DISCHARGE_TEMP_MAX + int "Maximum discharge temperature (°C)" if BAT_TYPE_CUSTOM + range 0 60 + default 50 + +config BAT_DISCHARGE_TEMP_MIN + int "Minimum discharge temperature (°C)" if BAT_TYPE_CUSTOM + range -20 20 + default -10 + +endmenu # Custom temperature settings + +endmenu # Battery default settings + +endmenu # Charge controller setup menu "UEXT port"