Fix spike detection bug causing stuck values

When a spike was detected in voltage, current, power, energy, or frequency,
the code sent previous values but never updated the prev_* variables.
This caused the system to get stuck reporting old values indefinitely
after any significant measurement change.

Now all prev_* variables are always updated after spike detection,
ensuring values can change again after being filtered.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ms
2026-02-14 21:09:30 +01:00
parent 2b16211ef2
commit 0524e7284d

View File

@@ -614,14 +614,15 @@ int main(int argc, char *argv[])
if (prev_voltage.U1 != 0 && (current_voltage.U1 > prev_voltage.U1 * 1.8 || current_voltage.U1 < prev_voltage.U1 * 0.2)) spike_U = 1;
if (prev_voltage.U2 != 0 && (current_voltage.U2 > prev_voltage.U2 * 1.8 || current_voltage.U2 < prev_voltage.U2 * 0.2)) spike_U = 1;
if (prev_voltage.U3 != 0 && (current_voltage.U3 > prev_voltage.U3 * 1.8 || current_voltage.U3 < prev_voltage.U3 * 0.2)) spike_U = 1;
if(spike_U) {
send_voltage = prev_voltage;
printf("ORNO: Voltage spike detected. Sending previous values.\n");
} else {
send_voltage = current_voltage;
prev_voltage = current_voltage;
}
// Always update prev_voltage to prevent getting stuck on old values
prev_voltage = current_voltage;
// CURRENT check
int spike_I = 0;
@@ -634,20 +635,22 @@ int main(int argc, char *argv[])
printf("ORNO: Current spike detected. Sending previous values.\n");
} else {
send_current = current_current;
prev_current = current_current;
}
// Always update prev_current to prevent getting stuck on old values
prev_current = current_current;
// POWER check
int spike_P = 0;
if (prev_power.P_Tot != 0 && (fabs(current_power.P_Tot - prev_power.P_Tot) > fabs(prev_power.P_Tot) * 0.8)) spike_P = 1;
if(spike_P) {
send_power = prev_power;
printf("ORNO: Power spike detected. Sending previous values.\n");
} else {
send_power = current_power;
prev_power = current_power;
}
// Always update prev_power to prevent getting stuck on old values
prev_power = current_power;
// ENERGY check (should only increase)
int spike_W = 0;
@@ -658,8 +661,9 @@ int main(int argc, char *argv[])
printf("ORNO: Energy spike/drop detected. Sending previous values.\n");
} else {
send_energy = current_energy;
prev_energy = current_energy;
}
// Always update prev_energy to prevent getting stuck on old values
prev_energy = current_energy;
// FREQUENCY check (5% threshold)
int spike_F = 0;
@@ -670,8 +674,9 @@ int main(int argc, char *argv[])
printf("ORNO: Frequency spike detected. Sending previous values.\n");
} else {
send_freq = current_freq;
prev_freq = current_freq;
}
// Always update prev_freq to prevent getting stuck on old values
prev_freq = current_freq;
// --- Now send the selected values ---