selfcheck.c 9.5 KB

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