فهرست منبع

1、check.c修改检表时读取累计流量的数据类型(无符号变成有符号)
2、pwm.c修改低功耗定时器,最小定时粒度可以到20ms
3、tdc_tof.h中加入数据有效性判断相关参数
4、tof_isr.c中加入,每次数据合法性判断,飞行时间变动大则直通以提升响应,降低反向计量概率,增加静水时动态调整offset功能

yw 3 روز پیش
والد
کامیت
7c7a1efae6
4فایلهای تغییر یافته به همراه572 افزوده شده و 102 حذف شده
  1. 2 2
      Users/check/check.c
  2. 1 1
      Users/pwm/pwm.c
  3. 24 0
      Users/tdc_tof/tdc_tof.h
  4. 545 99
      Users/tdc_tof/tof_isr.c

+ 2 - 2
Users/check/check.c

@@ -66,7 +66,7 @@ void SetCheckFlow(double cum_flow, float spdLvl, float temp)
     CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_index]);
 
     uint8_t c_idx = CheckCValueIndex(temp_index, (uint16_t)(Check.total_spd_lvl / Check.flow_cnt));
-    LOG("++++++++++++++++++++++c_idx=%d\n",c_idx);
+    //LOG("++++++++++++++++++++++c_idx=%d\n",c_idx);
     // Pb = Vs*6400*q/C Vs unit: L 6.4 = 6400 / 1000
     // modified by yuewei 20260317 change q_value to q
     // Check.total_pulse = (uint32_t)((Check.curr_cum_flow - Check.start_cum_flow) / p->cur_c[c_idx] * 6.4 * param->q_value);
@@ -749,7 +749,7 @@ void CheckReadDeltaTC(void)
 
 void CheckReadCumFlow(void)
 {
-    uint32_t cum = (uint32_t)(CumFlow * 0.1);
+    uint32_t cum = (int32_t)(CumFlow * 0.1);//modified by yuewei 261127 uint32_t->int32_t
     uint8_t i;
     Param_t *param;
     uint16_t state;

+ 1 - 1
Users/pwm/pwm.c

@@ -96,7 +96,7 @@ void LpTim0Start(uint16_t tim)
 	
 
 	if(tim > 16384)ntm=LPTIM_PERIOD_500MS;//×î´ó500ms
-	else if(tim < 1310)ntm = LPTIM_PERIOD_40MS;//×îС45ms
+	else if(tim < 655)ntm = LPTIM_PERIOD_20MS;//×îС45ms
 	else  ntm = 65536-tim;
 	
     rt_base_t level;

+ 24 - 0
Users/tdc_tof/tdc_tof.h

@@ -151,5 +151,29 @@ void set_tdc_fw_ofst(uint8_t fw_ofst);
 uint8_t get_tdc_int_step(void);
 void set_tdc_int_step(uint8_t int_step);
 
+#define ERR_FLY_CHECK //用飞行时间和 扣除异常点
+#define ERR_DIFF_FLY_CHECK //用飞行时间差扣除异常点
+#define ERR_CLEAR_DIFF_FLY_BUFF //在飞行时间差的缓冲区中,清除个别异常点数据 用前后两个正常点的数据求平均得到
+
+#ifdef ERR_FLY_CHECK
+#define RES_FLY_NUM 16
+#define CH_NUM	1
+typedef struct
+{
+    int32_t dat[CH_NUM][RES_FLY_NUM];
+    uint8_t cnt[CH_NUM]; // 数据索引
+    uint8_t gcnt;   // 指示数据是否装满缓冲区
+    uint32_t vflag[CH_NUM];
+} ERR_CHECK_DEF;
+extern ERR_CHECK_DEF fly_sum_check;
+extern ERR_CHECK_DEF fly_diff_check;
+
+extern uint8_t get_error_fly(ERR_CHECK_DEF *, uint8_t, int32_t);
+#endif
+#ifdef ERR_CLEAR_DIFF_FLY_BUFF
+extern void clear_err_dat_buf(ERR_CHECK_DEF *, uint8_t);
+#endif
+
+
 #endif
 

+ 545 - 99
Users/tdc_tof/tof_isr.c

@@ -3,8 +3,6 @@
 #include "alarm.h"
 #include "check.h"
 
-
-
 #define TOF_PWFW_DIFF ((uint16_t)(0x1620u * 25 / 100))
 #define TOF_PWFW_LOW ((uint16_t)(0x1620u * 40 / 100))
 #define TOF_PWFW_HIGH ((uint16_t)(0x1620u * 50 / 100))
@@ -91,6 +89,170 @@ void set_tdc_int_step(uint8_t int_step)
     TdcIntStep = int_step;
 }
 
