From ee00176cdd59517650b9c53c2add09d1274d7234 Mon Sep 17 00:00:00 2001 From: wangfq Date: Mon, 29 Jun 2026 17:27:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E9=80=9F=20IIR=20=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=94=B9=E4=B8=BA=E9=99=90=E5=B9=85=E5=90=8E=E5=8E=9F?= =?UTF-8?q?=E5=A7=8B=E5=80=BC=EF=BC=8C=E4=B8=8D=E5=86=8D=E7=BB=8F=E6=85=A2?= =?UTF-8?q?=E9=80=9F=20IIR=20=E6=BB=9E=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: CAPVD_fast = (CAPVD_fast + CAPVD) / 2 中 CAPVD 已被 ALFA_CAP1=18 (τ=135ms) 严重滞后。快速 IIR 的 α=0.5 无法 恢复前级丢失的响应速度,导致 ALFA_CAP1=18 的实际进入灵敏度 显著低于 ALFA_CAP1=79(旧设计)。 修复: 快速 IIR 直接吃斜率限幅后的 clamped_value / Value, 完全跳过慢速 IIR,真正实现 τ≈28ms 的响应速度。 数据流: 旧: Value → 斜率限幅 → IIR_slow(α=18/256) → CAPVD └→ CAPVD_fast = avg(CAPVD_fast, CAPVD) ← 滞后 新: Value → 斜率限幅 → IIR_slow(α=18/256) → CAPVD └→ CAPVD_fast = avg(CAPVD_fast, clamped_value) ← 快速 --- .../at32f421_freertos_demo/src/TaskLoop.c | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/utilities/at32f421_freertos_demo/src/TaskLoop.c b/utilities/at32f421_freertos_demo/src/TaskLoop.c index eb05e51..aea83cb 100644 --- a/utilities/at32f421_freertos_demo/src/TaskLoop.c +++ b/utilities/at32f421_freertos_demo/src/TaskLoop.c @@ -654,25 +654,31 @@ void vd1_task(void) *================================================================*/ /* 1a. 慢速 IIR — 斜率限幅 */ - if (loop1_CAPVD == 0) { - loop1_CAPVD = loop1_Value; - loop1_CAPVD_fast = loop1_Value; - } else { - /* 斜率限幅: 物理车辆不可能让频率瞬间跳变 > MAX_SLOPE_RATE% */ - int32_t raw_delta = (int32_t)loop1_Value - (int32_t)loop1_CAPVD; - int32_t max_step = (int32_t)(loop1_CAPVD * MAX_SLOPE_RATE / 100); - if (max_step < 100) max_step = 100; // 最小限幅,防止 origin 很小时锁死 - if (raw_delta > max_step) raw_delta = max_step; - if (raw_delta < -max_step) raw_delta = -max_step; - uint32_t clamped_value = (uint32_t)((int32_t)loop1_CAPVD + raw_delta); - loop1_CAPVD = get_flt_value(clamped_value, loop1_CAPVD); - } + { + uint32_t fast_input; // 快速 IIR 的输入:限幅后的原始值,不经慢速 IIR 滞后 - /* 1b. 快速 IIR — α=0.5: (old + new) / 2 */ - if (loop1_CAPVD_fast == 0) { - loop1_CAPVD_fast = loop1_CAPVD; // 首次直接锁定 - } else { - loop1_CAPVD_fast = (loop1_CAPVD_fast + loop1_CAPVD) / 2; + if (loop1_CAPVD == 0) { + loop1_CAPVD = loop1_Value; + fast_input = loop1_Value; + } else { + /* 斜率限幅: 物理车辆不可能让频率瞬间跳变 > MAX_SLOPE_RATE% */ + int32_t raw_delta = (int32_t)loop1_Value - (int32_t)loop1_CAPVD; + int32_t max_step = (int32_t)(loop1_CAPVD * MAX_SLOPE_RATE / 100); + if (max_step < 100) max_step = 100; // 最小限幅,防止 origin 很小时锁死 + if (raw_delta > max_step) raw_delta = max_step; + if (raw_delta < -max_step) raw_delta = -max_step; + uint32_t clamped_value = (uint32_t)((int32_t)loop1_CAPVD + raw_delta); + loop1_CAPVD = get_flt_value(clamped_value, loop1_CAPVD); + fast_input = clamped_value; + } + + /* 1b. 快速 IIR — α=0.5: (old + new) / 2 + * 输入直接用限幅后的原始值,不经过慢速 IIR,保持 τ≈28ms 的响应速度 */ + if (loop1_CAPVD_fast == 0) { + loop1_CAPVD_fast = fast_input; + } else { + loop1_CAPVD_fast = (loop1_CAPVD_fast + fast_input) / 2; + } } /*--- 2. 稳定期:只跟踪基线,不检测车辆 ---*/