control.c 31 KB

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