From 88bb8059f3fbdb28df947fc551269719d4b4ce5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 29 Jul 2021 12:44:07 +0200 Subject: [PATCH] Charger: Add follower state for power sharing This state is enabled if the device receives a current target signal from a device with higher priority via CAN. After not receiving the messages for 1 second the state machine automatically goes back to normal operation. --- src/bat_charger.cpp | 26 ++++++++++++++++++++++++++ src/bat_charger.h | 22 +++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index c2a8e83..7324945 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -459,6 +459,10 @@ void Charger::charge_control(BatConf *bat_conf) dev_stat.clear_error(ERR_BAT_OVERVOLTAGE); } + if (state != CHG_STATE_FOLLOWER && (uptime() - time_last_ctrl_msg) <= 1) { + enter_state(CHG_STATE_FOLLOWER); + } + // state machine switch (state) { case CHG_STATE_IDLE: { @@ -475,6 +479,7 @@ void Charger::charge_control(BatConf *bat_conf) port->bus->sink_voltage_intercept = bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25); port->pos_current_limit = bat_conf->charge_current_max; + target_current_control = port->pos_current_limit; full = false; dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP); dev_stat.clear_error(ERR_BAT_CHG_UNDERTEMP); @@ -499,6 +504,9 @@ void Charger::charge_control(BatConf *bat_conf) port->bus->sink_voltage_intercept = bat_conf->topping_voltage + bat_conf->temperature_compensation * (bat_temperature - 25); + // power sharing: multiple devices in parallel supply the same current + target_current_control = port->current_filtered; + if (port->bus->voltage_filtered >= 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 @@ -546,6 +554,8 @@ void Charger::charge_control(BatConf *bat_conf) port->bus->sink_voltage_intercept = bat_conf->trickle_voltage + bat_conf->temperature_compensation * (bat_temperature - 25); + target_current_control = port->current_filtered; + if (port->bus->voltage >= port->bus->sink_control_voltage()) { time_target_voltage_reached = uptime(); } @@ -568,6 +578,8 @@ void Charger::charge_control(BatConf *bat_conf) port->bus->sink_voltage_intercept = bat_conf->equalization_voltage + bat_conf->temperature_compensation * (bat_temperature - 25); + target_current_control = port->current_filtered; + // current or time limit for equalization reached if (uptime() - time_state_changed > bat_conf->equalization_duration) { @@ -589,6 +601,20 @@ void Charger::charge_control(BatConf *bat_conf) } break; } + case CHG_STATE_FOLLOWER: { + if ((uptime() - time_last_ctrl_msg) > 1) { + // go back to normal state machine + port->pos_current_limit = bat_conf->charge_current_max; + enter_state(CHG_STATE_BULK); + } + else { + // set current target as received from external device + port->pos_current_limit = target_current_control; + // set safety limit for voltage + port->bus->sink_voltage_intercept = bat_conf->voltage_absolute_max; + } + break; + } } } diff --git a/src/bat_charger.h b/src/bat_charger.h index f9f9172..60319ef 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -322,7 +322,16 @@ enum ChargerState { * components attached to the battery. (currently, no equalization charging is * enabled in the software) */ - CHG_STATE_EQUALIZATION + CHG_STATE_EQUALIZATION, + + /** + * Parallel operation of multiple converters + * + * This mode is enabled if the device receives commands from an external controller with + * higher priority on the CAN bus. The device goes in current control modes and tries to + * match the current of the other controller. + */ + CHG_STATE_FOLLOWER }; /** @@ -417,6 +426,17 @@ public: */ bool empty; + /** + * Last time a control message from external device was received + */ + time_t time_last_ctrl_msg = CHARGER_TIME_NEVER; + + /** + * Target current of the converter if operating in follower mode (received from external + * converter with higher priority via CAN) + */ + float target_current_control; + /** * Detect if two batteries are connected in series (12V/24V auto-detection) */