implement a new version to read battery pack

This commit is contained in:
2022-03-04 13:25:38 +01:00
parent f7570a6486
commit 77aa588ca0
4 changed files with 169 additions and 112 deletions
+72
View File
@@ -0,0 +1,72 @@
/**
* @file battery.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2022-02-05
*
* @copyright Copyright (c) 2022
*
*/
#include "battery.h"
Battery::Battery(uint8_t pin, uint32_t r1, uint32_t r2) {
this->pin = pin;
this->r1 = r1;
this->r2 = r2;
this->calculateBatteryVoltage();
}
void Battery::loop() {
if (millis() - this->lastMillis < this->delay)
return;
this->calculateBatteryVoltage();
this->calculateBatteryPercent();
this->lastMillis = millis();
}
double Battery::getBatteryVoltage() {
return this->batteryVoltage;
}
uint8_t Battery::getBatteryPercent() {
return this->batteryPercent;
}
double Battery::readInputVoltage() {
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = analogRead(this->pin);
if(reading < 1 || reading > 4095) return 0;
return - 0.000000000000016 * pow(reading,4)
+ 0.000000000118171 * pow(reading,3)
- 0.000000301211691 * pow(reading,2)
+ 0.001109019271794 * reading
+ 0.034143524634089;
}
void Battery::calculateBatteryVoltage() {
this->batteryVoltage = (readInputVoltage() * (double) (this->r1 + this->r2)) / (double) this->r2;
}
void Battery::calculateBatteryPercent() {
int8_t size = sizeof(this->capacityVoltages) / sizeof(this->capacityVoltages);
uint8_t i;
for (i = 0; i < size; i++) {
if (this->batteryVoltage <= this->capacityVoltages[i])
break;
}
if (i == 0) {
std::cout << "Critical low battery!" << std::endl;
} else if (i == size - 1) {
} else {
double diffToLowerVal = this->batteryVoltage - this->capacityVoltages[i - 1];
double diffToHigherVal = this->capacityVoltages[i] - this->batteryVoltage;
if (diffToLowerVal > diffToHigherVal)
i--;
}
this->batteryPercent = i * (100 / (size - 1));
}
+97
View File
@@ -0,0 +1,97 @@
/**
* @file battery.h
* @author Alexander Klein (alex@kleiax.de)
* @brief Contains a class for battery monitoring
* @version 0.1
* @date 2022-02-05
*
* @copyright Copyright (c) 2022
*
*/
#ifndef AKKU_H
#define AKKU_H
#include <stdint.h>
#include <iostream>
#include <math.h>
#include <Arduino.h>
/**
* @brief A class for battery monitoring
*
* This class reads the voltage from an analog pin to calculate the
* charge level of a 3 Cell Li-Poly battery pack. The battery pack have
* to be after a voltage diveder, so that maximum voltage for the
* microcontroller is 3.3 Volt.
*/
class Battery {
public:
/**
* @brief Construct a new Battery object
*
* The voltage devider have to be calculated, so that the input
* voltage from 3.3 Volt is never exceeded. It is assumed that
* the microcontroller is connected to the second resistor.
*
* @param pin The analog to read from.
* @param r1 First resistor of the voltage devider.
* @param r2 Second resistor of the voltage devider.
*/
Battery(uint8_t pin, uint32_t r1, uint32_t r2);
/**
* @brief Read new values if the delay is reached.
*/
void loop();
/**
* @brief Set the delay to wait befor new values are read
*
* @param delay in milliseconds
*/
void setDelay(uint16_t delay) {this->delay = delay; }
/**
* @brief Get the delay to wait befor new values are read
*
* @return uint16_t delay in milliseconds
*/
uint16_t getDelay() {return this->delay; }
/**
* @brief Get the battery voltage
*
* @return double in Volt
*/
double getBatteryVoltage() {return this->batteryVoltage;}
/**
* @brief Get the charge level of the battery
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() {return this->batteryPercent;}
private:
double readInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
uint8_t pin;
uint8_t batteryPercent;
uint16_t delay = 5000;
uint32_t r1;
uint32_t r2;
uint64_t lastMillis = 0;
double batteryVoltage;
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12,
11.18, 11.24, 11.3, 11.36,
11.39, 11.45, 11.51, 11.56,
11.62, 11.74, 11.86, 11.95,
12.07, 12.25, 12.33, 12.45,
12.6 };
};
#endif // AKKU_H