selfcheck.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. LOG_E("t_takephoto_callback");
  37. //关闭补光灯
  38. ctr_led(OFF);
  39. //关闭滑台驱动
  40. slidelock();
  41. Beep_Tip(5000);
  42. }
  43. int selfcheck(uint8_t type)
  44. {
  45. int current_i[6];
  46. int current_,current_open = 0;
  47. //删除workloop线程 防止自动运行干扰
  48. LOG_I("删除线程workloop:%d",thread_delete_workloop());
  49. //初始化硬件
  50. HardWare_Init(0);
  51. /********************************************************/
  52. //读取5次电流值,取平均值
  53. for(int i = 0;i<5;i++)
  54. {
  55. current_i[i] = atoi(device_status.power);
  56. rt_thread_delay(1000);
  57. }
  58. current_ = calculate_average(current_i,5);
  59. if(current_<50||current_>3000)
  60. {
  61. device_status.C01 |= (1<<12);
  62. HMI_WRITE("selfcheck.t13.bco=63488");
  63. }
  64. else
  65. {
  66. HMI_WRITE("selfcheck.t13.bco=2024");
  67. device_status.C01 &= ~(1<<12);
  68. }
  69. if(atoi(device_status.gps_state)==0)
  70. {
  71. HMI_WRITE("selfcheck.t12.bco=63488");
  72. device_status.C01 |= (1<<11);
  73. HMI_WRITE("sysSet.va0.txt=\"GPS无定位\"");
  74. }
  75. else {
  76. HMI_WRITE("selfcheck.t12.bco=2024");
  77. device_status.C01 &= ~(1<<11);
  78. }
  79. if(Read_Ntc_Voltage()==0)
  80. {
  81. HMI_WRITE("selfcheck.t15.bco=63488");
  82. }
  83. else{
  84. HMI_WRITE("selfcheck.t15.bco=2024");
  85. }
  86. if(device_status.bat_status==0)
  87. {
  88. HMI_WRITE("selfcheck.t5.bco=2024");
  89. device_status.C01 &= ~(1<<4);
  90. }
  91. else
  92. {
  93. HMI_WRITE("selfcheck.t5.bco=63488");
  94. device_status.C01 |= (1<<4);
  95. }
  96. if(device_status.rain_status)
  97. {
  98. HMI_WRITE("selfcheck.t6.bco=63488");
  99. device_status.C01 |= (1<<5);
  100. }
  101. else
  102. {
  103. device_status.C01 &= ~(1<<5);
  104. HMI_WRITE("selfcheck.t6.bco=2024");
  105. }
  106. if(device_status.temp_status)
  107. {
  108. HMI_WRITE("selfcheck.t8.bco=63488");
  109. device_status.C01 |= (1<<7);
  110. }
  111. else
  112. {
  113. device_status.C01 &= ~(1<<7);
  114. HMI_WRITE("selfcheck.t8.bco=2024");
  115. }
  116. if(device_status.sun_status)
  117. {
  118. HMI_WRITE("selfcheck.t7.bco=63488");
  119. device_status.C01 |= (1<<6);
  120. }
  121. else
  122. {
  123. device_status.C01 &= ~(1<<6);
  124. HMI_WRITE("selfcheck.t7.bco=2024");
  125. }
  126. rt_thread_delay(1000);
  127. /********************************************************/
  128. ctr_lamp(ON, 1); //打开镇流器
  129. rt_thread_delay(1000);
  130. for(int i = 0;i<5;i++)
  131. {
  132. current_i[i] = atoi(device_status.power);
  133. rt_thread_delay(1000);
  134. }
  135. ctr_lamp(OFF, 1); //关闭镇流器
  136. current_open = calculate_average(current_i,5);
  137. LOG_I("待机电流:%d",current_);
  138. LOG_I("镇流器开启电流:%d",current_open);
  139. if(current_open-current_>100)
  140. {
  141. HMI_WRITE("selfcheck.t1.bco=2024");
  142. device_status.C01 &= ~(1<<0);
  143. HMI_WRITE("sysSet.va0.txt=\"灯管正常\"");
  144. }
  145. else
  146. {
  147. HMI_WRITE("selfcheck.t1.bco=63488");
  148. device_status.C01 |= (1<<0);
  149. HMI_WRITE("sysSet.va0.txt=\"灯管异常\"");
  150. }
  151. rt_thread_delay(1000);
  152. /********************************************************/
  153. //打开上仓门
  154. ctr_cangmen_up(OPEN);
  155. for(int i = 0;i<5;i++)
  156. {
  157. current_i[i] = atoi(device_status.power);
  158. rt_thread_delay(1000);
  159. }
  160. ctr_cangmen_up(OFF);
  161. current_open = calculate_average(current_i,5);
  162. if(current_open-current_>100)
  163. {
  164. HMI_WRITE("selfcheck.t9.bco=2024");
  165. device_status.C01 &= ~(1<<8);
  166. HMI_WRITE("sysSet.va0.txt=\"上仓门正常\"");
  167. }
  168. else{
  169. HMI_WRITE("selfcheck.t9.bco=63488");
  170. device_status.C01 |= (1<<8);
  171. HMI_WRITE("sysSet.va0.txt=\"上仓门故障\"");
  172. }
  173. rt_thread_delay(1000);
  174. /********************************************************/
  175. //打开下仓门
  176. ctr_cangmen_down(OPEN);
  177. for(int i = 0;i<5;i++)
  178. {
  179. current_i[i] = atoi(device_status.power);
  180. rt_thread_delay(1000);
  181. }
  182. ctr_cangmen_down(OFF);
  183. current_open = calculate_average(current_i,5);
  184. if(current_open-current_>100)
  185. {
  186. HMI_WRITE("selfcheck.t10.bco=2024");
  187. device_status.C01 &= ~(1<<9);
  188. HMI_WRITE("sysSet.va0.txt=\"下仓门正常\"");
  189. }
  190. else{
  191. HMI_WRITE("selfcheck.t10.bco=63488");
  192. device_status.C01 |= (1<<9);
  193. HMI_WRITE("sysSet.va0.txt=\"下仓门故障\"");
  194. }
  195. //开启加热
  196. rt_thread_delay(1000);
  197. //
  198. if(atoi(device_status.heat_power)>5000)
  199. {
  200. HMI_WRITE("selfcheck.t2.bco=2024");
  201. device_status.C01 &= ~(1<<1);
  202. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  203. }
  204. else {
  205. ctr_Heat(ON);
  206. //持续5S钟
  207. for(int i = 5;i>0;i--)
  208. {
  209. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  210. rt_thread_delay(1000);
  211. }
  212. LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power);
  213. //比较当前电压、温度探头温度和开启前
  214. if(atoi(device_status.heat_power)>5000)
  215. {
  216. HMI_WRITE("selfcheck.t2.bco=2024");
  217. device_status.C01 &= ~(1<<1);
  218. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  219. }
  220. else
  221. {
  222. HMI_WRITE("selfcheck.t2.bco=63488");
  223. device_status.C01 |= (1<<1);
  224. HMI_WRITE("sysSet.va0.txt=\"加热故障\"");
  225. }
  226. }
  227. ctr_Heat(OFF);
  228. rt_thread_delay(1000);
  229. /*********************************************************/
  230. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  231. if(device_status.camsta==0)
  232. {
  233. rt_timer_start(timer_takephoto);
  234. HK_TakePhoto();
  235. for(int i = 30;i>0;i--)
  236. {
  237. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  238. rt_thread_delay(1000);
  239. }
  240. }else {
  241. HMI_WRITE("sysSet.va0.txt=\"相机未连接\"");
  242. HMI_WRITE("selfcheck.t4.bco=63488");
  243. HMI_WRITE("selfcheck.t3.bco=63488");
  244. }
  245. rt_thread_delay(1000);
  246. /*******************************************************/
  247. //清扫
  248. device_status.clear_status = 1;
  249. rt_thread_delay(2000);
  250. for(int i = 0;i<5;i++)
  251. {
  252. current_i[i] = atoi(device_status.power);
  253. rt_thread_delay(1000);
  254. }
  255. current_open = calculate_average(current_i,5);
  256. rt_thread_delay(1000);
  257. if(atoi(device_status.power)-current_open>150)
  258. {
  259. HMI_WRITE("selfcheck.t11.bco=2024");
  260. device_status.C01 &= ~(1<<10);
  261. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  262. }
  263. else
  264. {
  265. HMI_WRITE("selfcheck.t11.bco=63488");
  266. device_status.C01 |= (1<<10);
  267. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  268. }
  269. /*******************************************************/
  270. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  271. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  272. DTU_Write("%s",js);
  273. free(js);
  274. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  275. device_status.selfcheck_ws = 0;
  276. device_status.process = 0.0;
  277. }
  278. int rt_software_timer_init(void)
  279. {
  280. //启动拍照超时定时器
  281. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 60000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
  282. if(timer_takephoto==RT_NULL)
  283. {
  284. LOG_E("rt_timer_create t_takephoto failed!");
  285. return RT_ERROR;
  286. }
  287. else
  288. {
  289. return RT_EOK;
  290. }
  291. }
  292. INIT_APP_EXPORT(rt_software_timer_init);