selfcheck.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-04 Administrator the first version
  9. */
  10. /*
  11. * Copyright (c) 2006-2021, RT-Thread Development Team
  12. *
  13. * SPDX-License-Identifier: Apache-2.0
  14. *
  15. * Change Logs:
  16. * Date Author Notes
  17. * 2024-09-29 Administrator the first version
  18. */
  19. #include <rtthread.h>
  20. #include <cJSON.h>
  21. #include <control.h>
  22. #include "selfcheck.h"
  23. #include <hmi.h>
  24. #include <dtu_uart2.h>
  25. #include "beep.h"
  26. #define DBG_ENABLE
  27. #define DBG_COLOR
  28. #define DBG_TAG "selfcheck"
  29. #define DBG_LEVEL DBG_LOG
  30. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  31. rt_timer_t timer_takephoto = RT_NULL;
  32. Device_Status_typedef device_status;
  33. static int calculate_average(int arr[], int n) {
  34. int sum = 0; // 用于存储数组元素的总和
  35. for (int i = 0; i < n; i++) {
  36. sum += arr[i]; // 累加每个元素
  37. }
  38. // 计算平均值
  39. int average = sum / n; // 强制类型转换确保得到浮点数结果
  40. return average;
  41. }
  42. static void t_takephoto_callback(void *parameter)
  43. {
  44. device_status.C01 |= (1<<2);
  45. device_status.C01 |= (1<<3);
  46. HMI_WRITE("selfcheck.t3.bco=63488");
  47. HMI_WRITE("selfcheck.t4.bco=63488");
  48. HMI_WRITE("sysSet.va0.txt=\"拍照超时\"");
  49. LOG_E("t_takephoto_callback");
  50. }
  51. int selfcheck(uint8_t type)
  52. {
  53. uint16_t C01 = 0;
  54. int current_i[6];
  55. int current_,current_open = 0;
  56. //删除workloop线程 防止自动运行干扰
  57. LOG_I("删除线程workloop:%d",thread_delete_workloop());
  58. //初始化硬件
  59. HardWare_Init(0);
  60. /********************************************************/
  61. //读取5次电流值,取平均值
  62. for(int i = 0;i<5;i++)
  63. {
  64. current_i[i] = atoi(device_status.power);
  65. rt_thread_delay(1000);
  66. }
  67. current_ = calculate_average(current_i,5);
  68. if(current_<50||current_>3000)
  69. {
  70. device_status.C01 |= (1<<12);
  71. HMI_WRITE("selfcheck.t13.bco=63488");
  72. }
  73. else
  74. {
  75. HMI_WRITE("selfcheck.t13.bco=2024");
  76. device_status.C01 &= ~(1<<12);
  77. }
  78. if(device_status.bat_status==0)
  79. {
  80. HMI_WRITE("selfcheck.t5.bco=2024");
  81. device_status.C01 &= ~(1<<4);
  82. }
  83. else
  84. {
  85. HMI_WRITE("selfcheck.t5.bco=63488");
  86. device_status.C01 |= (1<<4);
  87. }
  88. if(device_status.rain_status)
  89. {
  90. HMI_WRITE("selfcheck.t6.bco=63488");
  91. device_status.C01 |= (1<<5);
  92. }
  93. else
  94. {
  95. device_status.C01 &= ~(1<<5);
  96. HMI_WRITE("selfcheck.t6.bco=2024");
  97. }
  98. if(device_status.temp_status)
  99. {
  100. HMI_WRITE("selfcheck.t8.bco=63488");
  101. device_status.C01 |= (1<<7);
  102. }
  103. else
  104. {
  105. device_status.C01 &= ~(1<<7);
  106. HMI_WRITE("selfcheck.t8.bco=2024");
  107. }
  108. if(device_status.sun_status)
  109. {
  110. HMI_WRITE("selfcheck.t7.bco=63488");
  111. device_status.C01 |= (1<<6);
  112. }
  113. else
  114. {
  115. device_status.C01 &= ~(1<<6);
  116. HMI_WRITE("selfcheck.t7.bco=2024");
  117. }
  118. rt_thread_delay(1000);
  119. /********************************************************/
  120. ctr_lamp(ON, 1); //打开镇流器
  121. rt_thread_delay(1000);
  122. for(int i = 0;i<5;i++)
  123. {
  124. current_i[i] = atoi(device_status.power);
  125. rt_thread_delay(1000);
  126. }
  127. ctr_lamp(OFF, 1); //关闭镇流器
  128. current_open = calculate_average(current_i,5);
  129. LOG_I("待机电流:%d",current_);
  130. LOG_I("镇流器开启电流:%d",current_open);
  131. if(current_open-current_>100)
  132. {
  133. HMI_WRITE("selfcheck.t1.bco=2024");
  134. device_status.C01 &= ~(1<<0);
  135. HMI_WRITE("sysSet.va0.txt=\"灯管正常\"");
  136. }
  137. else
  138. {
  139. HMI_WRITE("selfcheck.t1.bco=63488");
  140. device_status.C01 |= (1<<0);
  141. HMI_WRITE("sysSet.va0.txt=\"灯管异常\"");
  142. }
  143. rt_thread_delay(1000);
  144. /********************************************************/
  145. //打开上仓门
  146. ctr_cangmen_up(OPEN);
  147. for(int i = 0;i<5;i++)
  148. {
  149. current_i[i] = atoi(device_status.power);
  150. rt_thread_delay(1000);
  151. }
  152. ctr_cangmen_up(OFF);
  153. current_open = calculate_average(current_i,5);
  154. if(current_open-current_>100)
  155. {
  156. HMI_WRITE("selfcheck.t9.bco=2024");
  157. device_status.C01 &= ~(1<<8);
  158. HMI_WRITE("sysSet.va0.txt=\"上仓门正常\"");
  159. }
  160. else{
  161. HMI_WRITE("selfcheck.t9.bco=63488");
  162. device_status.C01 |= (1<<8);
  163. HMI_WRITE("sysSet.va0.txt=\"上仓门故障\"");
  164. }
  165. rt_thread_delay(1000);
  166. /********************************************************/
  167. //打开下仓门
  168. ctr_cangmen_down(OPEN);
  169. for(int i = 0;i<5;i++)
  170. {
  171. current_i[i] = atoi(device_status.power);
  172. rt_thread_delay(1000);
  173. }
  174. ctr_cangmen_down(OFF);
  175. current_open = calculate_average(current_i,5);
  176. if(current_open-current_>100)
  177. {
  178. HMI_WRITE("selfcheck.t10.bco=2024");
  179. device_status.C01 &= ~(1<<9);
  180. HMI_WRITE("sysSet.va0.txt=\"下仓门正常\"");
  181. }
  182. else{
  183. HMI_WRITE("selfcheck.t10.bco=63488");
  184. device_status.C01 |= (1<<9);
  185. HMI_WRITE("sysSet.va0.txt=\"下仓门故障\"");
  186. }
  187. rt_thread_delay(1000);
  188. /********************************************************/
  189. //记录当前电压、温度探头温度
  190. float voltage_off = atof(device_status.voltage);
  191. uint8_t heat_temp_off = device_status.heattem;
  192. HMI_WRITE("sysSet.va0.txt=\"当前电压:%s-温度:%d\"",device_status.voltage,heat_temp_off);
  193. //开启加热
  194. rt_thread_delay(1000);
  195. /* 注意:原实现用“电压是否在50~150之间”判断加热是否已工作,该条件恒不成立(死代码)。
  196. * 这里改为始终执行加热测试:加热60s后,温度上升≥2℃ 或 电压跌落≥1.5V 视为加热回路正常。 */
  197. ctr_Heat(ON);
  198. //持续1分钟
  199. for(int i = 60;i>0;i--)
  200. {
  201. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  202. rt_thread_delay(1000);
  203. }
  204. LOG_I("当前温度:%d电压:%s",device_status.heatvalue,device_status.voltage);
  205. //比较当前电压、温度探头温度和开启前(注意使用有符号比较,避免无符号回绕误判为正常)
  206. if(((int)device_status.heattem - (int)heat_temp_off)>=2 || (atof(device_status.voltage) - voltage_off)>=1.5)
  207. {
  208. HMI_WRITE("selfcheck.t2.bco=2024");
  209. device_status.C01 &= ~(1<<1);
  210. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  211. }
  212. else
  213. {
  214. HMI_WRITE("selfcheck.t2.bco=63488");
  215. device_status.C01 |= (1<<1);
  216. HMI_WRITE("sysSet.va0.txt=\"加热故障\"");
  217. }
  218. ctr_Heat(OFF);
  219. rt_thread_delay(1000);
  220. /*********************************************************/
  221. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  222. rt_timer_start(timer_takephoto);
  223. HK_TakePhoto();
  224. for(int i = 20;i>0;i--)
  225. {
  226. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  227. rt_thread_delay(1000);
  228. }
  229. rt_thread_delay(1000);
  230. /*******************************************************/
  231. /* 清扫测试:自检开始时清扫线程已被删除(device_status.clear_status 无人消费),
  232. * 原来仅置位 clear_status 并不会真正执行清扫。这里在自检流程内直接驱动
  233. * 清扫电机,并在运行过程中采样电流,与待机电流比较判定。 */
  234. HMI_WRITE("sysSet.va0.txt=\"清扫测试中...\"");
  235. #if PWM_EN == 0
  236. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  237. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  238. rt_pin_write(CLEAR_R, ON);
  239. rt_pin_write(CLEAR_L, OFF);
  240. #else
  241. Motor_Qingsao_R_PWM(ON);
  242. Motor_Qingsao_L_PWM(OFF);
  243. #endif
  244. rt_thread_delay(2000);
  245. for(int i = 0;i<5;i++)
  246. {
  247. current_i[i] = atoi(device_status.power);
  248. rt_thread_delay(1000);
  249. }
  250. current_open = calculate_average(current_i,5);
  251. #if PWM_EN == 0
  252. rt_pin_write(CLEAR_R, ON);
  253. rt_pin_write(CLEAR_L, ON);
  254. #else
  255. Motor_Qingsao_R_PWM(OFF);
  256. Motor_Qingsao_L_PWM(OFF);
  257. #endif
  258. rt_thread_delay(1000);
  259. /* 与待机电流比较(判据与灯管/仓门一致) */
  260. if(current_open-current_>150)
  261. {
  262. HMI_WRITE("selfcheck.t11.bco=2024");
  263. device_status.C01 &= ~(1<<10);
  264. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  265. }
  266. else
  267. {
  268. HMI_WRITE("selfcheck.t11.bco=63488");
  269. device_status.C01 |= (1<<10);
  270. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  271. }
  272. device_status.clear_status = 0; /* 自检自行完成清扫,清除标志防止残留触发 */
  273. /*******************************************************/
  274. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  275. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  276. DTU_Write("%s",js);
  277. free(js);
  278. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  279. device_status.selfcheck_ws = 0;
  280. device_status.process = 0.0;
  281. }
  282. int rt_software_timer_init(void)
  283. {
  284. //启动拍照超时定时器
  285. /* 必须使用软件定时器:回调中会调用 HMI_WRITE/LOG 等阻塞接口,不能运行在中断上下文 */
  286. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 100000,
  287. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  288. if(timer_takephoto==RT_NULL)
  289. {
  290. LOG_E("rt_timer_create t_takephoto failed!");
  291. return RT_ERROR;
  292. }
  293. else
  294. {
  295. return RT_EOK;
  296. }
  297. }
  298. INIT_APP_EXPORT(rt_software_timer_init);
  299. /* ==================== 串口自环回自检(自动化测试用) ====================
  300. * 前置:目标串口 TX-RX 短接(自环回)。
  301. * 用途:一次调用即可验证 驱动→DMA接收→IDLE中断→FIFO→读 的完整收发链路,
  302. * 排除应用层线程/消息队列的干扰,用于区分"接线问题"与"固件接收路径问题"。
  303. * 用法:uart_loop_test <2|4> [len] len 默认 64,最大 2048(uart4 限制 512)
  304. * 说明:测试期间临时接管该串口的接收回调(close→open→换回调),结束后恢复应用侧
  305. * (uart2: dtu_uart2_reinit_rx / uart4: hmi_uart4_reinit_rx)。
  306. */
  307. #define LP_MAX_LEN 2048
  308. static rt_uint8_t lp_tx[LP_MAX_LEN];
  309. static rt_uint8_t lp_rx[LP_MAX_LEN + 64];
  310. static struct rt_semaphore lp_sem;
  311. static rt_bool_t lp_sem_ready = RT_FALSE;
  312. static rt_err_t lp_rx_ind(rt_device_t dev, rt_size_t size)
  313. {
  314. rt_sem_release(&lp_sem);
  315. return RT_EOK;
  316. }
  317. /* 恢复应用侧接收回调。
  318. * 注意:这里刻意【不做 close/open】。本驱动的 close/open 会调 HAL_UART_Receive_DMA
  319. * 重新武装接收,一旦失败(例如 HAL 的 RxState 未复位导致返回 HAL_BUSY),
  320. * 该串口的接收链路就会永久失效直到重启。仅重新注册 rx_indicate 即可达到
  321. * "把接收权交还应用"的目的,对硬件状态零扰动。 */
  322. static void lp_resume_app(rt_device_t dev)
  323. {
  324. if (dev == dtuserial)
  325. {
  326. dtu_uart2_reinit_rx();
  327. }
  328. else
  329. {
  330. hmi_uart4_reinit_rx();
  331. }
  332. }
  333. static void uart_loop_test(int argc, char **argv)
  334. {
  335. rt_device_t dev;
  336. const char *port;
  337. int len = 64;
  338. int i, ok = 0, hdr_ok = 0;
  339. rt_uint32_t got = 0, t0, timeout_ms;
  340. char drain[64];
  341. if (argc < 2)
  342. {
  343. rt_kprintf("usage: uart_loop_test <2|4> [len<=%d]\n", LP_MAX_LEN);
  344. return;
  345. }
  346. if (argv[1][0] == '2') { dev = dtuserial; port = "uart2"; }
  347. else if (argv[1][0] == '4') { dev = serial; port = "uart4"; }
  348. else
  349. {
  350. rt_kprintf("[LP] invalid port(arg=%s)\n", argv[1]);
  351. return;
  352. }
  353. if (dev == RT_NULL)
  354. {
  355. rt_kprintf("[LP %s] device handle is NULL\n", port);
  356. return;
  357. }
  358. if (argc >= 3)
  359. {
  360. len = atoi(argv[2]);
  361. }
  362. if (len < 4) len = 4;
  363. if (len > LP_MAX_LEN) len = LP_MAX_LEN;
  364. if (dev == serial && len > 512)
  365. {
  366. len = 512; /* uart4 接收缓冲 512B,避免回绕丢数据 */
  367. rt_kprintf("[LP uart4] len clamped to 512\n");
  368. }
  369. if (!lp_sem_ready)
  370. {
  371. rt_sem_init(&lp_sem, "lp_sem", 0, RT_IPC_FLAG_FIFO);
  372. lp_sem_ready = RT_TRUE;
  373. }
  374. /* 图案:55 AA + 长度 + 递增异或值,便于在被其它串口流量混入时定位 */
  375. rt_memset(lp_tx, 0, sizeof(lp_tx));
  376. for (i = 4; i < len; i++)
  377. {
  378. lp_tx[i] = (rt_uint8_t)(0xA5 ^ i);
  379. }
  380. lp_tx[0] = 0x55;
  381. lp_tx[1] = 0xAA;
  382. lp_tx[2] = (rt_uint8_t)(len & 0xFF);
  383. lp_tx[3] = (rt_uint8_t)((len >> 8) & 0xFF);
  384. rt_memset(lp_rx, 0, sizeof(lp_rx));
  385. /* ---- 接管端口(只换回调,不做 close/open)----
  386. * 只替换 rx_indicate:应用接收线程收不到信号量就不会来抢数据。
  387. * 应用线程此前已把 FIFO 读空并阻塞在信号量上,我们保留这段等待以确保它进入阻塞。
  388. * 刻意避开 close/open:该路径会重新武装 DMA,历史上曾因 HAL RxState 未复位
  389. * 而静默失败,导致该串口接收永久失效(详见 lp_resume_app 注释)。 */
  390. rt_device_set_rx_indicate(dev, lp_rx_ind);
  391. rt_thread_mdelay(120);
  392. /* 清掉在途残留,避免把上次的尾巴当成回环数据 */
  393. while (rt_device_read(dev, 0, drain, sizeof(drain)) > 0) { }
  394. /* ---- 发送 ---- */
  395. t0 = rt_tick_get();
  396. if (rt_device_write(dev, 0, lp_tx, len) != (rt_size_t)len)
  397. {
  398. rt_kprintf("[LP %s] FAIL: write error\n", port);
  399. lp_resume_app(dev);
  400. return;
  401. }
  402. /* 读回:超时 = 10bit/字节@115200 + 200ms 余量 */
  403. timeout_ms = (rt_uint32_t)(((rt_uint32_t)len * 10UL * 1000UL) / 115200UL + 200UL);
  404. while (got < (rt_uint32_t)len && (rt_tick_get() - t0) < timeout_ms)
  405. {
  406. rt_size_t n = rt_device_read(dev, 0, lp_rx + got, (rt_size_t)(len - (int)got));
  407. if (n > 0)
  408. {
  409. got += n;
  410. continue;
  411. }
  412. rt_sem_take(&lp_sem, 20);
  413. }
  414. /* ---- 校验:允许混入其它串口流量,按图案子串匹配 ---- */
  415. for (i = 0; i + len <= (int)got; i++)
  416. {
  417. if (rt_memcmp(lp_rx + i, lp_tx, len) == 0)
  418. {
  419. ok = 1;
  420. break;
  421. }
  422. }
  423. /* 次判据:整帧未匹配上时,若能找到 4 字节报文头(55 AA len_lo len_hi),
  424. * 说明收发链路是通的,只是被应用线程/其它流量抢走了部分字节。 */
  425. if (!ok && got >= 4)
  426. {
  427. for (i = 0; i + 4 <= (int)got; i++)
  428. {
  429. if (rt_memcmp(lp_rx + i, lp_tx, 4) == 0)
  430. {
  431. hdr_ok = 1;
  432. break;
  433. }
  434. }
  435. }
  436. rt_kprintf("[LP %s] len=%d recv=%d t=%dms %s\n", port, len, (int)got,
  437. (int)(rt_tick_get() - t0),
  438. ok ? "PASS" : (hdr_ok ? "PASS(仅报文头)" : "FAIL"));
  439. if (!ok && hdr_ok)
  440. {
  441. rt_kprintf("[LP %s] 找到报文头但整帧不全:链路通,部分字节被应用线程取走\n", port);
  442. }
  443. if (!ok && !hdr_ok)
  444. {
  445. if (got > 0)
  446. {
  447. rt_kprintf("[LP %s] head:", port);
  448. for (i = 0; i < ((int)got < 16 ? (int)got : 16); i++)
  449. {
  450. rt_kprintf(" %02x", lp_rx[i]);
  451. }
  452. rt_kprintf("\n[LP %s] 收到数据但与图案不符(可能有其它流量混入或丢字节)\n", port);
  453. }
  454. else
  455. {
  456. rt_kprintf("[LP %s] 未收到任何数据:请检查 TX-RX 是否短接、DMA/中断配置\n", port);
  457. }
  458. }
  459. /* 清空测试残留:否则这些图案字节会被应用解析器当成非法报文 */
  460. while (rt_device_read(dev, 0, drain, sizeof(drain)) > 0) { }
  461. lp_resume_app(dev);
  462. }
  463. MSH_CMD_EXPORT(uart_loop_test, uart_loop_test <2|4> [len] : UART TX-RX loopback self test);