control.c 46 KB

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