control.c 32 KB

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