control.c 33 KB

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