implement a new version to read battery pack
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* @file akku.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-02-05
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#include "akku.h"
|
||||
|
||||
Akku::Akku(uint8_t pin, double maxVolt, double emptyVolt) {
|
||||
this->pin = pin;
|
||||
this->maxVolt = maxVolt;
|
||||
this->emptyVolt = emptyVolt;
|
||||
this->updateOnePercent();
|
||||
}
|
||||
|
||||
void Akku::setEmptyVoltage(double volt) {
|
||||
this->emptyVolt = volt;
|
||||
this->updateOnePercent();
|
||||
}
|
||||
|
||||
void Akku::setMaxVoltage(double volt) {
|
||||
this->maxVolt = volt;
|
||||
this->updateOnePercent();
|
||||
}
|
||||
|
||||
double Akku::getVoltage() {
|
||||
double rawVoltage = this->readVoltage();
|
||||
if (this->maxVolt) {
|
||||
|
||||
}
|
||||
return rawVoltage;
|
||||
}
|
||||
|
||||
uint8_t Akku::getPercent() {
|
||||
uint8_t res = 0;
|
||||
if (this->emptyVolt)
|
||||
res = (uint8_t) ((this->getVoltage() - this->emptyVolt) / this->onePercent);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Akku::isEmpty() {
|
||||
if (this->getVoltage() < this->emptyVolt) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double Akku::readVoltage() {
|
||||
// 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;
|
||||
}
|
||||
|
||||
double Akku::updateOnePercent() {
|
||||
this->onePercent = (this->maxVolt - this->emptyVolt) / 100.0;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* @file akku.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-02-05
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AKKU_H
|
||||
#define AKKU_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class Akku {
|
||||
public:
|
||||
Akku(uint8_t pin, double maxVolt = 0, double emptyVolt = 0);
|
||||
|
||||
void setEmptyVoltage(double volt);
|
||||
void setMaxVoltage(double volt);
|
||||
|
||||
double getVoltage();
|
||||
uint8_t getPercent();
|
||||
bool isEmpty();
|
||||
|
||||
private:
|
||||
double readVoltage();
|
||||
double updateOnePercent();
|
||||
|
||||
uint8_t pin;
|
||||
double maxVolt;
|
||||
double emptyVolt;
|
||||
double onePercent;
|
||||
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
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user