From 0339b521ddd92848659c12ae6206bb97f61f7279 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 10 Jul 2021 16:29:48 +0200 Subject: [PATCH] charge_control: add extra state to fix initial charging Before this change, initial charging ended exactly HOLD_TIME after the CHARGE_INITIAL state was entered. This was usually not sufficient to reach the INITIAL_FULL voltage. Now a new state is entered once the INITIAL_FULL voltage is reached. --- src/charge_control.c | 28 +++++++++++++++++++++++++++- src/charge_control.h | 1 + src/config.h | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/charge_control.c b/src/charge_control.c index df13794..cef43f0 100644 --- a/src/charge_control.c +++ b/src/charge_control.c @@ -13,6 +13,7 @@ static const char *CHARGE_STATE_TEXT[] = { "WAIT_CHARGEPUMP", "INITIAL", + "INITIAL_HOLD", "TRANSITION", "FLOAT", "SLEEP", @@ -40,6 +41,8 @@ static fxp_t u_bat_regulation_corridor; static fxp_t u_bat_initial_full; static fxp_t u_bat_initial_low; +static fxp_t u_bat_initial_hold_cancel; + static fxp_t u_bat_float_full; static fxp_t u_bat_float_low; @@ -127,7 +130,28 @@ static void solar_fsm_update(uint64_t uptime_ms, struct MeasurementResult *meas) charge_state, charge_time_in_state); - // time limit for initial charging + // switch to hold state when high threshold is reached + if(meas->u_bat >= u_bat_initial_full) { + charge_state = CHARGE_INITIAL_HOLD; + } + + break; + + case CHARGE_INITIAL_HOLD: + charge_state = control_solar_charging( + u_bat_initial_full, + u_bat_initial_low, + uptime_ms, + meas, + charge_state, + charge_time_in_state); + + // cancel charge hold if battery voltage is below threshold + if(meas->u_bat <= u_bat_initial_hold_cancel) { + charge_state = CHARGE_INITIAL; + } + + // time limit for initial hold charging if(charge_time_in_state > INITIAL_CHARGE_HOLD_TIME) { charge_state = CHARGE_TRANSITION; } @@ -295,6 +319,8 @@ void charge_control_init(void) u_bat_initial_full = fxp_div(FXP_FROM_INT(U_BAT_INITIAL_FULL), FXP_FROM_INT(1000)); u_bat_initial_low = fxp_sub(u_bat_initial_full, u_bat_regulation_corridor); + u_bat_initial_hold_cancel = fxp_div(FXP_FROM_INT(U_BAT_INITIAL_HOLD_CANCEL), FXP_FROM_INT(1000)); + u_bat_float_full = fxp_div(FXP_FROM_INT(U_BAT_FLOAT_FULL), FXP_FROM_INT(1000)); u_bat_float_low = fxp_sub(u_bat_float_full, u_bat_regulation_corridor); diff --git a/src/charge_control.h b/src/charge_control.h index e943e6c..ecb26bb 100644 --- a/src/charge_control.h +++ b/src/charge_control.h @@ -11,6 +11,7 @@ enum ChargeState { CHARGE_WAIT_CHARGEPUMP, CHARGE_INITIAL, + CHARGE_INITIAL_HOLD, CHARGE_TRANSITION, CHARGE_FLOAT, CHARGE_SLEEP, diff --git a/src/config.h b/src/config.h index 7676c5b..3944c7d 100644 --- a/src/config.h +++ b/src/config.h @@ -9,6 +9,9 @@ /* Initial charge battery voltage threshold (in mV). */ #define U_BAT_INITIAL_FULL 28600 // stop charging if battery voltage reaches this threshold +/* Cancel initial charge voltage hold below this battery voltage (in mV). */ +#define U_BAT_INITIAL_HOLD_CANCEL 28000 + /* Transition to floating voltage levels after this time (in ms). */ #define INITIAL_CHARGE_HOLD_TIME 3600000