selfcheck.c 8.5 KB

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