Refactored voltage readings

This commit is contained in:
CommanderRedYT
2022-01-27 01:09:52 +01:00
parent 23e94b213f
commit e3640046c0
11 changed files with 102 additions and 94 deletions

View File

@@ -45,13 +45,19 @@ set(BOBBYCAR_BUILDFLAGS
-DDEFAULT_FIELDADVMAX=40 -DDEFAULT_FIELDADVMAX=40
-DFEATURE_WEBSERVER -DFEATURE_WEBSERVER
-DFEATURE_OTA -DFEATURE_OTA
-DFEATURE_DPAD_6WIRESW # -DFEATURE_DPAD_6WIRESW
-DPINS_DPAD_6WIRESW_OUT=4 # -DPINS_DPAD_6WIRESW_OUT=4
-DPINS_DPAD_6WIRESW_IN1=5 # -DPINS_DPAD_6WIRESW_IN1=5
-DPINS_DPAD_6WIRESW_IN2=27 # -DPINS_DPAD_6WIRESW_IN2=27
-DPINS_DPAD_6WIRESW_IN3=18 # -DPINS_DPAD_6WIRESW_IN3=18
-DPINS_DPAD_6WIRESW_IN4=19 # -DPINS_DPAD_6WIRESW_IN4=19
-DPINS_DPAD_6WIRESW_IN5=26 # -DPINS_DPAD_6WIRESW_IN5=26
-DFEATURE_DPAD_5WIRESW_2OUT
-DPINS_DPAD_5WIRESW_OUT1=4
-DPINS_DPAD_5WIRESW_OUT2=5
-DPINS_DPAD_5WIRESW_IN1=18
-DPINS_DPAD_5WIRESW_IN2=19
-DPINS_DPAD_5WIRESW_IN3=27
-DFEATURE_BLE -DFEATURE_BLE
-DFEATURE_UDPCLOUD -DFEATURE_UDPCLOUD
-DFEATURE_LEDBACKLIGHT -DFEATURE_LEDBACKLIGHT

View File

@@ -123,14 +123,12 @@ float getRemainingWattHours()
{ {
float target_mah = getTarget_mAh(); float target_mah = getTarget_mAh();
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); return (target_mah / 1000.f) * 3.7 * configs.battery.cellsParallel.value * configs.battery.cellsSeries.value * (getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value)) / 100);
} }
avgVoltage = avgVoltage / controllers.size(); else
return 0.;
return (target_mah / 1000.f) * 3.7 * configs.battery.cellsParallel.value * configs.battery.cellsSeries.value * (getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value)) / 100);
} }
float getPercentageByWh(float wh) float getPercentageByWh(float wh)
@@ -152,15 +150,13 @@ float getTarget_mAh()
std::string getBatteryPercentageString() std::string getBatteryPercentageString()
{ {
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); std::string output = fmt::format("Battery: {:.1f}%", getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value)));
return output;
} }
avgVoltage = avgVoltage / controllers.size(); else
return "No Battery.";
std::string output = fmt::format("Battery: {:.1f}%", getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value)));
return output;
} }
std::string getBatteryAdvancedPercentageString() std::string getBatteryAdvancedPercentageString()
@@ -186,13 +182,11 @@ std::string getRemainingRangeString()
std::string getBatteryDebugString() std::string getBatteryDebugString()
{ {
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); return fmt::format("{:.1f}V {}A", *avgVoltage, sumCurrent);
} }
avgVoltage = avgVoltage / controllers.size(); return "No Battery";
return fmt::format("{:.1f}V {}A", avgVoltage, sumCurrent);
} }
namespace battery { namespace battery {

View File

@@ -129,4 +129,9 @@ void handle_handbremse()
} }
} }
void batteryDebugPrint()
{
ESP_LOGI("BATTERY", "");
}
} // namespace quickactions } // namespace quickactions

View File

