Calibrate battery ADC and document LiPo curve
- Add BATTERY_CAL config and debug logging for raw ADC samples - Use LiPo voltage curve (4.2V full, 2.9V empty) for % mapping - Document battery calibration, curve, and debug output in README
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
#include <esp_sleep.h>
|
||||
|
||||
static constexpr float BATTERY_DIVIDER = 2.0f;
|
||||
static constexpr float BATTERY_CAL = 1.0f;
|
||||
static constexpr float ADC_REF_V = 3.3f;
|
||||
|
||||
void power_sender_init() {
|
||||
@@ -35,25 +34,79 @@ void power_configure_unused_pins_sender() {
|
||||
|
||||
void read_battery(MeterData &data) {
|
||||
uint32_t sum = 0;
|
||||
uint16_t samples[5] = {};
|
||||
for (uint8_t i = 0; i < 5; ++i) {
|
||||
sum += analogRead(PIN_BAT_ADC);
|
||||
samples[i] = analogRead(PIN_BAT_ADC);
|
||||
sum += samples[i];
|
||||
}
|
||||
float avg = static_cast<float>(sum) / 5.0f;
|
||||
float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL;
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
Serial.printf("bat_adc: %u %u %u %u %u avg=%.1f v=%.3f\n",
|
||||
samples[0], samples[1], samples[2], samples[3], samples[4],
|
||||
static_cast<double>(avg), static_cast<double>(v));
|
||||
}
|
||||
|
||||
data.battery_voltage_v = v;
|
||||
data.battery_percent = battery_percent_from_voltage(v);
|
||||
}
|
||||
|
||||
uint8_t battery_percent_from_voltage(float voltage_v) {
|
||||
float pct = (voltage_v - 3.0f) / (4.2f - 3.0f) * 100.0f;
|
||||
if (pct < 0.0f) {
|
||||
pct = 0.0f;
|
||||
if (isnan(voltage_v)) {
|
||||
return 0;
|
||||
}
|
||||
if (pct > 100.0f) {
|
||||
pct = 100.0f;
|
||||
struct LutPoint {
|
||||
float v;
|
||||
uint8_t pct;
|
||||
};
|
||||
static const LutPoint kCurve[] = {
|
||||
{4.20f, 100},
|
||||
{4.15f, 95},
|
||||
{4.11f, 90},
|
||||
{4.08f, 85},
|
||||
{4.02f, 80},
|
||||
{3.98f, 75},
|
||||
{3.95f, 70},
|
||||
{3.91f, 60},
|
||||
{3.87f, 50},
|
||||
{3.85f, 45},
|
||||
{3.84f, 40},
|
||||
{3.82f, 35},
|
||||
{3.80f, 30},
|
||||
{3.77f, 25},
|
||||
{3.75f, 20},
|
||||
{3.73f, 15},
|
||||
{3.70f, 10},
|
||||
{3.65f, 5},
|
||||
{3.60f, 2},
|
||||
{2.90f, 0},
|
||||
};
|
||||
if (voltage_v >= kCurve[0].v) {
|
||||
return kCurve[0].pct;
|
||||
}
|
||||
return static_cast<uint8_t>(pct + 0.5f);
|
||||
if (voltage_v <= kCurve[sizeof(kCurve) / sizeof(kCurve[0]) - 1].v) {
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i + 1 < sizeof(kCurve) / sizeof(kCurve[0]); ++i) {
|
||||
const LutPoint &hi = kCurve[i];
|
||||
const LutPoint &lo = kCurve[i + 1];
|
||||
if (voltage_v <= hi.v && voltage_v >= lo.v) {
|
||||
float span = hi.v - lo.v;
|
||||
if (span <= 0.0f) {
|
||||
return lo.pct;
|
||||
}
|
||||
float t = (voltage_v - lo.v) / span;
|
||||
float pct = lo.pct + t * (hi.pct - lo.pct);
|
||||
if (pct < 0.0f) {
|
||||
pct = 0.0f;
|
||||
}
|
||||
if (pct > 100.0f) {
|
||||
pct = 100.0f;
|
||||
}
|
||||
return static_cast<uint8_t>(pct + 0.5f);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void light_sleep_ms(uint32_t ms) {
|
||||
|
||||
Reference in New Issue
Block a user