From 30fdbc35ddb1a72a5102aece8863921af0e4078d Mon Sep 17 00:00:00 2001 From: Razvalyaev Date: Fri, 28 Nov 2025 18:13:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=20bench=5Ftime=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit улучшение фильтров (эксп и авераге) --- MyLibs/Inc/bench_time.h | 55 +++++++++++++++++++++++++++++++++++++++++ MyLibs/Inc/filters.h | 21 +++++++++++++++- MyLibs/Src/filters.c | 4 +-- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/MyLibs/Inc/bench_time.h b/MyLibs/Inc/bench_time.h index 731ccc3..bb8e68f 100644 --- a/MyLibs/Inc/bench_time.h +++ b/MyLibs/Inc/bench_time.h @@ -182,6 +182,61 @@ static inline uint32_t BenchTime_End(uint8_t channel, uint32_t ticks) { return elapsed_ticks; } + +/** + * @brief Измерение периода на указанном канале + * @param channel Номер канала (0..BENCH_TIME_MAX_CHANNELS-1) + * @param ticks Источник тиков (например: HAL_GetTick(), TIM2->CNT, DWT->CYCCNT) + * @param tick_period Период тиков для переполнения + * @return Измеренное время в тиках, 0 - в случае первого запуска или ошибки + */ +static inline uint32_t BenchTime_Period(uint8_t channel, uint32_t ticks, uint32_t tick_period) { + if (channel >= BENCH_TIME_MAX_CHANNELS) return 0; + if (!hbt.channels[channel].is_running) + { + hbt.channels[channel].start_tick = ticks; + hbt.channels[channel].tick_period = tick_period; + hbt.channels[channel].is_running = 1; + return 0; + }; + + uint32_t end_tick = ticks; + uint32_t start_tick = hbt.channels[channel].start_tick; + uint32_t elapsed_ticks; + + // Инит для следующего измерения + hbt.channels[channel].start_tick = end_tick; + hbt.channels[channel].tick_period = tick_period; + + if (end_tick >= start_tick) { + elapsed_ticks = end_tick - start_tick; + } else { + elapsed_ticks = (tick_period - start_tick) + end_tick + 1; + } + + if (elapsed_ticks > tick_period) { + elapsed_ticks = tick_period; + } + + // Обновление статистики + BenchTimeStats_t* stats = &hbt.channels[channel].stats; + stats->last_ticks = elapsed_ticks; + + if (elapsed_ticks < stats->min_ticks) { + stats->min_ticks = elapsed_ticks; + } + + if (elapsed_ticks > stats->max_ticks) { + stats->max_ticks = elapsed_ticks; + } + + stats->total_ticks += elapsed_ticks; + stats->count++; + + return elapsed_ticks; +} + + /** * @brief Получение минимального времени измерения */ diff --git a/MyLibs/Inc/filters.h b/MyLibs/Inc/filters.h index 964690e..bbbf3c9 100644 --- a/MyLibs/Inc/filters.h +++ b/MyLibs/Inc/filters.h @@ -264,13 +264,32 @@ typedef struct _FilterLUT_t{ int FilterMedian_Init(FilterMedian_t* filter, uint8_t size); float FilterMedian_Process(FilterMedian_t* filter, float input); int FilterExp_Init(FilterExp_t* filter, float alpha); -float FilterExp_Process(FilterExp_t* filter, float input); +float FilterExp_Process(FilterExp_t* filter, float input); int FilterAverage_Init(FilterAverage_t* filter, uint32_t size, FilterMode_t mode); float FilterAverage_Process(FilterAverage_t* filter, float input); int FilterPoly_Init(FilterPoly_t* filter, float* coeffs, uint8_t order); float FilterPoly_Process(FilterPoly_t* filter, float input); int FilterLUT_Init(FilterLUT_t* filter, float* input_arr, float* output_arr, uint16_t size, uint8_t interpolation); float FilterLUT_Process(FilterLUT_t* filter, float input); + + +// Расчет всякого +#define FilterExp_CalcAlpha(tau, TsUs) (((float)TsUs/1000000) / (((float)TsUs/1000000) + (tau))) + +/* Расчет alpha для времени нарастания до 63% */ +#define FilterExp_CalcAlpha63(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 1.0f, TsUs) + +/* Расчет alpha для времени нарастания до 86% */ +#define FilterExp_CalcAlpha86(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 2.0f, TsUs) + +/* Расчет alpha для времени нарастания до 95% */ +#define FilterExp_CalcAlpha95(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 3.0f, TsUs) + +/* Расчет alpha для времени нарастания до 98% */ +#define FilterExp_CalcAlpha98(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 4.0f, TsUs) + +/* Расчет alpha для времени нарастания до 99% */ +#define FilterExp_CalcAlpha99(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 5.0f, TsUs) // ==================== INT32_T ВЕРСИИ ==================== /** diff --git a/MyLibs/Src/filters.c b/MyLibs/Src/filters.c index 72f0d1b..7e0702a 100644 --- a/MyLibs/Src/filters.c +++ b/MyLibs/Src/filters.c @@ -158,7 +158,7 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) { } else { - if (filter->count > filter->size) + if (filter->count >= filter->size) { filter->lastValue = filter->sum / filter->count; filter->count = 0; @@ -431,7 +431,7 @@ int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input) { } else { - if (filter->count == filter->size) + if (filter->count >= filter->size) { filter->lastValue = filter->sum / filter->count; filter->count = 0;