Files
bobbycar-boardcomputer-firm…/main/drivingstatistics.cpp

126 lines
3.8 KiB
C++
Raw Permalink Normal View History

2021-11-18 01:11:20 +01:00
#include "drivingstatistics.h"
2021-11-21 01:36:44 +01:00
// 3rdparty lib includes
2021-11-18 01:11:20 +01:00
#include <FastLED.h>
2021-11-21 01:36:44 +01:00
#include <TFT_eSPI.h>
#include <date/date.h>
2021-11-18 01:11:20 +01:00
// Local
#include "globals.h"
#include "battery.h"
#include "utils.h"
using namespace std::chrono_literals;
2021-11-21 01:36:44 +01:00
DrivingStatistics drivingStatistics;
2021-11-18 01:11:20 +01:00
float getAvgWhPerKm()
{
return drivingStatistics.wh_used / (drivingStatistics.meters_driven / 1000.f);
}
2022-01-11 11:16:15 +01:00
float getAvgKmh()
{
2022-02-12 19:07:34 +01:00
return (drivingStatistics.meters_driven / 1000.f) / ((drivingStatistics.currentDrivingTime / 1ms) / 1000.f / 60.f / 60.f); // (meter / 1000) / (ms / 1000 / 60 / 60)
2022-01-11 11:16:15 +01:00
}
2022-01-17 10:10:08 +01:00
float getEstimatedKmLeft()
{
return (getRemainingWattHours() / getAvgWhPerKm());
}
2021-11-18 01:11:20 +01:00
std::string getEfficiencyClassString()
{
const float avgWhPerKm = getAvgWhPerKm();
2021-11-21 01:36:44 +01:00
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";
2021-11-18 01:11:20 +01:00
}
uint16_t getEfficiencyClassColor()
{
const float avgWhPerKm = getAvgWhPerKm();
2021-11-21 01:36:44 +01:00
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;
2021-11-18 01:11:20 +01:00
}
2022-01-17 10:10:08 +01:00
std::string getRemainingEstimateRangeString()
{
return fmt::format("{:.1f} km", getEstimatedKmLeft());
}
void initStatistics()
{
}
2021-11-18 01:11:20 +01:00
void calculateStatistics()
{
2022-02-12 19:07:34 +01:00
static bool saveTotal = false;
2021-11-18 01:11:20 +01:00
2022-04-29 22:40:49 +02:00
if ((configs.savedStatistics.totalCentimeters.value() / 100.f) > drivingStatistics.totalMeters)
2022-02-12 19:07:34 +01:00
{
2022-04-29 22:40:49 +02:00
drivingStatistics.totalMeters = configs.savedStatistics.totalCentimeters.value() / 100.f;
drivingStatistics.last_cm_written = configs.savedStatistics.totalCentimeters.value();
2022-02-12 19:07:34 +01:00
}
2021-11-18 01:11:20 +01:00
2022-02-12 19:07:34 +01:00
static auto last_km_calculation = espchrono::millis_clock::now();
const auto duration = espchrono::ago(last_km_calculation);
last_km_calculation = espchrono::millis_clock::now();
2021-11-18 01:11:20 +01:00
2022-06-12 17:51:31 +02:00
const float meters_driven_now = (abs(avgSpeedKmh) / 3.6f) * ((duration / 1ms) / 1000.f);
2022-02-12 19:07:34 +01:00
drivingStatistics.meters_driven += meters_driven_now;
2022-06-12 17:51:31 +02:00
drivingStatistics.totalMeters += meters_driven_now; // Update meters driven
2021-11-18 01:11:20 +01:00
2022-02-12 19:07:34 +01:00
if (abs(avgSpeedKmh) > 1)
{
if (!saveTotal && abs(avgSpeedKmh) > 5)
2021-11-18 01:11:20 +01:00
{
2022-02-12 19:07:34 +01:00
saveTotal = true;
2021-11-18 01:11:20 +01:00
}
2022-02-12 19:07:34 +01:00
drivingStatistics.currentDrivingTime += duration;
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
2021-11-18 01:11:20 +01:00
{
2022-06-12 17:51:31 +02:00
const float watt = sumCurrent * *avgVoltage;
const float ws_driven_now = watt * ((duration / 1ms) / 1000.f);
drivingStatistics.wh_used += ws_driven_now / 3600.f; // Wh
drivingStatistics.batteryWhEstimate -= ws_driven_now / 3600.f;
2021-11-18 01:11:20 +01:00
}
2022-02-12 19:07:34 +01:00
}
else
{
2022-06-12 17:51:31 +02:00
drivingStatistics.wh_used += (13 * ((duration / 1ms) / 1000.f)) / 3600.f; // Wh
2022-02-12 19:07:34 +01:00
drivingStatistics.batteryWhEstimate = getRemainingWattHours();
}
2021-11-18 01:11:20 +01:00
2022-06-12 17:51:31 +02:00
if ((drivingStatistics.totalMeters > ((drivingStatistics.last_cm_written / 100.f) + 100)) || (saveTotal && abs(avgSpeedKmh) < 0.5f))
2022-02-12 19:07:34 +01:00
{
if (saveTotal)
2021-11-18 01:11:20 +01:00
{
2022-02-12 19:07:34 +01:00
saveTotal = false;
2021-11-18 01:11:20 +01:00
}
2022-02-12 19:07:34 +01:00
drivingStatistics.last_cm_written = drivingStatistics.totalMeters * 100; // Save total Meters
2022-10-06 10:43:03 +02:00
if (!configs.emulateFeedback.value())
{
configs.write_config(configs.savedStatistics.totalCentimeters, drivingStatistics.last_cm_written);
}
2021-11-18 01:11:20 +01:00
}
}