Skip to content

Commit

Permalink
DPL: insist on power meter value more recent than inverter stats
Browse files Browse the repository at this point in the history
avoid performing a calculation based on a (slightly) outdated power
meter reading, which was aquired just before the limit was actually
applied by the inverter, but which was received by OpenDTU-OnBattery
after the inverter stats.
  • Loading branch information
schlimmchen committed Apr 15, 2024
1 parent 5fcf09d commit 4bc4def
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/PowerLimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PowerLimiterClass {

int32_t _lastRequestedPowerLimit = 0;
bool _shutdownPending = false;
std::optional<uint32_t> _oInverterStatsMillis = std::nullopt;
std::optional<uint32_t> _oUpdateStartMillis = std::nullopt;
std::optional<int32_t> _oTargetPowerLimitWatts = std::nullopt;
std::optional<bool> _oTargetPowerState = std::nullopt;
Expand Down
20 changes: 17 additions & 3 deletions src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,28 @@ void PowerLimiterClass::loop()
_inverter->SystemConfigPara()->getLastUpdateCommand(),
_inverter->PowerCommand()->getLastUpdateCommand());

if (_inverter->Statistics()->getLastUpdate() <= lastUpdateCmd) {
return announceStatus(Status::InverterStatsPending);
// we need inverter stats younger than the last update command
if (_oInverterStatsMillis.has_value() && lastUpdateCmd > *_oInverterStatsMillis) {
_oInverterStatsMillis = std::nullopt;
}

if (!_oInverterStatsMillis.has_value()) {
auto lastStats = _inverter->Statistics()->getLastUpdate();
if (lastStats <= lastUpdateCmd) {
return announceStatus(Status::InverterStatsPending);
}

_oInverterStatsMillis = lastStats;
}

// if the power meter is being used, i.e., if its data is valid, we want to
// wait for a new reading after adjusting the inverter limit. otherwise, we
// proceed as we will use a fallback limit independent of the power meter.
if (PowerMeter.isDataValid() && PowerMeter.getLastPowerMeterUpdate() <= lastUpdateCmd) {
// the power meter reading is expected to be at most 2 seconds old when it
// arrives. this can be the case for readings provided by networked meter
// readers, where a packet needs to travel through the network for some
// time after the actual measurement was done by the reader.
if (PowerMeter.isDataValid() && PowerMeter.getLastPowerMeterUpdate() <= (*_oInverterStatsMillis + 2000)) {
return announceStatus(Status::PowerMeterPending);
}

Expand Down

0 comments on commit 4bc4def

Please sign in to comment.