control.c 31 KB

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