Index: firmware/App/Monitors/Battery.c =================================================================== diff -u -r0c6f5d0d80edbb2461721d840769bee5481c18c7 -rbbbc0d82d0f73ca7b0aa4e23fc9dbe38ee089655 --- firmware/App/Monitors/Battery.c (.../Battery.c) (revision 0c6f5d0d80edbb2461721d840769bee5481c18c7) +++ firmware/App/Monitors/Battery.c (.../Battery.c) (revision bbbc0d82d0f73ca7b0aa4e23fc9dbe38ee089655) @@ -18,6 +18,7 @@ #include "Battery.h" #include "BatteryDriver.h" #include "Messaging.h" +#include "math.h" #include "PersistentAlarm.h" #include "TaskGeneral.h" #include "TestSupport.h" @@ -38,6 +39,14 @@ #define BATTERY_MIN_CAPACITY_MAH 1950U ///< Minimum battery capacity for starting a treatment. #define BATTERY_CHARGER_ERROR_BITS 0x08 ///< Error codes are in the first byte of third bit. #define BATTERY_CHARGER_FAULTY_ERROR_BITS 0XFF ///< Error codes are in the first and second byte. +#define BATTERY_FULL_CAPACITY_MAH 3900U +#define BATTERY_NOMINAL_CAPACITY_WH ( 25.20 * 3.90 ) +#define BATTERY_60WH_CAPACITY_MAH ( ( BATTERY_FULL_CAPACITY_MAH / BATTERY_NOMINAL_CAPACITY_WH ) * 60 ) +#define V_REGN 5.0 +#define R_PULLUP 10000.0 +#define R0 10000.0 +#define T0 298.15 +#define BETA 3435 // ********** private data ********** @@ -68,6 +77,9 @@ void initBattery( void ) { initSetupI2cDriver(); + + (void)enableBatteryChargerAdc(); + currentBmValue = BEGINNING_OF_LIST; lastBatteryMonitorTime = 0; batteryDataPublicationTimerCounter = BATTERY_DATA_PUB_INTERVAL; @@ -202,6 +214,11 @@ static void getBatteryManagementData( void ) { U32 responseData; + float TS_ratio = 0.0f; + float V_TS = 0.0f; + float R_NTC = 0.0f; + float T_K = 0.0f; + float T_C = 0.0f; // Increment the position in the enum. Starting value is BEGINNING_OF_LIST (0), so on the first // cycle through this function it will be set to the first valid position (1) @@ -255,7 +272,13 @@ responseData = 0U; if ( TRUE == getBatteryData( BATTERY_CHARGER_ADDRESS, BATTERY_CHARGER_TS, &responseData ) ) { - setBatteryRegisterData( BATTERY_CHARGER_TS, (U32)( ( responseData * 1024.0F ) / 100.0F ) ); + TS_ratio = responseData/1024.0f; + V_TS = TS_ratio * V_REGN; + R_NTC = R_PULLUP * V_TS / (V_REGN - V_TS); + T_K = 1 / (1/T0 + (1/BETA) * log(R_NTC / R0)); + T_C = T_K - 273.15; + U32 temp_decic = (U32)roundf(T_C * 10.0f); + setBatteryRegisterData( BATTERY_CHARGER_TS, temp_decic ); } break; @@ -319,6 +342,19 @@ /*********************************************************************//** * @brief + * The isBatteryRemainingCapacityLessThan60Wh function checks whether the + * battery remaining capacity is below the equivalent of 60 Wh. + * @details \b Inputs: batteryRegisterOverrides[] + * @details \b Outputs: none + * @return TRUE if the battery remaining capacity is below 60 Wh, FALSE if not + *************************************************************************/ +BOOL isBatteryRemainingCapacityLessThan60Wh( void ) +{ + return ( getBatteryRegisterValue( BATTERY_PACK_REMAINING_CAPACITY ) < BATTERY_60WH_CAPACITY_MAH ) ? TRUE : FALSE; +} + +/*********************************************************************//** + * @brief * The publishBatteryDataPayload function updates and publishes the battery * data payload over CAN at the configured publish interval. * @details \b Message \b Sent: MSG_ID_TD_BATTERY_DATA