control.c 32 KB

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