OpenEVSE / openevse_esp32_firmware

OpenEVSE V4 WiFi gateway using ESP32
155 stars 107 forks source link

Clean up, simplify, and correct some energy meter code #810

Closed toofishes closed 2 days ago

toofishes commented 4 months ago

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.