From 802ebc5341705ad5f4f5e9aadc48d146263f07d6 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Thu, 7 Mar 2024 22:07:41 -0600 Subject: [PATCH] Clean up, simplify, and correct some energy meter code Much of this code was copied from the low-level firmware side, where we don't have floating point available, and had to use some tricks to ensure we didn't lose precision. (I should know- I wrote it!) Since this doesn't apply in our ESP32 code, we can just multiply the three values (volts, amps, milliseconds) together to get milliwatt-seconds. Additionally, we should store elapsed as a floating point value, not an integer number of seconds. As we're going through the pool loop, which repeats every 1000 ms, we might not hit that value exactly. Imagine we are closer to 1020 ms instead due to the work we need to do each iteration. That would allow our elapsed second counter, which stores a truncated value (1020ms gets rounded to integer 1 second), to drift by 1 second every 50 seconds. --- src/energy_meter.cpp | 45 ++++++++------------------------------------ src/energy_meter.h | 3 +-- src/evse_man.h | 2 +- 3 files changed, 10 insertions(+), 40 deletions(-) diff --git a/src/energy_meter.cpp b/src/energy_meter.cpp index 6b1bfe0c..f221cbc6 100644 --- a/src/energy_meter.cpp +++ b/src/energy_meter.cpp @@ -119,7 +119,7 @@ void EnergyMeterData::deserialize(StaticJsonDocument &doc) // old OpenEvse total_energy imported flag imported = doc["im"]; } - if (doc.containsKey("el") && doc["el"].is()) + if (doc.containsKey("el") && doc["el"].is()) { // elapsed elapsed = doc["el"]; @@ -134,7 +134,6 @@ void EnergyMeterData::deserialize(StaticJsonDocument &doc) EnergyMeter::EnergyMeter() : _last_upd(0), _write_upd(0), _rotate_upd(0), - _elapsed(0), _switch_state(0){}; EnergyMeter::~EnergyMeter() @@ -230,41 +229,13 @@ bool EnergyMeter::update() if (_monitor->isCharging()) { // increment elapsed time - _data.elapsed += dms / 1000U; + _data.elapsed += dms / 1000.0; DBUGLN("Energy Meter: Incrementing"); // accumulate data - uint32_t mv = _monitor->getVoltage() * 1000; - uint32_t ma = _monitor->getAmps() * 1000; - - /* - * The straightforward formula to compute 'milliwatt-seconds' would be: - * mws = (mv/1000) * (ma/1000) * dms; - * - * However, this has some serious drawbacks, namely, truncating values - * when using integer math. This can introduce a lot of error! - * 5900 milliamps -> 5.9 amps (float) -> 5 amps (integer) - * 0.9 amps difference / 5.9 amps -> 15.2% error - * - * The crazy equation below helps ensure our intermediate results always - * fit in a 32-bit unsigned integer, but retain as much precision as - * possible throughout the calculation. Here is how it was derived: - * mws = (mv/1000) * (ma/1000) * dms; - * mws = (mv/(2**3 * 5**3)) * (ma/(2**3 * 5**3)) * dms; - * mws = (mv/2**3) * (ma/(2**3) / 5**6 * dms; - * mws = (mv/2**4) * (ma/(2**2) / 5**6 * dms; - * - * By breaking 1000 into prime factors of 2 and 5, and shifting things - * around, we almost match the precision of floating-point math. - * - * Using 16 and 4 divisors, rather than 8 and 8, helps precision because - * mv is always greater than ~100000, but ma can be as low as ~6000. - * - * A final note- the divisions by factors of 2 are done with right shifts - * by the compiler, so the revised equation, although it looks quite - * complex, only requires one divide operation. - */ - double mws = (mv / 16) * (ma / 4) / 15625 * dms; + double v = _monitor->getVoltage(); + double a = _monitor->getAmps(); + double mws = v * a * dms; if (config_threephase_enabled()) { // Multiply calculation by 3 to get 3-phase energy. @@ -272,9 +243,9 @@ bool EnergyMeter::update() } // convert to w/h - double wh = mws / 3600000UL; + double wh = mws / 3600000; _data.session += wh; - double kwh = wh / 1000UL; + double kwh = wh / 1000; _data.total += kwh; DBUGVAR(_data.session); _data.daily += kwh; @@ -318,7 +289,7 @@ bool EnergyMeter::publish() void EnergyMeter::createEnergyMeterJsonDoc(JsonDocument &doc) { - doc["session_elapsed"] = _data.elapsed; // sec + doc["session_elapsed"] = (uint32_t)_data.elapsed; // sec doc["session_energy"] = _data.session; // wh doc["total_energy"] = _data.total; // kwh doc["total_day"] = _data.daily; // kwh diff --git a/src/energy_meter.h b/src/energy_meter.h index d9cc1631..294e1f25 100644 --- a/src/energy_meter.h +++ b/src/energy_meter.h @@ -39,7 +39,7 @@ class EnergyMeterData double weekly; // kwh double monthly; // kwh double yearly; // kwh - uint32_t elapsed; // sec + double elapsed; // sec uint32_t switches; // homw many switches the relay/contactor got bool imported; // has imported old counter already EnergyMeterDate date; @@ -57,7 +57,6 @@ class EnergyMeter uint32_t _write_upd; uint32_t _event_upd; uint32_t _rotate_upd; - uint32_t _elapsed; uint8_t _switch_state; // 0: Undefined, 1: Enabled, 2: Disabled EvseMonitor *_monitor; diff --git a/src/evse_man.h b/src/evse_man.h index 3321fc2d..310b4b68 100644 --- a/src/evse_man.h +++ b/src/evse_man.h @@ -348,7 +348,7 @@ class EvseManager : public MicroTasks::Task bool publishEnergyMeter() { return _monitor.publishEnergyMeter(); } - void createEnergyMeterJsonDoc(JsonDocument &doc) { + void createEnergyMeterJsonDoc(JsonDocument &doc) { _monitor.createEnergyMeterJsonDoc(doc); } long getFaultCountGFCI() {