@@ -26,5 +26,6 @@ void blink_left();
void blink_right(); void blink_right();
void handle_handbremse(); void handle_handbremse();
void action_wifi_scan(); void action_wifi_scan();
void batteryDebugPrint();
} // namespace quickactions } // namespace quickactions

View File

@@ -50,16 +50,14 @@ class BatteryDebug2Text : public virtual espgui::TextInterface
public: public:
std::string text() const override std::string text() const override
{ {
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); auto watt = sumCurrent * *avgVoltage;
auto w_per_kmh = watt / avgSpeedKmh;
return fmt::format("{:.0f} {:.0f}W/kmh", avgSpeedKmh, w_per_kmh);
} }
avgVoltage = avgVoltage / controllers.size(); else
return "No battery";
auto watt = sumCurrent * avgVoltage;
auto w_per_kmh = watt / avgSpeedKmh;
return fmt::format("{:.0f} {:.0f}W/kmh", avgSpeedKmh, w_per_kmh);
} }
}; };

View File

@@ -113,24 +113,20 @@ void BatteryMenu::redraw()
{ {
Base::redraw(); Base::redraw();
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); const auto batPercent = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value));
} if (battery::bootBatPercentage)
avgVoltage = avgVoltage / controllers.size(); {
m_doubleProgressBarBatPercentage.redraw(batPercent, *battery::bootBatPercentage);
const auto batPercent = getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value)); tft.setTextFont(2);
if (battery::bootBatPercentage) tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
{ m_batPercentNowLabel.redraw(fmt::format("{:.2f} %", batPercent));
m_doubleProgressBarBatPercentage.redraw(batPercent, *battery::bootBatPercentage); m_batPercentBootLabel.redraw(fmt::format("{:.2f} %", *battery::bootBatPercentage));
tft.setTextFont(4);
tft.setTextFont(2); tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextColor(TFT_DARKGREY, TFT_BLACK); }
m_batPercentNowLabel.redraw(fmt::format("{:.2f} %", batPercent));
m_batPercentBootLabel.redraw(fmt::format("{:.2f} %", *battery::bootBatPercentage));
tft.setTextFont(4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
} }
} }

View File

@@ -27,14 +27,13 @@ class WhPerKmText : public virtual espgui::TextInterface
public: public:
std::string text() const override std::string text() const override
{ {
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers) {
avgVoltage += controller.getCalibratedVoltage(); auto watt = sumCurrent * *avgVoltage;
avgVoltage = avgVoltage / controllers.size(); auto w_per_kmh = watt / std::abs(avgSpeedKmh);
return fmt::format("{:.0f} Wh/km", w_per_kmh);
auto watt = sumCurrent * avgVoltage; }
auto w_per_kmh = watt / std::abs(avgSpeedKmh); return "No Battery";
return fmt::format("{:.0f} Wh/km", w_per_kmh);
} }
}; };

View File

@@ -97,17 +97,13 @@ void calculateStatistics()
} }
drivingStatistics.currentDrivingTime += duration; drivingStatistics.currentDrivingTime += duration;
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); auto watt = sumCurrent * *avgVoltage;
const float ws_driven_now = watt * (duration.count() / 1000.);
drivingStatistics.wh_used += ws_driven_now / 3600; // Wh
drivingStatistics.batteryWhEstimate -= ws_driven_now / 3600;
} }
avgVoltage = avgVoltage / controllers.size();
auto watt = sumCurrent * avgVoltage;
const float ws_driven_now = watt * (duration.count() / 1000.);
drivingStatistics.wh_used += ws_driven_now / 3600; // Wh
drivingStatistics.batteryWhEstimate -= ws_driven_now / 3600;
} }
else else
{ {

View File

@@ -83,6 +83,23 @@ public:
Controller &front{operator[](0)}; Controller &front{operator[](0)};
Controller &back{operator[](1)}; Controller &back{operator[](1)};
std::optional<float> getAvgVoltage() const
{
uint8_t voltages{0};
float avgVoltage{0.};
for (auto &controller : *this)
{
if (const auto result = controller.getCalibratedVoltage(); !std::isnan(result) && controller.feedbackValid)
{
avgVoltage += result;
voltages++;
}
}
if (!voltages)
return std::nullopt;
return avgVoltage / voltages;
}
}; };
extern Controllers controllers; extern Controllers controllers;

