selfcheck.c 10 KB

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