Moving Avg mod {default disabled) (#1586)

This commit is contained in:
XJ
2025-03-29 18:10:15 +01:00
committed by GitHub
parent 71ce6d33bc
commit 28ba3622e6
4 changed files with 67 additions and 1 deletions

View File

@ -654,6 +654,11 @@ void BL_ProcessUpdate(float voltage, float current, float power,
}
}
#ifdef ENABLE_BL_MOVINGAVG
power = XJ_MovingAverage_float((float)sensdataset->sensors[OBK_POWER].lastReading, power);
current = XJ_MovingAverage_float((float)sensdataset->sensors[OBK_CURRENT].lastReading, current);
#endif
sensdataset->sensors[OBK_VOLTAGE].lastReading = voltage;
sensdataset->sensors[OBK_CURRENT].lastReading = current;
sensdataset->sensors[OBK_POWER].lastReading = power;

View File

@ -1451,6 +1451,12 @@ void CHANNEL_Set_Ex(int ch, int iVal, int iFlags, int ausemovingaverage) {
if (bSilent == 0) {
addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "CHANNEL_Set channel %i has changed to %i (flags %i)\n\r", ch, iVal, iFlags);
}
#ifdef ENABLE_BL_MOVINGAVG
//addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "CHANNEL_Set debug channel %i has changed to %i (flags %i)\n\r", ch, iVal, iFlags);
if (ausemovingaverage) {
iVal=XJ_MovingAverage_int(prevValue, iVal);
}
#endif
g_channelValues[ch] = iVal;
Channel_OnChanged(ch, prevValue, iFlags);
@ -2477,6 +2483,47 @@ static commandResult_t showgpi(const void* context, const char* cmd, const char*
return CMD_RES_OK;
}
#ifdef ENABLE_BL_MOVINGAVG
static int movingavg_cnt = 0;
static commandResult_t CMD_setMovingAvg(const void* context, const char* cmd,
const char* args, int cmdFlags) {
Tokenizer_TokenizeString(args, 0);
if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 1)) {
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}
int fval = Tokenizer_GetArgInteger(0);
if (fval < 0) {
ADDLOG_ERROR(LOG_FEATURE_ENERGYMETER, "%s",
CMD_GetResultString(CMD_RES_BAD_ARGUMENT));
return CMD_RES_BAD_ARGUMENT;
}
addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER, "MovingAvg=%i", fval);
movingavg_cnt = fval;
return CMD_RES_OK;
}
float XJ_MovingAverage_float(float aprevvalue, float aactvalue) {
//if aprevvalue not set, return actual
if (aprevvalue <= 0) return aactvalue;
if (movingavg_cnt <= 1) return aactvalue;
//if aprevvalue set, calculate simple average value
float res = (((movingavg_cnt - 1) * aprevvalue + aactvalue) / movingavg_cnt);
//addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %.2f a: %.2f r: %.2f", aprevvalue, aactvalue, res);
return res;
}
int XJ_MovingAverage_int(int aprevvalue, int aactvalue) {
//if aprevvalue not set, return actual
if (aprevvalue <= 0) return aactvalue;
if (movingavg_cnt <= 1) return aactvalue;
//if aprevvalue set, calculate simple average value
int res = (((movingavg_cnt - 1) * aprevvalue + aactvalue) / movingavg_cnt);
//addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %i a: %i r: %i", aprevvalue, aactvalue, res);
return res;
}
#endif
void PIN_AddCommands(void)
{
//cmddetail:{"name":"showgpi","args":"NULL",
@ -2504,6 +2551,13 @@ void PIN_AddCommands(void)
//cmddetail:"fn":"CMD_setButtonHoldRepeat","file":"new_pins.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("setButtonHoldRepeat", CMD_setButtonHoldRepeat, NULL);
#ifdef ENABLE_BL_MOVINGAVG
//cmddetail:{"name":"setMovingAvg","args":"MovingAvg",
//cmddetail:"descr":"Moving average value for power and current. <=1 disable, >=2 count of avg values. The setting is temporary and need to be set at startup.",
//cmddetail:"fn":"NULL);","file":"new_pins.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("setMovingAvg", CMD_setMovingAvg, NULL);
#endif
#if ALLOW_SSID2
//cmddetail:{"name":"setStartupSSIDChannel","args":"[Value]",
//cmddetail:"descr":"Sets retain channel number to store last used SSID, 0..MAX_RETAIN_CHANNELS-1, -1 to disable. Suggested channel number is 7 (MAXMAX_RETAIN_CHANNELS-5)",

View File

@ -1496,4 +1496,9 @@ int FV_GetStartupSSID_StoredValue(int adefault);
void FV_UpdateStartupSSIDIfChanged_StoredValue(int assidindex);
#endif
#ifdef ENABLE_BL_MOVINGAVG
float XJ_MovingAverage_float(float aprevvalue, float aactvalue);
int XJ_MovingAverage_int(int aprevvalue, int aactvalue);
#endif
#endif

View File

@ -413,8 +413,10 @@
// if power metering chip is enabled, also enable backend for that
#if ENABLE_DRIVER_BL0937 || ENABLE_DRIVER_BL0942 || ENABLE_DRIVER_BL0942SPI || ENABLE_DRIVER_CSE7766
#define ENABLE_BL_SHARED 1
//allow use two BL0942 on two ports +600 bytes
//allow use two BL0942 on two ports +940 bytes
//#define ENABLE_BL_TWIN 1
//allow moving average energy calculation +180 bytes
//#define ENABLE_BL_MOVINGAVG 1
#endif
// closing OBK_CONFIG_H