Battery remaining algorithm modified

This commit is contained in:
2024-03-08 23:09:26 +07:00
parent 69c736b1eb
commit 271bd845bf

View File

@@ -25,15 +25,12 @@ class BatteryController {
int percent(const float minVoltage = 7.2f, const float maxVoltage = 8.4f,
const float r1 = 3.3f, const float r2 = 2.f, const float k = 2.87f) {
auto batteryVoltage = measureVoltage() * ((r1 + r2) / k);
return round(_map(batteryVoltage, minVoltage, maxVoltage, 5.f, 100.f));
auto percent = ((batteryVoltage - minVoltage) / (maxVoltage - minVoltage)) * 100;
return constrain(round(percent), 0, 100);
}
private:
uint16_t _battery_pin;
uint16_t _battery_data_switch_pin;
static constexpr const char *_tag = "BatteryController";
float _map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
};