selfcheck.c 7.9 KB

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