extend battery lib with messuared values and new
calculation
This commit is contained in:
+73
-9
@@ -15,17 +15,29 @@ Battery::Battery(uint8_t pin, uint32_t r1, uint32_t r2) {
|
||||
this->pin = pin;
|
||||
this->r1 = r1;
|
||||
this->r2 = r2;
|
||||
this->calculateBatteryVoltage();
|
||||
this->initBuffer();
|
||||
}
|
||||
|
||||
void Battery::loop() {
|
||||
Battery::Battery(uint8_t pin) {
|
||||
this->pin = pin;
|
||||
this->initBuffer();
|
||||
}
|
||||
|
||||
bool Battery::loop() {
|
||||
if (millis() - this->lastMillis < this->delay)
|
||||
return;
|
||||
|
||||
this->calculateBatteryVoltage();
|
||||
this->calculateBatteryPercent();
|
||||
return false;
|
||||
|
||||
this->readAdcToBuf();
|
||||
this->loopCounter++;
|
||||
this->lastMillis = millis();
|
||||
|
||||
if (this->loopCounter == this->calulationDelayMultiplier) {
|
||||
this->calculateBatteryVoltage();
|
||||
this->calculateBatteryPercent();
|
||||
return true;
|
||||
this->loopCounter = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double Battery::getBatteryVoltage() {
|
||||
@@ -39,9 +51,9 @@ bool Battery::isBatteryLow(uint8_t percent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
double Battery::readInputVoltage() {
|
||||
double Battery::calculateInputVoltage() {
|
||||
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
|
||||
double reading = analogRead(this->pin);
|
||||
double reading = this->getBufAvg();
|
||||
if(reading < 1 || reading > 4095) return 0;
|
||||
return - 0.000000000000016 * pow(reading,4)
|
||||
+ 0.000000000118171 * pow(reading,3)
|
||||
@@ -51,7 +63,34 @@ double Battery::readInputVoltage() {
|
||||
}
|
||||
|
||||
void Battery::calculateBatteryVoltage() {
|
||||
this->batteryVoltage = (readInputVoltage() * (double) (this->r1 + this->r2)) / (double) this->r2;
|
||||
if (this->r1 && this->r2) {
|
||||
this->batteryVoltage = (this->calculateInputVoltage() * (double) (this->r1 + this->r2)) / (double) this->r2;
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t adcValue = this->getBufAvg();
|
||||
if (adcValue < this->rawAdcVoltages[0]) {
|
||||
this->batteryVoltage = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (adcValue > this->rawAdcVoltages[this->rawAdcVoltagesCount] + 50) {
|
||||
this->batteryVoltage = -2;
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t index;
|
||||
for (index = 1; index < this->rawAdcVoltagesCount; index++) {
|
||||
if (adcValue < this->rawAdcVoltages[index])
|
||||
break;
|
||||
}
|
||||
double indexDelta = this->rawAdcVoltages[index] - this->rawAdcVoltages[index - 1];
|
||||
double valueDelta = this->rawAdcVoltages[index] - adcValue;
|
||||
double voltage = this->startVoltage + (index - 1) * this->stepVoltage;
|
||||
voltage += valueDelta / indexDelta * this->stepVoltage;
|
||||
this->batteryVoltage = voltage;
|
||||
|
||||
// std::cout << "Battery::calculateBatteryVoltage() - Voltage: " << voltage << " Index: " <<(int) index << " adcValue: " << (int) adcValue <<" iD: " << indexDelta << " vD: " << valueDelta << std::endl;
|
||||
}
|
||||
|
||||
void Battery::calculateBatteryPercent() {
|
||||
@@ -75,3 +114,28 @@ void Battery::calculateBatteryPercent() {
|
||||
}
|
||||
this->batteryPercent = i * (100 / (size - 1));
|
||||
}
|
||||
|
||||
void Battery::readAdcToBuf() {
|
||||
this->adcBuffer[this->bufferPos] = analogRead(this->pin);
|
||||
this->bufferPos++;
|
||||
if (this->bufferPos == Battery::bufferSize)
|
||||
this->bufferPos = 0;
|
||||
|
||||
// std::cout << "Battery::readAdcToBuf added Value: " << this->adcBuffer[this->bufferPos] << std::endl;
|
||||
}
|
||||
|
||||
void Battery::initBuffer() {
|
||||
for (uint8_t i = 0; i < Battery::bufferSize; i++)
|
||||
this->adcBuffer[i] = 0;
|
||||
}
|
||||
|
||||
uint16_t Battery::getBufAvg() {
|
||||
uint32_t res = 0;
|
||||
uint8_t emptyPos = 0;
|
||||
for (uint8_t i = 0; i < Battery::bufferSize; i++) {
|
||||
if (this->adcBuffer[i] == 0)
|
||||
emptyPos++;
|
||||
res += this->adcBuffer[i];
|
||||
}
|
||||
return res / (Battery::bufferSize - emptyPos);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user