Attempt to stabilize Shelly Pro 4PM values

This commit is contained in:
Theo Arends 2023-02-20 18:02:43 +01:00
parent d2f450316c
commit 3ce011c2ce
2 changed files with 20 additions and 12 deletions

View File

@ -272,10 +272,6 @@ void ShellyProUpdateIsr(void) {
/* /*
The goal if this function is to minimize SPI and SetVirtualPinState calls The goal if this function is to minimize SPI and SetVirtualPinState calls
*/ */
noInterrupts();
detachInterrupt(SPro.pin_mcp23s17_int);
interrupts();
uint32_t input_state = SP4Mcp23S17Read16(SP4_MCP23S17_INTCAPA); // Read intcap and clear interrupt uint32_t input_state = SP4Mcp23S17Read16(SP4_MCP23S17_INTCAPA); // Read intcap and clear interrupt
input_state &= 0x806F; // Only test input bits input_state &= 0x806F; // Only test input bits
@ -302,8 +298,6 @@ void ShellyProUpdateIsr(void) {
mask <<= 1; mask <<= 1;
} }
SPro.input_state = input_state; SPro.input_state = input_state;
attachInterrupt(SPro.pin_mcp23s17_int, ShellyProUpdateIsr, CHANGE);
} }
bool ShellyProButton(void) { bool ShellyProButton(void) {

View File

@ -231,6 +231,7 @@ typedef struct {
} tAde7953Channel; } tAde7953Channel;
struct Ade7953 { struct Ade7953 {
uint32_t last_update;
uint32_t voltage_rms[ADE7953_MAX_CHANNEL] = { 0, 0 }; uint32_t voltage_rms[ADE7953_MAX_CHANNEL] = { 0, 0 };
uint32_t current_rms[ADE7953_MAX_CHANNEL] = { 0, 0 }; uint32_t current_rms[ADE7953_MAX_CHANNEL] = { 0, 0 };
uint32_t active_power[ADE7953_MAX_CHANNEL] = { 0, 0 }; uint32_t active_power[ADE7953_MAX_CHANNEL] = { 0, 0 };
@ -523,6 +524,19 @@ void Ade7953GetData(void) {
} }
if (Energy->power_on) { // Powered on if (Energy->power_on) { // Powered on
float correction = 1.0f;
if (Ade7953.use_spi) { // SPI
uint32_t time = millis();
if (Ade7953.last_update) {
uint32_t difference = time - Ade7953.last_update;
correction = (float)difference / 1000; // Correction to 1 second
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: Correction %4_f"), &correction);
}
Ade7953.last_update = time;
}
float divider; float divider;
for (uint32_t channel = 0; channel < Energy->phase_count; channel++) { for (uint32_t channel = 0; channel < Energy->phase_count; channel++) {
Energy->data_valid[channel] = 0; Energy->data_valid[channel] = 0;
@ -557,6 +571,12 @@ void Ade7953GetData(void) {
divider = (Ade7953.calib_data[channel][ADE7953_CAL_VAGAIN] != ADE7953_GAIN_DEFAULT) ? ADE7953_LSB_PER_WATTSECOND : power_calibration; divider = (Ade7953.calib_data[channel][ADE7953_CAL_VAGAIN] != ADE7953_GAIN_DEFAULT) ? ADE7953_LSB_PER_WATTSECOND : power_calibration;
Energy->apparent_power[channel] = (float)apparent_power[channel] / divider; Energy->apparent_power[channel] = (float)apparent_power[channel] / divider;
if (Ade7953.use_spi) { // SPI
Energy->active_power[channel] /= correction;
Energy->reactive_power[channel] /= correction;
Energy->apparent_power[channel] /= correction;
}
if (0 == Energy->active_power[channel]) { if (0 == Energy->active_power[channel]) {
Energy->current[channel] = 0; Energy->current[channel] = 0;
} else { } else {
@ -836,7 +856,6 @@ bool Xnrg07(uint32_t function) {
bool result = false; bool result = false;
switch (function) { switch (function) {
/*
case FUNC_ENERGY_EVERY_SECOND: // Use energy interrupt timer (fails on SPI) case FUNC_ENERGY_EVERY_SECOND: // Use energy interrupt timer (fails on SPI)
if (!Ade7953.use_spi) { // No SPI if (!Ade7953.use_spi) { // No SPI
Ade7953EnergyEverySecond(); Ade7953EnergyEverySecond();
@ -847,11 +866,6 @@ bool Xnrg07(uint32_t function) {
Ade7953EnergyEverySecond(); Ade7953EnergyEverySecond();
} }
break; break;
*/
case FUNC_ENERGY_EVERY_SECOND: // Use energy interrupt timer (fails on SPI)
Ade7953EnergyEverySecond();
break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = Ade7953Command(); result = Ade7953Command();
break; break;