control.c 32 KB

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