From 0545131a990e251a0229176d969b16cc74fa82a0 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Wed, 16 Oct 2019 10:02:27 +0200 Subject: [PATCH] Equalization charging phase added --- src/bat_charger.cpp | 119 +++++++++++++++++++++------------- src/bat_charger.h | 43 +++++++------ src/data_objects.cpp | 21 +++--- test/tests_bat_charger.cpp | 128 ++++++++++++++++++++++++++++++------- test/tests_dcdc.cpp | 4 +- 5 files changed, 218 insertions(+), 97 deletions(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 93f9926..f5fadb5 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -34,7 +34,7 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ bat->charge_current_max = bat->nominal_capacity; // 1C should be safe for all batteries bat->time_limit_recharge = 60; // sec - bat->time_limit_topping = 120*60; // sec + bat->topping_duration = 120*60; // sec bat->charge_temp_max = 50; bat->charge_temp_min = -10; @@ -47,7 +47,7 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ case BAT_TYPE_AGM: case BAT_TYPE_GEL: bat->voltage_absolute_max = num_cells * 2.45; - bat->voltage_topping = num_cells * 2.4; + bat->topping_voltage = num_cells * 2.4; bat->voltage_recharge = num_cells * 2.3; // Cell-level thresholds based on EN 62509:2011 (both thresholds current-compensated) @@ -64,29 +64,29 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ bat->ocv_empty = num_cells * 1.90; // https://batteryuniversity.com/learn/article/charging_the_lead_acid_battery - bat->current_cutoff_topping = bat->nominal_capacity * 0.04; // 3-5 % of C/1 + bat->topping_current_cutoff = bat->nominal_capacity * 0.04; // 3-5 % of C/1 bat->trickle_enabled = true; - bat->time_trickle_recharge = 30*60; + bat->trickle_recharge_time = 30*60; // Values as suggested in EN 62509:2011 - bat->voltage_trickle = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.35 : 2.3); + bat->trickle_voltage = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.35 : 2.3); // Enable for flooded batteries only, according to // https://discoverbattery.com/battery-101/equalizing-flooded-batteries-only bat->equalization_enabled = false; // Values as suggested in EN 62509:2011 - bat->voltage_equalization = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.50 : 2.45); - bat->time_limit_equalization = 60*60; - bat->current_limit_equalization = (1.0 / 7.0) * bat->nominal_capacity; - bat->equalization_trigger_time = 8; // weeks - bat->equalization_trigger_deep_cycles = 10; // times + bat->equalization_voltage = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.50 : 2.45); + bat->equalization_duration = 60*60; + bat->equalization_current_limit = (1.0 / 7.0) * bat->nominal_capacity; + bat->equalization_trigger_days = 60; + bat->equalization_trigger_deep_cycles = 10; bat->temperature_compensation = -0.003; // -3 mV/°C/cell break; case BAT_TYPE_LFP: bat->voltage_absolute_max = num_cells * 3.60; - bat->voltage_topping = num_cells * 3.55; // CV voltage + bat->topping_voltage = num_cells * 3.55; // CV voltage bat->voltage_recharge = num_cells * 3.35; bat->voltage_load_disconnect = num_cells * 3.0; @@ -100,7 +100,7 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ bat->ocv_empty = num_cells * 3.0; // because of flat OCV of LFP cells... // C/10 cut-off at end of CV phase by default - bat->current_cutoff_topping = bat->nominal_capacity / 10; + bat->topping_current_cutoff = bat->nominal_capacity / 10; bat->trickle_enabled = false; bat->equalization_enabled = false; @@ -110,8 +110,8 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ case BAT_TYPE_NMC: case BAT_TYPE_NMC_HV: - bat->voltage_topping = num_cells * ((type == BAT_TYPE_NMC_HV) ? 4.35 : 4.20); - bat->voltage_absolute_max = num_cells * (bat->voltage_topping + 0.05); + bat->topping_voltage = num_cells * ((type == BAT_TYPE_NMC_HV) ? 4.35 : 4.20); + bat->voltage_absolute_max = num_cells * (bat->topping_voltage + 0.05); bat->voltage_recharge = num_cells * 3.9; bat->voltage_load_disconnect = num_cells * 3.3; @@ -126,7 +126,7 @@ void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_ bat->ocv_empty = num_cells * 3.0; // C/10 cut-off at end of CV phase by default - bat->current_cutoff_topping = bat->nominal_capacity / 10; + bat->topping_current_cutoff = bat->nominal_capacity / 10; bat->trickle_enabled = false; bat->equalization_enabled = false; @@ -156,26 +156,26 @@ bool battery_conf_check(BatConf *bat_conf) bat_conf->current_cutoff_CV < (bat_conf->nominal_capacity / 10.0), // C/10 or lower allowed bat_conf->current_cutoff_CV > 0.01, (bat_conf->trickle_enabled == false || - (bat_conf->voltage_trickle < bat_conf->voltage_max && - bat_conf->voltage_trickle > bat_conf->voltage_load_disconnect)) + (bat_conf->trickle_voltage < bat_conf->voltage_max && + bat_conf->trickle_voltage > bat_conf->voltage_load_disconnect)) ); */ return (bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.4) && - bat_conf->voltage_recharge < (bat_conf->voltage_topping - 0.4) && + bat_conf->voltage_recharge < (bat_conf->topping_voltage - 0.4) && bat_conf->voltage_recharge > (bat_conf->voltage_load_disconnect + 1) && bat_conf->voltage_load_disconnect > (bat_conf->voltage_absolute_min + 0.4) && // max. 10% drop bat_conf->internal_resistance < bat_conf->voltage_load_disconnect * 0.1 / LOAD_CURRENT_MAX && // max. 3% loss - bat_conf->wire_resistance < bat_conf->voltage_topping * 0.03 / LOAD_CURRENT_MAX && + bat_conf->wire_resistance < bat_conf->topping_voltage * 0.03 / LOAD_CURRENT_MAX && // C/10 or lower current cutoff allowed - bat_conf->current_cutoff_topping < (bat_conf->nominal_capacity / 10.0) && - bat_conf->current_cutoff_topping > 0.01 && + bat_conf->topping_current_cutoff < (bat_conf->nominal_capacity / 10.0) && + bat_conf->topping_current_cutoff > 0.01 && (bat_conf->trickle_enabled == false || - (bat_conf->voltage_trickle < bat_conf->voltage_topping && - bat_conf->voltage_trickle > bat_conf->voltage_load_disconnect)) + (bat_conf->trickle_voltage < bat_conf->topping_voltage && + bat_conf->trickle_voltage > bat_conf->voltage_load_disconnect)) ); } @@ -183,18 +183,18 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char { // TODO: stop DC/DC - destination->voltage_topping = source->voltage_topping; + destination->topping_voltage = source->topping_voltage; destination->voltage_recharge = source->voltage_recharge; destination->voltage_load_reconnect = source->voltage_load_reconnect; destination->voltage_load_disconnect = source->voltage_load_disconnect; destination->voltage_absolute_max = source->voltage_absolute_max; destination->voltage_absolute_min = source->voltage_absolute_min; destination->charge_current_max = source->charge_current_max; - destination->current_cutoff_topping = source->current_cutoff_topping; - destination->time_limit_topping = source->time_limit_topping; + destination->topping_current_cutoff = source->topping_current_cutoff; + destination->topping_duration = source->topping_duration; destination->trickle_enabled = source->trickle_enabled; - destination->voltage_trickle = source->voltage_trickle; - destination->time_trickle_recharge = source->time_trickle_recharge; + destination->trickle_voltage = source->trickle_voltage; + destination->trickle_recharge_time = source->trickle_recharge_time; destination->charge_temp_max = source->charge_temp_max; destination->charge_temp_min = source->charge_temp_min; destination->discharge_temp_max = source->discharge_temp_max; @@ -232,10 +232,10 @@ void charger_detect_num_batteries(Charger *charger, BatConf *bat, DcBus *bus) if (bus->voltage > bat->voltage_absolute_min * 2 && bus->voltage < bat->voltage_absolute_max * 2) { charger->num_batteries = 2; - printf("Detected two batteries (total %.2f V max)\n", bat->voltage_topping * 2); + printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } else { - printf("Detected single battery (%.2f V max)\n", bat->voltage_topping); + printf("Detected single battery (%.2f V max)\n", bat->topping_voltage); } } @@ -335,7 +335,7 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) && charger->bat_temperature < bat_conf->charge_temp_max - 1 && charger->bat_temperature > bat_conf->charge_temp_min + 1) { - bus->chg_voltage_target = charger->num_batteries * (bat_conf->voltage_topping + + bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); bus->chg_current_max = bat_conf->charge_current_max; bus->chg_allowed = true; @@ -346,7 +346,7 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) } case CHG_STATE_BULK: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->voltage_topping + + bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); if (bus->voltage > charger->num_batteries * @@ -358,7 +358,7 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) } case CHG_STATE_TOPPING: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->voltage_topping + + bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); if (bus->voltage >= charger->num_batteries * @@ -369,23 +369,27 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) // cut-off limit reached because battery full (i.e. CV limit still // reached by available solar power within last 2s) or CV period long enough? - if ((bus->current < bat_conf->current_cutoff_topping && + if ((bus->current < bat_conf->topping_current_cutoff && (time(NULL) - charger->time_voltage_limit_reached) < 2) - || (time(NULL) - charger->time_state_changed) > bat_conf->time_limit_topping) + || (time(NULL) - charger->time_state_changed) > bat_conf->topping_duration) { charger->full = true; charger->num_full_charges++; charger->discharged_Ah = 0; // reset coulomb counter charger->first_full_charge_reached = true; - /*if (bat_conf->equalization_enabled) { - // TODO: additional conditions! - bus->chg_voltage_target = bat_conf->voltage_equalization; - bus->chg_current_max = bat_conf->current_limit_equalization; + if (bat_conf->equalization_enabled && ( + (time(NULL) - charger->time_last_equalization) / (24*60*60) + >= bat_conf->equalization_trigger_days || + charger->num_deep_discharges - charger->deep_dis_last_equalization + >= bat_conf->equalization_trigger_deep_cycles)) + { + bus->chg_voltage_target = bat_conf->equalization_voltage; + bus->chg_current_max = bat_conf->equalization_current_limit; _enter_state(charger, CHG_STATE_EQUALIZATION); } - else */if (bat_conf->trickle_enabled) { - bus->chg_voltage_target = charger->num_batteries * (bat_conf->voltage_trickle + + else if (bat_conf->trickle_enabled) { + bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); _enter_state(charger, CHG_STATE_TRICKLE); } @@ -399,7 +403,7 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) } case CHG_STATE_TRICKLE: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->voltage_trickle + + bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); if (bus->voltage >= charger->num_batteries * @@ -408,7 +412,7 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) charger->time_voltage_limit_reached = time(NULL); } - if (time(NULL) - charger->time_voltage_limit_reached > bat_conf->time_trickle_recharge) + if (time(NULL) - charger->time_voltage_limit_reached > bat_conf->trickle_recharge_time) { bus->chg_current_max = bat_conf->charge_current_max; charger->full = false; @@ -418,6 +422,33 @@ void charger_state_machine(DcBus *bus, BatConf *bat_conf, Charger *charger) // (for Li-ion battery: disable trickle!) break; } + case CHG_STATE_EQUALIZATION: { + // continuously adjust voltage setting for temperature compensation + bus->chg_voltage_target = charger->num_batteries * (bat_conf->equalization_voltage + + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + + // current or time limit for equalization reached + if (time(NULL) - charger->time_state_changed > bat_conf->equalization_duration) + { + // reset triggers + charger->time_last_equalization = time(NULL); + charger->deep_dis_last_equalization = charger->num_deep_discharges; + + charger->discharged_Ah = 0; // reset coulomb counter again + + if (bat_conf->trickle_enabled) { + bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + _enter_state(charger, CHG_STATE_TRICKLE); + } + else { + bus->chg_current_max = 0; + bus->chg_allowed = false; + _enter_state(charger, CHG_STATE_IDLE); + } + } + break; + } } } @@ -434,7 +465,7 @@ void battery_init_dc_bus(DcBus *bus, BatConf *bat, unsigned int num_batteries) bus->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n; bus->chg_allowed = true; - bus->chg_voltage_target = bat->voltage_topping * n; + bus->chg_voltage_target = bat->topping_voltage * n; bus->chg_voltage_min = bat->voltage_absolute_min * n; bus->chg_current_max = bat->charge_current_max; diff --git a/src/bat_charger.h b/src/bat_charger.h index 5dc5a0d..ce51775 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -89,19 +89,19 @@ typedef struct * * Charger target voltage, switching from CC to CV at this voltage threshold. */ - float voltage_topping; + float topping_voltage; /** CV phase cut-off current limit (A) * * Constant voltage charging phase stopped if current is below this value. */ - float current_cutoff_topping; + float topping_current_cutoff; /** CV phase cut-off time limit (s) * * After this time, CV charging is stopped independent of current. */ - int time_limit_topping; + int topping_duration; /** Enable float/trickle charging * @@ -113,7 +113,7 @@ typedef struct * * Charger target voltage for trickle charging of lead-acid batteries */ - float voltage_trickle; + float trickle_voltage; /** Trickle recharge time (s) * @@ -121,7 +121,7 @@ typedef struct * input power) for this period of time, the charger state machine goes back * into CC/bulk charging mode. */ - int time_trickle_recharge; + int trickle_recharge_time; /** Enable equalization charging * @@ -133,25 +133,25 @@ typedef struct * * Charger target voltage for equalization charging of lead-acid batteries */ - float voltage_equalization; + float equalization_voltage; - /** Equalization phase cut-off time limit (s) + /** Equalization cut-off time limit (s) * - * After this time, CV charging is stopped independent of current. + * After this time, equalization charging is stopped. */ - int time_limit_equalization; + int equalization_duration; - /** Equalization phase cut-off time limit (s) + /** Equalization phase maximum current (A) * - * After this time, CV charging is stopped independent of current. + * The current of the charger is controlled to stay below this limit during equalization. */ - float current_limit_equalization; + float equalization_current_limit; - /** Equalization trigger time (weeks) + /** Equalization trigger time interval (days) * - * After passing specified amount of weeks, an equalization charge is triggered. + * After passing specified amount of days, an equalization charge is triggered. */ - int equalization_trigger_time; + int equalization_trigger_days; /** Equalization trigger deep-discharge cycles * @@ -231,9 +231,11 @@ typedef struct { unsigned int state; ///< Current charger state (see enum ChargerState) - int num_batteries; ///< Used for automatic 12V/24V battery detection at start-up (can be 1 or 2 only) + int num_batteries; ///< Used for automatic 12V/24V battery detection at + ///< start-up (can be 1 or 2 only) - float bat_temperature; ///< Battery temperature in °C from ext. temperature sensor (if existing) + float bat_temperature; ///< Battery temperature in °C from ext. temperature sensor + ///< (if existing) bool ext_temp_sensor; ///< True if external temperature sensor was detected float usable_capacity; ///< Estimated usable capacity (Ah) based on coulomb counting @@ -249,9 +251,12 @@ typedef struct int time_state_changed; ///< Timestamp of last state change int time_voltage_limit_reached; ///< Last time the CV limit was reached - bool full; ///< Flag to indicate if battery was fully charged - bool first_full_charge_reached; ///< Set to true if battery was fully charged at least once (necessary for proper capacity estimation) + int time_last_equalization; ///< Timestamp after finish of last equalization charge + int deep_dis_last_equalization; ///< Deep discharge counter value after last equalization + bool full; ///< Flag to indicate if battery was fully charged + bool first_full_charge_reached; ///< Set to true if battery was fully charged at least once + ///< (necessary for proper capacity estimation) } Charger; diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 744ec87..e32b519 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -84,15 +84,13 @@ const data_object_t data_objects[] = { {0x31, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_recharge), "BatRecharge_V"}, {0x32, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_absolute_min), "BatAbsMin_V"}, {0x33, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 1, (void*) &(bat_conf_user.charge_current_max), "BatChgMax_A"}, - {0x34, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_topping), "BatTarget_V"}, - {0x35, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 1, (void*) &(bat_conf_user.current_cutoff_topping), "BatCutoff_A"}, - {0x36, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(bat_conf_user.time_limit_topping), "BatCutoff_s"}, + {0x34, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.topping_voltage), "BatTarget_V"}, + {0x35, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 1, (void*) &(bat_conf_user.topping_current_cutoff), "BatCutoff_A"}, + {0x36, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(bat_conf_user.topping_duration), "BatCutoff_s"}, {0x37, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(bat_conf_user.trickle_enabled), "TrickleEn"}, - {0x38, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_trickle), "Trickle_V"}, - {0x39, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(bat_conf_user.time_trickle_recharge), "TrickleRecharge_s"}, - //{0x3A, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(bat_conf_user.equalization_enabled), "EqualEn"}, - //{0x3B, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_equalization), "Equal_V"}, - // 0x3A-0x3E reserved for equalization charging + {0x38, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.trickle_voltage), "Trickle_V"}, + {0x39, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(bat_conf_user.trickle_recharge_time), "TrickleRecharge_s"}, + {0x3F, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 3, (void*) &(bat_conf_user.temperature_compensation), "TempFactor"}, {0x50, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 3, (void*) &(bat_conf_user.internal_resistance), "BatInt_Ohm"}, {0x51, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 3, (void*) &(bat_conf_user.wire_resistance), "BatWire_Ohm"}, @@ -101,6 +99,13 @@ const data_object_t data_objects[] = { {0x54, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 1, (void*) &(bat_conf_user.discharge_temp_max), "BatDisMax_degC"}, {0x55, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 1, (void*) &(bat_conf_user.discharge_temp_min), "BatDisMin_degC"}, + {0x58, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(bat_conf_user.equalization_enabled), "EqEn"}, + {0x59, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.equalization_voltage), "Eq_V"}, + {0x5A, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.equalization_current_limit), "Eq_A"}, + {0x5B, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 2, (void*) &(bat_conf_user.equalization_duration), "EqDuration_s"}, + {0x5C, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 2, (void*) &(bat_conf_user.equalization_trigger_days), "EqInterval_d"}, + {0x5D, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 2, (void*) &(bat_conf_user.equalization_trigger_deep_cycles), "EqDeepDisTrigger"}, + // load settings {0x40, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.enabled_target), "LoadEnDefault"}, {0x41, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.usb_enabled_target), "UsbEnDefault"}, diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index f0914a6..cfe910e 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -65,7 +65,7 @@ void enter_topping_at_voltage_setpoint() bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); - bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + bat_terminal->voltage = bat_conf.topping_voltage + 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -74,14 +74,14 @@ void stop_topping_after_time_limit() { enter_topping_at_voltage_setpoint(); - charger.time_state_changed = time(NULL) - bat_conf.time_limit_topping + 1; - bat_terminal->voltage = bat_conf.voltage_topping + 0.1; - bat_terminal->current = bat_conf.current_cutoff_topping + 0.1; + charger.time_state_changed = time(NULL) - bat_conf.topping_duration + 1; + bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->current = bat_conf.topping_current_cutoff + 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); - charger.time_state_changed = time(NULL) - bat_conf.time_limit_topping - 1; + charger.time_state_changed = time(NULL) - bat_conf.topping_duration - 1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -91,8 +91,8 @@ void stop_topping_at_cutoff_current() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.voltage_topping + 0.1; - bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -103,32 +103,20 @@ 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.voltage_topping + 0.1; - bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } -void trickle_to_equalization_if_enabled() -{ - enter_topping_at_voltage_setpoint(); - bat_conf.equalization_enabled = true; - - charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.voltage_topping + 0.1; - bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; - charger_state_machine(bat_terminal, &bat_conf, &charger); - TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); -} - void no_trickle_if_low_current_because_of_low_input() { enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 200; charger.time_voltage_limit_reached = time(NULL) - 3; - bat_terminal->voltage = bat_conf.voltage_topping - 0.1; - bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + bat_terminal->voltage = bat_conf.topping_voltage - 0.1; + bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); @@ -138,6 +126,87 @@ void no_trickle_if_low_current_because_of_low_input() TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } +void no_equalization_if_disabled() +{ + enter_topping_at_voltage_setpoint(); + bat_conf.equalization_enabled = false; + + // set triggers such that it would normally start + charger.deep_dis_last_equalization = + charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles; + charger.time_last_equalization = + 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->current = bat_conf.topping_current_cutoff - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); +} + +void no_equalization_if_limits_not_reached() +{ + enter_topping_at_voltage_setpoint(); + bat_conf.equalization_enabled = true; + + // set triggers such that they are just NOT reached + charger.deep_dis_last_equalization = + charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles + 1; + charger.time_last_equalization = + 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->current = bat_conf.topping_current_cutoff - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); +} + +void trickle_to_equalization_if_enabled_and_time_limit_reached() +{ + enter_topping_at_voltage_setpoint(); + bat_conf.equalization_enabled = true; + charger.time_last_equalization = + 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->current = bat_conf.topping_current_cutoff - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); +} + + +void trickle_to_equalization_if_enabled_and_deep_dis_limit_reached() +{ + enter_topping_at_voltage_setpoint(); + bat_conf.equalization_enabled = true; + charger.deep_dis_last_equalization = + 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->current = bat_conf.topping_current_cutoff - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); +} + + +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; + + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); + + charger.time_state_changed = time(NULL) - bat_conf.topping_duration - 1; + charger_state_machine(bat_terminal, &bat_conf, &charger); + TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); +} + void restart_bulk_from_trickle_if_voltage_drops() { TEST_ASSERT(0); @@ -151,12 +220,23 @@ void bat_charger_tests() RUN_TEST(no_start_after_short_rest); RUN_TEST(no_start_outside_temperature_limits); RUN_TEST(start_if_everything_just_fine); + + // topping RUN_TEST(enter_topping_at_voltage_setpoint); RUN_TEST(stop_topping_after_time_limit); RUN_TEST(stop_topping_at_cutoff_current); + + // trickle RUN_TEST(trickle_to_idle_for_li_ion); - //RUN_TEST(trickle_to_equalization_if_enabled); RUN_TEST(no_trickle_if_low_current_because_of_low_input); + + // equalization + RUN_TEST(no_equalization_if_disabled); + RUN_TEST(no_equalization_if_limits_not_reached); + RUN_TEST(trickle_to_equalization_if_enabled_and_time_limit_reached); + RUN_TEST(trickle_to_equalization_if_enabled_and_deep_dis_limit_reached); + RUN_TEST(stop_equalization_after_time_limit); + //RUN_TEST(restart_bulk_from_trickle_if_voltage_drops); // TODO: temperature compensation diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index 72fd7c2..aef6826 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -124,7 +124,7 @@ void no_buck_start_if_bat_voltage_low() void no_buck_start_if_bat_voltage_high() { init_structs_buck(); - lv_bus_int.voltage = bat_conf.voltage_topping + 0.1; + lv_bus_int.voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } @@ -161,7 +161,7 @@ void no_boost_start_if_bat_voltage_low() void no_boost_start_if_bat_voltage_high() { init_structs_boost(); - hv_terminal.voltage = bat_conf.voltage_topping + 0.1; + hv_terminal.voltage = bat_conf.topping_voltage + 0.1; TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); }