+//////////////////////扣除 飞行时间异常的点//////////////////////////////////
+#ifdef ERR_FLY_CHECK
+ERR_CHECK_DEF fly_sum_check;
+#endif
+#ifdef ERR_DIFF_FLY_CHECK
+
+ERR_CHECK_DEF fly_diff_check;
+#endif
+uint32_t isqrt_u64(uint64_t);
+
+uint8_t get_error_fly(ERR_CHECK_DEF *ctrl, uint8_t ch, int32_t dat)
+{
+    int32_t *p = ctrl->dat[ch];  // 飞行时间存储
+    uint8_t cnt = ctrl->cnt[ch]; // 存储指针
+    // uint32_t dat = (uint32_t)(sum);
+
+    if (ctrl->gcnt < RES_FLY_NUM * 4) // 还没有采集到足够的数据
+    {
+        p[cnt] = dat; // 最后更新数据
+        cnt++;
+        cnt &= (RES_FLY_NUM - 1);
+        ctrl->cnt[ch] = cnt; // zuihou
+        ctrl->gcnt++;
+        return 0;
+    }
+
+    uint64_t ssum = 0;
+    int64_t delta;
+    int32_t av, av_err;
+    uint8_t i;
+
+    // 求均值
+    for (i = 0; i < RES_FLY_NUM; i++)
+    {
+        ssum += p[i];
+    }
+    av = (int32_t)(ssum / RES_FLY_NUM);
+
+    // 计算方差和:【重点:转有符号再算差值,防止无符号下溢】
+    ssum = 0;
+    for (i = 0; i < RES_FLY_NUM; i++)
+    {
+        delta = (int64_t)(p[i] - av);
+        ssum += (int64_t)(delta * delta);
+    }
+    ssum /= RES_FLY_NUM;
+    av_err = isqrt_u64(ssum);
+
+    // 3σ判断:全部转有符号比较,能识别正负偏差
+    int32_t new_delta = dat - av;
+    if (new_delta < 0)
+    {
+        new_delta = -new_delta;
+    }
+    // const uint8_t min_err[8] = {54, 54, 37, 19, 44, 44, 44, 9};
+#define MIN_ERR 10
+    uint8_t flag = 0;
+    if ((new_delta > 2LL * av_err) && new_delta > MIN_ERR /*min_err[type*4+cur_sta]*/) // 大于3*err且大于最低数值
+    {
+        flag = 1;
+    }
+    else
+        flag = 0;
+
+    if (flag == 0)
+    {
+        ctrl->vflag[ch] &= ~(1U << cnt);
+    }
+    else
+    {
+        ctrl->vflag[ch] |= (1U << cnt);
+    }
+    p[cnt] = dat; // 最后更新数据
+    cnt++;
+    cnt &= (RES_FLY_NUM - 1);
+    ctrl->cnt[ch] = cnt; // zuihou
+    return flag;
+}
+// void
+
+//////////整数开平方////////////////
+uint32_t isqrt_u64(uint64_t x)
+{
+    uint64_t res = 0;
+    uint64_t bit = 1ULL << 62; // 从最高的偶次bit开始
+
+    if (x == 0)
+        return 0;
+
+    // 先把bit降到不大于x的位置
+    while (bit > x)
+        bit >>= 2;
+
+    while (bit != 0)
+    {
+        if (x >= res + bit)
+        {
+            x -= res + bit;
+            res += bit << 1;
+        }
+        res >>= 1;
+        bit >>= 2;
+    }
+    return (uint32_t)res;
+}
+///////////////////////////////////////
+// 在判断异常点的缓冲区,需要那种前后都正常,但当前不正常的点
+// 用前后数据的平均值代替当前点,更新缓冲区,避免影响后续点的判别
+// 连续两个异常点的处理不了,仅处理突然一个很大数据的那种异常
+//////////////////////////////////////
+void clear_err_dat_buf(ERR_CHECK_DEF *ctrl, uint8_t ch)
+{
+    uint8_t cnt1, cnt2, cnt3;
+    cnt1 = ((ctrl->cnt[ch] + RES_FLY_NUM - 3) & (RES_FLY_NUM - 1)); // 倒数第三个数据
+    cnt2 = ((ctrl->cnt[ch] + RES_FLY_NUM - 2) & (RES_FLY_NUM - 1)); // 倒数第二个数
+    cnt3 = ((ctrl->cnt[ch] + RES_FLY_NUM - 1) & (RES_FLY_NUM - 1)); // 最后一个数的索引
+    if (!(ctrl->vflag[ch] & (1U << cnt1))                           // 正常点
+        && (ctrl->vflag[ch] & (1U << cnt2))                         // 异常点
+        && !(ctrl->vflag[ch] & (1U << cnt3)))                       // 正常点
+    {
+        ctrl->dat[ch][cnt2] = (ctrl->dat[ch][cnt1] + ctrl->dat[ch][cnt3]) / 2; // 用1和3的平均数更新2
+    }
+}
+/////////////////////////////////////////////
+////将判别代码全部封装起来
+////////////////////////////////////////////
+uint8_t err_data_check(uint8_t ch,float diff1,float SumToF)
+{        
+    uint8_t errflag = 0;
+
+    // 脉冲宽度除数不能为0
+    if(TdcDPwHw == 0)
+    {
+        return 1;
+    }
+
+    float ratio = (float)TdcDPwFw / (float)TdcDPwHw;
+    // 比例小于0.6判定无效
+    if(ratio < 0.6f)
+    {
+        LOG("R=%.6f/r/n", ratio);
+        return 1;
+    }
+
+#ifdef ERR_FLY_CHECK
+    errflag = get_error_fly(&fly_sum_check, ch, (int32_t)SumToF);
+#endif
+    /////////////////////////////////////////////
+    //////////////飞行时间差异常点扣除 不管稳定跳窗////////////////////////////////
+#ifdef ERR_DIFF_FLY_CHECK
+    uint8_t differrflag = 0;
+    if (errflag == 0)
+    {
+        differrflag = get_error_fly(&fly_diff_check, ch, (int32_t)(diff1 * 10));
+    }
+    errflag |= differrflag;
+#endif
+#ifdef ERR_CLEAR_DIFF_FLY_BUFF
+    clear_err_dat_buf(&fly_diff_check, ch);
+#endif
+	return errflag;
+}
+
+
 // 可调滤波参数
 #define BUF_SIZE            3       // 减小窗口,避免整段温漂进入缓存
 #define LPF_ALPHA           0.20f   // 大幅降低,强化平滑,压制缓慢漂移
