From 75950a226d80588b2e52a62b78835fdd8fec7b86 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Fri, 20 Jun 2025 00:04:48 +0200 Subject: [PATCH] change wss to moving sum --- Software/Core/Src/main.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index e882e71..eac2132 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -40,6 +40,7 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define TIM_BASE_FREQ 96000000UL +#define WSS_HISTORY_SIZE 10 /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -66,6 +67,9 @@ TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3}; static uint8_t pwm_ch_active[8]; static uint16_t wss_flanks[2]; +static uint16_t wss_flanks_avg[2]; +static uint8_t wss_flanks_history[2][WSS_HISTORY_SIZE]; +static uint8_t wss_flanks_history_idx[2]; // index of the oldest entry (next to be overwritten) /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -119,7 +123,11 @@ void loop_1kHz() { break; case FIN: - value = wss_flanks[signal->channel]; + uint8_t oldest_hist_idx = wss_flanks_history_idx[signal->channel]; + // subtract oldest entry from avg + wss_flanks_avg[signal->channel] -= wss_flanks_history[signal->channel][oldest_hist_idx]; + // overwrite oldest history entry with new value and reset counter + wss_flanks_history[signal->channel][oldest_hist_idx] = wss_flanks[signal->channel]; wss_flanks[signal->channel] = 0; break; @@ -247,6 +255,10 @@ int main(void) mscounter = 0; setup_complete = 1; + memset(wss_flanks, 0, sizeof(wss_flanks)); + memset(wss_flanks_avg, 0, sizeof(wss_flanks)); + memset(wss_flanks_history, 0, sizeof(wss_flanks_history)); + memset(wss_flanks_history_idx, 0, sizeof(wss_flanks_history_idx)); /* USER CODE END 2 */ /* Infinite loop */