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
-DFEATURE_WEBSERVER
-DFEATURE_OTA
-DFEATURE_DPAD_6WIRESW
-DPINS_DPAD_6WIRESW_OUT=4
-DPINS_DPAD_6WIRESW_IN1=5
-DPINS_DPAD_6WIRESW_IN2=27
-DPINS_DPAD_6WIRESW_IN3=18
-DPINS_DPAD_6WIRESW_IN4=19
-DPINS_DPAD_6WIRESW_IN5=26
# -DFEATURE_DPAD_6WIRESW
# -DPINS_DPAD_6WIRESW_OUT=4
# -DPINS_DPAD_6WIRESW_IN1=5
# -DPINS_DPAD_6WIRESW_IN2=27
# -DPINS_DPAD_6WIRESW_IN3=18
# -DPINS_DPAD_6WIRESW_IN4=19
# -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_UDPCLOUD
-DFEATURE_LEDBACKLIGHT

View File

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

View File

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

View File

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

View File

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

View File

@ -113,24 +113,20 @@ void BatteryMenu::redraw()
{
Base::redraw();
float avgVoltage = 0;
for (auto &controller : controllers)
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
avgVoltage += controller.getCalibratedVoltage();
}
avgVoltage = avgVoltage / controllers.size();
const auto batPercent = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value));
if (battery::bootBatPercentage)
{
m_doubleProgressBarBatPercentage.redraw(batPercent, *battery::bootBatPercentage);
const auto batPercent = getBatteryPercentage(avgVoltage, BatteryCellType(configs.battery.cellType.value));
if (battery::bootBatPercentage)
{
m_doubleProgressBarBatPercentage.redraw(batPercent, *battery::bootBatPercentage);
tft.setTextFont(2);
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);
tft.setTextFont(2);
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:
std::string text() const override
{
float avgVoltage = 0;
for (auto &controller : controllers)
avgVoltage += controller.getCalibratedVoltage();
avgVoltage = avgVoltage / controllers.size();
auto watt = sumCurrent * avgVoltage;
auto w_per_kmh = watt / std::abs(avgSpeedKmh);
return fmt::format("{:.0f} Wh/km", w_per_kmh);
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
auto watt = sumCurrent * *avgVoltage;
auto w_per_kmh = watt / std::abs(avgSpeedKmh);
return fmt::format("{:.0f} Wh/km", w_per_kmh);
}
return "No Battery";
}
};

View File

@ -97,17 +97,13 @@ void calculateStatistics()
}
drivingStatistics.currentDrivingTime += duration;
float avgVoltage = 0;
for (auto &controller : controllers)
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
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
{

View File

@ -83,6 +83,23 @@ public:
Controller &front{operator[](0)};
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;

View File

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

View File

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