control.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. #include <control.h>
  2. /*
  3. * Copyright (c) 2006-2020, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-05-30 Administrator the first version
  10. */
  11. #include "beep.h"
  12. #include "hmi.h"
  13. #include "easyflash.h"
  14. #include "stdio.h"
  15. #include "sound.h"
  16. #define DBG_TAG "control.c"
  17. #define DBG_LVL LOG_LVL_INFO
  18. #include "dtu_uart2.h"
  19. #include "sht30_collect.h"
  20. //工作检测
  21. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  22. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  23. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  24. //工作流程
  25. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  26. #define WORK_LOOP_THREAD_STACK_SIZE 1024 //栈大小
  27. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  28. //设备状态表结构体
  29. Device_Status_typedef device_status;
  30. rt_thread_t tid_workloop = RT_NULL;
  31. rt_thread_t tid_wkstc = RT_NULL;
  32. /*光控*/
  33. uint8_t In_Light_status(void)
  34. {
  35. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  36. return rt_pin_read(LIGHT_PIN);
  37. }
  38. /*雨控*/
  39. uint8_t In_Rain_status(void)
  40. {
  41. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  42. return rt_pin_read(RAIN_PIN);
  43. }
  44. /*加热*/
  45. uint8_t ctr_Heat(uint8_t status_heat)
  46. {
  47. device_status.heat_status = status_heat;
  48. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  49. rt_pin_write(Heat_Pin,status_heat);
  50. return status_heat;
  51. }
  52. /*相机供电*/
  53. void ctr_photo(uint8_t status_photo)
  54. {
  55. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  56. rt_pin_write(PHOTO,status_photo);
  57. }
  58. /*采集风机/上仓门*/
  59. uint8_t ctr_cangmen_up(uint8_t status_up)
  60. {
  61. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  62. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  63. if(status_up==OFF)
  64. {
  65. rt_pin_write(Collect_fan_R,ON);
  66. rt_pin_write(Collect_fan_L,ON);
  67. device_status.ctr_up_motor = 0;
  68. rt_thread_delay(1000);
  69. }
  70. if(status_up==OPEN)
  71. {
  72. device_status.up_status = 1;
  73. rt_pin_write(Collect_fan_R,OFF);
  74. rt_pin_write(Collect_fan_L,OFF);
  75. rt_pin_write(Collect_fan_R,ON);
  76. rt_pin_write(Collect_fan_L,OFF);
  77. }
  78. if(status_up==CLOSE)
  79. {
  80. device_status.up_status = 0;
  81. rt_pin_write(Collect_fan_R,OFF);
  82. rt_pin_write(Collect_fan_L,OFF);
  83. rt_pin_write(Collect_fan_R,OFF);
  84. rt_pin_write(Collect_fan_L,ON);
  85. }
  86. return device_status.up_status;
  87. }
  88. //下仓门
  89. uint8_t ctr_cangmen_down(uint8_t status_down)
  90. {
  91. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  92. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  93. if(status_down==OFF)
  94. {
  95. rt_pin_write(C_DOWN_R,ON);
  96. rt_pin_write(C_DOWN_L,ON);
  97. device_status.ctr_down_motor = 0;
  98. rt_thread_delay(1000);
  99. }
  100. if(status_down==OPEN)
  101. {
  102. device_status.down_status = 1;
  103. rt_pin_write(C_DOWN_R,OFF);
  104. rt_pin_write(C_DOWN_L,OFF);
  105. rt_pin_write(C_DOWN_R,ON);
  106. rt_pin_write(C_DOWN_L,OFF);
  107. }
  108. if(status_down==CLOSE)
  109. {
  110. device_status.down_status = 0;
  111. rt_pin_write(C_DOWN_R,OFF);
  112. rt_pin_write(C_DOWN_L,OFF);
  113. rt_pin_write(C_DOWN_R,OFF);
  114. rt_pin_write(C_DOWN_L,ON);
  115. }
  116. return device_status.down_status;
  117. }
  118. //镇流器
  119. uint8_t ctr_lamp(uint8_t status_lamp)
  120. {
  121. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  122. if(status_lamp==ON)
  123. {
  124. rt_pin_write(LAMP_SW,ON);
  125. device_status.lamp_status = status_lamp;
  126. }
  127. else {
  128. rt_pin_write(LAMP_SW, OFF);
  129. device_status.lamp_status = status_lamp;
  130. }
  131. return device_status.lamp_status;
  132. }
  133. //震动电机
  134. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  135. {
  136. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  137. if(status_shake==ON&&device_status.shake_ws==1)
  138. {
  139. rt_pin_write(SHAKE_SW,ON);
  140. rt_thread_delay(shake_sec*100);
  141. rt_pin_write(SHAKE_SW,OFF);
  142. HMI_WRITE("cangWei.switchShake.val=%d",0);
  143. }
  144. else
  145. {
  146. rt_pin_write(SHAKE_SW,OFF);
  147. HMI_WRITE("cangWei.switchShake.val=%d",0);
  148. }
  149. return status_shake;
  150. }
  151. //补光
  152. uint8_t ctr_led(uint8_t status_led)
  153. {
  154. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  155. if(status_led==ON)
  156. {
  157. rt_pin_write(LED_SW,ON);
  158. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  159. }
  160. else if(status_led==OFF){
  161. rt_pin_write(LED_SW,OFF);
  162. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  163. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  164. }
  165. return status_led;
  166. }
  167. //转仓
  168. void turnzp(void)
  169. {
  170. static uint16_t turn_timeout = 0;
  171. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  172. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  173. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  174. rt_pin_write(CLEAR_R,OFF);
  175. rt_pin_write(CLEAR_L,OFF);
  176. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  177. {
  178. rt_thread_delay(20);
  179. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  180. {
  181. rt_pin_write(CLEAR_R,ON);
  182. rt_pin_write(CLEAR_L,OFF);
  183. rt_thread_delay(50);
  184. turn_timeout++;
  185. if(turn_timeout==1000) break;
  186. }
  187. break;
  188. }
  189. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  190. {
  191. rt_thread_delay(20);
  192. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  193. {
  194. rt_thread_delay(50);
  195. rt_pin_write(CLEAR_R,ON);
  196. rt_pin_write(CLEAR_L,OFF);
  197. turn_timeout++;
  198. if(turn_timeout==1000) break;
  199. }
  200. break;
  201. }
  202. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  203. {
  204. rt_thread_delay(20);
  205. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  206. {
  207. rt_pin_write(CLEAR_R,OFF);
  208. rt_pin_write(CLEAR_L,OFF);
  209. break;
  210. }
  211. else {
  212. break;
  213. }
  214. }
  215. rt_pin_write(CLEAR_R,OFF);
  216. rt_pin_write(CLEAR_L,OFF);
  217. turn_timeout = 0;
  218. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  219. HMI_WRITE("sysTest.switchClear.val=%d",0);
  220. rt_thread_delay(1000);
  221. }
  222. //清扫-常闭型限位开关
  223. //新结构使用推杆电机做清扫
  224. uint8_t ctr_clear(uint16_t status_clear)
  225. {
  226. static uint16_t clear_num = 0;
  227. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  228. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  229. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  230. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  231. /*************************************************************外接清扫
  232. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  233. rt_pin_write(POWER_CLEAR,ON);
  234. rt_thread_delay(1000*100);
  235. rt_pin_write(POWER_CLEAR,OFF);
  236. ************************************************************/
  237. //停止
  238. rt_pin_write(CLEAR_R,OFF);
  239. rt_pin_write(CLEAR_L,OFF);
  240. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  241. rt_thread_delay(100);
  242. //如果清扫位置在中间
  243. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  244. {
  245. rt_pin_write(CLEAR_R,ON);
  246. rt_pin_write(CLEAR_L,OFF);
  247. rt_thread_delay(50);
  248. clear_num++;
  249. //已到达检测位置
  250. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  251. {
  252. rt_pin_write(CLEAR_R,ON);
  253. rt_pin_write(CLEAR_L,ON);
  254. rt_thread_delay(2000);
  255. break;
  256. }
  257. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  258. {
  259. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  260. DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  261. rt_pin_write(CLEAR_R,ON);
  262. rt_pin_write(CLEAR_L,ON);
  263. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  264. if(Read_Real_Voltage()/100<12)
  265. {
  266. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  267. HMI_WRITE("sysTest.switchClear.val=%d",0);
  268. device_status.clear_status = 0;
  269. return 0;
  270. }
  271. break;
  272. }
  273. }
  274. clear_num = 0;
  275. //如果清扫位置在左侧
  276. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  277. {
  278. rt_pin_write(CLEAR_L,ON);
  279. rt_pin_write(CLEAR_R,OFF);
  280. rt_thread_delay(50);
  281. clear_num++;
  282. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  283. {
  284. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  285. DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  286. rt_pin_write(CLEAR_R,ON);
  287. rt_pin_write(CLEAR_L,ON);
  288. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  289. if(Read_Real_Voltage()/100<12)
  290. {
  291. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  292. HMI_WRITE("sysTest.switchClear.val=%d",0);
  293. device_status.clear_status = 0;
  294. return 0;
  295. }
  296. break;
  297. }
  298. }
  299. clear_num = 0;
  300. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  301. {
  302. rt_pin_write(CLEAR_R,ON);
  303. rt_pin_write(CLEAR_L,ON);
  304. if(Read_Real_Voltage()/100<12)
  305. {
  306. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  307. HMI_WRITE("sysTest.switchClear.val=%d",0);
  308. device_status.clear_status = 0;
  309. return 0;
  310. }
  311. rt_thread_delay(2000);
  312. break;
  313. }
  314. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  315. {
  316. rt_pin_write(CLEAR_L,OFF);
  317. rt_pin_write(CLEAR_R,ON);
  318. rt_thread_delay(50);
  319. clear_num++;
  320. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  321. {
  322. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  323. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  324. DTU_Write("%s",Clear_ERR);
  325. rt_pin_write(CLEAR_R,ON);
  326. rt_pin_write(CLEAR_L,ON);
  327. rt_free(Clear_ERR);
  328. if(Read_Real_Voltage()/100<12)
  329. {
  330. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  331. HMI_WRITE("sysTest.switchClear.val=%d",0);
  332. device_status.clear_status = 0;
  333. return 0;
  334. }
  335. break;
  336. }
  337. }
  338. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  339. {
  340. rt_pin_write(CLEAR_R,ON);
  341. rt_pin_write(CLEAR_L,ON);
  342. rt_thread_delay(2000);
  343. break;
  344. }
  345. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  346. {
  347. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  348. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  349. DTU_Write("%s",Clear_ERR);
  350. rt_free(Clear_ERR);
  351. rt_pin_write(CLEAR_R,ON);
  352. rt_pin_write(CLEAR_L,ON);
  353. return 0;
  354. }
  355. clear_num = 0;
  356. rt_thread_delay(3000);
  357. rt_pin_write(CLEAR_R,OFF);
  358. rt_pin_write(CLEAR_L,OFF);
  359. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  360. HMI_WRITE("sysTest.switchClear.val=%d",0);
  361. device_status.clear_status = 0;
  362. return 1;
  363. }
  364. //路由器供电
  365. void ctr_router(uint8_t status_router)
  366. {
  367. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  368. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  369. if(status_router==ON)
  370. {
  371. rt_pin_write(ROUTER,ON);
  372. rt_pin_write(POWER_CLEAR,ON);
  373. }
  374. else {
  375. rt_pin_write(ROUTER,OFF);
  376. rt_pin_write(POWER_CLEAR,OFF);
  377. }
  378. }
  379. //DTU_RTU供电
  380. void ctr_DTU_Power(uint8_t DTU_Power)
  381. {
  382. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  383. if(DTU_Power==ON)
  384. {
  385. rt_pin_write(ROUTER,ON);
  386. }
  387. else {
  388. rt_pin_write(ROUTER,OFF);
  389. }
  390. }
  391. //清扫-推杆电机
  392. void ctr_TG_Clear(uint16_t clear_timeout)
  393. {
  394. static uint16_t clear_count = 0;
  395. clear_count = clear_timeout;
  396. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  397. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  398. while(clear_count!=0)
  399. {
  400. rt_pin_write(CLEAR_R,ON);
  401. rt_pin_write(CLEAR_L,OFF);
  402. clear_count--;
  403. rt_thread_delay(100);
  404. }
  405. rt_pin_write(CLEAR_R,ON);
  406. rt_pin_write(CLEAR_L,ON);
  407. rt_thread_delay(3000);
  408. while(clear_timeout!=clear_count)
  409. {
  410. rt_pin_write(CLEAR_R,OFF);
  411. rt_pin_write(CLEAR_L,ON);
  412. clear_count++;
  413. rt_thread_delay(100);
  414. }
  415. rt_pin_write(CLEAR_R,ON);
  416. rt_pin_write(CLEAR_L,ON);
  417. rt_thread_delay(3000);
  418. rt_pin_write(CLEAR_R,OFF);
  419. rt_pin_write(CLEAR_L,OFF);
  420. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  421. HMI_WRITE("sysTest.switchClear.val=%d",0);
  422. }
  423. //DTU,RTU供电
  424. void ctr_RTU_DTU(uint8_t status_rtu)
  425. {
  426. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  427. if(status_rtu==ON)
  428. {
  429. rt_pin_write(RTU_DTU,ON);
  430. }
  431. else
  432. {
  433. rt_kprintf("RTU电源复位!!!\n");
  434. rt_pin_write(RTU_DTU,OFF);
  435. }
  436. }
  437. //海康拍照
  438. void Hk_Take_io(void)
  439. {
  440. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  441. rt_pin_write(HK_TAKE, 1);
  442. Beep_Tip(1000);
  443. rt_pin_write(HK_TAKE, 0);
  444. }
  445. void calorstat(uint16_t heat_time)
  446. {
  447. uint16_t n = 0;
  448. uint16_t delay_heat = 60;
  449. FlagStatus heat_flg = RESET;
  450. device_status.heattime = 0;
  451. ctr_Heat(ON);
  452. //恒温模式
  453. for(n=0;n < heat_time;n++)
  454. {
  455. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  456. {
  457. heat_flg = SET;
  458. ctr_Heat(OFF);
  459. while(delay_heat!=0)
  460. {
  461. delay_heat --;
  462. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  463. rt_thread_delay(1000);
  464. }
  465. delay_heat = 60;
  466. //跳出FOR循环
  467. n = n + 60;
  468. }
  469. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  470. {
  471. ctr_Heat(ON);
  472. }
  473. else if (device_status.heattem>device_status.heatvalue&&heat_flg==SET)
  474. {
  475. ctr_Heat(OFF);
  476. }
  477. if((n % 60)==0)
  478. {
  479. device_status.heattime = device_status.heattime + 1;
  480. }
  481. //如果触发异常退出加热
  482. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  483. {
  484. rt_kprintf("============================触发异常退出加热==========================");
  485. ctr_lamp(OFF);
  486. ctr_Heat(OFF);
  487. break;
  488. }
  489. rt_thread_delay(1000);
  490. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  491. }
  492. ctr_Heat(OFF);
  493. }
  494. //初始化所有控制驱动,关闭
  495. void HardWare_Init(uint8_t boot)
  496. {
  497. Beep_Tip(200);
  498. //关闭上仓门
  499. ctr_cangmen_up(OFF);
  500. if(boot==1)
  501. {
  502. HMI_WRITE("hardcheck.checktip.txt=\"上仓门电机已停止!!!\"");
  503. rt_thread_delay(1000);
  504. }
  505. //关闭下仓门电机
  506. ctr_cangmen_down(OFF);
  507. if(boot==1)
  508. {
  509. HMI_WRITE("hardcheck.checktip.txt=\"下仓门电机已停止!!!\"");
  510. rt_thread_delay(1000);
  511. }
  512. //关闭镇流器
  513. ctr_lamp(OFF);
  514. if(boot==1)
  515. {
  516. HMI_WRITE("hardcheck.checktip.txt=\"镇流器已关闭!!!\"");
  517. rt_thread_delay(1000);
  518. }
  519. //关闭震动电机
  520. ctr_shake(OFF,0);
  521. if(boot==1)
  522. {
  523. HMI_WRITE("hardcheck.checktip.txt=\"震动电机已关闭!!!\"");
  524. rt_thread_delay(1000);
  525. }
  526. //关闭清扫电机
  527. if(boot==1)
  528. {
  529. HMI_WRITE("hardcheck.checktip.txt=\"清扫电机已关闭!!!\"");
  530. rt_thread_delay(1000);
  531. }
  532. //关闭加热
  533. ctr_Heat(OFF);
  534. if(boot==1)
  535. {
  536. HMI_WRITE("hardcheck.checktip.txt=\"加热带已关闭!!!\"");
  537. rt_thread_delay(1000);
  538. }
  539. //关闭补光灯
  540. ctr_led(OFF);
  541. if(boot==1)
  542. {
  543. HMI_WRITE("hardcheck.checktip.txt=\"补光灯已关闭!!!\"");
  544. rt_thread_delay(1000);
  545. }
  546. //关闭12V供电(海康相机,RTU)
  547. ctr_photo(OFF);
  548. if(boot==1)
  549. {
  550. HMI_WRITE("hardcheck.checktip.txt=\"12V供电已关闭!!!\"");
  551. rt_thread_delay(1000);
  552. }
  553. }
  554. //拍照
  555. void HK_TakePhoto(void)
  556. {
  557. //打开补光灯
  558. ctr_led(ON);
  559. DTU_Write("takephoto.mp3");
  560. //延时
  561. rt_thread_delay(1000*10);
  562. rt_kprintf("\n测试拍照!\n");
  563. char *takephotojson = makeJson_takephoto();
  564. DTU_Write("%s",takephotojson);
  565. rt_free(takephotojson);
  566. //触发海康相机拍照
  567. Hk_Take_io();
  568. rt_thread_delay(1000*45);
  569. ctr_led(OFF);
  570. }
  571. //光控测试任务
  572. void SunTest(void)
  573. {
  574. while(Read_Light_Voltage()<500)
  575. {
  576. rt_thread_delay(1000);
  577. }
  578. rt_thread_delay(3000);
  579. if(Read_Light_Voltage()<500)
  580. {
  581. device_status.sun_status = 0;//光控状态
  582. }
  583. if(Read_Light_Voltage()>500)
  584. {
  585. rt_thread_delay(1000);
  586. }
  587. rt_thread_delay(3000);
  588. if(Read_Light_Voltage()>500)
  589. {
  590. device_status.sun_status = 1;
  591. ctr_lamp(OFF);
  592. }
  593. HMI_WRITE("sysTest.switchSun.val=%d",0);
  594. device_status.sun_test = 0;
  595. device_status.work_status=0;
  596. }
  597. /*上电初始化*/
  598. void ResetInit(void)
  599. {
  600. device_status.work_done = 1;
  601. //1.打开路由器供电
  602. ctr_router(ON);
  603. ctr_RTU_DTU(ON);
  604. //2.打开相机供电
  605. ctr_photo(ON);
  606. //3.关闭镇流器
  607. ctr_lamp(OFF);
  608. //4.关闭震动电机
  609. ctr_shake(OFF, 0);
  610. //5.关闭加热
  611. ctr_Heat(OFF);
  612. //6.关闭补光灯
  613. ctr_led(OFF);
  614. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  615. {
  616. rt_kprintf("欠压!!!\n");
  617. return;
  618. }
  619. //7.关闭上仓门
  620. device_status.ctr_up_motor = 2;
  621. while(device_status.ctr_up_motor!=0)
  622. {
  623. rt_kprintf("关闭上仓门...\n");
  624. rt_thread_delay(100);
  625. }
  626. //8.关闭下仓门
  627. device_status.ctr_down_motor = 2;
  628. while(device_status.ctr_down_motor!=0)
  629. {
  630. rt_kprintf("关闭下仓门....\n");
  631. rt_thread_delay(100);
  632. }
  633. //9.清扫
  634. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  635. {
  636. rt_kprintf("异常退出!!!\n");
  637. return;
  638. }
  639. if(device_status.version_type==4)
  640. {
  641. if(ctr_clear(650)==0)
  642. ctr_TG_Clear(350);
  643. }
  644. else if (device_status.version_type==1) {
  645. turnzp();
  646. }
  647. rt_thread_delay(1000);
  648. }
  649. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  650. void HardWare_Check(uint8_t boot)
  651. {
  652. if(boot==0)
  653. {
  654. //停止所有动作 -完成
  655. HardWare_Init(boot);
  656. }
  657. else {
  658. //停止所有动作
  659. //读取记录待机功耗(实测大概70mA)
  660. //(需减去屏幕的功耗大概120mA)
  661. //动作上仓门,读取功率值保存
  662. //动作下仓门,读取功率值保存
  663. //动作清扫,读取功率值保存
  664. //打开镇流器,读取功率值保存
  665. //打开海康相机供电,读取功率值保存
  666. //打开补光灯,读取功率值保存
  667. //打开路由器/RTU供电,读取功率值保存
  668. //打开加热,读取功率值保存
  669. }
  670. }
  671. //工作状态检测
  672. void WorkCheck(void)
  673. {
  674. //雨控检测,温控检测,电池电压检测
  675. while(device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  676. {
  677. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  678. if(device_status.timeSwitch==1)
  679. {
  680. rt_kprintf("***************时控*****************\n");
  681. //如果开始和结束是同一天
  682. if(device_status.startBar<device_status.stopBar)
  683. {
  684. rt_kprintf("开始和结束是同一天");
  685. time_t now;
  686. time(&now);
  687. static struct tm nowtime; //现在
  688. static struct tm starttime; //开始
  689. static struct tm endtime; //结束
  690. //获取当前时间戳
  691. nowtime =*localtime(&now);
  692. nowtime.tm_hour = nowtime.tm_hour-8;
  693. //获取定时开始的时间戳
  694. starttime = *localtime(&now);
  695. starttime.tm_hour = device_status.startBar-8;
  696. starttime.tm_min = 0;
  697. starttime.tm_sec = 0;
  698. //获取定时结束的时间戳
  699. endtime = *localtime(&now);
  700. endtime.tm_hour = device_status.stopBar-8;
  701. endtime.tm_min = 0;
  702. endtime.tm_sec = 0;
  703. rt_kprintf("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  704. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  705. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  706. {
  707. rt_kprintf("当前时间大于开始时间,小于结束时间,工作中!!!\n");
  708. device_status.work_status = 1;
  709. }
  710. //如果当前时间小于开始时间
  711. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  712. {
  713. rt_kprintf("当前时间小于开始时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
  714. device_status.work_status = 0;
  715. }
  716. //当前时间大于结束时间
  717. else {
  718. rt_kprintf("当前时间大于结束时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
  719. device_status.work_status = 0;
  720. }
  721. }
  722. //如果结束时间是第二天
  723. else if (device_status.startBar>device_status.stopBar) {
  724. time_t now;
  725. time(&now);
  726. static struct tm nowtime; //现在
  727. static struct tm starttime; //开始
  728. static struct tm endtime; //结束
  729. //获取当前时间戳
  730. nowtime =*localtime(&now);
  731. nowtime.tm_hour = nowtime.tm_hour-8;
  732. //获取定时开始的时间戳
  733. starttime = *localtime(&now);
  734. starttime.tm_hour = device_status.startBar-8;
  735. starttime.tm_min = 0;
  736. starttime.tm_sec = 0;
  737. //获取定时结束的时间戳
  738. endtime = *localtime(&now);
  739. endtime.tm_hour = device_status.stopBar-8;
  740. endtime.tm_min = 0;
  741. endtime.tm_sec = 0;
  742. rt_kprintf("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  743. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  744. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  745. {
  746. //获取定时结束的时间戳
  747. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  748. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  749. endtime.tm_min = 0;
  750. endtime.tm_sec = 0;
  751. device_status.work_status = 1;
  752. rt_kprintf("当前前时间大于开始时间,且没有开始工作\n");
  753. }
  754. //当前时间小于开始时间
  755. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  756. {
  757. //获取定时结束的时间戳
  758. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  759. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  760. endtime.tm_min = 0;
  761. endtime.tm_sec = 0;
  762. //如果当前时间戳小于结束时间戳
  763. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  764. {
  765. rt_kprintf("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间\n");
  766. device_status.work_status = 1;
  767. }
  768. else
  769. {
  770. rt_kprintf("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间!!!\n");
  771. device_status.work_status = 0; //不在工作时间,待机
  772. }
  773. }
  774. }
  775. }
  776. else {
  777. rt_kprintf("光控定时模式!!!\n");
  778. time_t now;
  779. time(&now);
  780. static struct tm nowstamp;
  781. nowstamp =*localtime(&now);
  782. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  783. static rt_uint32_t nowstamp_num = 0;
  784. nowstamp_num = mktime(&nowstamp);
  785. rt_kprintf("now stamp:%d\n",nowstamp_num);
  786. if(device_status.sun_status==0)
  787. {
  788. //检测到光控,计算定时
  789. if(device_status.light_over_tim > nowstamp_num)
  790. {
  791. device_status.work_status = 1;
  792. rt_kprintf("光控定时,计时中:%d----%d\n",device_status.light_over_tim,nowstamp_num);
  793. }
  794. else
  795. {
  796. device_status.work_status = 0; //置位工作状态
  797. rt_kprintf("光控定时任务已完成!!!\n");
  798. }
  799. }
  800. else
  801. {
  802. device_status.work_status = 0;
  803. device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  804. rt_kprintf("光控中,白天:%d!!!\n",nowstamp_num);
  805. }
  806. }
  807. rt_thread_delay(1000);
  808. }
  809. rt_kprintf("雨控状态,温控状态,电压状态:\n%d\n%d\n%d\n",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  810. device_status.work_status = 0;
  811. rt_thread_delay(1000);
  812. }
  813. //工作任务流程s
  814. void WorkTast(void)
  815. {
  816. ctr_router(ON); //打开路由器供电
  817. static int clt_time = 0; //收集时间差秒数
  818. rt_kprintf("工作状态:%d\n",device_status.work_status);
  819. rt_kprintf("开关机状态:%d\n",device_status.ds);
  820. while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1)
  821. {
  822. device_status.work_done = 0;
  823. if(clt_time==0) DTU_Write("workstart.mp3");
  824. //1.打开灯管
  825. ctr_lamp(ON);
  826. rt_kprintf("打开灯管!!!\n");
  827. rt_thread_delay(5000); //待灯管启动再往下执行,否则会触发电机过流
  828. //2.打开上仓门
  829. rt_kprintf("打开上仓门!!!\n");
  830. device_status.ctr_up_motor = 1;
  831. //3.收集
  832. clt_time = device_status.clt_time * 60;
  833. while(clt_time!=1)
  834. {
  835. clt_time --;
  836. rt_thread_delay(1000);
  837. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  838. //如果触发异常
  839. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  840. {
  841. ctr_lamp(OFF);
  842. rt_kprintf("异常退出!!!\n");
  843. break;
  844. }
  845. }
  846. clt_time = 1;
  847. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  848. {
  849. ctr_lamp(OFF);
  850. rt_kprintf("异常退出!!!\n");
  851. break;
  852. }
  853. //4.关闭上仓门
  854. device_status.ctr_up_motor = 2;
  855. rt_kprintf("关闭上仓门!!!\n");
  856. // while(device_status.ctr_up_motor!=0)
  857. // {
  858. // rt_thread_delay(100);
  859. // rt_kprintf("等待上仓门关闭!!!\n");
  860. // }
  861. //5.加热
  862. rt_kprintf("加热!!!\n");
  863. calorstat(device_status.heattime*60);
  864. //6.打开下仓门
  865. if(device_status.rain_status==0)
  866. {
  867. device_status.ctr_down_motor =1;
  868. }
  869. //待下仓门完全打开
  870. while(device_status.ctr_down_motor!=0)
  871. {
  872. rt_thread_delay(100);
  873. rt_kprintf("等待下仓门打开!!!\n");
  874. }
  875. rt_thread_delay(3000);
  876. //关闭下仓门
  877. device_status.ctr_down_motor = 2;
  878. while(device_status.ctr_down_motor!=0)
  879. {
  880. rt_thread_delay(100);
  881. rt_kprintf("等待下仓门关闭!!!\n");
  882. }
  883. if(device_status.version_type==4)
  884. {
  885. //震动
  886. ctr_shake(ON, 1);
  887. //补光&&拍照
  888. rt_kprintf("拍照!!!\n");
  889. HK_TakePhoto();
  890. //清扫
  891. if(ctr_clear(1200)==0)
  892. ctr_TG_Clear(350);
  893. //清扫失效则使用震动
  894. ctr_shake(ON, 100);
  895. }
  896. }
  897. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1||device_status.ds==0)
  898. {
  899. rt_kprintf("-------------------not work--------------------\n");
  900. device_status.work_status = 0;
  901. }
  902. //工作结束
  903. if(device_status.work_done==0&&device_status.work_status==0)
  904. {
  905. DTU_Write("workdone");
  906. ResetInit();
  907. }
  908. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  909. }
  910. /* 入口函数-工作状态检测 */
  911. static void Work_status_thread_entry(void *parameter)
  912. {
  913. static uint16_t sun_status_num = 300;
  914. //如果是光控定时,进行光控监测的消抖
  915. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  916. {
  917. rt_kprintf("光控定时!!!\n");
  918. while(sun_status_num!=0)
  919. {
  920. sun_status_num--;
  921. rt_thread_delay(1000);
  922. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  923. }
  924. time_t now;
  925. time(&now);
  926. static struct tm nowstamp;
  927. nowstamp =*localtime(&now);
  928. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  929. static rt_uint32_t nowstamp_num = 0;
  930. nowstamp_num = mktime(&nowstamp);
  931. device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  932. }
  933. while(1)
  934. {
  935. WorkCheck();
  936. rt_thread_delay(1);
  937. }
  938. }
  939. /*入口函数-工作流程*/
  940. static void Work_Loop_thread_entry(void *parameter)
  941. {
  942. rt_thread_delay(1000);
  943. ResetInit();
  944. while(1)
  945. {
  946. WorkTast();
  947. rt_thread_delay(1000);
  948. }
  949. }
  950. /* 创建线程 -是否置位工作状态*/
  951. int Workstatus_check(void)
  952. {
  953. /* 创建线程*/
  954. tid_wkstc = rt_thread_create("work_status_check_thread",
  955. Work_status_thread_entry, RT_NULL,
  956. WORK_CHECK_THREAD_STACK_SIZE, //2048
  957. WORK_CHECK_THREAD_PRIORITY, //23
  958. WORK_CHECK_THREAD_TIMESLICE); //5
  959. if(tid_wkstc!=RT_NULL)
  960. {
  961. rt_thread_startup(tid_wkstc);
  962. }
  963. return 0;
  964. }
  965. /*工作流程线程*/
  966. int Work_Loop_Task(void)
  967. {
  968. /* 创建线程*/
  969. tid_workloop = rt_thread_create("work_loop_thread",
  970. Work_Loop_thread_entry, RT_NULL,
  971. WORK_LOOP_THREAD_STACK_SIZE, //2048
  972. WORK_LOOP_THREAD_PRIORITY, //12
  973. WORK_LOOP_THREAD_TIMESLICE); //5
  974. if(tid_workloop!=RT_NULL)
  975. {
  976. rt_thread_startup(tid_workloop);
  977. }
  978. return 0;
  979. }
  980. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  981. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  982. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  983. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);