Skip to content

Commit

Permalink
DPL: cache load-corrected voltage
Browse files Browse the repository at this point in the history
avoid re-calculation of the load-corrected voltage within the same DPL
loop, as the calculation is quite costly.
schlimmchen committed Nov 30, 2024
1 parent be9e2aa commit 4fa5c36
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/PowerLimiter.h
Original file line number Diff line number Diff line change
@@ -91,7 +91,10 @@ class PowerLimiterClass {
uint16_t getSolarPassthroughPower();
std::optional<uint16_t> getBatteryDischargeLimit();
float getBatteryInvertersOutputAcWatts();

std::optional<float> _oLoadCorrectedVoltage = std::nullopt;
float getLoadCorrectedVoltage();

bool testThreshold(float socThreshold, float voltThreshold,
std::function<bool(float, float)> compare);
bool isStartThresholdReached();
9 changes: 8 additions & 1 deletion src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
@@ -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<uint16_t> 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,

0 comments on commit 4fa5c36

Please sign in to comment.