- added batteryCalibration
- start with default Menus for DriveModes
This commit is contained in:
+51
-1
@@ -27,10 +27,15 @@ Battery::Battery(uint8_t pin) {
|
||||
}
|
||||
|
||||
void Battery::run() {
|
||||
if (this->calibrationState != CalibrationState::None) {
|
||||
this->runCalibration();
|
||||
return;
|
||||
}
|
||||
|
||||
this->readAdcToBuf();
|
||||
this->loopCounter++;
|
||||
|
||||
if (this->loopCounter == this->calulationDelayMultiplier) {
|
||||
if (this->loopCounter >= this->calulationDelayMultiplier) {
|
||||
this->calculateBatteryVoltage();
|
||||
this->calculateBatteryPercent();
|
||||
this->loopCounter = 0;
|
||||
@@ -40,6 +45,28 @@ void Battery::run() {
|
||||
return;
|
||||
}
|
||||
|
||||
void Battery::runCalibration() {
|
||||
if (this->calibrationState != CalibrationState::Reading)
|
||||
return;
|
||||
|
||||
this->readAdcToBuf();
|
||||
if (this->bufferPos == 0) {
|
||||
uint16_t res = this->getBufAvg();
|
||||
this->newRawAdcVoltages[this->currentCalibrationVoltage] = res;
|
||||
std::cout << "Index: "
|
||||
<< (int) this->currentCalibrationVoltage
|
||||
<< " Value: "
|
||||
<< (int) res
|
||||
<< std::endl;
|
||||
this->currentCalibrationVoltage++;
|
||||
this->calibrationState = CalibrationState::Waiting;
|
||||
|
||||
if (this->currentCalibrationVoltage == 60) {
|
||||
this->calibrationState = CalibrationState::Finished;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double Battery::getBatteryVoltage() const {
|
||||
double res = this->batteryVoltage;
|
||||
return (int)(res*100+0.5)/100.0;
|
||||
@@ -59,6 +86,29 @@ bool Battery::isNewValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Battery::nextVoltageIsReady() {
|
||||
if (this->calibrationState == CalibrationState::Waiting) {
|
||||
this->calibrationState = CalibrationState::Reading;
|
||||
}
|
||||
}
|
||||
|
||||
void Battery::startCalibration() {
|
||||
this->calibrationState = CalibrationState::Waiting;
|
||||
this->currentCalibrationVoltage = 0;
|
||||
this->bufferPos = 0;
|
||||
this->loopDelay = 50;
|
||||
this->newRawAdcVoltages = new uint16_t[60];
|
||||
}
|
||||
|
||||
void Battery::finishCalibration() {
|
||||
if (this->calibrationState != CalibrationState::None)
|
||||
return;
|
||||
|
||||
delete[] this->newRawAdcVoltages;
|
||||
this->calibrationState = CalibrationState::None;
|
||||
this->loopDelay = 100;
|
||||
}
|
||||
|
||||
double Battery::calculateInputVoltage() {
|
||||
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
|
||||
double reading = this->getBufAvg();
|
||||
|
||||
+25
-6
@@ -29,6 +29,13 @@
|
||||
*/
|
||||
class Battery : public Component {
|
||||
public:
|
||||
enum CalibrationState {
|
||||
None,
|
||||
Reading,
|
||||
Waiting,
|
||||
Finished
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Construct a new Battery object
|
||||
*
|
||||
@@ -72,8 +79,16 @@ class Battery : public Component {
|
||||
|
||||
bool isNewValue();
|
||||
|
||||
//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();
|
||||
@@ -83,6 +98,8 @@ class Battery : public Component {
|
||||
|
||||
static const uint8_t bufferSize = 30;
|
||||
|
||||
CalibrationState calibrationState = CalibrationState::None;
|
||||
|
||||
uint8_t absurdLowVoltage = 5;
|
||||
uint8_t pin;
|
||||
uint8_t batteryPercent = 0;
|
||||
@@ -90,7 +107,9 @@ class Battery : public Component {
|
||||
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;
|
||||
@@ -108,12 +127,12 @@ class Battery : public Component {
|
||||
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};
|
||||
{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};
|
||||
};
|
||||
|
||||
#endif // BATTERY_H
|
||||
|
||||
Reference in New Issue
Block a user