control.c 32 KB

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