control.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. #define PWM_EN 1 //0关闭,1-打开
  29. //设备状态表结构体
  30. Device_Status_typedef device_status;
  31. rt_thread_t tid_workloop = RT_NULL;
  32. rt_thread_t tid_wkstc = RT_NULL;
  33. rt_thread_t tid_ctr = RT_NULL;
  34. /**************************1.0定制2000W*************************************/
  35. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  36. {
  37. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  38. if(status_ws==ON)
  39. {
  40. rt_pin_write(FAN_P,ON);
  41. }
  42. else {
  43. rt_pin_write(FAN_P, OFF);
  44. }
  45. return 0;
  46. }
  47. uint8_t ctr_Fan_IN(uint8_t status_ws)
  48. {
  49. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  50. if(status_ws==ON)
  51. {
  52. rt_pin_write(FAN_I,ON);
  53. }
  54. else {
  55. rt_pin_write(FAN_I, OFF);
  56. }
  57. return 0;
  58. }
  59. /**************************机器人********************************/
  60. void ctr_Motor_Robot(uint8_t status_ws)
  61. {
  62. unsigned int close_timeout = 0;
  63. unsigned int open_timeout = 0;
  64. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  65. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  66. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  67. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  68. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  69. rt_pin_write(Motor_Power,OFF);
  70. rt_pin_write(Motor_Direction_Ctr,ON);
  71. if(status_ws == ON)
  72. {
  73. //检测按键状态
  74. while(rt_pin_read(Motor_Direction_Button)==1)
  75. {
  76. while(rt_pin_read(LIMIT_CLOSE)==1)
  77. {
  78. rt_pin_write(Motor_Direction_Ctr,ON);
  79. close_timeout++;
  80. //打开电机驱动器电源,默认关闭
  81. rt_pin_write(Motor_Power,ON);
  82. if(close_timeout==1)
  83. {
  84. rt_pin_write(Motor_Power,OFF);
  85. rt_thread_delay(50);
  86. rt_pin_write(Motor_Power,ON);
  87. }
  88. rt_thread_delay(50);
  89. if(close_timeout>500)
  90. {
  91. close_timeout = 500;
  92. rt_pin_write(Motor_Power,OFF);
  93. }
  94. }
  95. if(rt_pin_read(LIMIT_CLOSE)==0)
  96. {
  97. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  98. }
  99. rt_thread_delay(50);
  100. open_timeout = 0;
  101. }
  102. rt_pin_write(Motor_Power,OFF);
  103. rt_thread_delay(100);
  104. while(rt_pin_read(Motor_Direction_Button)==0)
  105. {
  106. while(rt_pin_read(LIMIT_OPEN)==1)
  107. {
  108. open_timeout ++;
  109. //打开电机驱动器电源,默认关闭
  110. rt_pin_write(Motor_Power,ON);
  111. rt_pin_write(Motor_Direction_Ctr,OFF);
  112. if(open_timeout>500)
  113. {
  114. open_timeout = 500;
  115. rt_pin_write(Motor_Power,OFF);
  116. }
  117. rt_thread_delay(50);
  118. }
  119. if(rt_pin_read(LIMIT_OPEN)==0)
  120. rt_pin_write(Motor_Power,OFF);
  121. rt_thread_delay(50);
  122. close_timeout = 0;
  123. }
  124. open_timeout = 0;
  125. close_timeout = 0;
  126. }
  127. else {
  128. rt_pin_write(Motor_Power,OFF);
  129. }
  130. }
  131. /*************************吸虫塔*********************************/
  132. //吸虫电机
  133. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  134. {
  135. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  136. if(status_ws==ON)
  137. {
  138. rt_pin_write(MOTOR1_CTR_PIN,ON);
  139. }
  140. else {
  141. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  142. }
  143. return 0;
  144. }
  145. //吸附电机
  146. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  147. {
  148. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  149. if(status_ws==ON)
  150. {
  151. rt_pin_write(MOTOR2_CTR_PIN,ON);
  152. }
  153. else {
  154. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  155. }
  156. return 0;
  157. }
  158. //清扫-卷带电机
  159. void ctr_JD_Clear()
  160. {
  161. rt_uint16_t turn_timeout = 0;
  162. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  163. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  164. {
  165. rt_thread_delay(20);
  166. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  167. {
  168. Motor_Juandai(ON);
  169. rt_thread_delay(50);
  170. turn_timeout++;
  171. if(turn_timeout==1000) break;
  172. }
  173. break;
  174. }
  175. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  176. {
  177. rt_thread_delay(20);
  178. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  179. {
  180. rt_thread_delay(50);
  181. Motor_Juandai(ON);
  182. turn_timeout++;
  183. if(turn_timeout==1000) break;
  184. }
  185. break;
  186. }
  187. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  188. {
  189. rt_thread_delay(20);
  190. if(rt_pin_read(MOTOR_C_LIMIT)==0)
  191. {
  192. Motor_Juandai(OFF);
  193. break;
  194. }
  195. else {
  196. break;
  197. }
  198. }
  199. Motor_Juandai(OFF);
  200. }
  201. //工作任务流程
  202. void WorkTask_XCT(void)
  203. {
  204. while(device_status.work_status==1&&device_status.ds==1)
  205. {
  206. ctr_Motor_xichong(ON);
  207. ctr_Motor_xifu(ON);
  208. //打开相机供电
  209. ctr_Heat(ON);
  210. uint32_t photo_fre = 0;
  211. photo_fre = device_status.dattim * 60;
  212. while(photo_fre!=0)
  213. {
  214. photo_fre --;
  215. rt_thread_delay(1000);
  216. rt_kprintf("收集中,等待开始拍照:%d\n",photo_fre);
  217. }
  218. //卷带
  219. ctr_JD_Clear();
  220. //拍照
  221. HK_TakePhoto();
  222. //关闭吸附风机
  223. ctr_Motor_xichong(ON);
  224. ctr_Motor_xifu(ON);
  225. //关闭相机供电
  226. ctr_Heat(OFF);
  227. //卷带
  228. ctr_JD_Clear();
  229. //震动
  230. Motor_shake(ON, device_status.shake_sec);
  231. }
  232. //关闭风机
  233. ctr_Motor_xichong(OFF);
  234. ctr_Motor_xifu(OFF);
  235. rt_kprintf("不在工作时间,未工作!!!\n");
  236. }
  237. /**********************************************************************/
  238. /*光控*/
  239. uint8_t In_Light_status(void)
  240. {
  241. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  242. return rt_pin_read(LIGHT_PIN);
  243. }
  244. /*雨控*/
  245. uint8_t In_Rain_status(void)
  246. {
  247. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  248. return rt_pin_read(RAIN_PIN);
  249. }
  250. /*加热*/
  251. uint8_t ctr_Heat(uint8_t status_heat)
  252. {
  253. device_status.heat_status = status_heat;
  254. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  255. rt_pin_write(Heat_Pin,status_heat);
  256. return status_heat;
  257. }
  258. /*相机供电*/
  259. void ctr_photo(uint8_t status_photo)
  260. {
  261. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  262. rt_pin_write(PHOTO,status_photo);
  263. }
  264. /*采集风机/上仓门*/
  265. uint8_t ctr_cangmen_up(uint8_t status_up)
  266. {
  267. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  268. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  269. if(status_up==OFF)
  270. {
  271. rt_pin_write(Collect_fan_R,ON);
  272. rt_pin_write(Collect_fan_L,ON);
  273. device_status.ctr_up_motor = 0;
  274. rt_thread_delay(1000);
  275. }
  276. if(status_up==OPEN)
  277. {
  278. device_status.up_status = 1;
  279. rt_pin_write(Collect_fan_R,OFF);
  280. rt_pin_write(Collect_fan_L,OFF);
  281. rt_pin_write(Collect_fan_R,ON);
  282. rt_pin_write(Collect_fan_L,OFF);
  283. }
  284. if(status_up==CLOSE)
  285. {
  286. device_status.up_status = 0;
  287. rt_pin_write(Collect_fan_R,OFF);
  288. rt_pin_write(Collect_fan_L,OFF);
  289. rt_pin_write(Collect_fan_R,OFF);
  290. rt_pin_write(Collect_fan_L,ON);
  291. }
  292. return device_status.up_status;
  293. }
  294. //下仓门
  295. uint8_t ctr_cangmen_down(uint8_t status_down)
  296. {
  297. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  298. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  299. if(status_down==OFF)
  300. {
  301. rt_pin_write(C_DOWN_R,ON);
  302. rt_pin_write(C_DOWN_L,ON);
  303. device_status.ctr_down_motor = 0;
  304. rt_thread_delay(1000);
  305. }
  306. if(status_down==OPEN)
  307. {
  308. device_status.down_status = 1;
  309. rt_pin_write(C_DOWN_R,OFF);
  310. rt_pin_write(C_DOWN_L,OFF);
  311. rt_pin_write(C_DOWN_R,ON);
  312. rt_pin_write(C_DOWN_L,OFF);
  313. }
  314. if(status_down==CLOSE)
  315. {
  316. device_status.down_status = 0;
  317. rt_pin_write(C_DOWN_R,OFF);
  318. rt_pin_write(C_DOWN_L,OFF);
  319. rt_pin_write(C_DOWN_R,OFF);
  320. rt_pin_write(C_DOWN_L,ON);
  321. }
  322. return device_status.down_status;
  323. }
  324. //镇流器
  325. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  326. {
  327. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  328. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  329. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  330. if(status_lamp==ON)
  331. {
  332. switch(lamp_num){
  333. case 1:
  334. rt_pin_write(LAMP_SW,ON);
  335. device_status.lamp_status = status_lamp;
  336. break;
  337. case 2:
  338. rt_pin_write(LAMP_1_SW,ON);
  339. device_status.lamp_status = status_lamp;
  340. break;
  341. case 3:
  342. rt_pin_write(LAMP_2_SW,ON);
  343. device_status.lamp_status = status_lamp;
  344. break;
  345. default:
  346. break;
  347. }
  348. }
  349. else {
  350. switch(lamp_num){
  351. case 1:
  352. rt_pin_write(LAMP_SW,OFF);
  353. device_status.lamp_status = status_lamp;
  354. break;
  355. case 2:
  356. rt_pin_write(LAMP_1_SW,OFF);
  357. device_status.lamp1_status = status_lamp;
  358. break;
  359. case 3:
  360. rt_pin_write(LAMP_2_SW,OFF);
  361. device_status.lamp2_status = status_lamp;
  362. break;
  363. default:
  364. break;
  365. }
  366. }
  367. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  368. return device_status.lamp_status;
  369. }
  370. //震动电机
  371. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  372. {
  373. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  374. if(status_shake==ON&&device_status.shake_ws==1)
  375. {
  376. rt_pin_write(SHAKE_SW,ON);
  377. rt_thread_delay(shake_sec*100);
  378. rt_pin_write(SHAKE_SW,OFF);
  379. HMI_WRITE("cangWei.switchShake.val=%d",0);
  380. }
  381. else
  382. {
  383. rt_pin_write(SHAKE_SW,OFF);
  384. HMI_WRITE("cangWei.switchShake.val=%d",0);
  385. }
  386. return status_shake;
  387. }
  388. //补光
  389. uint8_t ctr_led(uint8_t status_led)
  390. {
  391. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  392. if(device_status.version_type==6)
  393. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  394. if(status_led==ON)
  395. {
  396. rt_pin_write(LED_SW,ON);
  397. if(device_status.version_type==6)
  398. rt_pin_write(LED_P,ON);
  399. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  400. }
  401. else if(status_led==OFF){
  402. rt_pin_write(LED_SW,OFF);
  403. if(device_status.version_type==6)
  404. rt_pin_write(LED_P,OFF);
  405. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  406. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  407. }
  408. return status_led;
  409. }
  410. //转仓
  411. void turnzp(void)
  412. {
  413. static uint16_t turn_timeout = 0;
  414. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  415. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  416. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  417. // rt_pin_write(CLEAR_R,OFF);
  418. // rt_pin_write(CLEAR_L,OFF);
  419. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  420. {
  421. rt_thread_delay(20);
  422. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  423. {
  424. // rt_pin_write(CLEAR_R,ON);
  425. // rt_pin_write(CLEAR_L,OFF);
  426. Motor_Juandai(ON);
  427. rt_thread_delay(50);
  428. turn_timeout++;
  429. if(turn_timeout==600) break;
  430. }
  431. break;
  432. }
  433. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  434. {
  435. rt_thread_delay(20);
  436. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  437. {
  438. // rt_pin_write(CLEAR_R,ON);
  439. // rt_pin_write(CLEAR_L,OFF);
  440. Motor_Juandai(ON);
  441. turn_timeout++;
  442. rt_thread_delay(50);
  443. if(turn_timeout==600) break;
  444. }
  445. break;
  446. }
  447. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  448. {
  449. rt_thread_delay(20);
  450. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  451. {
  452. // rt_pin_write(CLEAR_R,OFF);
  453. // rt_pin_write(CLEAR_L,OFF);
  454. Motor_Juandai(OFF);
  455. break;
  456. }
  457. else {
  458. break;
  459. }
  460. }
  461. Motor_Juandai(OFF);
  462. // rt_pin_write(CLEAR_R,OFF);
  463. // rt_pin_write(CLEAR_L,OFF);
  464. turn_timeout = 0;
  465. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  466. HMI_WRITE("sysTest.switchClear.val=%d",0);
  467. rt_thread_delay(1000);
  468. }
  469. //清扫-常闭型限位开关
  470. //新结构使用推杆电机做清扫
  471. uint8_t ctr_clear(uint16_t status_clear)
  472. {
  473. static uint16_t clear_num = 0;
  474. #if PWM_EN == 0
  475. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  476. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  477. #endif
  478. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  479. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  480. /*************************************************************外接清扫
  481. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  482. rt_pin_write(POWER_CLEAR,ON);
  483. rt_thread_delay(1000*100);
  484. rt_pin_write(POWER_CLEAR,OFF);
  485. ************************************************************/
  486. //停止
  487. #if PWM_EN == 0
  488. rt_pin_write(CLEAR_R,OFF);
  489. rt_pin_write(CLEAR_L,OFF);
  490. #else
  491. Motor_Qingsao_R_PWM(OFF);
  492. Motor_Qingsao_L_PWM(OFF);
  493. #endif
  494. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  495. rt_thread_delay(100);
  496. //如果清扫位置在中间
  497. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  498. {
  499. #if PWM_EN == 0
  500. rt_pin_write(CLEAR_R,ON);
  501. rt_pin_write(CLEAR_L,OFF);
  502. #else
  503. Motor_Qingsao_R_PWM(ON);
  504. Motor_Qingsao_L_PWM(OFF);
  505. #endif
  506. rt_thread_delay(50);
  507. clear_num++;
  508. //已到达检测位置
  509. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  510. {
  511. #if PWM_EN == 0
  512. rt_pin_write(CLEAR_R,ON);
  513. rt_pin_write(CLEAR_L,ON);
  514. #else
  515. Motor_Qingsao_R_PWM(OFF);
  516. Motor_Qingsao_L_PWM(OFF);
  517. #endif
  518. rt_thread_delay(2000);
  519. break;
  520. }
  521. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  522. {
  523. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  524. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  525. #if PWM_EN == 0
  526. rt_pin_write(CLEAR_R,ON);
  527. rt_pin_write(CLEAR_L,ON);
  528. #else
  529. Motor_Qingsao_R_PWM(OFF);
  530. Motor_Qingsao_L_PWM(OFF);
  531. #endif
  532. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  533. if(Read_Real_Voltage()/100<12)
  534. {
  535. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  536. HMI_WRITE("sysTest.switchClear.val=%d",0);
  537. device_status.clear_status = 0;
  538. return 0;
  539. }
  540. break;
  541. }
  542. }
  543. clear_num = 0;
  544. //如果清扫位置在左侧
  545. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  546. {
  547. #if PWM_EN == 0
  548. rt_pin_write(CLEAR_R,ON);
  549. rt_pin_write(CLEAR_L,OFF);
  550. #else
  551. Motor_Qingsao_R_PWM(ON);
  552. Motor_Qingsao_L_PWM(OFF);
  553. #endif
  554. rt_thread_delay(50);
  555. clear_num++;
  556. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  557. {
  558. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  559. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  560. #if PWM_EN == 0
  561. rt_pin_write(CLEAR_R,ON);
  562. rt_pin_write(CLEAR_L,ON);
  563. #else
  564. Motor_Qingsao_R_PWM(OFF);
  565. Motor_Qingsao_L_PWM(OFF);
  566. #endif
  567. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  568. if(Read_Real_Voltage()/100<12)
  569. {
  570. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  571. HMI_WRITE("sysTest.switchClear.val=%d",0);
  572. device_status.clear_status = 0;
  573. return 0;
  574. }
  575. break;
  576. }
  577. }
  578. clear_num = 0;
  579. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  580. {
  581. #if PWM_EN == 0
  582. rt_pin_write(CLEAR_R,ON);
  583. rt_pin_write(CLEAR_L,ON);
  584. #else
  585. Motor_Qingsao_R_PWM(OFF);
  586. Motor_Qingsao_L_PWM(OFF);
  587. #endif
  588. if(Read_Real_Voltage()/100<12)
  589. {
  590. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  591. HMI_WRITE("sysTest.switchClear.val=%d",0);
  592. device_status.clear_status = 0;
  593. return 0;
  594. }
  595. rt_thread_delay(2000);
  596. break;
  597. }
  598. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  599. {
  600. #if PWM_EN == 0
  601. rt_pin_write(CLEAR_R,OFF);
  602. rt_pin_write(CLEAR_L,ON);
  603. #else
  604. Motor_Qingsao_R_PWM(OFF);
  605. Motor_Qingsao_L_PWM(ON);
  606. #endif
  607. rt_thread_delay(50);
  608. clear_num++;
  609. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  610. {
  611. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  612. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  613. //DTU_Write("%s",Clear_ERR);
  614. #if PWM_EN == 0
  615. rt_pin_write(CLEAR_R,ON);
  616. rt_pin_write(CLEAR_L,ON);
  617. #else
  618. Motor_Qingsao_R_PWM(OFF);
  619. Motor_Qingsao_L_PWM(OFF);
  620. #endif
  621. rt_free(Clear_ERR);
  622. if(Read_Real_Voltage()/100<12)
  623. {
  624. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  625. HMI_WRITE("sysTest.switchClear.val=%d",0);
  626. device_status.clear_status = 0;
  627. return 0;
  628. }
  629. break;
  630. }
  631. }
  632. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  633. {
  634. #if PWM_EN == 0
  635. rt_pin_write(CLEAR_R,ON);
  636. rt_pin_write(CLEAR_L,ON);
  637. #else
  638. Motor_Qingsao_R_PWM(OFF);
  639. Motor_Qingsao_L_PWM(OFF);
  640. #endif
  641. rt_thread_delay(2000);
  642. break;
  643. }
  644. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  645. {
  646. #if PWM_EN == 0
  647. rt_pin_write(CLEAR_R,ON);
  648. rt_pin_write(CLEAR_L,ON);
  649. #else
  650. Motor_Qingsao_R_PWM(OFF);
  651. Motor_Qingsao_L_PWM(OFF);
  652. #endif
  653. if(device_status.version_type==41)
  654. {
  655. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  656. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  657. //DTU_Write("%s",Clear_ERR);
  658. rt_free(Clear_ERR);
  659. }
  660. return 0;
  661. }
  662. clear_num = 0;
  663. rt_thread_delay(2000);
  664. #if PWM_EN == 0
  665. rt_pin_write(CLEAR_R,ON);
  666. rt_pin_write(CLEAR_L,ON);
  667. #else
  668. Motor_Qingsao_R_PWM(OFF);
  669. Motor_Qingsao_L_PWM(OFF);
  670. #endif
  671. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  672. HMI_WRITE("sysTest.switchClear.val=%d",0);
  673. device_status.clear_status = 0;
  674. return 1;
  675. }
  676. //路由器供电
  677. void ctr_router(uint8_t status_router)
  678. {
  679. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  680. if(device_status.version_type==6)
  681. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  682. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  683. if(status_router==ON)
  684. {
  685. rt_pin_write(ROUTER,ON);
  686. //
  687. #if LAMP_EN == 0
  688. if(device_status.version_type==6)
  689. rt_pin_write(ROUTER_P,ON);
  690. #endif
  691. }
  692. else {
  693. rt_pin_write(ROUTER,OFF);
  694. //
  695. #if LAMP_EN == 0
  696. if(device_status.version_type==6)
  697. rt_pin_write(ROUTER_P,OFF);
  698. #endif
  699. }
  700. }
  701. //DTU_RTU供电
  702. void ctr_DTU_Power(uint8_t DTU_Power)
  703. {
  704. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  705. if(DTU_Power==ON)
  706. {
  707. rt_pin_write(ROUTER,ON);
  708. }
  709. else {
  710. rt_pin_write(ROUTER,OFF);
  711. }
  712. }
  713. //清扫-推杆电机
  714. void ctr_TG_Clear(uint16_t clear_timeout)
  715. {
  716. static uint16_t clear_count = 0;
  717. clear_count = clear_timeout;
  718. #if PWM_EN == 0
  719. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  720. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  721. #endif
  722. while(clear_count!=0)
  723. {
  724. #if PWM_EN == 0
  725. rt_pin_write(CLEAR_R,ON);
  726. rt_pin_write(CLEAR_L,OFF);
  727. #else
  728. Motor_Qingsao_R_PWM(ON);
  729. Motor_Qingsao_L_PWM(OFF);
  730. #endif
  731. clear_count--;
  732. rt_thread_delay(100);
  733. }
  734. #if PWM_EN == 0
  735. rt_pin_write(CLEAR_R,ON);
  736. rt_pin_write(CLEAR_L,ON);
  737. #else
  738. Motor_Qingsao_R_PWM(OFF);
  739. Motor_Qingsao_L_PWM(OFF);
  740. #endif
  741. rt_thread_delay(3000);
  742. while(clear_timeout!=clear_count)
  743. {
  744. #if PWM_EN == 0
  745. rt_pin_write(CLEAR_R,OFF);
  746. rt_pin_write(CLEAR_L,ON);
  747. #else
  748. Motor_Qingsao_R_PWM(OFF);
  749. Motor_Qingsao_L_PWM(ON);
  750. #endif
  751. clear_count++;
  752. rt_thread_delay(100);
  753. }
  754. #if PWM_EN == 0
  755. rt_pin_write(CLEAR_R,ON);
  756. rt_pin_write(CLEAR_L,ON);
  757. #else
  758. Motor_Qingsao_R_PWM(OFF);
  759. Motor_Qingsao_L_PWM(OFF);
  760. #endif
  761. rt_thread_delay(2000);
  762. device_status.clear_status = 0;
  763. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  764. HMI_WRITE("sysTest.switchClear.val=%d",0);
  765. }
  766. //DTU,RTU供电
  767. void ctr_RTU_DTU(uint8_t status_rtu)
  768. {
  769. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  770. if(status_rtu==ON)
  771. {
  772. rt_pin_write(RTU_DTU,ON);
  773. }
  774. else
  775. {
  776. rt_kprintf("RTU电源复位!!!\n");
  777. rt_pin_write(RTU_DTU,OFF);
  778. }
  779. }
  780. //海康拍照
  781. void Hk_Take_io(void)
  782. {
  783. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  784. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  785. rt_pin_write(HK_TAKE, 1);
  786. rt_pin_write(HK_IO, 0);
  787. Beep_Tip(1000);
  788. rt_pin_write(HK_TAKE, 0);
  789. rt_pin_write(HK_IO, 1);
  790. }
  791. void calorstat(uint16_t heat_time)
  792. {
  793. uint8_t heat_up_cnt = 0;
  794. uint16_t n = 0;
  795. uint16_t delay_heat = 60;
  796. FlagStatus heat_flg = RESET;
  797. device_status.heat_time_min = 0;
  798. ctr_Heat(ON);
  799. UpLoad_Data(); //开始加热时的第一条数据
  800. //恒温模式
  801. for(n=0;n < heat_time;n++)
  802. {
  803. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  804. {
  805. heat_flg = SET;
  806. ctr_Heat(OFF);
  807. while(delay_heat!=0)
  808. {
  809. delay_heat --;
  810. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  811. rt_thread_delay(1000);
  812. }
  813. delay_heat = 60;
  814. //跳出FOR循环
  815. n = n + 60;
  816. }
  817. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  818. {
  819. heat_up_cnt = 0;
  820. ctr_Heat(ON);
  821. }
  822. else if (device_status.heattem>device_status.heatvalue&&heat_flg==SET)
  823. {
  824. if(heat_up_cnt==0)
  825. {
  826. UpLoad_Data(); //加热到设定温度的数据
  827. heat_up_cnt = 1;
  828. }
  829. ctr_Heat(OFF);
  830. }
  831. if((n % 60)==0)
  832. {
  833. device_status.heat_time_min = device_status.heat_time_min + 1;
  834. if(device_status.heat_time_min % 2 == 0)
  835. {
  836. UpLoad_Data(); //从加热开始每两分钟上传的数据
  837. }
  838. }
  839. //如果触发异常退出加热
  840. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  841. {
  842. rt_thread_delay(1000);
  843. if(device_status.bat_status == 1)
  844. {
  845. rt_kprintf("============================触发异常退出加热==========================");
  846. ctr_lamp(OFF,1);
  847. ctr_Heat(OFF);
  848. break;
  849. }
  850. }
  851. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  852. {
  853. ctr_Heat(OFF);
  854. rt_kprintf("异常退出!!!\n");
  855. break;
  856. }
  857. rt_thread_delay(1000);
  858. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  859. }
  860. ctr_Heat(OFF);
  861. //UpLoad_Data(); //加热结束的数据
  862. }
  863. //初始化所有控制驱动,关闭
  864. void HardWare_Init(uint8_t boot)
  865. {
  866. Beep_Tip(200);
  867. //关闭上仓门
  868. ctr_cangmen_up(OFF);
  869. if(boot==1)
  870. {
  871. HMI_WRITE("hardcheck.checktip.txt=\"上仓门电机已停止!!!\"");
  872. rt_thread_delay(1000);
  873. }
  874. //关闭下仓门电机
  875. ctr_cangmen_down(OFF);
  876. if(boot==1)
  877. {
  878. HMI_WRITE("hardcheck.checktip.txt=\"下仓门电机已停止!!!\"");
  879. rt_thread_delay(1000);
  880. }
  881. //关闭镇流器
  882. ctr_lamp(OFF,1);
  883. if(boot==1)
  884. {
  885. HMI_WRITE("hardcheck.checktip.txt=\"镇流器已关闭!!!\"");
  886. rt_thread_delay(1000);
  887. }
  888. //关闭震动电机
  889. ctr_shake(OFF,0);
  890. if(boot==1)
  891. {
  892. HMI_WRITE("hardcheck.checktip.txt=\"震动电机已关闭!!!\"");
  893. rt_thread_delay(1000);
  894. }
  895. //关闭清扫电机
  896. if(boot==1)
  897. {
  898. HMI_WRITE("hardcheck.checktip.txt=\"清扫电机已关闭!!!\"");
  899. rt_thread_delay(1000);
  900. }
  901. //关闭加热
  902. ctr_Heat(OFF);
  903. if(boot==1)
  904. {
  905. HMI_WRITE("hardcheck.checktip.txt=\"加热带已关闭!!!\"");
  906. rt_thread_delay(1000);
  907. }
  908. //关闭补光灯
  909. ctr_led(OFF);
  910. if(boot==1)
  911. {
  912. HMI_WRITE("hardcheck.checktip.txt=\"补光灯已关闭!!!\"");
  913. rt_thread_delay(1000);
  914. }
  915. //关闭12V供电(海康相机,RTU)
  916. ctr_photo(OFF);
  917. if(boot==1)
  918. {
  919. HMI_WRITE("hardcheck.checktip.txt=\"12V供电已关闭!!!\"");
  920. rt_thread_delay(1000);
  921. }
  922. }
  923. //拍照
  924. void HK_TakePhoto(void)
  925. {
  926. //打开补光灯
  927. ctr_led(ON);
  928. DTU_Write("takephoto.mp3");
  929. //延时
  930. rt_thread_delay(1000*15);
  931. rt_kprintf("\n测试拍照!\n");
  932. char *takephotojson = makeJson_takephoto();
  933. DTU_Write("%s",takephotojson);
  934. rt_free(takephotojson);
  935. //触发海康相机拍照
  936. Hk_Take_io();
  937. rt_thread_delay(1000*45);
  938. ctr_led(OFF);
  939. }
  940. //光控测试任务
  941. void SunTest(void)
  942. {
  943. rt_uint16_t ui_timeout_l = 0;
  944. //未检测到光照,一秒扫描等待,超时30S
  945. while(Read_Light_Voltage()<500)
  946. {
  947. ui_timeout_l ++;
  948. rt_thread_delay(1000);
  949. if(ui_timeout_l==30) break;
  950. }
  951. rt_thread_delay(3000);
  952. if(Read_Light_Voltage()<500)
  953. {
  954. device_status.sun_status = 0;//光控状态
  955. }
  956. if(Read_Light_Voltage()>500)
  957. {
  958. rt_thread_delay(1000);
  959. }
  960. rt_thread_delay(3000);
  961. if(Read_Light_Voltage()>500)
  962. {
  963. device_status.sun_status = 1;
  964. ctr_lamp(OFF,1);
  965. }
  966. HMI_WRITE("sysTest.switchSun.val=%d",0);
  967. device_status.sun_test = 0;
  968. device_status.work_status=0;
  969. }
  970. /*上电初始化*/
  971. void ResetInit(void)
  972. {
  973. if(device_status.version_type!=6)
  974. {
  975. device_status.work_done = 1;
  976. //1.打开路由器供电
  977. ctr_router(ON);
  978. ctr_RTU_DTU(ON);
  979. //2.打开相机供电
  980. ctr_photo(ON);
  981. //3.关闭镇流器
  982. ctr_lamp(OFF,1);
  983. #if LAMP_EN == 1
  984. ctr_lamp(OFF,2);
  985. ctr_lamp(OFF,3);
  986. #endif
  987. //4.关闭震动电机
  988. ctr_shake(OFF, 0);
  989. //5.关闭加热
  990. ctr_Heat(OFF);
  991. //6.关闭补光灯
  992. ctr_led(OFF);
  993. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  994. {
  995. rt_kprintf("欠压!!!\n");
  996. return;
  997. }
  998. //7.关闭上仓门
  999. device_status.ctr_up_motor = 2;
  1000. while(device_status.ctr_up_motor!=0)
  1001. {
  1002. rt_kprintf("关闭上仓门...\n");
  1003. rt_thread_delay(100);
  1004. }
  1005. //8.关闭下仓门
  1006. device_status.ctr_down_motor = 2;
  1007. while(device_status.ctr_down_motor!=0)
  1008. {
  1009. rt_kprintf("关闭下仓门....\n");
  1010. rt_thread_delay(100);
  1011. }
  1012. //9.清扫
  1013. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1014. {
  1015. rt_kprintf("异常退出!!!\n");
  1016. return;
  1017. }
  1018. #if LAMP_EN == 0
  1019. ctr_Fan_PUT(OFF);
  1020. ctr_Fan_IN(OFF);
  1021. #endif
  1022. if(device_status.version_type==4||device_status.version_type==41)
  1023. {
  1024. // if(ctr_clear(650)==0)
  1025. // ctr_TG_Clear(400);
  1026. device_status.clear_status=1;
  1027. }
  1028. else if (device_status.version_type==1) {
  1029. turnzp();
  1030. }
  1031. rt_thread_delay(1000);
  1032. }
  1033. else {
  1034. //1.打开路由器供电
  1035. ctr_router(ON);
  1036. ctr_RTU_DTU(ON);
  1037. //2.打开相机供电
  1038. ctr_photo(ON);
  1039. //5.关闭加热
  1040. //6.关闭补光灯
  1041. ctr_led(OFF);
  1042. }
  1043. }
  1044. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1045. void HardWare_Check(uint8_t boot)
  1046. {
  1047. if(boot==0&&device_status.version_type!=6)
  1048. {
  1049. //停止所有动作 -完成
  1050. HardWare_Init(boot);
  1051. }
  1052. else {
  1053. //停止所有动作
  1054. //读取记录待机功耗(实测大概70mA)
  1055. //(需减去屏幕的功耗大概120mA)
  1056. //动作上仓门,读取功率值保存
  1057. //动作下仓门,读取功率值保存
  1058. //动作清扫,读取功率值保存
  1059. //打开镇流器,读取功率值保存
  1060. //打开海康相机供电,读取功率值保存
  1061. //打开补光灯,读取功率值保存
  1062. //打开路由器/RTU供电,读取功率值保存
  1063. //打开加热,读取功率值保存
  1064. }
  1065. }
  1066. //工作状态检测
  1067. void WorkCheck(void)
  1068. {
  1069. //雨控检测,温控检测,电池电压检测
  1070. while(device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1071. {
  1072. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1073. if(device_status.timeSwitch==1)
  1074. {
  1075. rt_kprintf("***************时控*****************\n");
  1076. //如果开始和结束是同一天
  1077. if(device_status.startBar<device_status.stopBar)
  1078. {
  1079. rt_kprintf("开始和结束是同一天");
  1080. time_t now;
  1081. time(&now);
  1082. static struct tm nowtime; //现在
  1083. static struct tm starttime; //开始
  1084. static struct tm endtime; //结束
  1085. //获取当前时间戳
  1086. nowtime =*localtime(&now);
  1087. nowtime.tm_hour = nowtime.tm_hour-8;
  1088. //获取定时开始的时间戳
  1089. starttime = *localtime(&now);
  1090. starttime.tm_hour = device_status.startBar-8;
  1091. starttime.tm_min = 0;
  1092. starttime.tm_sec = 0;
  1093. //获取定时结束的时间戳
  1094. endtime = *localtime(&now);
  1095. endtime.tm_hour = device_status.stopBar-8;
  1096. endtime.tm_min = 0;
  1097. endtime.tm_sec = 0;
  1098. rt_kprintf("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1099. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1100. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1101. {
  1102. rt_kprintf("当前时间大于开始时间,小于结束时间,工作中!!!\n");
  1103. device_status.work_status = 1;
  1104. }
  1105. //如果当前时间小于开始时间
  1106. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1107. {
  1108. rt_kprintf("当前时间小于开始时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
  1109. device_status.work_status = 0;
  1110. }
  1111. //当前时间大于结束时间
  1112. else {
  1113. rt_kprintf("当前时间大于结束时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
  1114. device_status.work_status = 0;
  1115. }
  1116. }
  1117. //如果结束时间是第二天
  1118. else if (device_status.startBar>device_status.stopBar) {
  1119. time_t now;
  1120. time(&now);
  1121. static struct tm nowtime; //现在
  1122. static struct tm starttime; //开始
  1123. static struct tm endtime; //结束
  1124. //获取当前时间戳
  1125. nowtime =*localtime(&now);
  1126. nowtime.tm_hour = nowtime.tm_hour-8;
  1127. //获取定时开始的时间戳
  1128. starttime = *localtime(&now);
  1129. starttime.tm_hour = device_status.startBar-8;
  1130. starttime.tm_min = 0;
  1131. starttime.tm_sec = 0;
  1132. //获取定时结束的时间戳
  1133. endtime = *localtime(&now);
  1134. endtime.tm_hour = device_status.stopBar-8;
  1135. endtime.tm_min = 0;
  1136. endtime.tm_sec = 0;
  1137. rt_kprintf("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1138. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1139. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1140. {
  1141. //获取定时结束的时间戳
  1142. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1143. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1144. endtime.tm_min = 0;
  1145. endtime.tm_sec = 0;
  1146. device_status.work_status = 1;
  1147. rt_kprintf("当前前时间大于开始时间,且没有开始工作\n");
  1148. }
  1149. //当前时间小于开始时间
  1150. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1151. {
  1152. //获取定时结束的时间戳
  1153. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1154. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1155. endtime.tm_min = 0;
  1156. endtime.tm_sec = 0;
  1157. //如果当前时间戳小于结束时间戳
  1158. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1159. {
  1160. rt_kprintf("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间\n");
  1161. device_status.work_status = 1;
  1162. }
  1163. else
  1164. {
  1165. rt_kprintf("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间!!!\n");
  1166. device_status.work_status = 0; //不在工作时间,待机
  1167. }
  1168. }
  1169. }
  1170. }
  1171. else {
  1172. rt_kprintf("光控定时模式!!!\n");
  1173. /*
  1174. time_t now;
  1175. time(&now);
  1176. static struct tm nowstamp;
  1177. nowstamp =*localtime(&now);
  1178. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1179. static rt_uint32_t nowstamp_num = 0;
  1180. nowstamp_num = mktime(&nowstamp);
  1181. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1182. */
  1183. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1184. {
  1185. device_status.light_over_tim--;
  1186. #if LAMP_EN == 1
  1187. if(device_status.light_over_tim>device_status.light_over_tim-6*3600)
  1188. {
  1189. device_status.work_status = 1;
  1190. device_status.lamp_status = 1;
  1191. device_status.lamp1_status = 0;
  1192. device_status.lamp2_status = 0;
  1193. rt_kprintf("光控定时,计时中:光源1计数:%d\n",device_status.light_over_tim);
  1194. }
  1195. else if (device_status.light_over_tim<device_status.light_over_tim-6*3600&&device_status.light_over_tim>3*3600) {
  1196. device_status.work_status = 1;
  1197. device_status.lamp_status = 0;
  1198. device_status.lamp1_status = 1;
  1199. device_status.lamp2_status = 0;
  1200. rt_kprintf("光控定时,计时中:光源2计数:%d\n",device_status.light_over_tim);
  1201. }
  1202. else if (device_status.light_over_tim<3*3600) {
  1203. device_status.work_status = 1;
  1204. device_status.lamp_status = 0;
  1205. device_status.lamp1_status = 0;
  1206. device_status.lamp2_status = 1;
  1207. rt_kprintf("光控定时,计时中:光源3计数:%d\n",device_status.light_over_tim);
  1208. }
  1209. #else
  1210. device_status.work_status = 1;;
  1211. rt_kprintf("光控定时,计时中>>>>>>>>>:%d\n",device_status.light_over_tim);
  1212. #endif
  1213. }
  1214. else
  1215. {
  1216. if(device_status.test_flag==0)
  1217. {
  1218. ctr_lamp(OFF,1);
  1219. #if LAMP_EN == 1
  1220. ctr_lamp(OFF,2);
  1221. ctr_lamp(OFF,3);
  1222. #endif
  1223. }
  1224. device_status.work_status = 0;
  1225. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1226. rt_kprintf("光控中,白天:%d!!!\n",device_status.sun_status);
  1227. }
  1228. }
  1229. rt_thread_delay(1000);
  1230. }
  1231. rt_kprintf("雨控状态,温控状态,电压状态:\n%d\n%d\n%d\n",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1232. device_status.work_status = 0;
  1233. rt_thread_delay(1000);
  1234. }
  1235. //工作任务流程s
  1236. void WorkTask(void)
  1237. {
  1238. ctr_router(ON); //打开路由器供电
  1239. static int clt_time = 0; //收集时间差秒数
  1240. static uint16_t controltime = 6;
  1241. rt_kprintf("工作状态:%d\n",device_status.work_status);
  1242. rt_kprintf("开关机状态:%d\n",device_status.ds);
  1243. 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)
  1244. {
  1245. device_status.work_done = 0;
  1246. if(clt_time==0)
  1247. {
  1248. #if LAMP_EN == 0
  1249. ctr_Fan_PUT(ON);
  1250. #endif
  1251. DTU_Write("workstart.mp3");
  1252. }
  1253. //1.打开灯管
  1254. #if LAMP_EN == 1
  1255. if(device_status.lamp_status==1)
  1256. {
  1257. ctr_lamp(ON,1);
  1258. ctr_lamp(OFF,2);
  1259. ctr_lamp(OFF,3);
  1260. }
  1261. else if (device_status.lamp1_status==1) {
  1262. ctr_lamp(OFF,1);
  1263. ctr_lamp(ON,2);
  1264. ctr_lamp(OFF,3);
  1265. }
  1266. else if (device_status.lamp2_status==1) {
  1267. ctr_lamp(OFF,1);
  1268. ctr_lamp(OFF,2);
  1269. ctr_lamp(ON,3);
  1270. }
  1271. #else
  1272. ctr_lamp(ON,1);
  1273. #endif
  1274. rt_kprintf("打开灯管!!!\n");
  1275. if(device_status.version_type!=5)
  1276. {
  1277. rt_thread_delay(5000); //待灯管启动再往下执行,否则会触发电机过流
  1278. //2.打开上仓门
  1279. rt_kprintf("打开上仓门!!!\n");
  1280. device_status.ctr_up_motor = 1;
  1281. //3.收集
  1282. clt_time = device_status.clt_time * 60;
  1283. while(clt_time!=1)
  1284. {
  1285. #if LAMP_EN == 0
  1286. ctr_Fan_IN(ON);
  1287. #endif
  1288. clt_time --;
  1289. rt_thread_delay(1000);
  1290. #if LAMP_EN == 0
  1291. if(clt_time%30==0)
  1292. ctr_Fan_PUT(OFF);
  1293. #endif
  1294. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  1295. //如果触发异常
  1296. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1297. {
  1298. ctr_lamp(OFF,1);
  1299. #if LAMP_EN == 1
  1300. ctr_lamp(OFF,2);
  1301. ctr_lamp(OFF,3);
  1302. #endif
  1303. #if LAMP_EN == 0
  1304. ctr_Fan_IN(OFF);
  1305. #endif
  1306. rt_kprintf("异常退出!!!\n");
  1307. break;
  1308. }
  1309. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1310. {
  1311. ctr_lamp(OFF,1);
  1312. #if LAMP_EN == 1
  1313. ctr_lamp(OFF,2);
  1314. ctr_lamp(OFF,3);
  1315. #endif
  1316. #if LAMP_EN == 0
  1317. ctr_Fan_IN(OFF);
  1318. #endif
  1319. rt_kprintf("异常退出!!!\n");
  1320. break;
  1321. }
  1322. }
  1323. clt_time = 1;
  1324. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1325. {
  1326. ctr_lamp(OFF,1);
  1327. #if LAMP_EN == 1
  1328. ctr_lamp(OFF,2);
  1329. ctr_lamp(OFF,3);
  1330. #endif
  1331. #if LAMP_EN == 0
  1332. ctr_Fan_IN(OFF);
  1333. #endif
  1334. rt_kprintf("异常退出!!!\n");
  1335. break;
  1336. }
  1337. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1338. {
  1339. ctr_lamp(OFF,1);
  1340. #if LAMP_EN == 1
  1341. ctr_lamp(OFF,2);
  1342. ctr_lamp(OFF,3);
  1343. #endif
  1344. #if LAMP_EN == 0
  1345. ctr_Fan_IN(OFF);
  1346. #endif
  1347. rt_kprintf("异常退出!!!\n");
  1348. break;
  1349. }
  1350. #if LAMP_EN == 0
  1351. ctr_Fan_IN(OFF);
  1352. #endif
  1353. //4.关闭上仓门
  1354. device_status.ctr_up_motor = 2;
  1355. rt_kprintf("关闭上仓门!!!\n");
  1356. // while(device_status.ctr_up_motor!=0)
  1357. // {
  1358. // rt_thread_delay(100);
  1359. // rt_kprintf("等待上仓门关闭!!!\n");
  1360. // }
  1361. //5.加热
  1362. rt_kprintf("加热!!!\n");
  1363. calorstat(device_status.heattime*60);
  1364. //6.打开下仓门
  1365. if(device_status.rain_status==0)
  1366. {
  1367. device_status.ctr_down_motor =1;
  1368. }
  1369. else {
  1370. device_status.ctr_down_motor = 2;
  1371. device_status.ctr_up_motor =2;
  1372. }
  1373. //待下仓门完全打开
  1374. while(device_status.ctr_down_motor!=0)
  1375. {
  1376. rt_thread_delay(100);
  1377. rt_kprintf("等待下仓门打开!!!\n");
  1378. }
  1379. rt_thread_delay(3000);
  1380. //关闭下仓门
  1381. device_status.ctr_down_motor = 2;
  1382. //2022-04-12add:
  1383. rt_kprintf("打开上仓门!!!\n");
  1384. device_status.ctr_up_motor = 1;
  1385. while(device_status.ctr_down_motor!=0)
  1386. {
  1387. rt_thread_delay(100);
  1388. rt_kprintf("等待下仓门关闭!!!\n");
  1389. }
  1390. if(device_status.version_type==4||device_status.version_type==41)
  1391. {
  1392. //震动
  1393. ctr_shake(ON, 1);
  1394. //补光&&拍照
  1395. rt_kprintf("拍照!!!\n");
  1396. HK_TakePhoto();
  1397. //清扫
  1398. device_status.clear_status=1;
  1399. //清扫失效则使用震动
  1400. //ctr_shake(ON, 100);
  1401. }
  1402. }
  1403. else {
  1404. //打开补光
  1405. ctr_led(ON);
  1406. rt_kprintf("打开补光!!!\n");
  1407. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1408. //2.翻转清虫
  1409. if (rt_pin_read(CLEAR_L_LIMIT)==0) //判断是否有触发信号
  1410. {
  1411. while(controltime!=0)
  1412. {
  1413. controltime--;
  1414. rt_thread_delay(1000);
  1415. rt_kprintf("延时中:%d\n",controltime);
  1416. }
  1417. controltime = 6;
  1418. turnzp(); //翻转
  1419. rt_kprintf("翻转清虫!!!\n");
  1420. }
  1421. }
  1422. }
  1423. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1||device_status.ds==0)
  1424. {
  1425. rt_kprintf("-------------------not work--------------------\n");
  1426. device_status.work_status = 0;
  1427. }
  1428. //工作结束
  1429. if(device_status.work_done==0&&device_status.work_status==0)
  1430. {
  1431. DTU_Write("workdone");
  1432. ResetInit();
  1433. }
  1434. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  1435. }
  1436. /* 入口函数-工作状态检测 */
  1437. static void Work_status_thread_entry(void *parameter)
  1438. {
  1439. static uint16_t sun_status_num = 120;
  1440. //如果是光控定时,进行光控监测的消抖
  1441. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1442. {
  1443. rt_kprintf("光控定时!!!\n");
  1444. while(sun_status_num!=0)
  1445. {
  1446. sun_status_num--;
  1447. rt_thread_delay(1000);
  1448. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1449. }
  1450. /*//2022-02-26-remove
  1451. time_t now;
  1452. time(&now);
  1453. static struct tm nowstamp;
  1454. nowstamp =*localtime(&now);
  1455. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1456. static rt_uint32_t nowstamp_num = 0;
  1457. nowstamp_num = mktime(&nowstamp);
  1458. ////2022-02-26-remove
  1459. */
  1460. device_status.light_over_tim = device_status.sunBar * 3600;
  1461. device_status.test_flag = 0;
  1462. }
  1463. while(1)
  1464. {
  1465. WorkCheck();
  1466. rt_thread_delay(1);
  1467. }
  1468. }
  1469. /*入口函数-工作流程*/
  1470. static void Work_Loop_thread_entry(void *parameter)
  1471. {
  1472. rt_thread_delay(1000);
  1473. ResetInit();
  1474. while(1)
  1475. {
  1476. if(device_status.version_type!=6)
  1477. {
  1478. WorkTask();
  1479. }
  1480. else {
  1481. WorkTask_XCT();
  1482. }
  1483. rt_thread_delay(1000);
  1484. }
  1485. }
  1486. /*入口函数清扫*/
  1487. static void Control_Clear_thread_entry(void *parameter)
  1488. {
  1489. while(1)
  1490. {
  1491. #if ROBOT_EN ==1
  1492. ctr_Motor_Robot(ON);
  1493. #else
  1494. if(device_status.clear_status==1)
  1495. {
  1496. if(ctr_clear(1200)==0)
  1497. ctr_TG_Clear(400);
  1498. }
  1499. #endif
  1500. rt_thread_delay(100);
  1501. }
  1502. }
  1503. /**/
  1504. int Control_clear(void)
  1505. {
  1506. /* 创建线程*/
  1507. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1508. Control_Clear_thread_entry, RT_NULL,
  1509. 512, //2048
  1510. 13, //23
  1511. WORK_CHECK_THREAD_TIMESLICE); //5
  1512. if(tid_ctr!=RT_NULL)
  1513. {
  1514. rt_thread_startup(tid_ctr);
  1515. }
  1516. return 0;
  1517. }
  1518. /* 创建线程 -是否置位工作状态*/
  1519. int Workstatus_check(void)
  1520. {
  1521. /* 创建线程*/
  1522. tid_wkstc = rt_thread_create("work_status_check_thread",
  1523. Work_status_thread_entry, RT_NULL,
  1524. WORK_CHECK_THREAD_STACK_SIZE, //2048
  1525. WORK_CHECK_THREAD_PRIORITY, //23
  1526. WORK_CHECK_THREAD_TIMESLICE); //5
  1527. if(tid_wkstc!=RT_NULL)
  1528. {
  1529. rt_thread_startup(tid_wkstc);
  1530. }
  1531. return 0;
  1532. }
  1533. /*工作流程线程*/
  1534. int Work_Loop_Task(void)
  1535. {
  1536. /* 创建线程*/
  1537. tid_workloop = rt_thread_create("work_loop_thread",
  1538. Work_Loop_thread_entry, RT_NULL,
  1539. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1540. WORK_LOOP_THREAD_PRIORITY, //12
  1541. WORK_LOOP_THREAD_TIMESLICE); //5
  1542. if(tid_workloop!=RT_NULL)
  1543. {
  1544. rt_thread_startup(tid_workloop);
  1545. }
  1546. return 0;
  1547. }
  1548. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  1549. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1550. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1551. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);