View File

@@ -128,16 +128,13 @@ extern "C" void app_main()
{ {
if(controllers.front.feedbackValid && controllers.back.feedbackValid) if(controllers.front.feedbackValid && controllers.back.feedbackValid)
{ {
float avgVoltage = 0; if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
for (auto &controller : controllers)
{ {
avgVoltage += controller.getCalibratedVoltage(); if (avgVoltage > 30)
} {
avgVoltage = avgVoltage / controllers.size(); battery::bootBatPercentage = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value));
if (avgVoltage > 30) battery::bootBatWh = getRemainingWattHours();
{ }
battery::bootBatPercentage = getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value));
battery::bootBatWh = getRemainingWattHours();
} }
} }
} }

View File

@@ -83,14 +83,11 @@ std::string buildUdpCloudJson()
std::string buf; std::string buf;
const auto uptime = espchrono::millis_clock::now().time_since_epoch().count(); const auto uptime = espchrono::millis_clock::now().time_since_epoch().count();
float avgVoltage = 0; float watt{0};
for (auto &controller : controllers) const auto avgVoltage = controllers.getAvgVoltage();
{ if(avgVoltage)
avgVoltage += controller.getCalibratedVoltage(); watt = sumCurrent * *avgVoltage;
}
avgVoltage = avgVoltage / controllers.size();
const auto watt = sumCurrent * avgVoltage;
// const auto w_per_kmh = watt / avgSpeedKmh; // const auto w_per_kmh = watt / avgSpeedKmh;
// User ID // User ID
@@ -142,8 +139,11 @@ std::string buildUdpCloudJson()
} }
// Statistics // Statistics
doc["bP"] = getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value)); if(avgVoltage)
doc["bV"] = avgVoltage; {
doc["bP"] = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value));
doc["bV"] = *avgVoltage;
}
doc["l"] = isLocked; doc["l"] = isLocked;
doc["mN"] = drivingStatistics.meters_driven; doc["mN"] = drivingStatistics.meters_driven;
doc["mT"] = drivingStatistics.totalMeters; doc["mT"] = drivingStatistics.totalMeters;
@@ -179,14 +179,10 @@ std::string buildUdpCloudString()
const auto uptime = espchrono::millis_clock::now().time_since_epoch().count(); const auto uptime = espchrono::millis_clock::now().time_since_epoch().count();
float avgVoltage = 0; float watt{0};
for (auto &controller : controllers) const auto avgVoltage = controllers.getAvgVoltage();
{ if(avgVoltage)
avgVoltage += controller.getCalibratedVoltage(); watt = sumCurrent * *avgVoltage;
}
avgVoltage = avgVoltage / controllers.size();
const auto watt = sumCurrent * avgVoltage;
// const auto w_per_kmh = watt / avgSpeedKmh; // const auto w_per_kmh = watt / avgSpeedKmh;
buf += "{"; buf += "{";
@@ -290,8 +286,11 @@ std::string buildUdpCloudString()
} }
// Statistics // Statistics
buf += fmt::format("\"bP\":{},", getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value))); if(avgVoltage)
buf += fmt::format("\"bV\":{},", avgVoltage); {
buf += fmt::format("\"bP\":{},", getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value)));
buf += fmt::format("\"bV\":{},", *avgVoltage);
}
buf += fmt::format("\"l\":{},", isLocked); buf += fmt::format("\"l\":{},", isLocked);
buf += fmt::format("\"mN\":{},", drivingStatistics.meters_driven); buf += fmt::format("\"mN\":{},", drivingStatistics.meters_driven);
buf += fmt::format("\"mT\":{},", drivingStatistics.totalMeters); buf += fmt::format("\"mT\":{},", drivingStatistics.totalMeters);