@@ -100,8 +262,25 @@ void set_tdc_int_step(uint8_t int_step)
 #define KALMAN_R            0.025f
 #define KALMAN_INIT_P       0.1f
 
+// 相对波动直通判定比例:±20%,超过该比例直接更新均值,不走低通平滑
+#define REL_JUMP_RATIO      0.20f
+
 #define TOF_MAX_CHANNELS    1
 
+//==== 方向判定防抖宏定义,恢复死区门槛FLT_DIR_DEADBAND ====
+#define FLT_DIR_DEADBAND        0.2f   // 零点死区,幅值小于该值判定为零点噪声,不参与方向统计
+#define FLT_DIR_LOCK_N          20      // 连续N帧同向且超出死区,才锁定该流向
+#define FLT_DIR_SWITCH_N        20      // 连续N帧反向且超出死区,才判定真实换向,放行切换方向
+
+extern uint8_t G_STATIC_OT_SETTING_FLAG;
+
+typedef enum
+{
+    FLT_DIR_IDLE = 0,   // 空闲未锁定流向
+    FLT_DIR_FWD,        // 锁定正向
+    FLT_DIR_BWD         // 锁定反向
+} FilterDirState_t;
+
 typedef struct
 {
     float filter_buf[BUF_SIZE];
@@ -117,6 +296,12 @@ typedef struct
     uint8_t lpf_ready;
 
     float last_filtered;
+
+    // 方向锁定相关状态
+    FilterDirState_t dir_state;     // 当前流向状态机
+    uint8_t dir_stable_cnt;         // 连续同向采样计数(用于初次锁定流向)
+    uint8_t cross_cnt;              // 连续跨向采样计数(气泡尖刺换向防抖)
+    float dir_last_valid;           // 当前锁定方向下最后一帧有效滤波输出
 } TOF_Channel_t;
 
 static TOF_Channel_t tof_channels[TOF_MAX_CHANNELS] = {
@@ -126,74 +311,227 @@ static TOF_Channel_t tof_channels[TOF_MAX_CHANNELS] = {
         .buf_index = 0,
         .buffer_ready = 0,
         .lpf_ready = 0,
+        .dir_state = FLT_DIR_IDLE,
+        .dir_stable_cnt = 0,
+        .cross_cnt = 0,
+        .dir_last_valid = 0.0f,
     }
 };
 
