selfcheck.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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-09-29 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include <cJSON.h>
  12. #include <control.h>
  13. #include "selfcheck.h"
  14. #include <hmi.h>
  15. #include <dtu_uart2.h>
  16. #include <sht30_collect.h>
  17. #define DBG_ENABLE
  18. #define DBG_COLOR
  19. #define DBG_TAG "selfcheck"
  20. #define DBG_LEVEL DBG_LOG
  21. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  22. rt_timer_t timer_takephoto = RT_NULL;
  23. /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */
  24. static int calculate_average(int arr[], int n) {
  25. int sum = 0; // 用于存储数组元素的总和
  26. for (int i = 0; i < n; i++) {
  27. sum += arr[i]; // 累加每个元素
  28. }
  29. // 计算平均值
  30. int average = sum / n; // 强制类型转换确保得到浮点数结果
  31. return average;
  32. }
  33. /* ★ 拍照超时回调。
  34. 定时器在 selfcheck_init() 里以 RT_TIMER_FLAG_SOFT_TIMER 创建(原来是 HARD_TIMER),
  35. 因为本回调会调 HMI_WRITE(), 而 HMI_WRITE() 内部有 rt_thread_mdelay(1)
  36. —— HARD_TIMER 的回调是在 SysTick 中断里执行的, 在 ISR 里挂起线程会触发
  37. RT-Thread 调度器断言 / 直接锁死, 表现就是自检拍照阶段反复重启。
  38. 改成 SOFT_TIMER 后回调运行在 timer 线程上下文, 可以安全调用线程 API。 */
  39. static void t_takephoto_callback(void *parameter)
  40. {
  41. // rt_timer_delete(timer_takephoto);
  42. LOG_E("拍照超时!");
  43. device_status.C01 |= (1<<2); //拍照故障
  44. device_status.C01 |= (1<<3); //补光灯故障
  45. HMI_WRITE("selfcheck.t4.bco=63488");
  46. HMI_WRITE("selfcheck.t3.bco=63488");
  47. HMI_WRITE("sysSet.va0.txt=\"拍照超时\"");
  48. }
  49. int selfcheck(uint8_t type)
  50. {
  51. int current_i[6];
  52. int current_,current_open = 0;
  53. //删除workloop线程 防止自动运行干扰
  54. LOG_I("删除线程workloop:%d",thread_delete_workloop());
  55. //关闭上仓门
  56. ctr_cangmen_down(CLOSE);
  57. //关闭下仓门
  58. ctr_cangmen_up(CLOSE);
  59. for(int c=0;c<8;c++)
  60. {
  61. rt_thread_delay(2000);
  62. HMI_WRITE("sysSet.va0.txt=\"等待上下仓门复位完成......\"");
  63. LOG_W("等待上下仓门复位完成.....%d",c);
  64. }
  65. ctr_cangmen_up(OFF);
  66. ctr_cangmen_down(OFF);
  67. /********************************************************/
  68. //读取5次电流值,取平均值
  69. for(int i = 0;i<5;i++)
  70. {
  71. current_i[i] = atoi(device_status.power);
  72. rt_thread_delay(1000);
  73. }
  74. current_ = calculate_average(current_i,5);
  75. if(current_<50||current_>3000)
  76. {
  77. device_status.C01 |= (1<<12);
  78. HMI_WRITE("selfcheck.t13.bco=63488");
  79. }
  80. else
  81. {
  82. HMI_WRITE("selfcheck.t13.bco=2024");
  83. device_status.C01 &= ~(1<<12);
  84. }
  85. HMI_WRITE("sysSet.va0.txt=\"待机电流:%d\"",current_);
  86. LOG_W("待机电流.....%d",current_);
  87. if(type==1)
  88. {
  89. char *readinfo = makeJson_ReadInfo();
  90. DTU_Write("%s",readinfo);
  91. rt_free(readinfo);
  92. rt_thread_delay(1000);
  93. if(atoi(device_status.gps)==0)
  94. {
  95. HMI_WRITE("selfcheck.t12.bco=63488");
  96. device_status.C01 |= (1<<11);
  97. HMI_WRITE("sysSet.va0.txt=\"GPS无定位\"");
  98. }
  99. else {
  100. HMI_WRITE("selfcheck.t12.bco=2024");
  101. device_status.C01 &= ~(1<<11);
  102. }
  103. rt_thread_delay(1000);
  104. if(Read_Ntc_Voltage()==0)
  105. {
  106. HMI_WRITE("selfcheck.t15.bco=63488");
  107. }
  108. else{
  109. HMI_WRITE("selfcheck.t15.bco=2024");
  110. }
  111. rt_thread_delay(1000);
  112. if(device_status.bat_status==0)
  113. {
  114. HMI_WRITE("selfcheck.t5.bco=2024");
  115. device_status.C01 &= ~(1<<4);
  116. }
  117. else
  118. {
  119. HMI_WRITE("selfcheck.t5.bco=63488");
  120. device_status.C01 |= (1<<4);
  121. }
  122. rt_thread_delay(1000);
  123. if(Read_Rain_Voltage()==0)
  124. {
  125. HMI_WRITE("selfcheck.t6.bco=63488");
  126. device_status.C01 |= (1<<5);
  127. }
  128. else
  129. {
  130. device_status.C01 &= ~(1<<5);
  131. HMI_WRITE("selfcheck.t6.bco=2024");
  132. }
  133. rt_thread_delay(1000);
  134. if(device_status.temp_status)
  135. {
  136. HMI_WRITE("selfcheck.t8.bco=63488");
  137. device_status.C01 |= (1<<7);
  138. }
  139. else
  140. {
  141. device_status.C01 &= ~(1<<7);
  142. HMI_WRITE("selfcheck.t8.bco=2024");
  143. }
  144. int sensor_light_value = 0;
  145. for(int i = 0;i<3;i++)
  146. {
  147. sensor_light_value += Read_Light_Voltage();
  148. rt_thread_delay(500);
  149. }
  150. if(sensor_light_value<50)
  151. {
  152. HMI_WRITE("selfcheck.t7.bco=63488");
  153. device_status.C01 |= (1<<6);
  154. }
  155. else
  156. {
  157. device_status.C01 &= ~(1<<6);
  158. HMI_WRITE("selfcheck.t7.bco=2024");
  159. }
  160. HMI_WRITE("sysSet.va0.txt=\"光控传感器采样数值:%d\"",Read_Light_Voltage());
  161. rt_thread_delay(1000);
  162. }
  163. /********************************************************/
  164. ctr_lamp(ON, 1); //打开镇流器
  165. rt_thread_delay(1000);
  166. for(int i = 0;i<5;i++)
  167. {
  168. current_i[i] = atoi(device_status.power);
  169. rt_thread_delay(1000);
  170. }
  171. ctr_lamp(OFF, 1); //关闭镇流器
  172. current_open = calculate_average(current_i,5);
  173. LOG_W("待机电流:%d",current_);
  174. LOG_W("镇流器开启电流:%d",current_open);
  175. if(current_open-current_>100)
  176. {
  177. HMI_WRITE("selfcheck.t1.bco=2024");
  178. device_status.C01 &= ~(1<<0);
  179. HMI_WRITE("sysSet.va0.txt=\"灯管正常:%d\"",current_open);
  180. }
  181. else
  182. {
  183. HMI_WRITE("selfcheck.t1.bco=63488");
  184. device_status.C01 |= (1<<0);
  185. HMI_WRITE("sysSet.va0.txt=\"灯管异常:%d\"",current_open);
  186. }
  187. rt_thread_delay(1000);
  188. /********************************************************/
  189. //打开上仓门
  190. ctr_cangmen_up(OPEN);
  191. rt_thread_delay(1000);
  192. for(int i = 0;i<5;i++)
  193. {
  194. current_i[i] = atoi(device_status.power);
  195. rt_thread_delay(1000);
  196. }
  197. ctr_cangmen_up(OFF);
  198. rt_thread_delay(1000);
  199. current_open = calculate_average(current_i,5);
  200. LOG_W("上仓门action电流:%d",current_open);
  201. if(current_open-current_>100)
  202. {
  203. HMI_WRITE("selfcheck.t9.bco=2024");
  204. device_status.C01 &= ~(1<<8);
  205. HMI_WRITE("sysSet.va0.txt=\"上仓门正常:%d\"",current_open);
  206. }
  207. else{
  208. HMI_WRITE("selfcheck.t9.bco=63488");
  209. device_status.C01 |= (1<<8);
  210. HMI_WRITE("sysSet.va0.txt=\"上仓门故障:%d\"",current_open);
  211. }
  212. rt_thread_delay(1000);
  213. /********************************************************/
  214. //打开下仓门
  215. ctr_cangmen_down(OPEN);
  216. rt_thread_delay(1000);
  217. for(int i = 0;i<5;i++)
  218. {
  219. current_i[i] = atoi(device_status.power);
  220. rt_thread_delay(1000);
  221. }
  222. ctr_cangmen_down(OFF);
  223. rt_thread_delay(1000);
  224. current_open = calculate_average(current_i,5);
  225. LOG_W("下仓门action电流:%d",current_open);
  226. if(current_open-current_>100)
  227. {
  228. HMI_WRITE("selfcheck.t10.bco=2024");
  229. device_status.C01 &= ~(1<<9);
  230. HMI_WRITE("sysSet.va0.txt=\"下仓门正常:%d\"",current_open);
  231. }
  232. else{
  233. HMI_WRITE("selfcheck.t10.bco=63488");
  234. device_status.C01 |= (1<<9);
  235. HMI_WRITE("sysSet.va0.txt=\"下仓门故障:%d\"",current_open);
  236. }
  237. //开启加热
  238. rt_thread_delay(1000);
  239. if(type==1)
  240. {
  241. //
  242. if(atoi(device_status.heat_power)>5000)
  243. {
  244. HMI_WRITE("selfcheck.t2.bco=2024");
  245. device_status.C01 &= ~(1<<1);
  246. HMI_WRITE("sysSet.va0.txt=\"加热正常:%d\"",atoi(device_status.heat_power));
  247. }
  248. else {
  249. ctr_Heat(ON);
  250. //持续5S钟
  251. for(int i = 5;i>0;i--)
  252. {
  253. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  254. rt_thread_delay(1000);
  255. }
  256. LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power);
  257. //比较当前电压、温度探头温度和开启前
  258. if(atoi(device_status.heat_power)>5000)
  259. {
  260. HMI_WRITE("selfcheck.t2.bco=2024");
  261. device_status.C01 &= ~(1<<1);
  262. HMI_WRITE("sysSet.va0.txt=\"加热正常:%d\"",atoi(device_status.heat_power));
  263. }
  264. else
  265. {
  266. HMI_WRITE("selfcheck.t2.bco=63488");
  267. device_status.C01 |= (1<<1);
  268. HMI_WRITE("sysSet.va0.txt=\"加热故障:%d\"",atoi(device_status.heat_power));
  269. }
  270. }
  271. ctr_Heat(OFF);
  272. rt_thread_delay(1000);
  273. /*********************************************************/
  274. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  275. if(device_status.camsta==0)
  276. {
  277. rt_timer_start(timer_takephoto);
  278. HK_TakePhoto();
  279. for(int i = 15;i>0;i--)
  280. {
  281. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  282. rt_thread_delay(1000);
  283. }
  284. }else {
  285. HMI_WRITE("sysSet.va0.txt=\"相机未连接\"");
  286. HMI_WRITE("selfcheck.t4.bco=63488");
  287. HMI_WRITE("selfcheck.t3.bco=63488");
  288. }
  289. rt_thread_delay(1000);
  290. }
  291. /*******************************************************/
  292. //清扫
  293. device_status.clear_status = 1;
  294. rt_thread_delay(2000);
  295. for(int i = 0;i<5;i++)
  296. {
  297. current_i[i] = atoi(device_status.power);
  298. rt_thread_delay(1000);
  299. }
  300. current_open = calculate_average(current_i,5);
  301. LOG_W("清扫action电流:%d@%d",current_open,current_);
  302. rt_thread_delay(1000);
  303. if(current_open-current_>=100)
  304. {
  305. HMI_WRITE("selfcheck.t11.bco=2024");
  306. device_status.C01 &= ~(1<<10);
  307. HMI_WRITE("sysSet.va0.txt=\"清扫正常:%d\"",atoi(device_status.power));
  308. }
  309. else
  310. {
  311. HMI_WRITE("selfcheck.t11.bco=63488");
  312. device_status.C01 |= (1<<10);
  313. HMI_WRITE("sysSet.va0.txt=\"清扫故障:%d\"",atoi(device_status.power));
  314. }
  315. rt_thread_delay(1000);
  316. /*******************************************************/
  317. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  318. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  319. if(js != NULL)
  320. {
  321. DTU_Write("%s",js);
  322. /* ★ makeJson_* 返回 cJSON_Print 的缓冲, __RTTHREAD__ 下由 rt_malloc 分配,
  323. 原来用 newlib 的 free() 释放: 两套堆不一致, 会把 heap 链表写坏。 */
  324. rt_free(js);
  325. }
  326. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  327. device_status.selfcheck_ws = 0;
  328. device_status.process = 0.0;
  329. }
  330. int rt_software_timer_init(void)
  331. {
  332. //启动拍照超时定时器
  333. /* ★ 必须是 SOFT_TIMER: 回调里调 HMI_WRITE() -> rt_thread_mdelay(), ISR 里不能挂起线程 */
  334. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 100000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER);
  335. if(timer_takephoto==RT_NULL)
  336. {
  337. LOG_E("rt_timer_create t_takephoto failed!");
  338. return RT_ERROR;
  339. }
  340. else
  341. {
  342. return RT_EOK;
  343. }
  344. }
  345. INIT_APP_EXPORT(rt_software_timer_init);