selfcheck.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #define DBG_ENABLE
  17. #define DBG_COLOR
  18. #define DBG_TAG "selfcheck"
  19. #define DBG_LEVEL DBG_LOG
  20. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  21. rt_timer_t timer_takephoto = RT_NULL;
  22. Device_Status_typedef device_status;
  23. static int calculate_average(int arr[], int n) {
  24. int sum = 0; // 用于存储数组元素的总和
  25. for (int i = 0; i < n; i++) {
  26. sum += arr[i]; // 累加每个元素
  27. }
  28. // 计算平均值
  29. int average = sum / n; // 强制类型转换确保得到浮点数结果
  30. return average;
  31. }
  32. static void t_takephoto_callback(void *parameter)
  33. {
  34. device_status.C01 |= (1<<2);
  35. device_status.C01 |= (1<<3);
  36. HMI_WRITE("selfcheck.t4.bco=63488");
  37. HMI_WRITE("selfcheck.t3.bco=63488");
  38. HMI_WRITE("sysSet.va0.txt=\"拍照超时\"");
  39. LOG_E("t_takephoto_callback");
  40. }
  41. int selfcheck(uint8_t type)
  42. {
  43. uint16_t C01 = 0;
  44. int current_i[6];
  45. int current_,current_open = 0;
  46. //删除workloop线程 防止自动运行干扰
  47. LOG_I("删除线程workloop:%d",thread_delete_workloop());
  48. //初始化硬件
  49. HardWare_Init(0);
  50. /********************************************************/
  51. //读取5次电流值,取平均值
  52. for(int i = 0;i<5;i++)
  53. {
  54. current_i[i] = atoi(device_status.power);
  55. rt_thread_delay(1000);
  56. }
  57. current_ = calculate_average(current_i,5);
  58. if(current_<50||current_>3000)
  59. {
  60. device_status.C01 |= (1<<12);
  61. HMI_WRITE("selfcheck.t13.bco=63488");
  62. }
  63. else
  64. {
  65. HMI_WRITE("selfcheck.t13.bco=2024");
  66. device_status.C01 &= ~(1<<12);
  67. }
  68. if(atoi(device_status.gps_state)==0)
  69. {
  70. HMI_WRITE("selfcheck.t12.bco=63488");
  71. device_status.C01 |= (1<<11);
  72. }
  73. else {
  74. HMI_WRITE("selfcheck.t12.bco=2024");
  75. device_status.C01 &= ~(1<<11);
  76. }
  77. if(device_status.bat_status==0)
  78. {
  79. HMI_WRITE("selfcheck.t5.bco=2024");
  80. device_status.C01 &= ~(1<<4);
  81. }
  82. else
  83. {
  84. HMI_WRITE("selfcheck.t5.bco=63488");
  85. device_status.C01 |= (1<<4);
  86. }
  87. if(device_status.rain_status)
  88. {
  89. HMI_WRITE("selfcheck.t6.bco=63488");
  90. device_status.C01 |= (1<<5);
  91. }
  92. else
  93. {
  94. device_status.C01 &= ~(1<<5);
  95. HMI_WRITE("selfcheck.t6.bco=2024");
  96. }
  97. if(device_status.temp_status)
  98. {
  99. HMI_WRITE("selfcheck.t8.bco=63488");
  100. device_status.C01 |= (1<<7);
  101. }
  102. else
  103. {
  104. device_status.C01 &= ~(1<<7);
  105. HMI_WRITE("selfcheck.t8.bco=2024");
  106. }
  107. if(device_status.sun_status)
  108. {
  109. HMI_WRITE("selfcheck.t7.bco=63488");
  110. device_status.C01 |= (1<<6);
  111. }
  112. else
  113. {
  114. device_status.C01 &= ~(1<<6);
  115. HMI_WRITE("selfcheck.t7.bco=2024");
  116. }
  117. rt_thread_delay(1000);
  118. /********************************************************/
  119. ctr_lamp(ON, 1); //打开镇流器
  120. rt_thread_delay(1000);
  121. for(int i = 0;i<5;i++)
  122. {
  123. current_i[i] = atoi(device_status.power);
  124. rt_thread_delay(1000);
  125. }
  126. ctr_lamp(OFF, 1); //关闭镇流器
  127. current_open = calculate_average(current_i,5);
  128. LOG_I("待机电流:%d",current_);
  129. LOG_I("镇流器开启电流:%d",current_open);
  130. if(current_open-current_>100)
  131. {
  132. HMI_WRITE("selfcheck.t1.bco=2024");
  133. device_status.C01 &= ~(1<<0);
  134. HMI_WRITE("sysSet.va0.txt=\"灯管正常\"");
  135. }
  136. else
  137. {
  138. HMI_WRITE("selfcheck.t1.bco=63488");
  139. device_status.C01 |= (1<<0);
  140. HMI_WRITE("sysSet.va0.txt=\"灯管异常\"");
  141. }
  142. rt_thread_delay(1000);
  143. /********************************************************/
  144. //打开上仓门
  145. ctr_cangmen_up(OPEN);
  146. for(int i = 0;i<5;i++)
  147. {
  148. current_i[i] = atoi(device_status.power);
  149. rt_thread_delay(1000);
  150. }
  151. ctr_cangmen_up(OFF);
  152. current_open = calculate_average(current_i,5);
  153. if(current_open-current_>100)
  154. {
  155. HMI_WRITE("selfcheck.t9.bco=2024");
  156. device_status.C01 &= ~(1<<8);
  157. HMI_WRITE("sysSet.va0.txt=\"上仓门正常\"");
  158. }
  159. else{
  160. HMI_WRITE("selfcheck.t9.bco=63488");
  161. device_status.C01 |= (1<<8);
  162. HMI_WRITE("sysSet.va0.txt=\"上仓门故障\"");
  163. }
  164. rt_thread_delay(1000);
  165. /********************************************************/
  166. //打开下仓门
  167. ctr_cangmen_down(OPEN);
  168. for(int i = 0;i<5;i++)
  169. {
  170. current_i[i] = atoi(device_status.power);
  171. rt_thread_delay(1000);
  172. }
  173. ctr_cangmen_down(OFF);
  174. current_open = calculate_average(current_i,5);
  175. if(current_open-current_>100)
  176. {
  177. HMI_WRITE("selfcheck.t10.bco=2024");
  178. device_status.C01 &= ~(1<<9);
  179. HMI_WRITE("sysSet.va0.txt=\"下仓门正常\"");
  180. }
  181. else{
  182. HMI_WRITE("selfcheck.t10.bco=63488");
  183. device_status.C01 |= (1<<9);
  184. HMI_WRITE("sysSet.va0.txt=\"下仓门故障\"");
  185. }
  186. //开启加热
  187. rt_thread_delay(1000);
  188. //
  189. if(atoi(device_status.heat_power)>5000)
  190. {
  191. HMI_WRITE("selfcheck.t2.bco=2024");
  192. device_status.C01 &= ~(1<<1);
  193. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  194. }
  195. else {
  196. ctr_Heat(ON);
  197. //持续5S钟
  198. for(int i = 5;i>0;i--)
  199. {
  200. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  201. rt_thread_delay(1000);
  202. }
  203. LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power);
  204. //比较当前电压、温度探头温度和开启前
  205. if(atoi(device_status.heat_power)>5000)
  206. {
  207. HMI_WRITE("selfcheck.t2.bco=2024");
  208. device_status.C01 &= ~(1<<1);
  209. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  210. }
  211. else
  212. {
  213. HMI_WRITE("selfcheck.t2.bco=63488");
  214. device_status.C01 |= (1<<1);
  215. HMI_WRITE("sysSet.va0.txt=\"加热故障\"");
  216. }
  217. }
  218. ctr_Heat(OFF);
  219. rt_thread_delay(1000);
  220. /*********************************************************/
  221. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  222. rt_timer_start(timer_takephoto);
  223. HK_TakePhoto();
  224. for(int i = 30;i>0;i--)
  225. {
  226. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  227. rt_thread_delay(1000);
  228. }
  229. rt_thread_delay(1000);
  230. /*******************************************************/
  231. //清扫
  232. device_status.clear_status = 1;
  233. rt_thread_delay(2000);
  234. for(int i = 0;i<5;i++)
  235. {
  236. current_i[i] = atoi(device_status.power);
  237. rt_thread_delay(1000);
  238. }
  239. current_open = calculate_average(current_i,5);
  240. rt_thread_delay(1000);
  241. if(atoi(device_status.power)-current_open>150)
  242. {
  243. HMI_WRITE("selfcheck.t11.bco=2024");
  244. device_status.C01 &= ~(1<<10);
  245. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  246. }
  247. else
  248. {
  249. HMI_WRITE("selfcheck.t11.bco=63488");
  250. device_status.C01 |= (1<<10);
  251. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  252. }
  253. /*******************************************************/
  254. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  255. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  256. DTU_Write("%s",js);
  257. free(js);
  258. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  259. device_status.selfcheck_ws = 0;
  260. device_status.process = 0.0;
  261. }
  262. int rt_software_timer_init(void)
  263. {
  264. //启动拍照超时定时器
  265. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 100000, RT_TIMER_FLAG_ONE_SHOT);
  266. if(timer_takephoto==RT_NULL)
  267. {
  268. LOG_E("rt_timer_create t_takephoto failed!");
  269. return RT_ERROR;
  270. }
  271. else
  272. {
  273. return RT_EOK;
  274. }
  275. }
  276. INIT_APP_EXPORT(rt_software_timer_init);