2024-06-14 17:01:32 +02:00
# include <Arduino.h>
2023-07-06 06:14:26 +02:00
# include "battery_utils.h"
2023-09-21 02:28:17 +02:00
# include "configuration.h"
2024-11-05 18:41:41 +01:00
# include "board_pinout.h"
2024-05-27 17:46:14 +02:00
# include "power_utils.h"
2024-05-24 20:24:40 +02:00
# include "utils.h"
2024-03-21 17:31:54 +01:00
2024-07-09 05:02:45 +02:00
extern Configuration Config ;
extern uint32_t lastBatteryCheck ;
2023-09-21 02:28:17 +02:00
2024-07-09 05:02:45 +02:00
bool shouldSleepLowVoltage = false ;
float adcReadingTransformation = ( 3.3 / 4095 ) ;
float voltageDividerCorrection = 0.288 ;
float readingCorrection = 0.125 ;
float multiplyCorrection = 0.035 ;
float voltageDividerTransformation = 0.0 ;
2024-09-22 18:34:13 +02:00
int telemetryCounter = random ( 1 , 999 ) ;
2024-07-09 05:02:45 +02:00
# ifdef HAS_ADC_CALIBRATION
# include <esp_adc_cal.h>
2024-10-29 13:57:26 +01:00
# if defined(TTGO_LORA32_V2_1) || defined(TTGO_LORA32_V2_1_915)
2024-07-09 05:02:45 +02:00
# define InternalBattery_ADC_Channel ADC1_CHANNEL_7 // t_lora32 pin35
# define ExternalVoltage_ADC_Channel ADC1_CHANNEL_6 // t_lora32 pin34
# endif
# if CONFIG_IDF_TARGET_ESP32
# define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_VREF
# elif CONFIG_IDF_TARGET_ESP32S2
# define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
# elif CONFIG_IDF_TARGET_ESP32C3
# define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
# elif CONFIG_IDF_TARGET_ESP32S3
# define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP_FIT
# endif
esp_adc_cal_characteristics_t adc_chars ;
# endif
2024-05-24 20:42:39 +02:00
2024-07-09 14:39:59 +02:00
bool calibrationEnable = false ;
2023-09-21 02:28:17 +02:00
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-07-09 05:02:45 +02:00
void adcCalibration ( ) {
2024-07-09 14:39:59 +02:00
# ifdef HAS_ADC_CALIBRATION
if ( calibrationEnable ) {
adc1_config_width ( ADC_WIDTH_BIT_12 ) ;
adc1_config_channel_atten ( InternalBattery_ADC_Channel , ADC_ATTEN_DB_12 ) ;
adc1_config_channel_atten ( ExternalVoltage_ADC_Channel , ADC_ATTEN_DB_12 ) ;
}
# endif
2024-07-09 05:02:45 +02:00
}
void adcCalibrationCheck ( ) {
2024-07-09 14:39:59 +02:00
# ifdef HAS_ADC_CALIBRATION
esp_err_t ret ;
ret = esp_adc_cal_check_efuse ( ADC_EXAMPLE_CALI_SCHEME ) ;
/*if (ret == ESP_ERR_NOT_SUPPORTED) {
Serial . println ( " Calibration scheme not supported, skip software calibration " ) ;
} else if ( ret = = ESP_ERR_INVALID_VERSION ) {
Serial . println ( " eFuse not burnt, skip software calibration " ) ;
} else */
if ( ret = = ESP_OK ) {
esp_adc_cal_characterize ( ADC_UNIT_1 , ADC_ATTEN_DB_12 , ADC_WIDTH_BIT_12 , 1100 , & adc_chars ) ;
//Serial.printf("eFuse Vref:%u mV\n", adc_chars.vref);
calibrationEnable = true ;
} /*else {
Serial . println ( " Invalid Calibration Arg " ) ;
} */
# endif
2024-07-09 05:02:45 +02:00
}
void setup ( ) {
2024-07-09 05:29:04 +02:00
if ( ( Config . battery . sendExternalVoltage | | Config . battery . monitorExternalVoltage ) & & Config . battery . voltageDividerR2 ! = 0 ) voltageDividerTransformation = ( Config . battery . voltageDividerR1 + Config . battery . voltageDividerR2 ) / Config . battery . voltageDividerR2 ;
2024-07-09 05:02:45 +02:00
# if defined(HAS_ADC_CALIBRATION)
2024-07-09 05:29:04 +02:00
if ( Config . battery . sendInternalVoltage | | Config . battery . monitorInternalVoltage | | Config . battery . sendExternalVoltage | | Config . battery . monitorExternalVoltage ) {
adcCalibrationCheck ( ) ;
adcCalibration ( ) ;
}
2024-07-09 05:02:45 +02:00
# endif
}
2024-05-24 19:05:28 +02:00
float checkInternalVoltage ( ) {
2024-05-27 17:46:14 +02:00
# if defined(HAS_AXP192) || defined(HAS_AXP2101)
if ( POWER_Utils : : isBatteryConnected ( ) ) {
return POWER_Utils : : getBatteryVoltage ( ) ;
} else {
return 0.0 ;
}
# else
int sample ;
int sampleSum = 0 ;
# ifdef ADC_CTRL
2025-01-10 15:53:52 +01:00
# if defined(HELTEC_WIRELESS_TRACKER) || defined(HELTEC_V3_2)
2024-05-27 17:46:14 +02:00
digitalWrite ( ADC_CTRL , HIGH ) ;
# endif
2024-08-17 19:16:44 +02:00
# if defined(HELTEC_V3) || defined(HELTEC_V2) || defined(HELTEC_WSL_V3) || defined(HELTEC_WP)
2024-05-27 17:46:14 +02:00
digitalWrite ( ADC_CTRL , LOW ) ;
# endif
2024-05-11 18:38:05 +02:00
# endif
2024-05-27 17:46:14 +02:00
for ( int i = 0 ; i < 100 ; i + + ) {
2024-07-09 05:02:45 +02:00
# if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_LoRa_915) || defined(ESP32_DIY_1W_LoRa) || defined(ESP32_DIY_1W_LoRa_915)
2024-05-27 17:46:14 +02:00
sample = 0 ;
2024-07-09 05:02:45 +02:00
# else
2024-07-09 14:39:59 +02:00
# ifdef HAS_ADC_CALIBRATION
if ( calibrationEnable ) {
sample = adc1_get_raw ( InternalBattery_ADC_Channel ) ;
} else {
sample = analogRead ( BATTERY_PIN ) ;
}
# else
2024-07-18 20:41:09 +02:00
# ifdef BATTERY_PIN
sample = analogRead ( BATTERY_PIN ) ;
# else
sample = 0 ;
# endif
2024-07-09 14:39:59 +02:00
# endif
2024-11-06 16:47:42 +01:00
# endif
2024-05-27 17:46:14 +02:00
sampleSum + = sample ;
delayMicroseconds ( 50 ) ;
}
2024-03-21 17:31:54 +01:00
2024-05-27 17:46:14 +02:00
# ifdef ADC_CTRL
2025-01-10 15:53:52 +01:00
# if defined(HELTEC_WIRELESS_TRACKER) || defined(HELTEC_V3_2)
2024-05-27 17:46:14 +02:00
digitalWrite ( ADC_CTRL , LOW ) ;
# endif
2024-08-17 19:16:44 +02:00
# if defined(HELTEC_V3) || defined(HELTEC_V2) || defined(HELTEC_WSL_V3) || defined(HELTEC_WP)
2024-05-27 17:46:14 +02:00
digitalWrite ( ADC_CTRL , HIGH ) ;
# endif
2024-08-19 03:06:57 +02:00
# ifdef HELTEC_WP
double inputDivider = ( 1.0 / ( 10.0 + 10.0 ) ) * 10.0 ; // The voltage divider is a 10k + 10k resistor in series
# else
2024-05-27 17:46:14 +02:00
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.
2024-08-19 03:06:57 +02:00
# endif
2024-05-27 17:46:14 +02:00
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.
# else
2024-07-17 16:47:42 +02:00
# ifdef HAS_ADC_CALIBRATION
if ( calibrationEnable ) {
float voltage = esp_adc_cal_raw_to_voltage ( sampleSum / 100 , & adc_chars ) ;
voltage * = 2 ; // for 100K/100K voltage divider
voltage / = 1000 ;
return voltage ;
} else {
return ( 2 * ( sampleSum / 100 ) * adcReadingTransformation ) + voltageDividerCorrection ; // raw voltage without mapping
}
# else
2024-07-09 05:02:45 +02:00
return ( 2 * ( sampleSum / 100 ) * adcReadingTransformation ) + voltageDividerCorrection ; // raw voltage without mapping
2024-07-17 16:47:42 +02:00
# endif
2024-05-11 18:38:05 +02:00
# endif
2024-05-27 17:46:14 +02:00
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage
# endif
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-07-09 14:39:59 +02:00
# ifdef HAS_ADC_CALIBRATION
if ( calibrationEnable ) {
sample = adc1_get_raw ( ExternalVoltage_ADC_Channel ) ;
} else {
sample = analogRead ( Config . battery . externalVoltagePin ) ;
}
# else
2024-07-09 05:02:45 +02:00
sample = analogRead ( Config . battery . externalVoltagePin ) ;
2024-07-09 14:39:59 +02:00
# endif
2023-09-21 02:28:17 +02:00
sampleSum + = sample ;
2024-07-09 05:02:45 +02:00
delayMicroseconds ( 50 ) ;
2023-09-21 02:28:17 +02:00
}
2024-07-09 05:02:45 +02:00
float extVoltage ;
2024-07-09 14:39:59 +02:00
# ifdef HAS_ADC_CALIBRATION
2024-11-06 16:47:42 +01:00
if ( calibrationEnable ) {
2024-07-09 14:39:59 +02:00
extVoltage = esp_adc_cal_raw_to_voltage ( sampleSum / 100 , & adc_chars ) * voltageDividerTransformation ; // in mV
extVoltage / = 1000 ;
} else {
extVoltage = ( ( ( ( sampleSum / 100 ) * adcReadingTransformation ) + readingCorrection ) * voltageDividerTransformation ) - multiplyCorrection ;
}
# else
2024-07-09 05:02:45 +02:00
extVoltage = ( ( ( ( sampleSum / 100 ) * adcReadingTransformation ) + readingCorrection ) * voltageDividerTransformation ) - multiplyCorrection ;
2024-07-09 14:39:59 +02:00
# endif
2024-07-09 05:02:45 +02:00
return extVoltage ; // raw voltage without mapping
2024-03-21 17:31:54 +01:00
// 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 ) {
2024-06-21 03:12:54 +02:00
lastBatteryCheck = millis ( ) ;
if ( checkInternalVoltage ( ) < 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
2024-05-27 03:51:22 +02:00
if ( Config . battery . monitorInternalVoltage & & checkInternalVoltage ( ) < Config . battery . internalSleepVoltage + 0.1 ) {
2024-05-24 20:24:40 +02:00
shouldSleepLowVoltage = true ;
}
# endif
2024-08-19 16:41:46 +02:00
# ifndef HELTEC_WP
if ( Config . battery . monitorExternalVoltage & & checkExternalVoltage ( ) < Config . battery . externalSleepVoltage + 0.1 ) {
shouldSleepLowVoltage = true ;
}
# endif
2024-05-24 20:24:40 +02:00
if ( shouldSleepLowVoltage ) {
Utils : : checkSleepByLowBatteryVoltage ( 0 ) ;
}
}
2024-09-22 18:34:13 +02:00
String generateEncodedTelemetryBytes ( float value , bool firstBytes , byte voltageType ) { // 0 = internal battery(0-4,2V) , 1 = external battery(0-15V)
String encodedBytes ;
int tempValue ;
if ( firstBytes ) {
tempValue = value ;
} else {
switch ( voltageType ) {
case 0 :
tempValue = value * 100 ; // Internal voltage calculation
break ;
case 1 :
tempValue = ( value * 100 ) / 2 ; // External voltage calculation
break ;
default :
tempValue = value ;
break ;
}
2024-11-06 16:47:42 +01:00
}
2024-09-22 18:34:13 +02:00
int firstByte = tempValue / 91 ;
tempValue - = firstByte * 91 ;
encodedBytes = char ( firstByte + 33 ) ;
encodedBytes + = char ( tempValue + 33 ) ;
return encodedBytes ;
}
String generateEncodedTelemetry ( ) {
String telemetry = " | " ;
telemetry + = generateEncodedTelemetryBytes ( telemetryCounter , true , 0 ) ;
telemetryCounter + + ;
if ( telemetryCounter = = 1000 ) {
telemetryCounter = 0 ;
}
2024-09-23 03:31:33 +02:00
if ( Config . battery . sendInternalVoltage ) telemetry + = generateEncodedTelemetryBytes ( checkInternalVoltage ( ) , false , 0 ) ;
if ( Config . battery . sendExternalVoltage ) telemetry + = generateEncodedTelemetryBytes ( checkExternalVoltage ( ) , false , 1 ) ;
2024-09-22 18:34:13 +02:00
telemetry + = " | " ;
return telemetry ;
}
2023-07-06 06:14:26 +02:00
}