From 4fa5c36a096d3609a7150d9f03503d79ad6d90c5 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Sat, 30 Nov 2024 20:32:26 +0100 Subject: [PATCH] DPL: cache load-corrected voltage avoid re-calculation of the load-corrected voltage within the same DPL loop, as the calculation is quite costly. --- include/PowerLimiter.h | 3 +++ src/PowerLimiter.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/PowerLimiter.h b/include/PowerLimiter.h index 79a4c610e..c76b06d34 100644 --- a/include/PowerLimiter.h +++ b/include/PowerLimiter.h @@ -91,7 +91,10 @@ class PowerLimiterClass { uint16_t getSolarPassthroughPower(); std::optional getBatteryDischargeLimit(); float getBatteryInvertersOutputAcWatts(); + + std::optional _oLoadCorrectedVoltage = std::nullopt; float getLoadCorrectedVoltage(); + bool testThreshold(float socThreshold, float voltThreshold, std::function compare); bool isStartThresholdReached(); diff --git a/src/PowerLimiter.cpp b/src/PowerLimiter.cpp index 7a8077ed3..081eb7933 100644 --- a/src/PowerLimiter.cpp +++ b/src/PowerLimiter.cpp @@ -259,6 +259,9 @@ void PowerLimiterClass::loop() _batteryDischargeEnabled = getBatteryPower(); + // re-calculate load-corrected voltage once (and only once) per DPL loop + _oLoadCorrectedVoltage = std::nullopt; + if (_verboseLogging && usesBatteryPoweredInverter()) { MessageOutput.printf("[DPL] battery interface %sabled, SoC %.1f %% (%s), age %u s (%s)\r\n", (config.Battery.Enabled?"en":"dis"), @@ -720,6 +723,8 @@ std::optional PowerLimiterClass::getBatteryDischargeLimit() float PowerLimiterClass::getLoadCorrectedVoltage() { + if (_oLoadCorrectedVoltage) { return *_oLoadCorrectedVoltage; } + auto const& config = Configuration.get(); // TODO(schlimmchen): use the battery's data if available, @@ -731,7 +736,9 @@ float PowerLimiterClass::getLoadCorrectedVoltage() return 0.0; } - return dcVoltage + (acPower * config.PowerLimiter.VoltageLoadCorrectionFactor); + _oLoadCorrectedVoltage = dcVoltage + (acPower * config.PowerLimiter.VoltageLoadCorrectionFactor); + + return *_oLoadCorrectedVoltage; } bool PowerLimiterClass::testThreshold(float socThreshold, float voltThreshold,