-extern uint8_t G_STATIC_OT_SETTING_FLAG;
-
+/**
+ * @brief 单通道滤波处理:3点中值滤波 + 卡尔曼 + 一阶低通,叠加流向尖刺抑制
+ * @param raw 输入原始DiffToF(U-D-path_skew)
+ * @retval 滤波后数值;偶然反向尖刺时返回上一次同方向有效值,不更新滤波状态
+ * @note 流向判定规则:
+ *       raw >  FLT_DIR_DEADBAND → 正向
+ *       raw < -FLT_DIR_DEADBAND → 反向
+ *       |raw| <= FLT_DIR_DEADBAND → 零点噪声区,不参与方向计数,正常执行滤波
+ * @note 仅连续达到FLT_DIR_SWITCH_N帧反向才认定为真实回流;零星气泡换向尖刺直接拦截
+ */
 static float filter_single_channel(float raw)
 {
     TOF_Channel_t *f = &tof_channels[0];
     float temp_buf[BUF_SIZE];
     uint8_t i, j;
 
-    f->filter_buf[f->buf_index++] = raw;
-    if (f->buf_index >= BUF_SIZE)
-    {
-        f->buf_index = 0;
-        f->buffer_ready = 1;
-    }
+    // 流向判断,恢复死区门槛
+    uint8_t is_fwd = (raw > FLT_DIR_DEADBAND);
+    uint8_t is_bwd = (raw < -FLT_DIR_DEADBAND);
 
-    if (!f->buffer_ready)
-    {
-        f->x_est = raw;
-        f->p_est = KALMAN_INIT_P;
-        f->lpf_out = raw;
-        f->lpf_ready = 1;
-        f->last_filtered = raw;
-        return raw;
-    }
+    uint8_t run_full_filter = 0;
+    float ret_val = 0.0f;
 
-    memcpy(temp_buf, f->filter_buf, sizeof(temp_buf));
-    for (i = 0; i < BUF_SIZE - 1; i++)
+    /* 流向状态机:优先判断当前采样属于有效流向信号、零点噪声还是气泡尖刺 */
+    switch(f->dir_state)
     {
-        for (j = 0; j < BUF_SIZE - i - 1; j++)
+        case FLT_DIR_IDLE:
         {
-            if (temp_buf[j] > temp_buf[j + 1])
+            // 未锁定流向,清零跨向尖刺计数
+            f->cross_cnt = 0;
+            if(is_fwd)
             {
-                float tmp = temp_buf[j];
-                temp_buf[j] = temp_buf[j + 1];
-                temp_buf[j + 1] = tmp;
+                f->dir_stable_cnt ++;
+                // 连续同向帧数达标,锁定正向
+                if(f->dir_stable_cnt >= FLT_DIR_LOCK_N)
+                {
+                    f->dir_state = FLT_DIR_FWD;
+                    f->dir_stable_cnt = 0;
+                }
+                run_full_filter = 1;
             }
+            else if(is_bwd)
+            {
+                f->dir_stable_cnt ++;
+                // 连续同向帧数达标,锁定反向
+                if(f->dir_stable_cnt >= FLT_DIR_LOCK_N)
+                {
+                    f->dir_state = FLT_DIR_BWD;
+                    f->dir_stable_cnt = 0;
+                }
+                run_full_filter = 1;
+            }
+            else
+            {
+                // 落在零点死区内:视为噪声,重置稳定计数,正常滤波
+                f->dir_stable_cnt = 0;
+                run_full_filter = 1;
+            }
+            break;
         }
-    }
-    float median_val = temp_buf[BUF_SIZE / 2];
 
-    // 卡尔曼更新
-    float x_pred = f->x_est;
-    float p_pred = f->p_est + f->Q;
-    float k_gain = p_pred / (p_pred + f->R);
-    float x_cur = x_pred + k_gain * (median_val - x_pred);
-    float p_cur = (1.0f - k_gain) * p_pred;
+        case FLT_DIR_FWD:
+        {
+            if(is_fwd)
+            {
+                // 和锁定方向一致,跨向尖刺计数清零,正常滤波
+                f->cross_cnt = 0;
+                run_full_filter = 1;
+            }
+            else if(is_bwd)
+            {
+                // 出现超出死区的反向信号,累加跨向计数,区分尖刺与真实换向
+                f->cross_cnt ++;
+                if(f->cross_cnt >= FLT_DIR_SWITCH_N)
+                {
+                    // 连续多帧反向,判定真实回流,切换流向,放行滤波
+                    f->dir_state = FLT_DIR_BWD;
+                    f->cross_cnt = 0;
+                    f->dir_stable_cnt = 0;
+                    run_full_filter = 1;
+                }
+                else
+                {
+                    // 零星反向气泡尖刺:拦截,不更新滤波缓存与卡尔曼,返回上一有效值
+                    ret_val = f->dir_last_valid;
+                    run_full_filter = 0;
+                }
+            }
+            else
+            {
+                // 落在零点死区内:不判定为换向,跨向计数清零,正常滤波
+                f->cross_cnt = 0;
+                run_full_filter = 1;
+            }
+            break;
+        }
 
-    f->x_est = x_cur;
-    f->p_est = p_cur;
+        case FLT_DIR_BWD:
+        {
+            if(is_bwd)
+            {
+                // 和锁定方向一致,跨向尖刺计数清零,正常滤波
+                f->cross_cnt = 0;
+                run_full_filter = 1;
+            }
+            else if(is_fwd)
+            {
+                // 出现超出死区的正向信号,累加跨向计数,区分尖刺与真实换向
+                f->cross_cnt ++;
+                if(f->cross_cnt >= FLT_DIR_SWITCH_N)
+                {
+                    // 连续多帧正向,判定真实正向流动,切换流向,放行滤波
+                    f->dir_state = FLT_DIR_FWD;
+                    f->cross_cnt = 0;
+                    f->dir_stable_cnt = 0;
+                    run_full_filter = 1;
+                }
+                else
+                {
+                    // 零星正向气泡尖刺:拦截,不更新滤波缓存与卡尔曼,返回上一有效值
+                    ret_val = f->dir_last_valid;
+                    run_full_filter = 0;
+                }
+            }
+            else
+            {
+                // 落在零点死区内:不判定为换向,跨向计数清零,正常滤波
+                f->cross_cnt = 0;
+                run_full_filter = 1;
+            }
+            break;
+        }
+        default:
+        {
+            // 非法状态,复位到空闲态
+            f->dir_state = FLT_DIR_IDLE;
+            f->dir_stable_cnt = 0;
+            f->cross_cnt = 0;
+            run_full_filter = 1;
+            break;
+        }
+    }
 
