stat.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 >= 3025) /* && (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 <= 2975 /*|| McuVTxbat <= 2975*/) // only one vol port in small meter wxl2026-6-2
  712. {
  713. low_pwr_down_cnt++;
  714. // ADC every 10 minutes monitor 5 day = 6 * 24 * 5 = 720
  715. if (low_pwr_down_cnt >= 5) // 720->5, modified by yuewei 20260407
  716. {
  717. Alarm(ERR_LOW_POWER, 1, (uint32_t)VbatMin);
  718. low_pwr_down_cnt = 0;
  719. }
  720. }
  721. else
  722. {
  723. if (low_pwr_down_cnt > 0)
  724. {
  725. low_pwr_down_cnt--;
  726. }
  727. }
  728. }
  729. }
  730. // void tdc_rw_err_monitor(void) {
  731. // uint32_t err_cnt = get_tdc_rw_err_cnt();
  732. //
  733. // if (alarm_status(ERR_TOF_SPI_RW)) {
  734. // if (err_cnt == 0) {
  735. // Alarm(ERR_TOF_SPI_RW, 0, 0);
  736. // }
  737. // } else {
  738. // if (err_cnt >= 30) {
  739. // Alarm(ERR_TOF_SPI_RW, 1, err_cnt);
  740. // }
  741. // }
  742. // }
  743. // void eeprom_rw_err_monitor(void) {
  744. // uint32_t err_cnt = get_eeprom_rw_err_cnt();
  745. //
  746. // if (alarm_status(ERR_MEMORY)) {
  747. // if (err_cnt == 0) {
  748. // Alarm(ERR_MEMORY, 0, 0);
  749. // }
  750. // } else {
  751. // if (err_cnt >= 30) {
  752. // Alarm(ERR_MEMORY, 1, err_cnt);
  753. // }
  754. // }
  755. // }
  756. /**
  757. * @brief TDC SPI 读写错误监测
  758. * @rule 错误计数 ≥30 报警,错误计数=0 恢复
  759. */
  760. void tdc_rw_err_monitor(void)
  761. {
  762. if (StartUp < 10)
  763. return; // wxl2026-5-18
  764. uint32_t err_cnt = get_tdc_rw_err_cnt();
  765. if (alarm_status(ERR_TOF_SPI_RW))
  766. {
  767. // 已报警:错误清零则恢复
  768. if (err_cnt == 0)
  769. {
  770. Alarm(ERR_TOF_SPI_RW, 0, 0);
  771. }
  772. }
  773. else
  774. {
  775. // 未报警:错误数≥30 触发报警
  776. if (err_cnt >= 30)
  777. {
  778. Alarm(ERR_TOF_SPI_RW, 1, err_cnt);
  779. }
  780. }
  781. }
  782. /**
  783. * @brief EEPROM 读写错误监测
  784. * @rule 错误计数 ≥30 报警,错误计数=0 恢复
  785. */
  786. void eeprom_rw_err_monitor(void)
  787. {
  788. if (StartUp < 10)
  789. return; // wxl2026-5-18
  790. uint32_t err_cnt = get_eeprom_rw_err_cnt();
  791. if (alarm_status(ERR_MEMORY))
  792. {
  793. // 已报警:错误清零则恢复
  794. if (err_cnt == 0)
  795. {
  796. Alarm(ERR_MEMORY, 0, 0);
  797. }
  798. }
  799. else
  800. {
  801. // 未报警:错误数≥30 触发报警
  802. if (err_cnt >= 30)
  803. {
  804. Alarm(ERR_MEMORY, 1, err_cnt);
  805. }
  806. }
  807. }
  808. // void water_flow_monitor(void) {
  809. // bw_flow_monitor();
  810. // large_flow_monitor();
  811. // cnti_flow_monitor();
  812. // leak_flow_monitor();
  813. //
  814. // high_water_temp_monitor();
  815. // low_water_temp_monitor();
  816. // tdc_rw_err_monitor();
  817. // eeprom_rw_err_monitor();
  818. //}
  819. /**
  820. * @brief 监测入口(未使用)
  821. * @detail 统一调度所有流量、温度、通信故障监测
  822. */
  823. void water_flow_monitor(void)
  824. {
  825. // 1. 流量类监测
  826. bw_flow_monitor(); // 小流量/始动流量监测
  827. large_flow_monitor(); // 大流量持续监测
  828. cnti_flow_monitor(); // 持续流量异常监测
  829. leak_flow_monitor(); // 渗漏流量监测
  830. // 2. 温度类监测
  831. high_water_temp_monitor(); // 高温报警监测
  832. low_water_temp_monitor(); // 低温报警监测
  833. // 3. 硬件通信故障监测
  834. tdc_rw_err_monitor(); // TDC SPI 读写错误监测
  835. eeprom_rw_err_monitor(); // EEPROM 读写错误监测
  836. }
  837. // void high_pressure_monitor(void) {
  838. // static uint16_t high_press_up_cnt = 0;
  839. // static uint16_t high_press_down_cnt = 0;
  840. // Param_t *param;
  841. // GetParam(&param);
  842. // //add by yuewei 20260407
  843. // if (param->pressure_sensor != 1) //no pressure sensor or not set
  844. // {
  845. // return;
  846. // }
  847. // if (param->high_press_alarm_thres == 0) {
  848. // return;
  849. // }
  850. // uint16_t pressure = (uint16_t)(WaterPressure * 100); //unit: 0.01MPa
  851. // if (alarm_status(ERR_HIGH_PRESSURE)) {
  852. // if (pressure <= (param->high_press_alarm_thres - 2)) {
  853. // high_press_down_cnt++;
  854. // if (high_press_down_cnt >= 3) {
  855. // Alarm(ERR_HIGH_PRESSURE, 0, pressure);
  856. // high_press_down_cnt = 0;
  857. // }
  858. // } else {
  859. // high_press_down_cnt = 0;
  860. // }
  861. // } else {
  862. // if (pressure >= (param->high_press_alarm_thres + 2)) {
  863. // high_press_up_cnt++;
  864. // if (high_press_up_cnt >= 3) {
  865. // Alarm(ERR_HIGH_PRESSURE, 1, pressure);
  866. // high_press_up_cnt = 0;
  867. // }
  868. // } else {
  869. // high_press_up_cnt = 0;
  870. // }
  871. // }
  872. //}
  873. // void low_pressure_monitor(void) {
  874. // static uint16_t low_press_up_cnt = 0;
  875. // static uint16_t low_press_down_cnt = 0;
  876. // Param_t *param;
  877. // GetParam(&param);
  878. // //add by yuewei 20260407
  879. // if (param->pressure_sensor != 1) //no pressure sensor or not set
  880. // {
  881. // return;
  882. // }
  883. // if (param->low_press_alarm_thres == 0) {
  884. // return;
  885. // }
  886. // uint16_t pressure = (uint16_t)(WaterPressure * 100); //unit: 0.01MPa
  887. //
  888. // if (alarm_status(ERR_LOW_PRESSURE)) {
  889. // if (pressure >= (param->low_press_alarm_thres + 2)) {
  890. // low_press_up_cnt++;
  891. // if (low_press_up_cnt >= 3) {
  892. // Alarm(ERR_LOW_PRESSURE, 0, pressure);
  893. // low_press_up_cnt = 0;
  894. // }
  895. // } else {
  896. // low_press_up_cnt = 0;
  897. // }
  898. //
  899. // } else {
  900. // if (pressure <= (param->low_press_alarm_thres - 2)) {
  901. // low_press_down_cnt++;
  902. // if (low_press_down_cnt >= 3) {
  903. // Alarm(ERR_LOW_PRESSURE, 1, pressure);
  904. // low_press_down_cnt = 0;
  905. // }
  906. // } else {
  907. // low_press_down_cnt = 0;
  908. // }
  909. // }
  910. //}
  911. /**
  912. * @brief 高压报警监测(每1分钟调用1次)
  913. * 规则:压力连续3次 ≥ 阈值+2 → 报警
  914. * 报警后连续3次 ≤ 阈值-2 → 恢复
  915. * 仅当压力传感器使能时(param->pressure_sensor == 1)生效
  916. * 单位:0.01MPa
  917. */
  918. void high_pressure_monitor(void)
  919. {
  920. static uint16_t alarm_confirm_cnt = 0; // 报警连续确认计数
  921. static uint16_t recover_confirm_cnt = 0; // 恢复连续确认计数
  922. Param_t *param = NULL;
  923. uint16_t curr_pressure; // 当前压力
  924. GetParam(&param);
  925. if (param == NULL)
  926. return;
  927. // 未安装压力传感器 / 未使能
  928. if (param->pressure_sensor != 1)
  929. {
  930. alarm_confirm_cnt = 0;
  931. recover_confirm_cnt = 0;
  932. return;
  933. }
  934. // 阈值未配置
  935. if (param->high_press_alarm_thres == 0)
  936. {
  937. return;
  938. }
  939. // 计算当前压力 0.01MPa
  940. curr_pressure = (uint16_t)(WaterPressure * 100);
  941. // 已报警 → 判断恢复条件(连续3次 ≤ 阈值-2)
  942. if (alarm_status(ERR_HIGH_PRESSURE))
  943. {
  944. if (curr_pressure <= (param->high_press_alarm_thres - 2))
  945. {
  946. if (++recover_confirm_cnt >= 3)
  947. {
  948. Alarm(ERR_HIGH_PRESSURE, 0, curr_pressure);
  949. recover_confirm_cnt = 0;
  950. }
  951. }
  952. else
  953. {
  954. recover_confirm_cnt = 0;
  955. }
  956. }
  957. // 未报警 → 判断触发条件(连续3次 ≥ 阈值+2)
  958. else
  959. {
  960. if (curr_pressure >= (param->high_press_alarm_thres + 2))
  961. {
  962. if (++alarm_confirm_cnt >= 3)
  963. {
  964. Alarm(ERR_HIGH_PRESSURE, 1, curr_pressure);
  965. alarm_confirm_cnt = 0;
  966. }
  967. }
  968. else
  969. {
  970. alarm_confirm_cnt = 0;
  971. }
  972. }
  973. }
  974. /**
  975. * @brief 低压报警监测(每1分钟调用1次)
  976. * 规则:压力连续3次 ≤ 阈值-2 → 报警
  977. * 报警后连续3次 ≥ 阈值+2 → 恢复
  978. * 仅当压力传感器使能时(param->pressure_sensor == 1)生效
  979. * 单位:0.01MPa
  980. */
  981. void low_pressure_monitor(void)
  982. {
  983. static uint16_t alarm_confirm_cnt = 0; // 报警连续确认计数
  984. static uint16_t recover_confirm_cnt = 0; // 恢复连续确认计数
  985. Param_t *param = NULL;
  986. uint16_t curr_pressure; // 当前压力
  987. GetParam(&param);
  988. if (param == NULL)
  989. return;
  990. // 未安装压力传感器 / 未使能
  991. if (param->pressure_sensor != 1)
  992. {
  993. alarm_confirm_cnt = 0;
  994. recover_confirm_cnt = 0;
  995. return;
  996. }
  997. // 阈值未配置
  998. if (param->low_press_alarm_thres == 0)
  999. {
  1000. return;
  1001. }
  1002. // 计算当前压力 0.01MPa
  1003. curr_pressure = (uint16_t)(WaterPressure * 100);
  1004. // 已报警 → 判断恢复条件(连续3次 ≥ 阈值+2)
  1005. if (alarm_status(ERR_LOW_PRESSURE))
  1006. {
  1007. if (curr_pressure >= (param->low_press_alarm_thres + 2))
  1008. {
  1009. if (++recover_confirm_cnt >= 3)
  1010. {
  1011. Alarm(ERR_LOW_PRESSURE, 0, curr_pressure);
  1012. recover_confirm_cnt = 0;
  1013. }
  1014. }
  1015. else
  1016. {
  1017. recover_confirm_cnt = 0;
  1018. }
  1019. }
  1020. // 未报警 → 判断触发条件(连续3次 ≤ 阈值-2)
  1021. else
  1022. {
  1023. if (curr_pressure <= (param->low_press_alarm_thres - 2))
  1024. {
  1025. if (++alarm_confirm_cnt >= 3)
  1026. {
  1027. Alarm(ERR_LOW_PRESSURE, 1, curr_pressure);
  1028. alarm_confirm_cnt = 0;
  1029. }
  1030. }
  1031. else
  1032. {
  1033. alarm_confirm_cnt = 0;
  1034. }
  1035. }
  1036. }
  1037. // void pressure_monitor(void) {
  1038. // high_pressure_monitor();
  1039. // low_pressure_monitor();
  1040. // }
  1041. /**
  1042. * @brief 水压监测总入口(未使用)
  1043. * @detail 统一调度高压、低压报警监测
  1044. */
  1045. void pressure_monitor(void)
  1046. {
  1047. // 水压上下限监测
  1048. high_pressure_monitor(); // 高压报警监测
  1049. low_pressure_monitor(); // 低压报警监测
  1050. }