driving statistics fixes
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
#include "drivingstatistics.h"
|
||||
|
||||
// 3rd party
|
||||
// 3rdparty lib includes
|
||||
#include <FastLED.h>
|
||||
#include "TFT_eSPI.h"
|
||||
#include <TFT_eSPI.h>
|
||||
#include <date/date.h>
|
||||
|
||||
// Local
|
||||
#include "globals.h"
|
||||
#include "battery.h"
|
||||
#include "utils.h"
|
||||
|
||||
DrivingStatistics drivingStatistics;
|
||||
|
||||
float getAvgWhPerKm()
|
||||
{
|
||||
return drivingStatistics.wh_used / (drivingStatistics.meters_driven / 1000.f);
|
||||
@@ -17,91 +20,31 @@ float getAvgWhPerKm()
|
||||
std::string getEfficiencyClassString()
|
||||
{
|
||||
const float avgWhPerKm = getAvgWhPerKm();
|
||||
if (avgWhPerKm <= 14)
|
||||
{
|
||||
return "A+++";
|
||||
}
|
||||
else if (avgWhPerKm <= 16)
|
||||
{
|
||||
return "A++";
|
||||
}
|
||||
else if (avgWhPerKm <= 18)
|
||||
{
|
||||
return "A+";
|
||||
}
|
||||
else if (avgWhPerKm <= 20)
|
||||
{
|
||||
return "A";
|
||||
}
|
||||
else if (avgWhPerKm <= 24)
|
||||
{
|
||||
return "B";
|
||||
}
|
||||
else if (avgWhPerKm <= 28)
|
||||
{
|
||||
return "C";
|
||||
}
|
||||
else if (avgWhPerKm <= 32)
|
||||
{
|
||||
return "D";
|
||||
}
|
||||
else if (avgWhPerKm <= 36)
|
||||
{
|
||||
return "E";
|
||||
}
|
||||
else if (avgWhPerKm <= 40)
|
||||
{
|
||||
return "F";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "G";
|
||||
}
|
||||
if (avgWhPerKm <= 14) return "A+++";
|
||||
else if (avgWhPerKm <= 16) return "A++";
|
||||
else if (avgWhPerKm <= 18) return "A+";
|
||||
else if (avgWhPerKm <= 20) return "A";
|
||||
else if (avgWhPerKm <= 24) return "B";
|
||||
else if (avgWhPerKm <= 28) return "C";
|
||||
else if (avgWhPerKm <= 32) return "D";
|
||||
else if (avgWhPerKm <= 36) return "E";
|
||||
else if (avgWhPerKm <= 40) return "F";
|
||||
else return "G";
|
||||
}
|
||||
|
||||
uint16_t getEfficiencyClassColor()
|
||||
{
|
||||
const float avgWhPerKm = getAvgWhPerKm();
|
||||
if (avgWhPerKm <= 14)
|
||||
{
|
||||
return 0x1700;
|
||||
}
|
||||
else if (avgWhPerKm <= 16)
|
||||
{
|
||||
return 0x3640;
|
||||
}
|
||||
else if (avgWhPerKm <= 18)
|
||||
{
|
||||
return 0x5560;
|
||||
}
|
||||
else if (avgWhPerKm <= 20)
|
||||
{
|
||||
return 0x6CA0;
|
||||
}
|
||||
else if (avgWhPerKm <= 24)
|
||||
{
|
||||
return 0x83E0;
|
||||
}
|
||||
else if (avgWhPerKm <= 28)
|
||||
{
|
||||
return 0x9B20;
|
||||
}
|
||||
else if (avgWhPerKm <= 32)
|
||||
{
|
||||
return 0xB240;
|
||||
}
|
||||
else if (avgWhPerKm <= 36)
|
||||
{
|
||||
return 0xC980;
|
||||
}
|
||||
else if (avgWhPerKm <= 40)
|
||||
{
|
||||
return 0xE0C0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0xF800;
|
||||
}
|
||||
if (avgWhPerKm <= 14) return 0x1700;
|
||||
else if (avgWhPerKm <= 16) return 0x3640;
|
||||
else if (avgWhPerKm <= 18) return 0x5560;
|
||||
else if (avgWhPerKm <= 20) return 0x6CA0;
|
||||
else if (avgWhPerKm <= 24) return 0x83E0;
|
||||
else if (avgWhPerKm <= 28) return 0x9B20;
|
||||
else if (avgWhPerKm <= 32) return 0xB240;
|
||||
else if (avgWhPerKm <= 36) return 0xC980;
|
||||
else if (avgWhPerKm <= 40) return 0xE0C0;
|
||||
else return 0xF800;
|
||||
}
|
||||
|
||||
void calculateStatistics()
|
||||
@@ -116,10 +59,10 @@ void calculateStatistics()
|
||||
}
|
||||
|
||||
static auto last_km_calculation = espchrono::millis_clock::now();
|
||||
const auto duration = espchrono::ago(last_km_calculation).count() / 1000.0f;
|
||||
const auto duration = espchrono::ago(last_km_calculation);
|
||||
last_km_calculation = espchrono::millis_clock::now();
|
||||
|
||||
const float meters_driven_now = (abs(avgSpeedKmh) / 3.6) * duration;
|
||||
const float meters_driven_now = (abs(avgSpeedKmh) / 3.6) * std::chrono::floor<espchrono::seconds32>(duration).count();
|
||||
drivingStatistics.meters_driven += meters_driven_now;
|
||||
drivingStatistics.totalMeters += meters_driven_now; // Udate meters driven
|
||||
|
||||
@@ -139,13 +82,13 @@ void calculateStatistics()
|
||||
avgVoltage = avgVoltage / controllers.size();
|
||||
|
||||
auto watt = sumCurrent * avgVoltage;
|
||||
const float ws_driven_now = watt * duration;
|
||||
const float ws_driven_now = watt * std::chrono::floor<espchrono::seconds32>(duration).count();
|
||||
drivingStatistics.wh_used += ws_driven_now / 3600; // Wh
|
||||
drivingStatistics.batteryWhEstimate -= ws_driven_now / 3600;
|
||||
}
|
||||
else
|
||||
{
|
||||
drivingStatistics.wh_used += (13 * duration) / 3600; // Wh
|
||||
drivingStatistics.wh_used += (13 * std::chrono::floor<espchrono::seconds32>(duration).count()) / 3600; // Wh
|
||||
drivingStatistics.batteryWhEstimate = getRemainingWattHours();
|
||||
}
|
||||
|
||||
@@ -161,3 +104,12 @@ void calculateStatistics()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_current_driving_time_string()
|
||||
{
|
||||
auto converted = date::make_time(drivingStatistics.currentDrivingTime);
|
||||
return fmt::format("Drive: {:02d}:{:02d}:{:02d}",
|
||||
converted.hours().count(),
|
||||
converted.minutes().count(),
|
||||
converted.seconds().count());
|
||||
}
|
||||
|
Reference in New Issue
Block a user