Skip to content

Commit 28ba362

Browse files
authored
Moving Avg mod {default disabled) (#1586)
1 parent 71ce6d3 commit 28ba362

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/driver/drv_bl_shared.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ void BL_ProcessUpdate(float voltage, float current, float power,
654654
}
655655
}
656656

657+
#ifdef ENABLE_BL_MOVINGAVG
658+
power = XJ_MovingAverage_float((float)sensdataset->sensors[OBK_POWER].lastReading, power);
659+
current = XJ_MovingAverage_float((float)sensdataset->sensors[OBK_CURRENT].lastReading, current);
660+
#endif
661+
657662
sensdataset->sensors[OBK_VOLTAGE].lastReading = voltage;
658663
sensdataset->sensors[OBK_CURRENT].lastReading = current;
659664
sensdataset->sensors[OBK_POWER].lastReading = power;

src/new_pins.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,12 @@ void CHANNEL_Set_Ex(int ch, int iVal, int iFlags, int ausemovingaverage) {
14511451
if (bSilent == 0) {
14521452
addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "CHANNEL_Set channel %i has changed to %i (flags %i)\n\r", ch, iVal, iFlags);
14531453
}
1454+
#ifdef ENABLE_BL_MOVINGAVG
1455+
//addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "CHANNEL_Set debug channel %i has changed to %i (flags %i)\n\r", ch, iVal, iFlags);
1456+
if (ausemovingaverage) {
1457+
iVal=XJ_MovingAverage_int(prevValue, iVal);
1458+
}
1459+
#endif
14541460
g_channelValues[ch] = iVal;
14551461

14561462
Channel_OnChanged(ch, prevValue, iFlags);
@@ -2477,6 +2483,47 @@ static commandResult_t showgpi(const void* context, const char* cmd, const char*
24772483
return CMD_RES_OK;
24782484
}
24792485

2486+
#ifdef ENABLE_BL_MOVINGAVG
2487+
static int movingavg_cnt = 0;
2488+
static commandResult_t CMD_setMovingAvg(const void* context, const char* cmd,
2489+
const char* args, int cmdFlags) {
2490+
Tokenizer_TokenizeString(args, 0);
2491+
if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 1)) {
2492+
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
2493+
}
2494+
2495+
int fval = Tokenizer_GetArgInteger(0);
2496+
if (fval < 0) {
2497+
ADDLOG_ERROR(LOG_FEATURE_ENERGYMETER, "%s",
2498+
CMD_GetResultString(CMD_RES_BAD_ARGUMENT));
2499+
return CMD_RES_BAD_ARGUMENT;
2500+
}
2501+
addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER, "MovingAvg=%i", fval);
2502+
movingavg_cnt = fval;
2503+
return CMD_RES_OK;
2504+
}
2505+
2506+
float XJ_MovingAverage_float(float aprevvalue, float aactvalue) {
2507+
//if aprevvalue not set, return actual
2508+
if (aprevvalue <= 0) return aactvalue;
2509+
if (movingavg_cnt <= 1) return aactvalue;
2510+
//if aprevvalue set, calculate simple average value
2511+
float res = (((movingavg_cnt - 1) * aprevvalue + aactvalue) / movingavg_cnt);
2512+
//addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %.2f a: %.2f r: %.2f", aprevvalue, aactvalue, res);
2513+
return res;
2514+
}
2515+
2516+
int XJ_MovingAverage_int(int aprevvalue, int aactvalue) {
2517+
//if aprevvalue not set, return actual
2518+
if (aprevvalue <= 0) return aactvalue;
2519+
if (movingavg_cnt <= 1) return aactvalue;
2520+
//if aprevvalue set, calculate simple average value
2521+
int res = (((movingavg_cnt - 1) * aprevvalue + aactvalue) / movingavg_cnt);
2522+
//addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %i a: %i r: %i", aprevvalue, aactvalue, res);
2523+
return res;
2524+
}
2525+
#endif
2526+
24802527
void PIN_AddCommands(void)
24812528
{
24822529
//cmddetail:{"name":"showgpi","args":"NULL",
@@ -2504,6 +2551,13 @@ void PIN_AddCommands(void)
25042551
//cmddetail:"fn":"CMD_setButtonHoldRepeat","file":"new_pins.c","requires":"",
25052552
//cmddetail:"examples":""}
25062553
CMD_RegisterCommand("setButtonHoldRepeat", CMD_setButtonHoldRepeat, NULL);
2554+
#ifdef ENABLE_BL_MOVINGAVG
2555+
//cmddetail:{"name":"setMovingAvg","args":"MovingAvg",
2556+
//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.",
2557+
//cmddetail:"fn":"NULL);","file":"new_pins.c","requires":"",
2558+
//cmddetail:"examples":""}
2559+
CMD_RegisterCommand("setMovingAvg", CMD_setMovingAvg, NULL);
2560+
#endif
25072561
#if ALLOW_SSID2
25082562
//cmddetail:{"name":"setStartupSSIDChannel","args":"[Value]",
25092563
//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)",

src/new_pins.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,4 +1496,9 @@ int FV_GetStartupSSID_StoredValue(int adefault);
14961496
void FV_UpdateStartupSSIDIfChanged_StoredValue(int assidindex);
14971497
#endif
14981498

1499+
#ifdef ENABLE_BL_MOVINGAVG
1500+
float XJ_MovingAverage_float(float aprevvalue, float aactvalue);
1501+
int XJ_MovingAverage_int(int aprevvalue, int aactvalue);
1502+
#endif
1503+
14991504
#endif

src/obk_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,10 @@
413413
// if power metering chip is enabled, also enable backend for that
414414
#if ENABLE_DRIVER_BL0937 || ENABLE_DRIVER_BL0942 || ENABLE_DRIVER_BL0942SPI || ENABLE_DRIVER_CSE7766
415415
#define ENABLE_BL_SHARED 1
416-
//allow use two BL0942 on two ports +600 bytes
416+
//allow use two BL0942 on two ports +940 bytes
417417
//#define ENABLE_BL_TWIN 1
418+
//allow moving average energy calculation +180 bytes
419+
//#define ENABLE_BL_MOVINGAVG 1
418420
#endif
419421

420422
// closing OBK_CONFIG_H

0 commit comments

Comments
 (0)