extend battery lib with messuared values and new

calculation
This commit is contained in:
2023-04-26 15:55:34 +02:00
parent 5081290164
commit c2048ae949
3 changed files with 129 additions and 32 deletions
+34 -11
View File
@@ -9,8 +9,8 @@
*
*/
#ifndef AKKU_H
#define AKKU_H
#ifndef BATTERY_H
#define BATTERY_H
#include <stdint.h>
#include <iostream>
@@ -39,11 +39,14 @@ class Battery {
* @param r2 Second resistor of the voltage devider.
*/
Battery(uint8_t pin, uint32_t r1, uint32_t r2);
Battery(uint8_t pin);
/**
* @brief Read new values if the delay is reached.
* @brief Read new values if the delay is reached.
*
* @return bool true if new values are calculated
*/
void loop();
bool loop();
/**
* @brief Set the delay to wait befor new values are read
@@ -87,19 +90,28 @@ class Battery {
bool isBatteryLow(uint8_t percent);
private:
double readInputVoltage();
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
void readAdcToBuf();
void initBuffer();
uint16_t getBufAvg();
static const uint8_t bufferSize = 30;
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent;
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint16_t delay = 20000;
uint32_t r1;
uint32_t r2;
uint8_t bufferPos = 0;
uint8_t delay = 100;
uint8_t calulationDelayMultiplier = 10;
uint8_t loopCounter = 0;
uint16_t adcBuffer[bufferSize];
uint32_t r1 = 0;
uint32_t r2 = 0;
uint64_t lastMillis = 0;
double batteryVoltage;
double batteryVoltage = 0;
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12, // 0 5 10 15
11.18, 11.24, 11.3, 11.36, // 20 25 30 35
@@ -107,6 +119,17 @@ class Battery {
11.62, 11.74, 11.86, 11.95, // 60 65 70 75
12.07, 12.25, 12.33, 12.45, // 80 85 90 95
12.6 };
const uint8_t rawAdcVoltagesCount = 60;
const double startVoltage = 7;
const double stepVoltage = 0.1;
const uint16_t rawAdcVoltages[60] = // from 7.0V to 12.9V in 0.1V steps
{1820, 1851, 1880, 1910, 1937, 1967, 1992, 2020, 2048, 2080,
2109, 2136, 2163, 2189, 2218, 2244, 2273, 2302, 2334, 2363,
2391, 2415, 2441, 2471, 2499, 2531, 2557, 2587, 2617, 2646,
2674, 2699, 2730, 2761, 2791, 2816, 2843, 2872, 2900, 2930,
2958, 2991, 3017, 3049, 3080, 3115, 3144, 3178, 3208, 3242,
3276, 3313, 3346, 3389, 3433, 3470, 3509, 3548, 3590, 3636};
};
#endif // AKKU_H
#endif // BATTERY_H