-    // 一阶低通
-    if (!f->lpf_ready)
+    /* run_full_filter=1:正常执行整套滤波链路:填充缓存→中值→卡尔曼→一阶低通 */
+    if(run_full_filter == 1)
     {
-        f->lpf_out = x_cur;
-        f->lpf_ready = 1;
-    }
-    f->lpf_out = LPF_ALPHA * x_cur + (1.0f - LPF_ALPHA) * f->lpf_out;
+        // 填充滑动缓存
+        f->filter_buf[f->buf_index++] = raw;
+        if (f->buf_index >= BUF_SIZE)
+        {
+            f->buf_index = 0;
+            f->buffer_ready = 1;
+        }
+
+        // 缓存未填满:直接用原始值初始化滤波
+        if (!f->buffer_ready)
+        {
+            f->x_est = raw;
+            f->p_est = KALMAN_INIT_P;
+            f->lpf_out = raw;
+            f->lpf_ready = 1;
+            f->last_filtered = raw;
+            f->dir_last_valid = raw;
+            ret_val = raw;
+        }
+        else
+        {
+            // 拷贝缓存并冒泡排序,取中值,抑制单点野值
+            memcpy(temp_buf, f->filter_buf, sizeof(temp_buf));
+            for (i = 0; i < BUF_SIZE - 1; i++)
+            {
+                for (j = 0; j < BUF_SIZE - i - 1; j++)
+                {
+                    if (temp_buf[j] > temp_buf[j + 1])
+                    {
+                        float tmp = temp_buf[j];
+                        temp_buf[j] = temp_buf[j + 1];
+                        temp_buf[j + 1] = tmp;
+                    }
+                }
+            }
+            float median_val = temp_buf[BUF_SIZE / 2];
 
-    // 仅保存上一帧输出,不再计算方差
-    f->last_filtered = f->lpf_out;
+            // 卡尔曼滤波预测更新
+            float x_pred = f->x_est;
+            float p_pred = f->p_est + f->Q;
+            float k_gain = p_pred / (p_pred + f->R);
+            float x_cur = x_pred + k_gain * (median_val - x_pred);
+            float p_cur = (1.0f - k_gain) * p_pred;
 
-    return f->lpf_out;
+            f->x_est = x_cur;
+            f->p_est = p_cur;
+
+            // 一阶低通二次平滑
+            if (!f->lpf_ready)
+            {
+                f->lpf_out = x_cur;
+                f->lpf_ready = 1;
+            }
+            f->lpf_out = LPF_ALPHA * x_cur + (1.0f - LPF_ALPHA) * f->lpf_out;
+
+            f->last_filtered = f->lpf_out;
+            f->dir_last_valid = f->lpf_out;
+            ret_val = f->lpf_out;
+        }
+    }
+
+    return ret_val;
 }
 
