stat.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. #include "board.h"
  2. #include "stat.h"
  3. #include "rtc_run.h"
  4. #include "param.h"
  5. #include "alarm.h"
  6. #include "tdc_tof.h"
  7. #include "eeprom.h"
  8. #include "app_nb.h"
  9. #include "bill.h"
  10. static rtc_run_t yesterday_max_flow_rate_time; // 昨天最高流量时间
  11. static double yesterday_max_flow_rate = 0.0f; // 昨天最高流量
  12. static rtc_run_t today_max_flow_rate_time; // 今天最高流量时间
  13. static double today_max_flow_rate = 0.0f; // 今天最高流量
  14. static double large_flow_cum = 0.0f;
  15. static double leak_flow_cum = 0.0f;
  16. static double cnti_flow_cum = 0.0f;
  17. static uint32_t BwFlowCnt = 0;
  18. extern float BwFlowThres;
  19. // void SetStatFlow(double flow_rate)
  20. //{
  21. // rtc_run_t *rr;
  22. //
  23. // if (flow_rate > today_max_flow_rate)
  24. // {
  25. // today_max_flow_rate = flow_rate;
  26. // get_rtc_run(&rr);
  27. // memcpy(&today_max_flow_rate_time, rr, sizeof(rtc_run_t));
  28. // }
  29. // large_flow_cum += flow_rate;
  30. // leak_flow_cum += flow_rate;
  31. // cnti_flow_cum += flow_rate;
  32. // if (flow_rate < BwFlowThres)
  33. // {
  34. // BwFlowCnt++;
  35. // }
  36. // else
  37. // {
  38. // if (BwFlowCnt > 0)
  39. // {
  40. // BwFlowCnt--;
  41. // }
  42. // }
  43. //}
  44. /**
  45. * @brief 流量统计与累积处理函数,在tdc_flow_itf中调用
  46. * @param flow_rate 当前瞬时流量 (ml/s)
  47. * @note 该函数负责:更新当日最大流量、流量累积、小流量计数
  48. */
  49. void SetStatFlow(double flow_rate)
  50. {
  51. rtc_run_t *rr; // RTC 时间结构体指针
  52. // ====================== 1. 更新当日最大瞬时流量 ======================
  53. // 如果当前流量 > 已记录的当日最大流量,则更新最大值和发生时间
  54. if (flow_rate > today_max_flow_rate)
  55. {
  56. today_max_flow_rate = flow_rate; // 更新最大流量值
  57. get_rtc_run(&rr); // 获取当前系统RTC时间
  58. // 保存最大流量发生的时间点
  59. memcpy(&today_max_flow_rate_time, rr, sizeof(rtc_run_t));
  60. }
  61. // ====================== 2. 各类流量累积计算 ======================
  62. large_flow_cum = flow_rate; // 大流量监测累积量
  63. leak_flow_cum = flow_rate; // 漏水监测累积量
  64. cnti_flow_cum = flow_rate; // 持续流量异常监测累积量
  65. // ====================== 3. 小流量(小流量/始动流量)计数 ======================
  66. // 当前流量 < 小流量阈值 → 小流量持续计数+1
  67. if (flow_rate < BwFlowThres)
  68. {
  69. BwFlowCnt++;
  70. }
  71. // 流量恢复正常 → 小流量计数递减(防抖/平滑处理)
  72. else
  73. {
  74. if (BwFlowCnt > 0)
  75. {
  76. BwFlowCnt--;
  77. }
  78. }
  79. }
  80. // void ClrStatFlow(void) {
  81. // yesterday_max_flow_rate = 0.0f;
  82. // memset(&yesterday_max_flow_rate_time, 0, sizeof(rtc_run_t));
  83. // today_max_flow_rate = 0.0f;
  84. // memset(&today_max_flow_rate_time, 0, sizeof(rtc_run_t));
  85. // large_flow_cum = 0.0f;
  86. // leak_flow_cum = 0.0f;
  87. // cnti_flow_cum = 0.0f;
  88. // }
  89. // double get_max_flow_rate(void) {
  90. // return yesterday_max_flow_rate * 0.000001f * 3600;
  91. // }
  92. // void clr_max_flow_rate(void) {
  93. // yesterday_max_flow_rate = today_max_flow_rate;
  94. // memcpy(&yesterday_max_flow_rate_time, &today_max_flow_rate_time, sizeof(rtc_run_t));
  95. // today_max_flow_rate = 0.0f;
  96. // }
  97. // void get_max_flow_rate_time(rtc_run_t **rr) {
  98. // *rr = &yesterday_max_flow_rate_time;
  99. // }
  100. // void bw_flow_monitor(void) {
  101. // if (alarm_status(ERR_BW_FLOW)) {
  102. // if (BwFlowCnt == 0) {
  103. // Alarm(ERR_BW_FLOW, 0, 0);
  104. // }
  105. // } else {
  106. // if (BwFlowCnt >= 60) {
  107. // Alarm(ERR_BW_FLOW, 1, BwFlowCnt);
  108. // }
  109. // }
  110. // }
  111. /**
  112. * @brief 清空所有流量统计数据(初始化/重置)
  113. */
  114. void ClrStatFlow(void)
  115. {
  116. // 昨日/今日峰值流量清零
  117. yesterday_max_flow_rate = 0.0f;
  118. memset(&yesterday_max_flow_rate_time, 0, sizeof(rtc_run_t));
  119. today_max_flow_rate = 0.0f;
  120. memset(&today_max_flow_rate_time, 0, sizeof(rtc_run_t));
  121. // 各类流量累积量清零
  122. large_flow_cum = 0.0f;
  123. leak_flow_cum = 0.0f;
  124. cnti_flow_cum = 0.0f;
  125. }
  126. /**
  127. * @brief 获取昨日最大流量(单位转换后)
  128. * @return 转换后流量值
  129. */
  130. double get_max_flow_rate(void)
  131. {
  132. // 单位转换:原始值 → 标准流量单位
  133. return yesterday_max_flow_rate * 0.000001f * 3600.0f;
  134. }
  135. /**
  136. * @brief 每日零点切换峰值流量(今日→昨日,今日清零)
  137. */
  138. void clr_max_flow_rate(void)
  139. {
  140. // 今日峰值移动到昨日
  141. yesterday_max_flow_rate = today_max_flow_rate;
  142. memcpy(&yesterday_max_flow_rate_time, &today_max_flow_rate_time, sizeof(rtc_run_t));
  143. memset(&today_max_flow_rate_time, 0, sizeof(rtc_run_t)); // wxl2026-7-22
  144. // 今日峰值清零
  145. today_max_flow_rate = 0.0f;
  146. }
  147. void clr_max_flow_rate_all(void)
  148. {
  149. memset(&yesterday_max_flow_rate_time, 0, sizeof(rtc_run_t));
  150. memset(&today_max_flow_rate_time, 0, sizeof(rtc_run_t));
  151. yesterday_max_flow_rate = 0;
  152. today_max_flow_rate = 0;
  153. }
  154. /**
  155. * @brief 获取昨日最大流量发生的时间
  156. * @param rr 二级指针,用于返回时间结构体地址
  157. */
  158. void get_max_flow_rate_time(rtc_run_t **rr)
  159. {
  160. if (rr != NULL) // 防二级指针空指针
  161. {
  162. *rr = &yesterday_max_flow_rate_time;
  163. }
  164. }
  165. /**
  166. * @brief 反向计量监测,每1秒调用1次
  167. * @note 连续小流量达到60次(1分钟)触发报警,计数归0时恢复
  168. */
  169. void bw_flow_monitor(void)
  170. {
  171. // 已报警:计数为0则恢复报警
  172. if (alarm_status(ERR_BW_FLOW))
  173. {
  174. if (BwFlowCnt == 0)
  175. {
  176. Alarm(ERR_BW_FLOW, 0, 0);
  177. }
  178. }
  179. // 未报警:计数≥60触发报警
  180. else
  181. {
  182. if (BwFlowCnt >= 60)
  183. {
  184. Alarm(ERR_BW_FLOW, 1, BwFlowCnt);
  185. }
  186. }
  187. }
  188. // void high_water_temp_monitor(void) {
  189. // static uint16_t high_temp_up_cnt = 0;
  190. // static uint16_t high_temp_down_cnt = 0;
  191. // Param_t *param;
  192. // uint16_t temp = (uint16_t)(WaterTemp * 10); //unit: 0.1 celsius degree
  193. // GetParam(&param);
  194. // if (alarm_status(ERR_HIGH_TEMP)) {
  195. // if (temp <= (param->high_temp_alarm_thres)) {
  196. // high_temp_down_cnt++;
  197. // if (high_temp_down_cnt >= 3) {
  198. // Alarm(ERR_HIGH_TEMP, 0, temp);
  199. // high_temp_down_cnt = 0;
  200. // }
  201. // } else {
  202. // high_temp_down_cnt = 0;
  203. // }
  204. // } else {
  205. // if (temp > (param->high_temp_alarm_thres)) {
  206. // high_temp_up_cnt++;
  207. // if (high_temp_up_cnt >= 3) {
  208. // Alarm(ERR_HIGH_TEMP, 1, temp);
  209. // high_temp_up_cnt = 0;
  210. // }
  211. // } else {
  212. // high_temp_up_cnt = 0;
  213. // }
  214. // }
  215. //}
  216. // void low_water_temp_monitor(void) {
  217. // static uint16_t low_temp_up_cnt = 0;
  218. // static uint16_t low_temp_down_cnt = 0;
  219. // Param_t *param;
  220. // uint16_t temp = (uint16_t)(WaterTemp * 10); //unit: 0.1 celsius degree
  221. // GetParam(&param);
  222. // if (alarm_status(ERR_LOW_TEMP)) {
  223. // if (temp >= (param->low_temp_alarm_thres)) {
  224. // low_temp_up_cnt++;
  225. // if (low_temp_up_cnt >= 3) {
  226. // Alarm(ERR_LOW_TEMP, 0, temp);
  227. // low_temp_up_cnt = 0;
  228. // }
  229. // } else {
  230. // low_temp_up_cnt = 0;
  231. // }
  232. // } else {
  233. // if (temp < (param->low_temp_alarm_thres)) {
  234. // low_temp_down_cnt++;
  235. // if (low_temp_down_cnt >= 3) {
  236. // Alarm(ERR_LOW_TEMP, 1, temp);
  237. // low_temp_down_cnt = 0;
  238. // }
  239. // } else {
  240. // low_temp_down_cnt = 0;
  241. // }
  242. // }
  243. //}
  244. /**
  245. * @brief 高温报警监测(每秒调用1次)
  246. * 规则:连续3秒超过阈值 → 报警;连续3秒低于阈值 → 恢复
  247. */
  248. void high_water_temp_monitor(void)
  249. {
  250. if (StartUp < 10)
  251. return; // wxl2026-5-18
  252. static uint16_t alarm_confirm_cnt = 0; // 报警确认计数
  253. static uint16_t recover_confirm_cnt = 0; // 恢复确认计数
  254. Param_t *param = NULL;
  255. uint16_t current_temp; // 单位:0.1℃
  256. if (StartUp < 10)
  257. return; // wxl2026-5-18
  258. GetParam(&param);
  259. if (param->high_temp_alarm_thres == 0 || WaterTemp > 100.0f || WaterTemp < 0.0f) // wxl2026-5-18 温度不在范围不判别
  260. {
  261. alarm_confirm_cnt = 0;
  262. if (alarm_status(ERR_HIGH_TEMP)) // temp err or temp high warning disabled, clear flag
  263. {
  264. Alarm(ERR_HIGH_TEMP, 0, 0);
  265. }
  266. return;
  267. }
  268. // 获取参数 & 温度采样
  269. current_temp = (uint16_t)(WaterTemp * 10);
  270. // 已报警状态:判断恢复条件(连续3次 ≤ 阈值)
  271. if (alarm_status(ERR_HIGH_TEMP))
  272. {
  273. if (current_temp <= param->high_temp_alarm_thres)
  274. {
  275. if (++recover_confirm_cnt >= 3)
  276. {
  277. Alarm(ERR_HIGH_TEMP, 0, (uint32_t)current_temp);
  278. recover_confirm_cnt = 0;
  279. }
  280. }
  281. else
  282. {
  283. recover_confirm_cnt = 0; // 不满足,清零
  284. }
  285. }
  286. // 未报警:判断触发条件(连续3次 > 阈值)
  287. else
  288. {
  289. if (current_temp > param->high_temp_alarm_thres)
  290. {
  291. if (++alarm_confirm_cnt >= 3)
  292. {
  293. Alarm(ERR_HIGH_TEMP, 1, (uint32_t)current_temp);
  294. alarm_confirm_cnt = 0;
  295. }
  296. }
  297. else
  298. {
  299. alarm_confirm_cnt = 0; // 不满足,清零
  300. }
  301. }
  302. }
  303. /**
  304. * @brief 低温报警监测(每秒调用1次)
  305. * 规则:连续3秒低于阈值 → 报警;连续3秒高于阈值 → 恢复
  306. */
  307. void low_water_temp_monitor(void)
  308. {
  309. static uint16_t alarm_confirm_cnt = 0; // 报警确认计数
  310. static uint16_t recover_confirm_cnt = 0; // 恢复确认计数
  311. Param_t *param = NULL;
  312. uint16_t current_temp; // 单位:0.1℃
  313. if (StartUp < 10)
  314. return; // wxl2026-5-18
  315. GetParam(&param);
  316. if (param->low_temp_alarm_thres == 0 || WaterTemp > 100.0f || WaterTemp < 0.0f) // wxl2026-5-18 温度不在范围不判别
  317. {
  318. alarm_confirm_cnt = 0;
  319. if (alarm_status(ERR_LOW_TEMP)) // temp err or temp low warning disabled,clear warn flag
  320. {
  321. Alarm(ERR_LOW_TEMP, 0, 0);
  322. }
  323. return;
  324. }
  325. // 获取参数 & 温度采样
  326. current_temp = (uint16_t)(WaterTemp * 10);
  327. // 已报警状态:判断恢复条件(连续3次 ≥ 阈值)
  328. if (alarm_status(ERR_LOW_TEMP))
  329. {
  330. if (current_temp >= param->low_temp_alarm_thres)
  331. {
  332. if (++recover_confirm_cnt >= 3)
  333. {
  334. Alarm(ERR_LOW_TEMP, 0, (uint32_t)current_temp);
  335. recover_confirm_cnt = 0;
  336. }
  337. }
  338. else
  339. {
  340. recover_confirm_cnt = 0;
  341. }
  342. }
  343. // 未报警:判断触发条件(连续3次 < 阈值)
  344. else
  345. {
  346. if (current_temp < param->low_temp_alarm_thres)
  347. {
  348. if (++alarm_confirm_cnt >= 3)
  349. {
  350. Alarm(ERR_LOW_TEMP, 1, (uint32_t)current_temp);
  351. alarm_confirm_cnt = 0;
  352. }
  353. }
  354. else
  355. {
  356. alarm_confirm_cnt = 0;
  357. }
  358. }
  359. }
  360. // void large_flow_monitor(void) {
  361. // static uint32_t large_flow_cnt = 0;
  362. // Param_t *param;
  363. // uint32_t cnti_moni_time;
  364. // uint32_t large_flow;
  365. // GetParam(&param);
  366. // if (param->large_flow_alarm_thres == 0 ||
  367. // param->large_flow_cnti_moni_time == 0) {
  368. // return;
  369. // }
  370. // large_flow_cnt++;
  371. // cnti_moni_time = param->large_flow_cnti_moni_time * 60;
  372. // if (large_flow_cnt < cnti_moni_time) {
  373. // return;
  374. // }
  375. // large_flow = (uint32_t)(large_flow_cum * 0.0001); //unit: 0.01m^3
  376. // if (alarm_status(ERR_LARGE_FLOW)) {
  377. // if (large_flow < param->large_flow_alarm_thres) {
  378. // Alarm(ERR_LARGE_FLOW, 0, large_flow);
  379. // }
  380. // } else {
  381. // if (large_flow >= param->large_flow_alarm_thres) {
  382. // Alarm(ERR_LARGE_FLOW, 1, large_flow_cnt);
  383. // }
  384. // }
  385. // large_flow_cnt = 0;
  386. // large_flow_cum = 0.0f;
  387. //}
  388. /**
  389. * @brief 大流量连续监测(每秒调用1次)
  390. * @功能 流量连续超过阈值 ≥ 设定分钟数,才报警;低于阈值立即清零计时
  391. * @参数 large_flow_cnti_moni_time 单位:分钟
  392. */
  393. void large_flow_monitor(void)
  394. {
  395. static uint32_t large_flow_continuous_sec = 0; // 连续超阈值计时(秒)
  396. Param_t *param = NULL;
  397. double flow_current; // 当前流量ml/s
  398. uint32_t alarm_threshold_sec; // 报警阈值(转换成秒)
  399. if (StartUp < 10)
  400. return; // wxl2026-5-18
  401. GetParam(&param);
  402. double large_flow_alarm_thres = param->large_flow_alarm_thres; // m^3/hX100
  403. large_flow_alarm_thres = large_flow_alarm_thres * (10000.0f / 3600.0f); // m^3/hX100 -> ml/s
  404. // 阈值/时间未配置,不监测
  405. if (param->large_flow_alarm_thres == 0 || param->large_flow_cnti_moni_time == 0)
  406. {
  407. large_flow_continuous_sec = 0;
  408. if (alarm_status(ERR_LARGE_FLOW)) // if large flow warning disabled ,clear flag
  409. {
  410. Alarm(ERR_LARGE_FLOW, 0, 0);
  411. }
  412. return;
  413. }
  414. // 核心:分钟 → 秒
  415. alarm_threshold_sec = param->large_flow_cnti_moni_time * 60;
  416. // 计算当前流量
  417. flow_current = large_flow_cum;
  418. // ============== 核心逻辑 ==============
  419. // 1. 当前流量 ≥ 阈值:计时+1
  420. if (flow_current >= large_flow_alarm_thres)
  421. {
  422. // 防止计数溢出
  423. if (large_flow_continuous_sec < alarm_threshold_sec)
  424. {
  425. large_flow_continuous_sec++;
  426. }
  427. // 连续超时达到设定分钟 → 触发报警
  428. if (large_flow_continuous_sec >= alarm_threshold_sec)
  429. {
  430. // 已报警则不重复上报
  431. if (!alarm_status(ERR_LARGE_FLOW))
  432. {
  433. Alarm(ERR_LARGE_FLOW, 1, (uint32_t)flow_current);
  434. }
  435. }
  436. }
  437. // 2. 流量 < 阈值:立即清零计时(中断连续状态)
  438. else
  439. {
  440. large_flow_continuous_sec = 0;
  441. // 已报警则恢复
  442. if (alarm_status(ERR_LARGE_FLOW))
  443. {
  444. Alarm(ERR_LARGE_FLOW, 0, (uint32_t)flow_current);
  445. }
  446. }
  447. }
  448. // void cnti_flow_monitor(void) {
  449. // static uint32_t cnti_flow_cnt = 0;
  450. // static uint32_t cnti_prev_flow_cum = 0;
  451. // Param_t *param;
  452. // uint32_t cnti_moni_time;
  453. // uint32_t cnti_flow;
  454. // GetParam(&param);
  455. // if (param->cnti_flow_cnti_moni_time == 0) {
  456. // return;
  457. // }
  458. // cnti_flow_cnt++;
  459. // cnti_moni_time = param->cnti_flow_cnti_moni_time * 60;
  460. // if (cnti_flow_cnt < cnti_moni_time) {
  461. // return;
  462. // }
  463. // cnti_flow = (uint32_t)(cnti_flow_cum * 0.0001); //unit: 0.01m^3
  464. // if (alarm_status(ERR_CNTI_FLOW)) {
  465. // if (cnti_flow < param->large_flow_alarm_thres
  466. // || (cnti_prev_flow_cum > 0
  467. // && cnti_flow < (cnti_prev_flow_cum * 12 / 10)
  468. // && cnti_flow > (cnti_prev_flow_cum * 8 / 10))) {
  469. // Alarm(ERR_CNTI_FLOW, 0, cnti_flow);
  470. // }
  471. // } else {
  472. // if (cnti_flow >= param->large_flow_alarm_thres
  473. // && cnti_prev_flow_cum > 0
  474. // && (cnti_flow >= (cnti_prev_flow_cum * 12 / 10)
  475. // || cnti_flow <= (cnti_prev_flow_cum * 8 / 10))) {
  476. // Alarm(ERR_CNTI_FLOW, 1, cnti_flow);
  477. // }
  478. // }
  479. // cnti_prev_flow_cum = cnti_flow;
  480. // cnti_flow_cum = 0.0f;
  481. // cnti_flow_cnt = 0;
  482. //}
  483. /**
  484. * @brief 持续流量异常监测
  485. * @call 每秒调用1次
  486. * @rule 流量异常持续 >= 设定分钟数 → 报警
  487. * @异常条件:
  488. * 1. 流量 >= 阈值
  489. * 2. 相比上周期波动 > ±20%
  490. */
  491. void cnti_flow_monitor(void)
  492. {
  493. static uint32_t abnormal_second_cnt = 0; // 异常持续秒数
  494. static uint32_t last_period_flow = 0; // 上一周期基准流量
  495. Param_t *param = NULL;
  496. double current_flow; // 当前流量 0.01m?
  497. uint8_t is_abnormal = 0;
  498. if (StartUp < 10)
  499. return; // wxl2026-5-18
  500. GetParam(&param);
  501. // 未开启监测
  502. if (param->cnti_flow_cnti_moni_time == 0)
  503. {
  504. abnormal_second_cnt = 0;
  505. if (alarm_status(ERR_CNTI_FLOW))
  506. {
  507. Alarm(ERR_CNTI_FLOW, 0, 0);
  508. }
  509. return;
  510. }
  511. // 当前周期流量
  512. // current_flow = (uint32_t)(cnti_flow_cum * 0.0001f);
  513. current_flow = cnti_flow_cum;
  514. // ==================== 异常判断 ====================
  515. // 条件1:超过流量阈值
  516. // 条件2:与上周期相比波动 > ±20% (突增 ≥120% 或 突降 ≤80%)
  517. if (current_flow > 0 && last_period_flow > 0)
  518. {
  519. if (current_flow <= last_period_flow * 12 / 10 &&
  520. current_flow >= last_period_flow * 8 / 10)
  521. {
  522. is_abnormal = 1;
  523. }
  524. }
  525. // ==================== 持续异常计时 ====================
  526. if (is_abnormal)
  527. {
  528. abnormal_second_cnt++; // 每秒 +1
  529. // 持续时间 >= 设定分钟 → 报警
  530. if (abnormal_second_cnt >= param->cnti_flow_cnti_moni_time * 60)
  531. {
  532. if (!alarm_status(ERR_CNTI_FLOW))
  533. {
  534. Alarm(ERR_CNTI_FLOW, 1, (uint32_t)current_flow);
  535. }
  536. abnormal_second_cnt = param->cnti_flow_cnti_moni_time * 60 + 2;
  537. }
  538. }
  539. else
  540. {
  541. // 恢复正常:清零计时 + 解除报警
  542. if (abnormal_second_cnt > 0)
  543. {
  544. abnormal_second_cnt = 0;
  545. if (alarm_status(ERR_CNTI_FLOW))
  546. {
  547. Alarm(ERR_CNTI_FLOW, 0, (uint32_t)current_flow);
  548. }
  549. }
  550. // 正常时更新基准流量
  551. last_period_flow = current_flow;
  552. }
  553. // 每秒累积量清零
  554. // cnti_flow_cum = 0.0f;
  555. }
  556. // void leak_flow_monitor(void) {
  557. // static uint32_t leak_flow_cnt = 0;
  558. // static uint32_t leak_prev_flow_cum = 0;
  559. // Param_t *param;
  560. // uint32_t cnti_moni_time;
  561. // uint32_t leak_flow;
  562. // GetParam(&param);
  563. // if (param->leak_flow_alarm_thres == 0
  564. // || param->leak_flow_cnti_moni_time == 0) {
  565. // return;
  566. // }
  567. // leak_flow_cnt++;
  568. // cnti_moni_time = param->leak_flow_cnti_moni_time * 60;
  569. // if (leak_flow_cnt < cnti_moni_time) {
  570. // return;
  571. // }
  572. // leak_flow = (uint32_t)(leak_flow_cum * 0.0001); //unit: 0.01m^3
  573. // if (alarm_status(ERR_LEAK)) {
  574. // if (leak_flow > param->leak_flow_alarm_thres
  575. // || (leak_prev_flow_cum > 0
  576. // && leak_flow < (leak_prev_flow_cum * 12 / 10)
  577. // && leak_flow > (leak_prev_flow_cum * 8 / 10))) {
  578. // Alarm(ERR_LEAK, 0, leak_flow);
  579. // }
  580. // } else {
  581. // if (leak_flow <= param->leak_flow_alarm_thres
  582. // && leak_prev_flow_cum > 0
  583. // && (leak_flow >= (leak_prev_flow_cum * 12 / 10)
  584. // || leak_flow <= (leak_prev_flow_cum * 8 / 10))) {
  585. // Alarm(ERR_LEAK, 1, leak_flow);
  586. // }
  587. // }
  588. // leak_prev_flow_cum = leak_flow;
  589. // leak_flow_cum = 0.0f;
  590. // leak_flow_cnt = 0;
  591. //}
  592. /**
  593. * @brief 渗漏流量异常监测
  594. * @call 每秒调用1次
  595. * @param leak_flow_cnti_moni_time 单位:分钟
  596. * @rule 满足以下条件并持续 N 分钟 → 报警
  597. * 1. 当前周期渗漏流量 ≤ 报警阈值
  598. * 2. 与上周期流量波动 > ±20%(突增/突降)
  599. */
  600. void leak_flow_monitor(void)
  601. {
  602. static uint32_t abnormal_cont_sec = 0; // 异常持续计时(秒)
  603. static uint32_t last_period_leak_flow = 0; // 上周期基准流量(0.01m?)
  604. Param_t *param = NULL;
  605. double curr_leak_flow; // 当前周期渗漏流量(0.01m?)
  606. uint32_t alarm_threshold_sec; // 报警阈值时间(秒)
  607. uint8_t is_flow_abnormal = 0; // 异常标志
  608. if (StartUp < 10)
  609. return; // wxl2026-5-18
  610. // 获取参数指针
  611. GetParam(&param);
  612. if (param == NULL)
  613. return;
  614. // 未使能监测(阈值或时间为0)
  615. if (param->leak_flow_alarm_thres == 0 || param->leak_flow_cnti_moni_time == 0)
  616. {
  617. abnormal_cont_sec = 0;
  618. if (alarm_status(ERR_LEAK))
  619. {
  620. Alarm(ERR_LEAK, 0, 0);
  621. }
  622. return;
  623. }
  624. double leak_flow_alarm_thres = param->leak_flow_alarm_thres; // m^3/hX100
  625. leak_flow_alarm_thres = leak_flow_alarm_thres * (10000.0f / 3600.0f); // m^3/hX100 -> ml/s
  626. // 转换监测时间:分钟 → 秒
  627. alarm_threshold_sec = param->leak_flow_cnti_moni_time * 60;
  628. // 计算当前周期渗漏流量(单位:0.01m?)
  629. curr_leak_flow = leak_flow_cum;
  630. // ====================== 渗漏异常判断逻辑 ======================
  631. // 异常条件:
  632. // 1. 当前流量 ≤ 渗漏阈值
  633. // 2. 存在上周期基准值
  634. // 3. 波动 > ±20% (≥120% 或 ≤80%)
  635. if (curr_leak_flow <= leak_flow_alarm_thres && last_period_leak_flow > 0)
  636. {
  637. if (curr_leak_flow <= last_period_leak_flow * 12 / 10 &&
  638. curr_leak_flow >= last_period_leak_flow * 8 / 10)
  639. {
  640. is_flow_abnormal = 1;
  641. }
  642. }
  643. // ====================== 持续异常计时 & 报警 ======================
  644. if (is_flow_abnormal)
  645. {
  646. // 异常状态:累计秒数
  647. if (abnormal_cont_sec < alarm_threshold_sec)
  648. {
  649. abnormal_cont_sec++;
  650. }
  651. // 达到持续时间 → 触发报警
  652. if (abnormal_cont_sec >= alarm_threshold_sec)
  653. {
  654. if (!alarm_status(ERR_LEAK))
  655. {
  656. Alarm(ERR_LEAK, 1, (uint32_t)curr_leak_flow);
  657. }
  658. }
  659. }
  660. else
  661. {
  662. // 恢复正常:清零计时
  663. if (abnormal_cont_sec > 0)
  664. {
  665. abnormal_cont_sec = 0;
  666. // 已报警则恢复
  667. if (alarm_status(ERR_LEAK))
  668. {
  669. Alarm(ERR_LEAK, 0, (uint32_t)curr_leak_flow);
  670. }
  671. }
  672. // 正常状态:更新基准流量
  673. last_period_leak_flow = curr_leak_flow;
  674. }
  675. // 周期累积量清零
  676. // leak_flow_cum = 0.0f;
  677. }
  678. /**
  679. * @brief 电池电压监控,1分钟调用1次
  680. * @param 无
  681. * @retval 无
  682. */
  683. void low_power_monitor(void)
  684. {
  685. static uint16_t low_pwr_up_cnt = 0;
  686. static uint16_t low_pwr_down_cnt = 0;
  687. uint16_t VbatMin; // mv
  688. VbatMin = McuVbat; // > McuVTxbat ? McuVTxbat : McuVbat;// wxl2026-6-2
  689. if (alarm_status(ERR_LOW_POWER))
  690. {
  691. // modified by yuewei 20260407
  692. // if (McuVbat >= 3025)
  693. if ((McuVbat >= WARN_VBAT_RET) /* && (McuVTxbat >= 3025)*/) //// wxl2026-6-2
  694. {
  695. low_pwr_up_cnt++;
  696. if (low_pwr_up_cnt >= 1) // 3->1,modified by yuewei 20260407
  697. {
  698. Alarm(ERR_LOW_POWER, 0, (uint32_t)VbatMin);
  699. low_pwr_up_cnt = 0;
  700. }
  701. }
  702. else
  703. {
  704. low_pwr_up_cnt = 0;
  705. }
  706. }
  707. else
  708. {
  709. // modified by yuewei 20260407
  710. // if (McuVbat <= 2975)
  711. // if (McuVbat <= 3000 /*|| McuVTxbat <= 2975*/) // only one vol port in small meter wxl2026-6-2
  712. if (McuVbat <= MIN_WARN_VBAT /*|| McuVTxbat <= 2975*/) // only one vol port in small meter wxl2026-6-2
  713. {
  714. low_pwr_down_cnt++;
  715. // ADC every 10 minutes monitor 5 day = 6 * 24 * 5 = 720
  716. if (low_pwr_down_cnt >= 5) // 720->5, modified by yuewei 20260407
  717. {
  718. Alarm(ERR_LOW_POWER, 1, (uint32_t)VbatMin);
  719. low_pwr_down_cnt = 0;
  720. }
  721. }
  722. else
  723. {
  724. if (low_pwr_down_cnt > 0)
  725. {
  726. low_pwr_down_cnt--;
  727. }
  728. }
  729. }
  730. }
  731. // void tdc_rw_err_monitor(void) {
  732. // uint32_t err_cnt = get_tdc_rw_err_cnt();
  733. //
  734. // if (alarm_status(ERR_TOF_SPI_RW)) {
  735. // if (err_cnt == 0) {
  736. // Alarm(ERR_TOF_SPI_RW, 0, 0);
  737. // }
  738. // } else {
  739. // if (err_cnt >= 30) {
  740. // Alarm(ERR_TOF_SPI_RW, 1, err_cnt);
  741. // }
  742. // }
  743. // }
  744. // void eeprom_rw_err_monitor(void) {
  745. // uint32_t err_cnt = get_eeprom_rw_err_cnt();
  746. //
  747. // if (alarm_status(ERR_MEMORY)) {
  748. // if (err_cnt == 0) {
  749. // Alarm(ERR_MEMORY, 0, 0);
  750. // }
  751. // } else {
  752. // if (err_cnt >= 30) {
  753. // Alarm(ERR_MEMORY, 1, err_cnt);
  754. // }
  755. // }
  756. // }
  757. /**
  758. * @brief TDC SPI 读写错误监测
  759. * @rule 错误计数 ≥30 报警,错误计数=0 恢复
  760. */
  761. void tdc_rw_err_monitor(void)
  762. {
  763. if (StartUp < 10)
  764. return; // wxl2026-5-18
  765. uint32_t err_cnt = get_tdc_rw_err_cnt();
  766. if (alarm_status(ERR_TOF_SPI_RW))
  767. {
  768. // 已报警:错误清零则恢复
  769. if (err_cnt == 0)
  770. {
  771. Alarm(ERR_TOF_SPI_RW, 0, 0);
  772. }
  773. }
  774. else
  775. {
  776. // 未报警:错误数≥30 触发报警
  777. if (err_cnt >= 30)
  778. {
  779. Alarm(ERR_TOF_SPI_RW, 1, err_cnt);
  780. }
  781. }
  782. }
  783. /**
  784. * @brief EEPROM 读写错误监测
  785. * @rule 错误计数 ≥30 报警,错误计数=0 恢复
  786. */
  787. void eeprom_rw_err_monitor(void)
  788. {
  789. if (StartUp < 10)
  790. return; // wxl2026-5-18
  791. uint32_t err_cnt = get_eeprom_rw_err_cnt();
  792. if (alarm_status(ERR_MEMORY))
  793. {
  794. // 已报警:错误清零则恢复
  795. if (err_cnt == 0)
  796. {
  797. Alarm(ERR_MEMORY, 0, 0);
  798. }
  799. }
  800. else
  801. {
  802. // 未报警:错误数≥30 触发报警
  803. if (err_cnt >= 30)
  804. {
  805. Alarm(ERR_MEMORY, 1, err_cnt);
  806. }
  807. }
  808. }
  809. // void water_flow_monitor(void) {
  810. // bw_flow_monitor();
  811. // large_flow_monitor();
  812. // cnti_flow_monitor();
  813. // leak_flow_monitor();
  814. //
  815. // high_water_temp_monitor();
  816. // low_water_temp_monitor();
  817. // tdc_rw_err_monitor();
  818. // eeprom_rw_err_monitor();
  819. //}
  820. /**
  821. * @brief 监测入口(未使用)
  822. * @detail 统一调度所有流量、温度、通信故障监测
  823. */
  824. void water_flow_monitor(void)
  825. {
  826. // 1. 流量类监测
  827. bw_flow_monitor(); // 小流量/始动流量监测
  828. large_flow_monitor(); // 大流量持续监测
  829. cnti_flow_monitor(); // 持续流量异常监测
  830. leak_flow_monitor(); // 渗漏流量监测
  831. // 2. 温度类监测
  832. high_water_temp_monitor(); // 高温报警监测
  833. low_water_temp_monitor(); // 低温报警监测
  834. // 3. 硬件通信故障监测
  835. tdc_rw_err_monitor(); // TDC SPI 读写错误监测
  836. eeprom_rw_err_monitor(); // EEPROM 读写错误监测
  837. }
  838. // void high_pressure_monitor(void) {
  839. // static uint16_t high_press_up_cnt = 0;
  840. // static uint16_t high_press_down_cnt = 0;
  841. // Param_t *param;
  842. // GetParam(&param);
  843. // //add by yuewei 20260407
  844. // if (param->pressure_sensor != 1) //no pressure sensor or not set
  845. // {
  846. // return;
  847. // }
  848. // if (param->high_press_alarm_thres == 0) {
  849. // return;
  850. // }
  851. // uint16_t pressure = (uint16_t)(WaterPressure * 100); //unit: 0.01MPa
  852. // if (alarm_status(ERR_HIGH_PRESSURE)) {
  853. // if (pressure <= (param->high_press_alarm_thres - 2)) {
  854. // high_press_down_cnt++;
  855. // if (high_press_down_cnt >= 3) {
  856. // Alarm(ERR_HIGH_PRESSURE, 0, pressure);
  857. // high_press_down_cnt = 0;
  858. // }
  859. // } else {
  860. // high_press_down_cnt = 0;
  861. // }
  862. // } else {
  863. // if (pressure >= (param->high_press_alarm_thres + 2)) {
  864. // high_press_up_cnt++;
  865. // if (high_press_up_cnt >= 3) {
  866. // Alarm(ERR_HIGH_PRESSURE, 1, pressure);
  867. // high_press_up_cnt = 0;
  868. // }
  869. // } else {
  870. // high_press_up_cnt = 0;
  871. // }
  872. // }
  873. //}
  874. // void low_pressure_monitor(void) {
  875. // static uint16_t low_press_up_cnt = 0;
  876. // static uint16_t low_press_down_cnt = 0;
  877. // Param_t *param;
  878. // GetParam(&param);
  879. // //add by yuewei 20260407
  880. // if (param->pressure_sensor != 1) //no pressure sensor or not set
  881. // {
  882. // return;
  883. // }
  884. // if (param->low_press_alarm_thres == 0) {
  885. // return;
  886. // }
  887. // uint16_t pressure = (uint16_t)(WaterPressure * 100); //unit: 0.01MPa
  888. //
  889. // if (alarm_status(ERR_LOW_PRESSURE)) {
  890. // if (pressure >= (param->low_press_alarm_thres + 2)) {
  891. // low_press_up_cnt++;
  892. // if (low_press_up_cnt >= 3) {
  893. // Alarm(ERR_LOW_PRESSURE, 0, pressure);
  894. // low_press_up_cnt = 0;
  895. // }
  896. // } else {
  897. // low_press_up_cnt = 0;
  898. // }
  899. //
  900. // } else {
  901. // if (pressure <= (param->low_press_alarm_thres - 2)) {
  902. // low_press_down_cnt++;
  903. // if (low_press_down_cnt >= 3) {
  904. // Alarm(ERR_LOW_PRESSURE, 1, pressure);
  905. // low_press_down_cnt = 0;
  906. // }
  907. // } else {
  908. // low_press_down_cnt = 0;
  909. // }
  910. // }
  911. //}
  912. /**
  913. * @brief 高压报警监测(每1分钟调用1次)
  914. * 规则:压力连续3次 ≥ 阈值+2 → 报警
  915. * 报警后连续3次 ≤ 阈值-2 → 恢复
  916. * 仅当压力传感器使能时(param->pressure_sensor == 1)生效
  917. * 单位:0.01MPa
  918. */
  919. void high_pressure_monitor(void)
  920. {
  921. static uint16_t alarm_confirm_cnt = 0; // 报警连续确认计数
  922. static uint16_t recover_confirm_cnt = 0; // 恢复连续确认计数
  923. Param_t *param = NULL;
  924. uint16_t curr_pressure; // 当前压力
  925. GetParam(&param);
  926. if (param == NULL)
  927. return;
  928. // 未安装压力传感器 / 未使能
  929. if (param->pressure_sensor != 1)
  930. {
  931. alarm_confirm_cnt = 0;
  932. recover_confirm_cnt = 0;
  933. return;
  934. }
  935. // 阈值未配置
  936. if (param->high_press_alarm_thres == 0)
  937. {
  938. return;
  939. }
  940. // 计算当前压力 0.01MPa
  941. curr_pressure = (uint16_t)(WaterPressure * 100);
  942. // 已报警 → 判断恢复条件(连续3次 ≤ 阈值-2)
  943. if (alarm_status(ERR_HIGH_PRESSURE))
  944. {
  945. if (curr_pressure <= (param->high_press_alarm_thres - 2))
  946. {
  947. if (++recover_confirm_cnt >= 3)
  948. {
  949. Alarm(ERR_HIGH_PRESSURE, 0, curr_pressure);
  950. recover_confirm_cnt = 0;
  951. }
  952. }
  953. else
  954. {
  955. recover_confirm_cnt = 0;
  956. }
  957. }
  958. // 未报警 → 判断触发条件(连续3次 ≥ 阈值+2)
  959. else
  960. {
  961. if (curr_pressure >= (param->high_press_alarm_thres + 2))
  962. {
  963. if (++alarm_confirm_cnt >= 3)
  964. {
  965. Alarm(ERR_HIGH_PRESSURE, 1, curr_pressure);
  966. alarm_confirm_cnt = 0;
  967. }
  968. }
  969. else
  970. {
  971. alarm_confirm_cnt = 0;
  972. }
  973. }
  974. }
  975. /**
  976. * @brief 低压报警监测(每1分钟调用1次)
  977. * 规则:压力连续3次 ≤ 阈值-2 → 报警
  978. * 报警后连续3次 ≥ 阈值+2 → 恢复
  979. * 仅当压力传感器使能时(param->pressure_sensor == 1)生效
  980. * 单位:0.01MPa
  981. */
  982. void low_pressure_monitor(void)
  983. {
  984. static uint16_t alarm_confirm_cnt = 0; // 报警连续确认计数
  985. static uint16_t recover_confirm_cnt = 0; // 恢复连续确认计数
  986. Param_t *param = NULL;
  987. uint16_t curr_pressure; // 当前压力
  988. GetParam(&param);
  989. if (param == NULL)
  990. return;
  991. // 未安装压力传感器 / 未使能
  992. if (param->pressure_sensor != 1)
  993. {
  994. alarm_confirm_cnt = 0;
  995. recover_confirm_cnt = 0;
  996. return;
  997. }
  998. // 阈值未配置
  999. if (param->low_press_alarm_thres == 0)
  1000. {
  1001. return;
  1002. }
  1003. // 计算当前压力 0.01MPa
  1004. curr_pressure = (uint16_t)(WaterPressure * 100);
  1005. // 已报警 → 判断恢复条件(连续3次 ≥ 阈值+2)
  1006. if (alarm_status(ERR_LOW_PRESSURE))
  1007. {
  1008. if (curr_pressure >= (param->low_press_alarm_thres + 2))
  1009. {
  1010. if (++recover_confirm_cnt >= 3)
  1011. {
  1012. Alarm(ERR_LOW_PRESSURE, 0, curr_pressure);
  1013. recover_confirm_cnt = 0;
  1014. }
  1015. }
  1016. else
  1017. {
  1018. recover_confirm_cnt = 0;
  1019. }
  1020. }
  1021. // 未报警 → 判断触发条件(连续3次 ≤ 阈值-2)
  1022. else
  1023. {
  1024. if (curr_pressure <= (param->low_press_alarm_thres - 2))
  1025. {
  1026. if (++alarm_confirm_cnt >= 3)
  1027. {
  1028. Alarm(ERR_LOW_PRESSURE, 1, curr_pressure);
  1029. alarm_confirm_cnt = 0;
  1030. }
  1031. }
  1032. else
  1033. {
  1034. alarm_confirm_cnt = 0;
  1035. }
  1036. }
  1037. }
  1038. // void pressure_monitor(void) {
  1039. // high_pressure_monitor();
  1040. // low_pressure_monitor();
  1041. // }
  1042. /**
  1043. * @brief 水压监测总入口(未使用)
  1044. * @detail 统一调度高压、低压报警监测
  1045. */
  1046. void pressure_monitor(void)
  1047. {
  1048. // 水压上下限监测
  1049. high_pressure_monitor(); // 高压报警监测
  1050. low_pressure_monitor(); // 低压报警监测
  1051. }