clangtidy corrections part 1

This commit is contained in:
2023-10-11 17:35:40 +02:00
parent 77a52b82c3
commit 23d854a826
36 changed files with 2383 additions and 1165 deletions
+118 -78
View File
@@ -4,30 +4,32 @@
* @brief Contains the implementation of the class Battery
* @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->batteryVoltageFactor = (double) (this->r1 + this->r2) / (double) this->r2;
Battery::Battery(uint8_t pin, uint32_t firstResistor, uint32_t secondResistor)
: pin{pin}, firstResistor{firstResistor}, secondResistor{secondResistor},
batteryVoltageFactor{firstResistor + secondResistor / static_cast<double>(secondResistor)}
{
this->initBuffer();
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
Battery::Battery(uint8_t pin) {
this->pin = pin;
Battery::Battery(uint8_t pin)
: pin{pin}
{
this->initBuffer();
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
void Battery::run() {
if (this->calibrationState != CalibrationState::None) {
void Battery::run()
{
if (this->calibrationState != CalibrationState::None)
{
this->runCalibration();
return;
}
@@ -35,164 +37,202 @@ void Battery::run() {
this->readAdcToBuf();
this->loopCounter++;
if (this->loopCounter >= this->calulationDelayMultiplier) {
if (this->loopCounter >= this->calulationDelayMultiplier)
{
this->calculateBatteryVoltage();
this->calculateBatteryPercent();
this->loopCounter = 0;
this->calculatetNewValues = true;
}
return;
}
void Battery::runCalibration() {
void Battery::runCalibration()
{
if (this->calibrationState != CalibrationState::Reading)
{
return;
}
this->readAdcToBuf();
if (this->bufferPos == 0) {
uint16_t res = this->getBufAvg();
this->readAdcToBuf();
if (this->bufferPos == 0)
{
const uint16_t res = this->getBufAvg();
this->newRawAdcVoltages[this->currentCalibrationVoltage] = res;
std::cout << "Index: "
<< (int) this->currentCalibrationVoltage
std::cout << "Index: "
<< (int)this->currentCalibrationVoltage
<< " Value: "
<< (int) res
<< (int)res
<< std::endl;
this->currentCalibrationVoltage++;
this->calibrationState = CalibrationState::Waiting;
if (this->currentCalibrationVoltage == 60) {
if (this->currentCalibrationVoltage == Battery::rawAdcVoltagesCount)
{
this->calibrationState = CalibrationState::Finished;
}
}
}
double Battery::getBatteryVoltage() const {
double res = this->batteryVoltage;
return (int)(res*100+0.5)/100.0;
double Battery::getBatteryVoltage() const
{
return static_cast<int>((this->batteryVoltage * 100 + 0.5)) / 100.0;
}
bool Battery::isBatteryLow(double voltage) const {
bool Battery::isBatteryLow(double voltage) const
{
if (this->getBatteryVoltage() <= voltage && this->batteryVoltage > this->absurdLowVoltage)
{
return true;
}
return false;
}
bool Battery::isNewValue() {
bool Battery::isNewValue()
{
if (!this->calculatetNewValues)
{
return false;
}
this->calculatetNewValues = false;
return true;
}
void Battery::nextVoltageIsReady() {
if (this->calibrationState == CalibrationState::Waiting) {
void Battery::nextVoltageIsReady()
{
if (this->calibrationState == CalibrationState::Waiting)
{
this->calibrationState = CalibrationState::Reading;
}
}
void Battery::startCalibration() {
void Battery::startCalibration()
{
this->calibrationState = CalibrationState::Waiting;
this->currentCalibrationVoltage = 0;
this->bufferPos = 0;
this->loopDelay = 50;
this->newRawAdcVoltages = new uint16_t[60];
Component::loopDelay = Battery::loopDelay / 2;
this->newRawAdcVoltages = new uint16_t[Battery::rawAdcVoltagesCount];
}
void Battery::finishCalibration() {
void Battery::finishCalibration()
{
if (this->calibrationState != CalibrationState::None)
{
return;
}
delete[] this->newRawAdcVoltages;
this->calibrationState = CalibrationState::None;
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
double Battery::calculateInputVoltage() {
double Battery::calculateInputVoltage()
{
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = this->getBufAvg();
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;
if (reading < 1 || reading > Battery::adcMaxValue)
{
return 0;
}
return -this->adcCurveCoeficient[0] * pow(reading, 4) + this->adcCurveCoeficient[1] * pow(reading, 3) - this->adcCurveCoeficient[2] * pow(reading, 2) + this->adcCurveCoeficient[3] * reading + this->adcCurveCoeficient[4];
}
void Battery::calculateBatteryVoltage() {
if (this->r1 && this->r2) {
void Battery::calculateBatteryVoltage()
{
if (this->firstResistor && this->secondResistor)
{
this->batteryVoltage = this->calculateInputVoltage() * this->batteryVoltageFactor;
return;
}
uint16_t adcValue = this->getBufAvg();
if (adcValue < this->rawAdcVoltages[0]) {
const 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++) {
uint8_t index = 1;
for (; 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;
const double indexDelta = this->rawAdcVoltages[index] - this->rawAdcVoltages[index - 1];
const 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() {
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])
uint8_t index = 0;
for (; index < size; index++)
{
if (this->batteryVoltage <= this->capacityVoltages[index])
{
break;
}
}
if (i == 0) {
if (this->batteryVoltage > 6)
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--;
if (index == 0)
{
if (this->batteryVoltage > this->absurdLowVoltage)
{
std::cout << "Critical low battery!" << std::endl;
}
}
this->batteryPercent = i * (100 / (size - 1));
else if (index == size - 1)
{
}
else
{
const double diffToLowerVal = this->batteryVoltage - this->capacityVoltages[index - 1];
const double diffToHigherVal = this->capacityVoltages[index] - this->batteryVoltage;
if (diffToLowerVal > diffToHigherVal)
{
index--;
}
}
this->batteryPercent = index * (100 / (size - 1));
}
void Battery::readAdcToBuf() {
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() {
void Battery::initBuffer()
{
for (uint8_t i = 0; i < Battery::bufferSize; i++)
{
this->adcBuffer[i] = 0;
}
}
uint16_t Battery::getBufAvg() const {
uint16_t Battery::getBufAvg() const
{
uint32_t res = 0;
uint8_t emptyPos = 0;
for (uint8_t i = 0; i < Battery::bufferSize; i++) {
for (uint8_t i = 0; i < Battery::bufferSize; i++)
{
if (this->adcBuffer[i] == 0)
{
emptyPos++;
}
res += this->adcBuffer[i];
}
return res / (Battery::bufferSize - emptyPos);
+106 -97
View File
@@ -4,9 +4,9 @@
* @brief Contains a class for battery monitoring
* @version 0.1
* @date 2022-02-05
*
*
* @copyright Copyright (c) 2022
*
*
*/
#ifndef BATTERY_H
@@ -21,118 +21,127 @@
/**
* @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 Component {
public:
enum CalibrationState {
None,
Reading,
Waiting,
Finished
};
class Battery : public Component
{
public:
enum CalibrationState
{
None,
Reading,
Waiting,
Finished
};
/**
* @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);
Battery(uint8_t pin);
/**
* @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 firstResistor First resistor of the voltage devider.
* @param secondResistor Second resistor of the voltage devider.
*/
Battery(uint8_t pin, uint32_t firstResistor, uint32_t secondResistor);
Battery(uint8_t pin);
/**
* @brief Get the battery voltage
*
* @return double in Volt
*/
double getBatteryVoltage() const;
/**
* @brief Get the battery voltage
*
* @return double in Volt
*/
double getBatteryVoltage() const;
/**
* @brief Get the charge level of the battery
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() const { return this->batteryPercent; }
/**
* @brief Get the charge level of the battery
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() const { return this->batteryPercent; }
/**
* @brief Checks if the battery is low.
*
* The function will also return false if the battery voltage is
* absurd low. This is for the case that the uController is powered
* by usb and no battery is connected.
*
* @param voltage the limit the battery have to
* @return true if the battery is low
* @return false if the battery is high
*/
bool isBatteryLow(double voltage) const;
/**
* @brief Checks if the battery is low.
*
* The function will also return false if the battery voltage is
* absurd low. This is for the case that the uController is powered
* by usb and no battery is connected.
*
* @param voltage the limit the battery have to
* @return true if the battery is low
* @return false if the battery is high
*/
bool isBatteryLow(double voltage) const;
bool isNewValue();
bool isNewValue();
//Calibration
CalibrationState getCalibrationState() const { return this->calibrationState; }
uint8_t getCurrentCalibrationVoltage() const { return this->currentCalibrationVoltage; }
void nextVoltageIsReady();
void startCalibration();
void finishCalibration();
// Calibration
CalibrationState getCalibrationState() const { return this->calibrationState; }
uint8_t getCurrentCalibrationVoltage() const { return this->currentCalibrationVoltage; }
void nextVoltageIsReady();
void startCalibration();
void finishCalibration();
private:
void run() override;
void runCalibration();
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
void readAdcToBuf();
void initBuffer();
uint16_t getBufAvg() const;
private:
void run() override;
void runCalibration();
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
void readAdcToBuf();
void initBuffer();
uint16_t getBufAvg() const;
static const uint8_t bufferSize = 30;
static constexpr uint8_t bufferSize = 30;
static constexpr uint8_t loopDelay = 100;
static constexpr uint16_t adcMaxValue = 4095;
CalibrationState calibrationState = CalibrationState::None;
CalibrationState calibrationState = CalibrationState::None;
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t calulationDelayMultiplier = 5;
uint8_t loopCounter = 0;
uint8_t currentCalibrationVoltage = 0; // *0.1 + 7
uint16_t adcBuffer[bufferSize];
uint16_t* newRawAdcVoltages;
uint32_t r1 = 0;
uint32_t r2 = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
double batteryVoltageFactor;
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t calulationDelayMultiplier = 5;
uint8_t loopCounter = 0;
uint8_t currentCalibrationVoltage = 0; // *0.1 + 7
uint16_t adcBuffer[bufferSize];
uint16_t *newRawAdcVoltages = nullptr;
uint32_t firstResistor = 0;
uint32_t secondResistor = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
double batteryVoltageFactor;
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
11.39, 11.45, 11.51, 11.56, // 40 45 50 55
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 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
11.39, 11.45, 11.51, 11.56, // 40 45 50 55
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
{1992, 2021, 2056, 2090, 2118, 2145, 2177, 2208, 2241, 2272,
2298, 2331, 2362, 2387, 2420, 2453, 2482, 2514, 2543, 2577,
2607, 2640, 2670, 2703, 2736, 2763, 2794, 2826, 2858, 2890,
2920, 2956, 2983, 3019, 3054, 3088, 3121, 3158, 3189, 3226,
3264, 3300, 3339, 3379, 3414, 3453, 3500, 3544, 3598, 3636,
3682, 3730, 3781, 3837, 3887, 3943, 3997, 4054, 4093, 4095};
static constexpr uint8_t rawAdcVoltagesCount = 60;
const double startVoltage = 7;
const double stepVoltage = 0.1;
const uint16_t rawAdcVoltages[rawAdcVoltagesCount] = // from 7.0V to 12.9V in 0.1V steps
{1992, 2021, 2056, 2090, 2118, 2145, 2177, 2208, 2241, 2272,
2298, 2331, 2362, 2387, 2420, 2453, 2482, 2514, 2543, 2577,
2607, 2640, 2670, 2703, 2736, 2763, 2794, 2826, 2858, 2890,
2920, 2956, 2983, 3019, 3054, 3088, 3121, 3158, 3189, 3226,
3264, 3300, 3339, 3379, 3414, 3453, 3500, 3544, 3598, 3636,
3682, 3730, 3781, 3837, 3887, 3943, 3997, 4054, 4093, 4095};
const double adcCurveCoeficient[5] = {0.000000000000016,
0.000000000118171,
0.000000301211691,
0.001109019271794,
0.034143524634089};
};
#endif // BATTERY_H