control.c 32 KB

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