-// 重置滤波全部状态
+/**
+ * @brief 滤波模块全部状态复位
+ * @note 调用时机:空管告警、检定开始、清零流量、重启计量
+ */
 void tof_filter_reset(void)
 {
     TOF_Channel_t *f = &tof_channels[0];
@@ -205,6 +543,12 @@ void tof_filter_reset(void)
     f->lpf_out = 0.0f;
     f->lpf_ready = 0;
     f->last_filtered = 0.0f;
+
+    // 复位流向状态机
+    f->dir_state = FLT_DIR_IDLE;
+    f->dir_stable_cnt = 0;
+    f->cross_cnt = 0;
+    f->dir_last_valid = 0.0f;
 }
 
 // 初始化
@@ -255,23 +599,90 @@ void DiffToF2Flow(double dtof)
     pTdcItf->flow_vol(vol, dtof);
 }
 
+/**
+ * @brief 有水但是小于始动流量时,进行offset设置
+ * @note 非法ratio直接跳过调整,维持原有offset;增加防抖,连续N次异常才步进调整
+ */
+#define OFST_STABLE_WIN_MIN      0.5f
+#define OFST_STABLE_WIN_MAX      0.9f
+#define OFST_DEBOUNCE_CNT        3U    // 连续3次异常才触发调整,可按需修改
+
+static uint8_t ofst_err_cnt = 0U;    // 异常累计计数器
+
+void set_wave_offs(void)
+{
+    float ratio = 0.0f;
+
+    /* 脉冲宽度除数为0,直接退出,保留原有offset,清零防抖计数 */
+    if (TdcDPwHw == 0U)
+    {
+        ofst_err_cnt = 0U;
+        return;
+    }
+
+    ratio = (float)TdcDPwFw / (float)TdcDPwHw;
+
+    /* ratio非法:等于0 或者 >=1,不修改offset,清零防抖计数 */
+    if ((ratio <= 0.0f) || (ratio >= 1.0f))
+    {
+        ofst_err_cnt = 0U;
+        return;
+    }
+
+    /* 稳定区间,清零异常计数,不调整offset */
+    if ((ratio >= OFST_STABLE_WIN_MIN) && (ratio <= OFST_STABLE_WIN_MAX))
+    {
+        ofst_err_cnt = 0U;
+        return;
+    }
+
+    /* 进入异常区间,累加防抖计数 */
+    ofst_err_cnt++;
+
+    /* 未达到防抖阈值,等待下一次采样 */
+    if (ofst_err_cnt < OFST_DEBOUNCE_CNT)
+    {
+        return;
+    }
+
+    /* 连续多次异常,执行一次offset步进,计数不清零,保持持续收敛;如需每次调整后重置改成=0 */
+    if (ratio < OFST_STABLE_WIN_MIN)
+    {
+        if (TdcFwOfst > TOF_OFST_LOW)
+        {
+            TdcFwOfst--;
+            //LOG("R=%.6f,O=%d\r\n", ratio, TdcFwOfst);
+        }
+    }
+    else if (ratio > OFST_STABLE_WIN_MAX)
+    {
+        if (TdcFwOfst < TOF_OFST_HIGH)
+        {
+            TdcFwOfst++;
+            //LOG("R=%.6f,O=%d\r\n", ratio, TdcFwOfst);
+        }
+    }
+}
+
+
+/**
+- @brief 读取TDC原始TOF,野值判断、滤波、均值、流量判据、打包输出
+- 主业务函数:每次TdcIntStep=0x03都会进入
+*/
 void read_tof_and_convert_to_flow(void)
 {
-    
     int16_t Tmp16;
     float TmpFloat;
     uint32_t Tu32;
     uint32_t ch = get_mux_ch();
 
-    // 清反向计量标识
-    pTdcItf->bw_flow_flag = 0;
-    // 空管
+    pTdcItf->bw_flow_flag = 0; // 清空反向计量标识
+
+    // ---------- 空管分支 ----------
     if (tof_flag & TOF_FLAG_BLANK_PIPE)
     {
-        // 设置对应德通道为空管
         pTdcItf->set_blank(ch);
-        // DiffTof from normal channel
-        // 正常的通道进行计量
+        // 其他非空管通道继续计量
         if (pTdcItf->ch_bit_map ^ TdcItf.blank_flag)
         {
             DiffToF2Flow(DiffToF);
@@ -279,67 +690,107 @@ void read_tof_and_convert_to_flow(void)
         }
         if(alarm_status(ERR_BLANK_PIPE))
         {
-            tdc_turn_on_off_high_clk(0);//关闭MS1030高频时钟,降低功耗
-            TDC_SAMPLE_TIMES_PER_RTC_INT = 1;//每次RTC中断采集1次数据,空管情况下2秒1次中断
+            tdc_turn_on_off_high_clk(0); // 空管关闭TDC高频时钟,降功耗
+            TDC_SAMPLE_TIMES_PER_RTC_INT = 1;
+            tof_filter_reset();
         }
         return;
     }
     else
     {
-        // 指定通道清除空管
         pTdcItf->clr_blank(ch);
     }
 
+    // 读取原始TDU并换算成真实飞行时间
     UToF = TdcScale * TdcUToF[0];
     DToF = TdcScale * TdcDToF[0];
-    SumToF = UToF + DToF;
+    SumToF = UToF + DToF; // 和值:声速稳定时SumToF几乎不变,适合做丢波/错波检测
 
-    if (firstSample || fabsf(SumToF - LastSumToF) < 50.0f)
-    {
-        // 合法采样,进入滤波
-        DiffToF = UToF - DToF - pTdcItf->path_skew[ch];
-        DiffToF = tof_filter_input_poll(ch, DiffToF);
-        const float alpha = 0.05f; //1/20=0.05f
-        DiffToFAvg += (DiffToF - DiffToFAvg) * alpha;
-        UToFAvg    += (UToF - UToFAvg) * alpha;
-        DToFAvg    += (DToF - DToFAvg) * alpha;
-
-        firstSample = 0;
-        sumErrCnt = 0;
-        LastSumToF = SumToF;
-    }
-    else
+    const float alpha = 0.05f; // 外层一阶低通系数,越小越平滑,响应越慢
+    uint8_t ErrDataCheck = 0;
+    ErrDataCheck = err_data_check(0, UToF - DToF, SumToF);//检查本次数据的有效性,0->有效,1->无效
+    if(ErrDataCheck == 0)
     {
-        sumErrCnt++;
-        if(sumErrCnt >= SUM_CONT_ERR_N)
+        //数据有效,正常进入滤波与数据防护
+        if (firstSample || fabsf(SumToF - LastSumToF) < 50.0f)
         {
-            // 连续多次异常,判定为真实工况漂移,强制放行
-            DiffToF = UToF - DToF - pTdcItf->path_skew[ch];
-            DiffToF = tof_filter_input_poll(ch, DiffToF);
-            const float alpha = 0.05f;//1/20=0.05f
-            DiffToFAvg += (DiffToF - DiffToFAvg) * alpha;
-            UToFAvg    += (UToF - UToFAvg) * alpha;
-            DToFAvg    += (DToF - DToFAvg) * alpha;
+            // SumToF正常,采样有效
+            DiffToF = UToF - DToF - pTdcItf->path_skew[ch]; // 减去通道固定偏置
+            DiffToF = tof_filter_input_poll(ch, DiffToF);   // 送入卡尔曼滤波
 
+            // ========== 相对波动判断:波动超过±REL_JUMP_RATIO就直通,无保底门槛 ==========
+            float delta = fabsf(DiffToF - DiffToFAvg);    // 当前值和历史均值的差值
+            float jump_threshold = fabsf(DiffToFAvg) * REL_JUMP_RATIO; // 相对门槛
+
+            if(delta > jump_threshold)
+            {
+                // 波动大:大开/大关阀,直接更新均值,不做低通平滑,保证响应快
+                DiffToFAvg = DiffToF;
+                UToFAvg = UToF;
+                DToFAvg = DToF;
+            }
+            else
+            {
+                // 波动小:平稳状态,做一阶低通平滑,抑制抖动
+                DiffToFAvg = DiffToFAvg + (DiffToF - DiffToFAvg) * alpha;
+                UToFAvg    = UToFAvg    + (UToF    - UToFAvg)    * alpha;
+                DToFAvg    = DToFAvg    + (DToF    - DToFAvg)    * alpha;
+            }
+
+            firstSample = 0;
             sumErrCnt = 0;
             LastSumToF = SumToF;
         }
-        // else:单次/少数几次尖刺,直接丢弃,不更新滤波、不更新基准
+        else
+        {
+            // SumToF跳变很大,大概率是丢波、噪声尖刺
+            sumErrCnt++;
+            if(sumErrCnt >= SUM_CONT_ERR_N)
+            {
+                // 连续多次异常,判定是水温缓慢变化,不再丢弃采样,放行处理
+                DiffToF = UToF - DToF - pTdcItf->path_skew[ch];
+                DiffToF = tof_filter_input_poll(ch, DiffToF);
+
+                // 和上面一样的波动判断逻辑
+                float delta = fabsf(DiffToF - DiffToFAvg);
+                float jump_threshold = fabsf(DiffToFAvg) * REL_JUMP_RATIO;
+
+                if(delta > jump_threshold)
+                {
+                    DiffToFAvg = DiffToF;
+                    UToFAvg = UToF;
+                    DToFAvg = DToF;
+                    LOG("----2:U=%.6f,D=%.6f\r\n",UToF,DToF);
+                }
+                else
+                {
+                    DiffToFAvg = DiffToFAvg + (DiffToF - DiffToFAvg) * alpha;
+                    UToFAvg    = UToFAvg    + (UToF    - UToFAvg)    * alpha;
+                    DToFAvg    = DToFAvg    + (DToF    - DToFAvg)    * alpha;
+                }
+
+                sumErrCnt = 0;
+                LastSumToF = SumToF;
+            }
+            // else:少数几次异常,直接丢弃这次采样,不更新滤波
+        }
     }
+    //else:数据无效,采用上次有效的数据进行流量判断与计算
 
-
+    // ---------- 计量 / 检定状态 ----------
     if (tof_state & (TOF_ST_METER | TOF_ST_CALIB))
     {
+        // 始动流量判据
         if (fabsf(DiffToFAvg * 100.00f) > Paramx.n)
         {
             tof_flag |= TOF_FLAG_VALID_FLOW;
             if(GetCheckState() == CHECKING)
             {
-                TDC_SAMPLE_TIMES_PER_RTC_INT = 20;//add by yuewei 261023,有水检定模式->流采集频次0.5秒20次
+                TDC_SAMPLE_TIMES_PER_RTC_INT = 20; // 检定模式:多次采样打包,更稳
             }
             else
             {
-                TDC_SAMPLE_TIMES_PER_RTC_INT = 3;//add by yuewei 261015,有水大于始动流量->流采集频次0.5秒3次
+                TDC_SAMPLE_TIMES_PER_RTC_INT = 6;  // 正常大流量打包
             }
         }
         else
@@ -347,40 +798,35 @@ void read_tof_and_convert_to_flow(void)
             tof_flag &= ~TOF_FLAG_VALID_FLOW;
             if(GetCheckState() == CHECKING)
             {
-                TDC_SAMPLE_TIMES_PER_RTC_INT = 20;//add by yuewei 261023,有水检定模式->流采集频次0.5秒20次
+                TDC_SAMPLE_TIMES_PER_RTC_INT = 20;
             }
             else
             {
-                TDC_SAMPLE_TIMES_PER_RTC_INT = 2;//add by yuewei 261015,有水小于始动流量->流采集频次0.5秒1次
+                TDC_SAMPLE_TIMES_PER_RTC_INT = 2;  // 小流量/零点:减少打包次数,省电
             }
         }
 
-        // 是有效的流量
+        // 流量有效,打包计数满了再计算流速
         if (tof_flag & TOF_FLAG_VALID_FLOW)
         {
-
-            // 正常计量模式
-            //DiffToF2Flow(DiffToF);
-            //add by yuewei 261015,当采集次数大于等于TDC_SAMPLE_TIMES_PER_RTC_INT时,才进行流量计算(节约功耗)
-            if(TDC_SAMPLE_CNT >= TDC_SAMPLE_TIMES_PER_RTC_INT)
+            if (TDC_SAMPLE_CNT >= TDC_SAMPLE_TIMES_PER_RTC_INT)
             {
+                // spdl = (U-D) * 1e11 / Uavg / Davg
                 spdl = DiffToFAvg;
-                spdl = spdl * 100000000000;//扩大100倍
+                spdl = spdl * 100000000000;
                 spdl = spdl / UToFAvg;
                 spdl = spdl / DToFAvg;
 
                 DiffToF2Flow(spdl);
-            } 
+            }
         }
         else
         {
-            FlowVol = 0; // 始动流量切除时,瞬时流量清零yw2026-6-25
+            FlowVol = 0; // 低于始动流量,瞬时流量清零
+            set_wave_offs();//有水但是小于始动流量时,进行offset设置
         }
-        // 串口打印,功能未开启
-        // SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, UToFAvg, DToFAvg, DiffToFAvg);
         SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, spdl, DToFAvg, DiffToFAvg);
     }
-        
 }
 
 void tof_isr_convert_tof_to_flow(void)