selfcheck.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include <beep.h>
  18. #define DBG_ENABLE
  19. #define DBG_COLOR
  20. #define DBG_TAG "selfcheck"
  21. #define DBG_LEVEL DBG_LOG
  22. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  23. rt_timer_t timer_takephoto = RT_NULL;
  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. device_status.autotakepic = 0;
  36. LOG_E("t_takephoto_callback");
  37. //关闭补光灯
  38. ctr_led(OFF);
  39. //关闭滑台驱动
  40. slidelock();
  41. }
  42. int selfcheck(uint8_t type)
  43. {
  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. HMI_WRITE("sysSet.va0.txt=\"GPS无定位\"");
  73. }
  74. else {
  75. HMI_WRITE("selfcheck.t12.bco=2024");
  76. device_status.C01 &= ~(1<<11);
  77. }
  78. if(Read_Ntc_Voltage()==0)
  79. {
  80. HMI_WRITE("selfcheck.t15.bco=63488");
  81. }
  82. else{
  83. HMI_WRITE("selfcheck.t15.bco=2024");
  84. }
  85. if(device_status.bat_status==0)
  86. {
  87. HMI_WRITE("selfcheck.t5.bco=2024");
  88. device_status.C01 &= ~(1<<4);
  89. }
  90. else
  91. {
  92. HMI_WRITE("selfcheck.t5.bco=63488");
  93. device_status.C01 |= (1<<4);
  94. }
  95. if(device_status.rain_status)
  96. {
  97. HMI_WRITE("selfcheck.t6.bco=63488");
  98. device_status.C01 |= (1<<5);
  99. }
  100. else
  101. {
  102. device_status.C01 &= ~(1<<5);
  103. HMI_WRITE("selfcheck.t6.bco=2024");
  104. }
  105. if(device_status.temp_status)
  106. {
  107. HMI_WRITE("selfcheck.t8.bco=63488");
  108. device_status.C01 |= (1<<7);
  109. }
  110. else
  111. {
  112. device_status.C01 &= ~(1<<7);
  113. HMI_WRITE("selfcheck.t8.bco=2024");
  114. }
  115. if(device_status.sun_status)
  116. {
  117. HMI_WRITE("selfcheck.t7.bco=63488");
  118. device_status.C01 |= (1<<6);
  119. }
  120. else
  121. {
  122. device_status.C01 &= ~(1<<6);
  123. HMI_WRITE("selfcheck.t7.bco=2024");
  124. }
  125. rt_thread_delay(1000);
  126. /********************************************************/
  127. ctr_lamp(ON, 1); //打开镇流器
  128. rt_thread_delay(1000);
  129. for(int i = 0;i<5;i++)
  130. {
  131. current_i[i] = atoi(device_status.power);
  132. rt_thread_delay(1000);
  133. }
  134. ctr_lamp(OFF, 1); //关闭镇流器
  135. current_open = calculate_average(current_i,5);
  136. LOG_I("待机电流:%d",current_);
  137. LOG_I("镇流器开启电流:%d",current_open);
  138. if(current_open-current_>100)
  139. {
  140. HMI_WRITE("selfcheck.t1.bco=2024");
  141. device_status.C01 &= ~(1<<0);
  142. HMI_WRITE("sysSet.va0.txt=\"灯管正常\"");
  143. }
  144. else
  145. {
  146. HMI_WRITE("selfcheck.t1.bco=63488");
  147. device_status.C01 |= (1<<0);
  148. HMI_WRITE("sysSet.va0.txt=\"灯管异常\"");
  149. }
  150. rt_thread_delay(1000);
  151. /********************************************************/
  152. //打开上仓门
  153. ctr_cangmen_up(OPEN);
  154. for(int i = 0;i<5;i++)
  155. {
  156. current_i[i] = atoi(device_status.power);
  157. rt_thread_delay(1000);
  158. }
  159. ctr_cangmen_up(OFF);
  160. current_open = calculate_average(current_i,5);
  161. if(current_open-current_>100)
  162. {
  163. HMI_WRITE("selfcheck.t9.bco=2024");
  164. device_status.C01 &= ~(1<<8);
  165. HMI_WRITE("sysSet.va0.txt=\"上仓门正常\"");
  166. }
  167. else{
  168. HMI_WRITE("selfcheck.t9.bco=63488");
  169. device_status.C01 |= (1<<8);
  170. HMI_WRITE("sysSet.va0.txt=\"上仓门故障\"");
  171. }
  172. rt_thread_delay(1000);
  173. /********************************************************/
  174. //打开下仓门
  175. ctr_cangmen_down(OPEN);
  176. for(int i = 0;i<5;i++)
  177. {
  178. current_i[i] = atoi(device_status.power);
  179. rt_thread_delay(1000);
  180. }
  181. ctr_cangmen_down(OFF);
  182. current_open = calculate_average(current_i,5);
  183. if(current_open-current_>100)
  184. {
  185. HMI_WRITE("selfcheck.t10.bco=2024");
  186. device_status.C01 &= ~(1<<9);
  187. HMI_WRITE("sysSet.va0.txt=\"下仓门正常\"");
  188. }
  189. else{
  190. HMI_WRITE("selfcheck.t10.bco=63488");
  191. device_status.C01 |= (1<<9);
  192. HMI_WRITE("sysSet.va0.txt=\"下仓门故障\"");
  193. }
  194. //开启加热
  195. rt_thread_delay(1000);
  196. //
  197. if(atoi(device_status.heat_power)>5000)
  198. {
  199. HMI_WRITE("selfcheck.t2.bco=2024");
  200. device_status.C01 &= ~(1<<1);
  201. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  202. }
  203. else {
  204. ctr_Heat(ON);
  205. //持续5S钟
  206. for(int i = 5;i>0;i--)
  207. {
  208. HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i);
  209. rt_thread_delay(1000);
  210. }
  211. LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power);
  212. //比较当前电压、温度探头温度和开启前
  213. if(atoi(device_status.heat_power)>5000)
  214. {
  215. HMI_WRITE("selfcheck.t2.bco=2024");
  216. device_status.C01 &= ~(1<<1);
  217. HMI_WRITE("sysSet.va0.txt=\"加热正常\"");
  218. }
  219. else
  220. {
  221. HMI_WRITE("selfcheck.t2.bco=63488");
  222. device_status.C01 |= (1<<1);
  223. HMI_WRITE("sysSet.va0.txt=\"加热故障\"");
  224. }
  225. }
  226. ctr_Heat(OFF);
  227. rt_thread_delay(1000);
  228. /*********************************************************/
  229. HMI_WRITE("sysSet.va0.txt=\"开始拍照\"");
  230. if(device_status.camsta==0)
  231. {
  232. /* 2.9.48: 定时器初始化入口当前未启用(rt_software_timer_init 导出被注释), 判空防崩 */
  233. if(timer_takephoto != RT_NULL)
  234. {
  235. rt_timer_start(timer_takephoto);
  236. }
  237. HK_TakePhoto();
  238. for(int i = 30;i>0;i--)
  239. {
  240. HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i);
  241. rt_thread_delay(1000);
  242. }
  243. }else {
  244. HMI_WRITE("sysSet.va0.txt=\"相机未连接\"");
  245. HMI_WRITE("selfcheck.t4.bco=63488");
  246. HMI_WRITE("selfcheck.t3.bco=63488");
  247. }
  248. rt_thread_delay(1000);
  249. /*******************************************************/
  250. //清扫
  251. device_status.clear_status = 1;
  252. rt_thread_delay(2000);
  253. for(int i = 0;i<5;i++)
  254. {
  255. current_i[i] = atoi(device_status.power);
  256. rt_thread_delay(1000);
  257. }
  258. current_open = calculate_average(current_i,5);
  259. rt_thread_delay(1000);
  260. if(atoi(device_status.power)-current_open>150)
  261. {
  262. HMI_WRITE("selfcheck.t11.bco=2024");
  263. device_status.C01 &= ~(1<<10);
  264. HMI_WRITE("sysSet.va0.txt=\"清扫正常\"");
  265. }
  266. else
  267. {
  268. HMI_WRITE("selfcheck.t11.bco=63488");
  269. device_status.C01 |= (1<<10);
  270. HMI_WRITE("sysSet.va0.txt=\"清扫故障\"");
  271. }
  272. /*******************************************************/
  273. HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01);
  274. char *js = makeJson_C01_Warn(device_status.C01,1.0);
  275. DTU_Write("%s",js);
  276. free(js);
  277. LOG_I("重启线程workloop:%d",thread_restart_workloop());
  278. device_status.selfcheck_ws = 0;
  279. device_status.process = 0.0;
  280. return 0; /* 2.9.48: 补全返回值 */
  281. }
  282. int rt_software_timer_init(void)
  283. {
  284. //启动拍照超时定时器
  285. timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 60000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
  286. if(timer_takephoto==RT_NULL)
  287. {
  288. LOG_E("rt_timer_create t_takephoto failed!");
  289. return RT_ERROR;
  290. }
  291. else
  292. {
  293. return RT_EOK;
  294. }
  295. }
  296. //INIT_APP_EXPORT(rt_software_timer_init);