Fix valid negative energy totals (#13363)

Fix valid negative energy totals (#13363)
This commit is contained in:
Theo Arends 2021-10-14 16:21:35 +02:00
parent 7d49b66ef9
commit eb901cfe3c
2 changed files with 13 additions and 13 deletions

View File

@ -483,9 +483,9 @@ typedef struct {
int16_t toffset[2]; // 30E
uint8_t display_font; // 312
DisplayOptions display_options; // 313
uint32_t energy_kWhtoday_ph[3]; // 314
uint32_t energy_kWhyesterday_ph[3]; // 320
uint32_t energy_kWhtotal_ph[3]; // 32C
int32_t energy_kWhtoday_ph[3]; // 314
int32_t energy_kWhyesterday_ph[3]; // 320
int32_t energy_kWhtotal_ph[3]; // 32C
uint8_t free_338[7]; // 338
@ -786,8 +786,8 @@ typedef struct {
uint8_t free_2d6[2]; // 2D6
uint32_t energy_kWhtoday_ph[3]; // 2D8
uint32_t energy_kWhtotal_ph[3]; // 2E4
int32_t energy_kWhtoday_ph[3]; // 2D8
int32_t energy_kWhtotal_ph[3]; // 2E4
// 2F0 - 2FF free locations
} TRtcSettings;

View File

@ -90,10 +90,10 @@ struct ENERGY {
float total_sum; // 12345.12345 kWh total energy
float yesterday_sum; // 123.123 kWh
uint32_t kWhtoday_delta[ENERGY_MAX_PHASES]; // 1212312345 Wh 10^-5 (deca micro Watt hours) - Overflows to Energy.kWhtoday (HLW and CSE only)
uint32_t kWhtoday_offset[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
uint32_t kWhtoday[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
uint32_t period[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
int32_t kWhtoday_delta[ENERGY_MAX_PHASES]; // 1212312345 Wh 10^-5 (deca micro Watt hours) - Overflows to Energy.kWhtoday (HLW and CSE only)
int32_t kWhtoday_offset[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
int32_t kWhtoday[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
int32_t period[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
uint8_t fifth_second;
uint8_t command_code;
@ -200,8 +200,8 @@ void EnergyUpdateToday(void) {
Energy.daily_sum = 0.0;
for (uint32_t i = 0; i < Energy.phase_count; i++) {
if (Energy.kWhtoday_delta[i] > 1000) {
uint32_t delta = Energy.kWhtoday_delta[i] / 1000;
if (abs(Energy.kWhtoday_delta[i]) > 1000) {
int32_t delta = Energy.kWhtoday_delta[i] / 1000;
Energy.kWhtoday_delta[i] -= (delta * 1000);
Energy.kWhtoday[i] += delta;
}
@ -255,12 +255,12 @@ void EnergyUpdateTotal(void) {
Energy.start_energy[i] = Energy.import_active[i]; // Init after restart and handle roll-over if any
}
else if (Energy.import_active[i] != Energy.start_energy[i]) {
Energy.kWhtoday[i] = (uint32_t)((Energy.import_active[i] - Energy.start_energy[i]) * 100000);
Energy.kWhtoday[i] = (int32_t)((Energy.import_active[i] - Energy.start_energy[i]) * 100000);
}
if ((Energy.total[i] < (Energy.import_active[i] - 0.01)) && // We subtract a little offset to avoid continuous updates
Settings->flag3.hardware_energy_total) { // SetOption72 - Enable hardware energy total counter as reference (#6561)
RtcSettings.energy_kWhtotal_ph[i] = (unsigned long)((Energy.import_active[i] * 100000) - Energy.kWhtoday_offset[i] - Energy.kWhtoday[i]);
RtcSettings.energy_kWhtotal_ph[i] = (int32_t)((Energy.import_active[i] * 100000) - Energy.kWhtoday_offset[i] - Energy.kWhtoday[i]);
Settings->energy_kWhtotal_ph[i] = RtcSettings.energy_kWhtotal_ph[i];
Energy.total[i] = (float)(RtcSettings.energy_kWhtotal_ph[i] + Energy.kWhtoday_offset[i] + Energy.kWhtoday[i]) / 100000;
Settings->energy_kWhtotal_time = (!Energy.kWhtoday_offset[i]) ? LocalTime() : Midnight();