selfcheck.c 16 KB

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