Fix PowerDelta calculation

Fix PowerDelta calculation not detecting zero power (#7515)
This commit is contained in:
Theo Arends 2020-01-17 10:12:57 +01:00
parent 016b204cfe
commit 65d1f3aace
1 changed files with 13 additions and 8 deletions

View File

@ -289,14 +289,19 @@ void EnergyMarginCheck(void)
if (Settings.energy_power_delta) {
uint16_t delta = abs(Energy.power_history[0] - energy_power_u);
uint16_t min_power = (Energy.power_history[0] > energy_power_u) ? energy_power_u : Energy.power_history[0];
DEBUG_DRIVER_LOG(PSTR("NRG: Delta %d, Power %d"), delta, min_power);
if ((delta > 0) && (min_power > 0)) { // Fix divide by 0 exception (#6741)
if (((Settings.energy_power_delta < 101) && (((delta * 100) / min_power) > Settings.energy_power_delta)) || // 1..100 = Percentage
((Settings.energy_power_delta > 100) && (delta > (Settings.energy_power_delta -100)))) { // 101..32000 = Absolute
Energy.power_delta = true;
if (delta > 0) {
if (Settings.energy_power_delta < 101) { // 1..100 = Percentage
uint16_t min_power = (Energy.power_history[0] > energy_power_u) ? energy_power_u : Energy.power_history[0];
if (0 == min_power) { min_power++; } // Fix divide by 0 exception (#6741)
if (((delta * 100) / min_power) > Settings.energy_power_delta) {
Energy.power_delta = true;
}
} else { // 101..32000 = Absolute
if (delta > (Settings.energy_power_delta -100)) {
Energy.power_delta = true;
}
}
if (Energy.power_delta) {
Energy.power_history[1] = Energy.active_power[0]; // We only want one report so reset history
Energy.power_history[2] = Energy.active_power[0];
}