Battery remaining algorithm modified
This commit is contained in:
@@ -25,15 +25,12 @@ class BatteryController {
|
|||||||
int percent(const float minVoltage = 7.2f, const float maxVoltage = 8.4f,
|
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) {
|
const float r1 = 3.3f, const float r2 = 2.f, const float k = 2.87f) {
|
||||||
auto batteryVoltage = measureVoltage() * ((r1 + r2) / k);
|
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:
|
private:
|
||||||
uint16_t _battery_pin;
|
uint16_t _battery_pin;
|
||||||
uint16_t _battery_data_switch_pin;
|
uint16_t _battery_data_switch_pin;
|
||||||
static constexpr const char *_tag = "BatteryController";
|
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user