Mercurial > public > ostc4
changeset 859:d32901746950 Evo_2_23
Improvment battery charger visualization:
In the previous version the green flash (charge complete) was shown even battery had not reached 100% charge. Root cause was that the pin signaling the end of charge could be raised for other reasons then a full battery. The new version will show the green flash only in case the battery is rated as full.
In addition the graph visualization has been updated to continously progress. In the previous version it only progressed in case the charge value changed. Especially at charging start the charger is doing some battery evaluation which does not increase the charge state. In such a case the graph seemed to be frozen.
author | Ideenmodellierer |
---|---|
date | Tue, 07 May 2024 21:20:33 +0200 |
parents | 08ea8e9d6cfe |
children | 3e499569baf3 |
files | Discovery/Src/t7.c Small_CPU/Inc/batteryCharger.h Small_CPU/Src/batteryCharger.c Small_CPU/Src/scheduler.c |
diffstat | 4 files changed, 41 insertions(+), 183 deletions(-) [+] |
line wrap: on
line diff
--- a/Discovery/Src/t7.c Tue May 07 21:12:37 2024 +0200 +++ b/Discovery/Src/t7.c Tue May 07 21:20:33 2024 +0200 @@ -4505,6 +4505,10 @@ { level = 10; } + if(curIndex > 1) + { + level = (level + ChargerLog[curIndex - 1]) / 2; /* smooth small jumps */ + } if(curIndex < 59) { ChargerLog[curIndex++] = level; @@ -4514,7 +4518,7 @@ memcpy (&ChargerLog[0],&ChargerLog[1],sizeof(ChargerLog) - 1); ChargerLog[curIndex] = level; } - if(curIndex > 1) + if(curIndex > 1) /* estimate time til charging is complete */ { averageSpeed = ((averageSpeed * (curIndex-1)) + charge) / curIndex; completeSec = (100.0 - stateUsed->lifeData.battery_charge) / averageSpeed; @@ -4571,30 +4575,28 @@ if(stateUsed->chargeStatus != CHARGER_off) { - if(lastCharge != localCharge) - { - curTick = HAL_GetTick(); - deltatime = (curTick - lastTick); - lastTick = curTick; - if(lastCharge < localCharge) + curTick = HAL_GetTick(); + deltatime = (curTick - lastTick); + + if((deltatime > 3000) || (lastCharge != localCharge)) /* Charge value update is expected every 2 second. */ + { /* Added timeout to keep graph moving in case charger is temporary idle */ + if(lastCharge != localCharge) { - speed = (localCharge - lastCharge) * 1000.0 / deltatime; + if(lastCharge < localCharge) + { + speed = (localCharge - lastCharge) * 1000.0 / deltatime; + } + + if(localCharge > 100.0) + { + localCharge = 100.0; + } + lastCharge = localCharge; } - - if(localCharge > 100.0) - { - localCharge = 100.0; - } - - lastCharge = localCharge; - } - - - if(deltatime > 1000) - { deltatime = 0; remainingSec = LogDeltaCharge(speed); speed = 0; + lastTick = curTick; } } textpointer += snprintf(&text[textpointer],50,"\n\r");
--- a/Small_CPU/Inc/batteryCharger.h Tue May 07 21:12:37 2024 +0200 +++ b/Small_CPU/Inc/batteryCharger.h Tue May 07 21:20:33 2024 +0200 @@ -51,7 +51,6 @@ } chargerState_t; -uint8_t get_charge_status(void); void init_battery_charger_status(void); void set_charge_state(chargerState_t newState); uint8_t get_charge_state(void);
--- a/Small_CPU/Src/batteryCharger.c Tue May 07 21:12:37 2024 +0200 +++ b/Small_CPU/Src/batteryCharger.c Tue May 07 21:20:33 2024 +0200 @@ -34,36 +34,17 @@ #include "scheduler.h" -/* Use This compile switch to select the new charger status control implementation */ -#define ENABLE_CHARGER_STATUS_V2 - #define CHARGER_DEBOUNCE_SECONDS (6u) /* 6 seconds used to avoid problems with charger interrupts / disconnections */ -static uint8_t battery_i_charge_status = 0; static uint16_t battery_charger_counter = 0; - -#ifdef ENABLE_CHARGER_STATUS_V2 static chargerState_t batteryChargerState = Charger_NotConnected; -#endif - -/* can be 0, 1 or 255 - * 0 is disconnected - * 1 is charging - * 255 is full - */ -uint8_t get_charge_status(void) -{ - return battery_i_charge_status; -} void set_charge_state(uint8_t newState) { -#ifdef ENABLE_CHARGER_STATUS_V2 if(newState < Charger_END) { batteryChargerState = newState; } -#endif } uint8_t get_charge_state(void) @@ -124,36 +105,20 @@ HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); } -/* static counter is used to avoid multiple counts of charge startings - and after that it is used, starting at 127 to count for the charge full signal - - there a short disconnections with the QI charger - therefore the battery_charger_counter has a countdown instead of = 0. - - battery_gas_gauge_set_charge_full and scheduleUpdateDeviceDataChargerFull are - set after disconnection as the charging process continues as long as not disconnected - to prevent the short disconnections the battery_charger_counter is used too including - upcounting again while battery_i_charge_status == 255 and the connection is established - - */ - void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase) { -#ifdef ENABLE_CHARGER_STATUS_V2 static uint8_t notifyChargeComplete = 0; -#endif #ifdef OSTC_ON_DISCOVERY_HARDWARE return; #endif -#ifdef ENABLE_CHARGER_STATUS_V2 - if(batteryChargerState == Charger_ColdStart) /* wait for the first valid voltage meassurement */ { - if((global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE) && (global.lifeData.battery_voltage < BATTERY_CHARGER_CONNECTED_VOLTAGE)) + if(global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE) /* wait for first valid voltage value */ { - if(global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE) /* Voltage close to full state => maybe new battery inserted */ + if((global.lifeData.battery_voltage < BATTERY_CHARGER_CONNECTED_VOLTAGE) + && (global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE)) /* Voltage close to full state => maybe new battery inserted */ { battery_gas_gauge_set_charge_full(); } @@ -179,7 +144,13 @@ battery_charger_counter = CHARGER_DEBOUNCE_SECONDS; } break; - case Charger_Finished: battery_charger_counter = 0; + case Charger_Finished: if((get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE) && (get_voltage() < BATTERY_CHARGER_CONNECTED_VOLTAGE)) /* stopping does not necessarily mean battery is full */ + { + global.dataSendToMaster.chargeStatus = CHARGER_complete; + global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; + notifyChargeComplete = 1; + } + battery_charger_counter = 10; batteryChargerState = Charger_LostConnection; /* no break */ case Charger_LostConnection: /* the charger stops charging when charge current is 1/10 */ @@ -188,10 +159,6 @@ { notifyChargeComplete = 1; } - else - { - notifyChargeComplete = 0; - } if(battery_charger_counter >= cycleTimeBase) { battery_charger_counter -= cycleTimeBase; @@ -199,7 +166,7 @@ else { battery_charger_counter = 0; - battery_i_charge_status = 0; + global.dataSendToMaster.chargeStatus = CHARGER_off; global.deviceDataSendToMaster.chargeStatus = CHARGER_off; @@ -207,12 +174,13 @@ { battery_gas_gauge_set_charge_full(); scheduleUpdateDeviceDataChargerFull(); - notifyChargeComplete = 0; } + notifyChargeComplete = 0; batteryChargerState = Charger_NotConnected; } break; - default: break; + default: batteryChargerState = Charger_NotConnected; /* unexpected state => reinitialize state machine */ + break; } } else @@ -221,8 +189,7 @@ /* wait for disconnection to write and reset */ switch(batteryChargerState) { - case Charger_NotConnected: battery_i_charge_status = 1; - battery_charger_counter = 0; + case Charger_NotConnected: battery_charger_counter = 0; batteryChargerState = Charger_WarmUp; break; case Charger_LostConnection: batteryChargerState = Charger_Active; @@ -230,7 +197,6 @@ case Charger_WarmUp: battery_charger_counter += cycleTimeBase; if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS ) { - battery_i_charge_status = 2; scheduleUpdateDeviceDataChargerCharging(); batteryChargerState = Charger_Active; } @@ -251,11 +217,8 @@ if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) /* high => charger stopped charging */ { + battery_charger_counter = 30; batteryChargerState = Charger_Finished; - global.dataSendToMaster.chargeStatus = CHARGER_complete; - global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; - battery_charger_counter = 30; - notifyChargeComplete = 1; } else { @@ -279,117 +242,11 @@ HAL_Delay(1); break; - default: /* wait for disconnection */ + default: batteryChargerState = Charger_NotConnected; /* unexpected state => reinitialize state machine */ break; } } } -#else - /* on disconnection or while disconnected */ - if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) - { - if(battery_charger_counter) - { - battery_charger_counter--; - global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; - global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; - } - /* max count down to 127+5 or 127+20 */ - if((battery_i_charge_status == 255) && battery_charger_counter < 127) - { -// battery_gas_gauge_set_charge_full(); -// scheduleUpdateDeviceDataChargerFull(); - battery_charger_counter = 0; - } - - if(battery_charger_counter == 0) - { - battery_i_charge_status = 0; - global.dataSendToMaster.chargeStatus = CHARGER_off; - global.deviceDataSendToMaster.chargeStatus = CHARGER_off; - - } - return; - } - - /* connected */ - - /* wait for disconnection to write and reset */ - if(battery_i_charge_status == 255) - { - global.dataSendToMaster.chargeStatus = CHARGER_complete; - global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; - - if(((cycleTimeBase > 1) && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20)) - battery_charger_counter++; - return; - } - - if(battery_charger_counter == 0) - battery_i_charge_status = 1; - - /* charger is connected and didn't signal full yet */ - global.dataSendToMaster.chargeStatus = CHARGER_running; - global.deviceDataSendToMaster.chargeStatus = CHARGER_running; - - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.Pin = CHARGE_OUT_PIN; - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStructure.Pull = GPIO_NOPULL; - GPIO_InitStructure.Speed = GPIO_SPEED_LOW; - HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); - HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); - HAL_Delay(1); - - - if(battery_charger_counter < 120) - { - if(cycleTimeBase == 1) - battery_charger_counter++; - else - { - battery_charger_counter += 30; - if(battery_charger_counter >= 127) - battery_charger_counter = 126; - } - } - else - if(battery_charger_counter < 127) - { - battery_charger_counter = 127; - if(battery_i_charge_status < 2) - { - battery_i_charge_status = 2; - scheduleUpdateDeviceDataChargerCharging(); - } - } - - if(battery_charger_counter >= 127) - { - if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f)) - { - battery_charger_counter++; - if(((cycleTimeBase > 1) && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20)) - { - battery_charger_counter = 127; - if(get_voltage() >= 4.1f) - { - battery_i_charge_status = 255; - battery_gas_gauge_set_charge_full(); - scheduleUpdateDeviceDataChargerFull(); - } - } - } - else - battery_charger_counter = 127; - } - - GPIO_InitStructure.Pin = CHARGE_OUT_PIN; - GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; - GPIO_InitStructure.Pull = GPIO_NOPULL; - GPIO_InitStructure.Speed = GPIO_SPEED_LOW; - HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); -#endif } /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
--- a/Small_CPU/Src/scheduler.c Tue May 07 21:12:37 2024 +0200 +++ b/Small_CPU/Src/scheduler.c Tue May 07 21:20:33 2024 +0200 @@ -142,7 +142,7 @@ global.dataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH;; global.dataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW;; - global.dataSendToMaster.chargeStatus = 0; + global.dataSendToMaster.chargeStatus = CHARGER_off; global.dataSendToMaster.power_on_reset = 0; global.dataSendToMaster.header.checkCode[0] = 0xA1; @@ -160,7 +160,7 @@ global.deviceDataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH; global.deviceDataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW; - global.deviceDataSendToMaster.chargeStatus = 0; + global.deviceDataSendToMaster.chargeStatus = CHARGER_off; global.deviceDataSendToMaster.power_on_reset = 0; global.deviceDataSendToMaster.header.checkCode[0] = 0xDF;