control.c 37 KB

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