2023-07-06 06:14:26 +02:00
# include "battery_utils.h"
2023-09-21 02:28:17 +02:00
# include "configuration.h"
2024-05-18 17:27:14 +02:00
# include "boards_pinout.h"
2024-05-24 20:24:40 +02:00
# include "utils.h"
2024-03-21 17:31:54 +01:00
2024-05-24 20:42:39 +02:00
extern Configuration Config ;
extern uint32_t lastBatteryCheck ;
2023-09-21 02:28:17 +02:00
2024-05-24 20:42:39 +02:00
bool shouldSleepLowVoltage = false ;
float adcReadingTransformation = ( 3.3 / 4095 ) ;
float voltageDividerCorrection = 0.288 ;
2023-07-06 06:14:26 +02:00
2023-09-21 02:28:17 +02:00
// for External Voltage Measurment (MAX = 15Volts !!!)
float R1 = 100.000 ; //in Kilo-Ohms
float R2 = 27.000 ; //in Kilo-Ohms
float readingCorrection = 0.125 ;
float multiplyCorrection = 0.035 ;
2024-02-25 16:00:44 +01:00
2023-07-06 06:14:26 +02:00
namespace BATTERY_Utils {
2024-03-21 17:31:54 +01:00
float mapVoltage ( float voltage , float in_min , float in_max , float out_min , float out_max ) {
return ( voltage - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min ;
}
2024-05-24 19:05:28 +02:00
float checkInternalVoltage ( ) {
2023-09-21 02:28:17 +02:00
int sample ;
int sampleSum = 0 ;
2024-05-02 19:41:21 +02:00
# ifdef ADC_CTRL
2024-05-16 21:43:13 +02:00
# if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
2024-05-11 18:38:05 +02:00
digitalWrite ( ADC_CTRL , HIGH ) ;
# endif
2024-05-20 13:49:52 +02:00
# if defined(HELTEC_V3) || defined(HELTEC_V2)
2024-05-11 18:38:05 +02:00
digitalWrite ( ADC_CTRL , LOW ) ;
# endif
2024-04-30 23:18:02 +02:00
# endif
2024-05-11 18:38:05 +02:00
2024-03-27 19:28:27 +01:00
for ( int i = 0 ; i < 100 ; i + + ) {
2024-05-02 19:41:21 +02:00
# ifdef BATTERY_PIN
2024-05-11 18:38:05 +02:00
sample = analogRead ( BATTERY_PIN ) ;
2023-12-26 21:27:36 +01:00
# endif
2024-04-19 17:06:26 +02:00
# if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
2024-05-11 18:38:05 +02:00
sample = 0 ;
2023-12-26 21:27:36 +01:00
# endif
2023-09-21 02:28:17 +02:00
sampleSum + = sample ;
delayMicroseconds ( 50 ) ;
}
2024-03-21 17:31:54 +01:00
2024-05-02 19:41:21 +02:00
# ifdef ADC_CTRL
2024-05-16 21:43:13 +02:00
# if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
2024-05-11 18:38:05 +02:00
digitalWrite ( ADC_CTRL , LOW ) ;
# endif
2024-05-20 13:49:52 +02:00
# if defined(HELTEC_V3) || defined(HELTEC_V2)
2024-05-11 18:38:05 +02:00
digitalWrite ( ADC_CTRL , HIGH ) ;
# endif
double inputDivider = ( 1.0 / ( 390.0 + 100.0 ) ) * 100.0 ; // The voltage divider is a 390k + 100k resistor in series, 100k on the low side.
return ( ( ( sampleSum / 100 ) * adcReadingTransformation ) / inputDivider ) + 0.285 ; // Yes, this offset is excessive, but the ADC on the ESP32s3 is quite inaccurate and noisy. Adjust to own measurements.
2024-04-19 17:06:26 +02:00
# else
2024-05-11 18:38:05 +02:00
return ( 2 * ( sampleSum / 100 ) * adcReadingTransformation ) + voltageDividerCorrection ; // raw voltage without mapping
2024-04-19 17:06:26 +02:00
# endif
2024-03-21 17:31:54 +01:00
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage
2023-09-21 02:28:17 +02:00
}
float checkExternalVoltage ( ) {
int sample ;
int sampleSum = 0 ;
2024-03-27 19:28:27 +01:00
for ( int i = 0 ; i < 100 ; i + + ) {
2024-05-24 02:13:05 +02:00
sample = analogRead ( Config . battery . externalVoltagePin ) ;
2023-09-21 02:28:17 +02:00
sampleSum + = sample ;
delayMicroseconds ( 50 ) ;
}
2024-03-21 17:31:54 +01:00
float voltage = ( ( ( ( sampleSum / 100 ) * adcReadingTransformation ) + readingCorrection ) * ( ( R1 + R2 ) / R2 ) ) - multiplyCorrection ;
2024-05-24 19:05:28 +02:00
2024-03-21 17:31:54 +01:00
return voltage ; // raw voltage without mapping
// return mapVoltage(voltage, 5.05, 6.32, 4.5, 5.5); // mapped voltage
}
2024-03-27 15:42:04 +01:00
void checkIfShouldSleep ( ) {
2024-03-21 17:31:54 +01:00
if ( lastBatteryCheck = = 0 | | millis ( ) - lastBatteryCheck > = 15 * 60 * 1000 ) {
lastBatteryCheck = millis ( ) ;
2024-05-24 19:05:28 +02:00
float voltage = checkInternalVoltage ( ) ;
2024-03-21 17:31:54 +01:00
2024-03-28 18:28:31 +01:00
if ( voltage < Config . lowVoltageCutOff ) {
2024-03-27 15:42:04 +01:00
ESP . deepSleep ( 1800000000 ) ; // 30 min sleep (60s = 60e6)
2024-03-21 17:31:54 +01:00
}
}
2023-07-06 06:14:26 +02:00
}
2024-05-24 20:24:40 +02:00
void startupBatteryHealth ( ) {
# ifdef BATTERY_PIN
if ( Config . battery . monitorInternalVoltage & & checkInternalVoltage ( ) < Config . battery . internalSleepVoltage ) {
shouldSleepLowVoltage = true ;
}
# endif
if ( Config . battery . monitorExternalVoltage & & checkExternalVoltage ( ) < Config . battery . externalSleepVoltage ) {
shouldSleepLowVoltage = true ;
}
if ( shouldSleepLowVoltage ) {
Utils : : checkSleepByLowBatteryVoltage ( 0 ) ;
}
}
2023-07-06 06:14:26 +02:00
}