selfcheck.c 8.6 KB

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