98 lines
2.8 KiB
C++
98 lines
2.8 KiB
C++
/**
|
|
* @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 = 20000;
|
|
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
|