selfcheck.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. device_status.autotakepic = 0;
  37. LOG_E("t_takephoto_callback");
  38. //关闭补光灯
  39. ctr_led(OFF);
  40. //关闭滑台驱动
  41. slidelock();
  42. Beep_Tip(5000);
  43. }
  44. int selfcheck(uint8_t type)
  45. {
  46. int current_i[6];
  47. int current_,current_open = 0;
  48. //删除workloop线程 防止自动运行干扰
  49. LOG_I("删除线程workloop:%d",thread_delete_workloop());
  50. //初始化硬件
  51. HardWare_Init(0);
  52. /********************************************************/
  53. //读取5次电流值,取平均值
  54. for(int i = 0;i<5;i++)
  55. {
  56. current_i[i] = atoi(device_status.power);
  57. rt_thread_delay(1000);
  58. }
  59. current_ = calculate_average(current_i,5);
  60. if(current_<50||current_>3000)
  61. {
  62. device_status.C01 |= (1<<12);
  63. HMI_WRITE("selfcheck.t13.bco=63488");
  64. }
  65. else
  66. {
  67. HMI_WRITE("selfcheck.t13.bco=2024");
  68. device_status.C01 &= ~(1<<12);
  69. }
  70. if(atoi(device_status.gps_state)==0)
  71. {
  72. HMI_WRITE("selfcheck.t12.bco=63488");
  73. device_status.C01 |= (1<<11);
  74. HMI_WRITE("sysSet.va0.txt=\"GPS无定位\"");
  75. }
  76. else {
  77. HMI_WRITE("selfcheck.t12.bco=2024");
  78. device_status.C01 &= ~(1<<11);
  79. }
  80. if(Read_Ntc_Voltage()==0)
  81. {
  82. HMI_WRITE("selfcheck.t15.bco=63488");
  83. }
  84. else{
  85. HMI_WRITE("selfcheck.t15.bco=2024");
  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. //开启加热
  197. rt_thread_delay(1000);
  198. //
  199. if(atoi(device_status.heat_power)>5000)
  200. {
  201. HMI_WRITE("selfcheck.t2.bco=2024");
  202. device_status.C01 &= ~(1<<1);
  203. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  204. }
  205. else {
  206. ctr_Heat(ON);
  207. //持续5S钟
  208. for(int i = 5;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.heat_power);
  214. //比较当前电压、温度探头温度和开启前
  215. if(atoi(device_status.heat_power)>5000)
  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. }
  228. ctr_Heat(OFF);
  229. rt_thread_delay(1000);
  230. /*********************************************************/
  231. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  232. if(device_status.camsta==0)
  233. {
  234. rt_timer_start(timer_takephoto);
  235. HK_TakePhoto();
  236. for(int i = 30;i>0;i--)
  237. {
  238. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  239. rt_thread_delay(1000);
  240. }
  241. }else {
  242. HMI_WRITE("sysSet.va0.txt=\"相机未连接\"");
  243. HMI_WRITE("selfcheck.t4.bco=63488");
  244. HMI_WRITE("selfcheck.t3.bco=63488");
  245. }
  246. rt_thread_delay(1000);
  247. /*******************************************************/
  248. //清扫
  249. device_status.clear_status = 1;
  250. rt_thread_delay(2000);
  251. for(int i = 0;i<5;i++)
  252. {
  253. current_i[i] = atoi(device_status.power);
  254. rt_thread_delay(1000);
  255. }
  256. current_open = calculate_average(current_i,5);
  257. rt_thread_delay(1000);
  258. if(atoi(device_status.power)-current_open>150)
  259. {
  260. HMI_WRITE("selfcheck.t11.bco=2024");
  261. device_status.C01 &= ~(1<<10);
  262. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  263. }
  264. else
  265. {
  266. HMI_WRITE("selfcheck.t11.bco=63488");
  267. device_status.C01 |= (1<<10);
  268. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  269. }
  270. /*******************************************************/
  271. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  272. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  273. DTU_Write("%s",js);
  274. free(js);
  275. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  276. device_status.selfcheck_ws = 0;
  277. device_status.process = 0.0;
  278. }
  279. int rt_software_timer_init(void)
  280. {
  281. //启动拍照超时定时器
  282. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 60000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
  283. if(timer_takephoto==RT_NULL)
  284. {
  285. LOG_E("rt_timer_create t_takephoto failed!");
  286. return RT_ERROR;
  287. }
  288. else
  289. {
  290. return RT_EOK;
  291. }
  292. }
  293. INIT_APP_EXPORT(rt_software_timer_init);