control.c 30 KB

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