Skip to content

Commit

Permalink
add explicit checks to avoid potential div. by zero on application of…
Browse files Browse the repository at this point in the history
… artificially increased power limit if channel power becomes zero
  • Loading branch information
qubeck committed Apr 12, 2023
1 parent cd4a327 commit b79619b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,20 @@ void PowerLimiterClass::setNewPowerLimit(std::shared_ptr<InverterAbstract> inver
}
MessageOutput.printf("[PowerLimiterClass::loop] Limit Non-Persistent: %d W\r\n", newPowerLimit);

int32_t effPowerLimit = newPowerLimit;
std::list<ChannelNum_t> dcChnls = inverter->Statistics()->getChannelsByType(TYPE_DC);
int dcProdChnls = 0, dcTotalChnls = dcChnls.size();
for (auto& c : dcChnls) {
if (inverter->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC) > 0) {
if (inverter->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC) > 1.0) {
dcProdChnls++;
}
}
int32_t effPowerLimit = round(newPowerLimit * static_cast<float>(dcTotalChnls) / dcProdChnls);
uint16_t inverterMaxPower = inverter->DevInfo()->getMaxPower();
if (effPowerLimit > inverterMaxPower) {
effPowerLimit = inverterMaxPower;
if (dcProdChnls > 0) {
effPowerLimit = round(newPowerLimit * static_cast<float>(dcTotalChnls) / dcProdChnls);
uint16_t inverterMaxPower = inverter->DevInfo()->getMaxPower();
if (effPowerLimit > inverterMaxPower) {
effPowerLimit = inverterMaxPower;
}
}

inverter->sendActivePowerControlRequest(Hoymiles.getRadio(), effPowerLimit, PowerLimitControlType::AbsolutNonPersistent);
Expand Down

0 comments on commit b79619b

Please sign in to comment.