selfcheck.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <beep.h>
  18. #define DBG_ENABLE
  19. #define DBG_COLOR
  20. #define DBG_TAG "selfcheck"
  21. #define DBG_LEVEL DBG_LOG
  22. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  23. rt_timer_t timer_takephoto = RT_NULL;
  24. Device_Status_typedef device_status;
  25. static int calculate_average(int arr[], int n) {
  26. int sum = 0; // 用于存储数组元素的总和
  27. for (int i = 0; i < n; i++) {
  28. sum += arr[i]; // 累加每个元素
  29. }
  30. // 计算平均值
  31. int average = sum / n; // 强制类型转换确保得到浮点数结果
  32. return average;
  33. }
  34. static void t_takephoto_callback(void *parameter)
  35. {
  36. /* 景深融合模式下,触发拍照指令 */
  37. if (depth_fusion_is_active()) {
  38. depth_fusion_on_capture_ready();
  39. return;
  40. }
  41. device_status.autotakepic = 0;
  42. LOG_E("t_takephoto_callback");
  43. //关闭补光灯
  44. ctr_led(OFF);
  45. //关闭滑台驱动
  46. slidelock();
  47. Beep_Tip(5000);
  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. HardWare_Init(0);
  57. /********************************************************/
  58. //读取5次电流值,取平均值
  59. for(int i = 0;i<5;i++)
  60. {
  61. current_i[i] = atoi(device_status.power);
  62. rt_thread_delay(1000);
  63. }
  64. current_ = calculate_average(current_i,5);
  65. if(current_<50||current_>3000)
  66. {
  67. device_status.C01 |= (1<<12);
  68. HMI_WRITE("selfcheck.t13.bco=63488");
  69. }
  70. else
  71. {
  72. HMI_WRITE("selfcheck.t13.bco=2024");
  73. device_status.C01 &= ~(1<<12);
  74. }
  75. if(atoi(device_status.gps_state)==0)
  76. {
  77. HMI_WRITE("selfcheck.t12.bco=63488");
  78. device_status.C01 |= (1<<11);
  79. HMI_WRITE("sysSet.va0.txt=\"GPS无定位\"");
  80. }
  81. else {
  82. HMI_WRITE("selfcheck.t12.bco=2024");
  83. device_status.C01 &= ~(1<<11);
  84. }
  85. if(Read_Ntc_Voltage()==0)
  86. {
  87. HMI_WRITE("selfcheck.t15.bco=63488");
  88. }
  89. else{
  90. HMI_WRITE("selfcheck.t15.bco=2024");
  91. }
  92. if(device_status.bat_status==0)
  93. {
  94. HMI_WRITE("selfcheck.t5.bco=2024");
  95. device_status.C01 &= ~(1<<4);
  96. }
  97. else
  98. {
  99. HMI_WRITE("selfcheck.t5.bco=63488");
  100. device_status.C01 |= (1<<4);
  101. }
  102. if(device_status.rain_status)
  103. {
  104. HMI_WRITE("selfcheck.t6.bco=63488");
  105. device_status.C01 |= (1<<5);
  106. }
  107. else
  108. {
  109. device_status.C01 &= ~(1<<5);
  110. HMI_WRITE("selfcheck.t6.bco=2024");
  111. }
  112. if(device_status.temp_status)
  113. {
  114. HMI_WRITE("selfcheck.t8.bco=63488");
  115. device_status.C01 |= (1<<7);
  116. }
  117. else
  118. {
  119. device_status.C01 &= ~(1<<7);
  120. HMI_WRITE("selfcheck.t8.bco=2024");
  121. }
  122. if(device_status.sun_status)
  123. {
  124. HMI_WRITE("selfcheck.t7.bco=63488");
  125. device_status.C01 |= (1<<6);
  126. }
  127. else
  128. {
  129. device_status.C01 &= ~(1<<6);
  130. HMI_WRITE("selfcheck.t7.bco=2024");
  131. }
  132. rt_thread_delay(1000);
  133. /********************************************************/
  134. ctr_lamp(ON, 1); //打开镇流器
  135. rt_thread_delay(1000);
  136. for(int i = 0;i<5;i++)
  137. {
  138. current_i[i] = atoi(device_status.power);
  139. rt_thread_delay(1000);
  140. }
  141. ctr_lamp(OFF, 1); //关闭镇流器
  142. current_open = calculate_average(current_i,5);
  143. LOG_I("待机电流:%d",current_);
  144. LOG_I("镇流器开启电流:%d",current_open);
  145. if(current_open-current_>100)
  146. {
  147. HMI_WRITE("selfcheck.t1.bco=2024");
  148. device_status.C01 &= ~(1<<0);
  149. HMI_WRITE("sysSet.va0.txt=\"灯管正常\"");
  150. }
  151. else
  152. {
  153. HMI_WRITE("selfcheck.t1.bco=63488");
  154. device_status.C01 |= (1<<0);
  155. HMI_WRITE("sysSet.va0.txt=\"灯管异常\"");
  156. }
  157. rt_thread_delay(1000);
  158. /********************************************************/
  159. //打开上仓门
  160. ctr_cangmen_up(OPEN);
  161. for(int i = 0;i<5;i++)
  162. {
  163. current_i[i] = atoi(device_status.power);
  164. rt_thread_delay(1000);
  165. }
  166. ctr_cangmen_up(OFF);
  167. current_open = calculate_average(current_i,5);
  168. if(current_open-current_>100)
  169. {
  170. HMI_WRITE("selfcheck.t9.bco=2024");
  171. device_status.C01 &= ~(1<<8);
  172. HMI_WRITE("sysSet.va0.txt=\"上仓门正常\"");
  173. }
  174. else{
  175. HMI_WRITE("selfcheck.t9.bco=63488");
  176. device_status.C01 |= (1<<8);
  177. HMI_WRITE("sysSet.va0.txt=\"上仓门故障\"");
  178. }
  179. rt_thread_delay(1000);
  180. /********************************************************/
  181. //打开下仓门
  182. ctr_cangmen_down(OPEN);
  183. for(int i = 0;i<5;i++)
  184. {
  185. current_i[i] = atoi(device_status.power);
  186. rt_thread_delay(1000);
  187. }
  188. ctr_cangmen_down(OFF);
  189. current_open = calculate_average(current_i,5);
  190. if(current_open-current_>100)
  191. {
  192. HMI_WRITE("selfcheck.t10.bco=2024");
  193. device_status.C01 &= ~(1<<9);
  194. HMI_WRITE("sysSet.va0.txt=\"下仓门正常\"");
  195. }
  196. else{
  197. HMI_WRITE("selfcheck.t10.bco=63488");
  198. device_status.C01 |= (1<<9);
  199. HMI_WRITE("sysSet.va0.txt=\"下仓门故障\"");
  200. }
  201. //开启加热
  202. rt_thread_delay(1000);
  203. //
  204. if(atoi(device_status.heat_power)>5000)
  205. {
  206. HMI_WRITE("selfcheck.t2.bco=2024");
  207. device_status.C01 &= ~(1<<1);
  208. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  209. }
  210. else {
  211. ctr_Heat(ON);
  212. //持续5S钟
  213. for(int i = 5;i>0;i--)
  214. {
  215. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  216. rt_thread_delay(1000);
  217. }
  218. LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power);
  219. //比较当前电压、温度探头温度和开启前
  220. if(atoi(device_status.heat_power)>5000)
  221. {
  222. HMI_WRITE("selfcheck.t2.bco=2024");
  223. device_status.C01 &= ~(1<<1);
  224. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  225. }
  226. else
  227. {
  228. HMI_WRITE("selfcheck.t2.bco=63488");
  229. device_status.C01 |= (1<<1);
  230. HMI_WRITE("sysSet.va0.txt=\"加热故障\"");
  231. }
  232. }
  233. ctr_Heat(OFF);
  234. rt_thread_delay(1000);
  235. /*********************************************************/
  236. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  237. if(device_status.camsta==0)
  238. {
  239. rt_timer_start(timer_takephoto);
  240. HK_TakePhoto();
  241. for(int i = 30;i>0;i--)
  242. {
  243. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  244. rt_thread_delay(1000);
  245. }
  246. }else {
  247. HMI_WRITE("sysSet.va0.txt=\"相机未连接\"");
  248. HMI_WRITE("selfcheck.t4.bco=63488");
  249. HMI_WRITE("selfcheck.t3.bco=63488");
  250. }
  251. rt_thread_delay(1000);
  252. /*******************************************************/
  253. //清扫
  254. device_status.clear_status = 1;
  255. rt_thread_delay(2000);
  256. for(int i = 0;i<5;i++)
  257. {
  258. current_i[i] = atoi(device_status.power);
  259. rt_thread_delay(1000);
  260. }
  261. current_open = calculate_average(current_i,5);
  262. rt_thread_delay(1000);
  263. if(atoi(device_status.power)-current_open>150)
  264. {
  265. HMI_WRITE("selfcheck.t11.bco=2024");
  266. device_status.C01 &= ~(1<<10);
  267. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  268. }
  269. else
  270. {
  271. HMI_WRITE("selfcheck.t11.bco=63488");
  272. device_status.C01 |= (1<<10);
  273. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  274. }
  275. /*******************************************************/
  276. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  277. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  278. DTU_Write("%s",js);
  279. free(js);
  280. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  281. device_status.selfcheck_ws = 0;
  282. device_status.process = 0.0;
  283. }
  284. int rt_software_timer_init(void)
  285. {
  286. //启动拍照超时定时器
  287. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 60000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_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);