增加温湿度数据滤波,防止数据频繁上报

This commit is contained in:
2026-09-03 14:57:41 +08:00
parent 9a2fdb1709
commit 59af5c4147
3 changed files with 50 additions and 26 deletions
+36 -15
View File
@@ -2,9 +2,8 @@
* @file app_th.c
* @brief CBR3 温湿度采集实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.31
* @version V1.1.0发令阶段 nack 也计入连续失败;fault_code 由上层映射为 1
* (规格书 V3.0.0:fault 编号不变,DP 号随协议右移为 DP24)
* @date 2026.09.03
* @version V1.3.0对外温湿度与显示/上报绑死(T>=0.5℃ / H>=2%
******************************************************************************/
#include "app_th.h"
@@ -32,8 +31,8 @@ typedef struct {
ThState_t state;
uint16_t poll_ms; /**< 距上次采样的累计毫秒 */
uint32_t wait_t0; /**< 测量命令发出时刻 */
int16_t temp_x10; /**< 最近有效温度 ×10 */
uint8_t humi; /**< 最近有效湿度 % */
int16_t temp_pub; /**< 对外温度 ×10(显示/上报/联动,|ΔT|>=0.5℃ 才改) */
uint8_t humi_pub; /**< 对外湿度 %(显示/上报/联动,|ΔH|>=2 才改) */
uint8_t valid; /**< 1=有有效采样 */
uint8_t fail_cnt; /**< 连续失败次数 */
uint8_t last_ok; /**< 最近一次采样是否成功(app_linkage 8.4 用) */
@@ -125,17 +124,39 @@ void app_th_poll(uint16_t dt)
/* IO 分离后无总线竞争,finish 不会返回 -2;0=成功,-1=通讯失败 */
s_th.state = TH_ST_IDLE;
if (ret == 0) {
s_th.temp_x10 = t;
s_th.humi = h;
int16_t dt_x10;
uint8_t dh;
/* 首次成功直接跟上;之后相对对外值达到阈值才改显示/上报/联动 */
if (!s_th.valid) {
s_th.temp_pub = t;
s_th.humi_pub = h;
} else {
dt_x10 = (int16_t)(t - s_th.temp_pub);
if (dt_x10 < 0) {
dt_x10 = (int16_t)(-dt_x10);
}
if (dt_x10 >= BOARD_TH_TEMP_PUB_DELTA_X10) {
s_th.temp_pub = t;
}
dh = (h > s_th.humi_pub) ? (uint8_t)(h - s_th.humi_pub)
: (uint8_t)(s_th.humi_pub - h);
if (dh >= BOARD_TH_HUMI_PUB_DELTA) {
s_th.humi_pub = h;
}
}
s_th.fail_cnt = 0;
s_th.valid = 1;
s_th.last_ok = 1;
/* 先填值再置 pending:消费者关中断取,生产者是中断,顺序无歧义 */
s_th_pending_t = t;
s_th_pending_h = h;
s_th_pending_t = s_th.temp_pub;
s_th_pending_h = s_th.humi_pub;
s_th_pending = 1;
log_info("th %d.%dC %d%%\n",
(int)(t / 10), (int)((t < 0) ? (-t % 10) : (t % 10)), h);
log_info("th raw=%d.%dC/%d%% pub=%d.%dC/%d%%\n",
(int)(t / 10), (int)((t < 0) ? (-t % 10) : (t % 10)), (int)h,
(int)(s_th.temp_pub / 10),
(int)((s_th.temp_pub < 0) ? (-s_th.temp_pub % 10) : (s_th.temp_pub % 10)),
(int)s_th.humi_pub);
} else {
th_on_fail();
}
@@ -153,21 +174,21 @@ uint8_t app_th_get_valid(void)
}
/**
* @brief 最近一次有效温度
* @brief 对外温度(数码管 + DP3 + 联动)
* @return 温度 ×10
*/
int16_t app_th_get_temp_x10(void)
{
return s_th.temp_x10;
return s_th.temp_pub;
}
/**
* @brief 最近一次有效湿度
* @brief 对外湿度(数码管 + DP4 + 联动)
* @return 湿度 0~100 整数 %
*/
uint8_t app_th_get_humi(void)
{
return s_th.humi;
return s_th.humi_pub;
}
/**