improve battery tracking
This commit is contained in:
@@ -33,6 +33,12 @@ double Battery::getBatteryVoltage() {
|
|||||||
return (int)(res*100+0.5)/100.0;
|
return (int)(res*100+0.5)/100.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Battery::isBatteryLow(uint8_t percent) {
|
||||||
|
if (this->getBatteryPercent() <= percent && this->batteryVoltage > this->absurdLowVoltage)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
double Battery::readInputVoltage() {
|
double Battery::readInputVoltage() {
|
||||||
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
|
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
|
||||||
double reading = analogRead(this->pin);
|
double reading = analogRead(this->pin);
|
||||||
|
|||||||
@@ -73,13 +73,28 @@ class Battery {
|
|||||||
*/
|
*/
|
||||||
uint8_t getBatteryPercent() {return this->batteryPercent;}
|
uint8_t getBatteryPercent() {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 percent the limit the battery have to
|
||||||
|
* @return true if the battery is low
|
||||||
|
* @return false if the battery is high
|
||||||
|
*/
|
||||||
|
bool isBatteryLow(uint8_t percent);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double readInputVoltage();
|
double readInputVoltage();
|
||||||
void calculateBatteryVoltage();
|
void calculateBatteryVoltage();
|
||||||
void calculateBatteryPercent();
|
void calculateBatteryPercent();
|
||||||
|
|
||||||
|
uint8_t absurdLowVoltage = 5;
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
uint8_t batteryPercent;
|
uint8_t batteryPercent;
|
||||||
|
uint8_t batteryLowPercent = 10;
|
||||||
uint16_t delay = 20000;
|
uint16_t delay = 20000;
|
||||||
uint32_t r1;
|
uint32_t r1;
|
||||||
uint32_t r2;
|
uint32_t r2;
|
||||||
|
|||||||
+5
-3
@@ -142,17 +142,19 @@ void loop() {
|
|||||||
lcdWrapper->loop();
|
lcdWrapper->loop();
|
||||||
|
|
||||||
mainBattery->loop();
|
mainBattery->loop();
|
||||||
if (mainBattery->getBatteryPercent() <= 10) {
|
if (mainBattery->isBatteryLow(10)) {
|
||||||
moveController.setDrivingStatus(DrivingStatus::stop);
|
moveController.setDrivingStatus(DrivingStatus::stop);
|
||||||
moveController.setSpeed(0);
|
moveController.setSpeed(0);
|
||||||
moveController.setRotationSpeed(0);
|
moveController.setRotationSpeed(0);
|
||||||
|
|
||||||
|
uint16_t voltage = (uint16_t) (mainBattery->getBatteryVoltage() * 100);
|
||||||
|
|
||||||
lcd->setCursor(0, 0);
|
lcd->setCursor(0, 0);
|
||||||
lcd->printf("Low Battery...");
|
lcd->printf("Low Battery: %u", voltage);
|
||||||
lcd->setCursor(0, 1);
|
lcd->setCursor(0, 1);
|
||||||
lcd->print("Please turn off.");
|
lcd->print("Please turn off.");
|
||||||
|
|
||||||
while (1);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (disconnectPs3) {
|
if (disconnectPs3) {
|
||||||
|
|||||||
Reference in New Issue
Block a user