selfcheck.c 10 KB

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