control.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include <app_pwm.h>
  2. #include <control.h>
  3. #include "hmi.h"
  4. #include "easyflash.h"
  5. #include "stdio.h"
  6. #include "sound.h"
  7. #include "dtu_uart2.h"
  8. #include "sht30_collect.h"
  9. #include <user_mb_app.h>
  10. #define DBG_ENABLE
  11. #define DBG_COLOR
  12. #define DBG_TAG "control"
  13. #define DBG_LEVEL DBG_LOG
  14. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  15. //工作检测
  16. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  17. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  18. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  19. //工作流程
  20. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  21. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  22. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  23. #define PWM_EN 1 //0关闭,1-打开
  24. //设备状态表结构体
  25. Device_Status_typedef device_status;
  26. rt_thread_t tid_workloop = RT_NULL;
  27. rt_thread_t tid_wkstc = RT_NULL;
  28. rt_thread_t tid_ctr = RT_NULL;
  29. rt_thread_t tid_heat = RT_NULL;
  30. rt_thread_t tid_485_play = RT_NULL;
  31. rt_event_t event_485_play;
  32. BOOL gaoya_hand;
  33. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  34. /***************************************************************************/
  35. int thread_delete_workloop(void)
  36. {
  37. rt_err_t result;
  38. if(tid_workloop!=RT_NULL)
  39. {
  40. result = rt_thread_delete(tid_workloop);
  41. }
  42. return result;
  43. }
  44. int thread_restart_workloop(void)
  45. {
  46. rt_err_t result;
  47. result = Work_Loop_Task();
  48. return result;
  49. }
  50. /**********************************************************************/
  51. /*光控*/
  52. uint8_t In_Light_status(void)
  53. {
  54. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  55. return rt_pin_read(LIGHT_PIN);
  56. }
  57. /*雨控*/
  58. uint8_t In_Rain_status(void)
  59. {
  60. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  61. return rt_pin_read(RAIN_PIN);
  62. }
  63. //补光
  64. uint8_t ctr_led(uint8_t status_led)
  65. {
  66. rt_pin_mode(LED_5V, PIN_MODE_OUTPUT);
  67. if(status_led==ON)
  68. {
  69. rt_pin_write(LED_5V,ON);
  70. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  71. }
  72. else if(status_led==OFF){
  73. rt_pin_write(LED_5V,OFF);
  74. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  75. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  76. }
  77. return status_led;
  78. }
  79. //路由器供电
  80. void ctr_router(uint8_t status_router)
  81. {
  82. rt_pin_mode(FourFaith_12V, PIN_MODE_OUTPUT);
  83. if(status_router==ON)
  84. {
  85. rt_pin_write(FourFaith_12V,ON);
  86. LOG_W("打开路由器电源!");
  87. }
  88. else {
  89. rt_pin_write(FourFaith_12V,OFF);
  90. LOG_W("关闭路由器电源!");
  91. }
  92. }
  93. //屏幕供电
  94. void ctr_hmi_5v(uint8_t sw)
  95. {
  96. rt_pin_mode(POWER_5V, PIN_MODE_OUTPUT);
  97. rt_pin_mode(SIGNAL_OUTPUT_LED, PIN_MODE_OUTPUT);
  98. if(sw==ON)
  99. {
  100. rt_pin_write(POWER_5V,ON);
  101. rt_pin_write(SIGNAL_OUTPUT_LED,OFF);
  102. LOG_W("打开5V电源!");
  103. device_status.hmi_status = 1;
  104. }
  105. else {
  106. rt_pin_write(POWER_5V,OFF);
  107. rt_pin_write(SIGNAL_OUTPUT_LED,ON);
  108. LOG_W("关闭5V电源!");
  109. device_status.hmi_status = 0;
  110. }
  111. }
  112. //高压包供电
  113. void ctr_gaoya_12v(uint8_t sw)
  114. {
  115. rt_pin_mode(GAOYA_12V, PIN_MODE_OUTPUT);
  116. if(sw==ON)
  117. {
  118. rt_pin_write(GAOYA_12V,ON);
  119. LOG_W("打开高压包12V电源!");
  120. }
  121. else {
  122. rt_pin_write(GAOYA_12V,OFF);
  123. LOG_W("关闭高压包12V电源!");
  124. }
  125. }
  126. //DTU,RTU供电
  127. void ctr_RTU_12V(uint8_t sw)
  128. {
  129. rt_pin_mode(RTU_12V, PIN_MODE_OUTPUT);
  130. if(sw==ON)
  131. {
  132. LOG_W("RTU电源启动!!!");
  133. rt_pin_write(RTU_12V,ON);
  134. device_status.rtu_status = 1;
  135. }
  136. else
  137. {
  138. LOG_W("RTU电源关闭!!!");
  139. rt_pin_write(RTU_12V,OFF);
  140. device_status.rtu_status = 0;
  141. }
  142. }
  143. void ctr_clear(void)
  144. {
  145. rt_pin_mode(DC_M1, PIN_MODE_OUTPUT);
  146. rt_pin_mode(DC_M2, PIN_MODE_OUTPUT);
  147. rt_pin_write(DC_M1,ON);
  148. rt_pin_write(DC_M2,OFF);
  149. rt_thread_delay(2000);
  150. rt_pin_write(DC_M1,OFF);
  151. rt_pin_write(DC_M2,OFF);
  152. rt_thread_delay(1000);
  153. rt_pin_write(DC_M1,OFF);
  154. rt_pin_write(DC_M2,ON);
  155. rt_thread_delay(2000);
  156. rt_pin_write(DC_M1,OFF);
  157. rt_pin_write(DC_M2,OFF);
  158. }
  159. void ctr_TG_Motor(uint8_t sw)
  160. {
  161. rt_pin_mode(DC_M21, PIN_MODE_OUTPUT);
  162. rt_pin_mode(DC_M22, PIN_MODE_OUTPUT);
  163. if(sw==OFF){
  164. rt_pin_write(DC_M21,OFF);
  165. rt_pin_write(DC_M22,OFF);
  166. }else if(sw==OPEN){
  167. rt_pin_write(DC_M21,ON);
  168. rt_pin_write(DC_M22,OFF);
  169. }else if(sw==CLOSE){
  170. rt_pin_write(DC_M21,OFF);
  171. rt_pin_write(DC_M22,ON);
  172. }
  173. }
  174. /*上电初始化*/
  175. void ResetInit(void)
  176. {
  177. LOG_I("ResetInit...");
  178. //1.打开路由器供电
  179. #if V2000W_EN == 0
  180. ctr_router(ON);
  181. #else
  182. ctr_gaoya_12v(ON); //相机供电
  183. ctr_TG_Motor(OPEN);
  184. #endif
  185. ctr_hmi_5v(ON);
  186. //2. RTU供电默认是开的,不能调用ctr_RTU_12V(ON);
  187. device_status.rtu_status = 1;
  188. //3.打开光栅供电
  189. ctr_led(OFF);
  190. }
  191. void WorkCheck(void)
  192. {
  193. static rt_uint32_t light_timeout = 3600;
  194. while(device_status.ds == 1 &&
  195. device_status.rain_status == 0 &&
  196. device_status.temp_status == 0 &&
  197. device_status.bat_status == 0)
  198. {
  199. if(device_status.timeSwitch == 1) // 时控模式
  200. {
  201. LOG_I("***********时控*****************");
  202. if(device_status.startBar == device_status.stopBar)
  203. {
  204. // 开始结束时间相同则24小时模式
  205. device_status.work_status = 1;
  206. rt_thread_delay(10000);
  207. continue;
  208. }
  209. time_t now;
  210. time(&now);
  211. struct tm now_tm = *localtime(&now);
  212. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  213. rt_uint32_t start_sec = device_status.startBar * 3600;
  214. rt_uint32_t end_sec = device_status.stopBar * 3600;
  215. // 修复跨天判断逻辑
  216. if (start_sec < end_sec)
  217. {
  218. // 不跨天模式
  219. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  220. }
  221. else
  222. {
  223. // 跨天模式
  224. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  225. }
  226. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  227. device_status.work_status ? "工作" : "待机",
  228. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  229. device_status.startBar, device_status.stopBar);
  230. rt_thread_delay(10000);
  231. }
  232. else // 光控模式
  233. {
  234. LOG_I("光控定时模式!");
  235. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  236. {
  237. device_status.light_over_tim--;
  238. light_timeout = 3600; // 重置超时计数器
  239. device_status.work_status = 1;
  240. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  241. }
  242. else
  243. {
  244. // 仅在白天递减超时计数器
  245. if(device_status.sun_status == 1 && light_timeout > 0) {
  246. light_timeout--;
  247. }
  248. device_status.work_status = 0;
  249. //没有在测试并且没有在自检
  250. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  251. {
  252. }
  253. // 连续1小时白天后重置
  254. if(light_timeout == 0) {
  255. device_status.light_over_tim = 0;
  256. light_timeout = 3600; // 重置防止负数
  257. }
  258. // 白天且倒计时结束:重置夜晚时长
  259. if(device_status.light_over_tim == 0 &&
  260. device_status.sun_status == 1)
  261. {
  262. device_status.light_over_tim = device_status.sunBar * 3600;
  263. }
  264. }
  265. }
  266. rt_thread_delay(1000);
  267. }
  268. // 异常状态处理
  269. device_status.work_status = 0;
  270. rt_thread_delay(1000);
  271. }
  272. void guanshan_on(void *args)
  273. {
  274. LOG_W("guanshan_on!");
  275. //if(gaoya_hand){
  276. device_status.all_ycct++;
  277. // gaoya_hand = FALSE;
  278. // }
  279. //rt_event_send(event_485_play, 0x01);
  280. }
  281. void button_on(void *args)
  282. {
  283. LOG_W("button_on!");
  284. if(device_status.sleep_mode==1&&device_status.rtu_status==0) ctr_RTU_12V(ON);
  285. if(device_status.hmi_status==0) ctr_hmi_5v(ON);
  286. //rt_event_send(event_485_play, 0x01);
  287. }
  288. void pin_button_bind(void)
  289. {
  290. LOG_W("pin_button_bind");
  291. /* PA11引脚为输入模式 */
  292. rt_pin_mode(SIGNAL_INPUT_KEY, PIN_MODE_INPUT);
  293. /* 绑定中断,上升沿沿模式,回调函数名为guanshan_on */
  294. rt_pin_attach_irq(SIGNAL_INPUT_KEY, PIN_IRQ_MODE_FALLING, button_on, RT_NULL);
  295. /* 使能中断 */
  296. rt_pin_irq_enable(SIGNAL_INPUT_KEY, PIN_IRQ_ENABLE);
  297. }
  298. void pin_guangshan_bind(void)
  299. {
  300. LOG_W("pin_guangshan_bind");
  301. /* PC14引脚为输入模式 */
  302. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  303. /* 绑定中断,下降沿模式,回调函数名为guanshan_on */
  304. rt_pin_attach_irq(SIGNAL_GuangShan, PIN_IRQ_MODE_RISING, guanshan_on, RT_NULL);
  305. /* 使能中断 */
  306. rt_pin_irq_enable(SIGNAL_GuangShan, PIN_IRQ_ENABLE);
  307. }
  308. void pin_guangshan_unbind(void)
  309. {
  310. LOG_D("pin_guangshan_unbind");
  311. rt_pin_detach_irq(SIGNAL_GuangShan);
  312. }
  313. //拍照性诱工作任务流程
  314. void WorkTask_XYV3(void)
  315. {
  316. static uint8_t gaoya_on = 0;
  317. static uint8_t gaoya_on_sec = 0;
  318. rt_uint32_t cal_n = device_status.cul_time * 60;
  319. LOG_I("工作状态:%d",device_status.work_status);
  320. LOG_I("开关机状态:%d",device_status.ds);
  321. LOG_I("温控状态:%d",device_status.temp_status);
  322. LOG_I("雨控状态:%d",device_status.rain_status);
  323. while(device_status.work_status==1&&
  324. device_status.bat_status==0&&
  325. device_status.ds==1&&
  326. device_status.temp_status==0)
  327. {
  328. //高压通电
  329. ctr_gaoya_12v(ON);
  330. //光栅传感器通电
  331. pin_guangshan_bind();
  332. //拍照(?触发条件)计数?没有虫呢?定时
  333. while(1){
  334. cal_n--;
  335. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  336. if(cal_n==0){
  337. //拍照时关闭高压
  338. //发送拍照计数指令
  339. device_status.photo_ws = 2;
  340. cal_n = device_status.cul_time * 60;
  341. }
  342. if(cal_n%2==0) //每隔2S钟高压通电5S
  343. {
  344. // ctr_router(ON);
  345. // ctr_RTU_12V(ON);
  346. ctr_gaoya_12v(ON);
  347. gaoya_on = 1;
  348. }
  349. if(device_status.sleep_mode==1){
  350. if(cal_n==100){
  351. ctr_RTU_12V(ON);
  352. }
  353. else if (cal_n==3400) {
  354. ctr_RTU_12V(OFF);
  355. }
  356. }
  357. if(gaoya_on){
  358. gaoya_on_sec++;
  359. if(gaoya_on_sec>5){
  360. gaoya_on=0;
  361. gaoya_on_sec = 0;
  362. ctr_gaoya_12v(OFF);
  363. }
  364. }
  365. if(device_status.temp_status==1 || device_status.bat_status==1 || device_status.work_status==0){
  366. ctr_gaoya_12v(OFF);
  367. device_status.photo_ws = 2;
  368. break;
  369. }
  370. rt_thread_delay(1000);
  371. }
  372. rt_thread_delay(10000);
  373. }
  374. }
  375. /* 入口函数-工作状态检测 */
  376. static void Work_status_thread_entry(void *parameter)
  377. {
  378. while(1)
  379. {
  380. WorkCheck();
  381. }
  382. }
  383. /*入口函数-工作流程*/
  384. static void Work_Loop_thread_entry(void *parameter)
  385. {
  386. rt_thread_delay(1000);
  387. ResetInit();
  388. pin_button_bind();
  389. while(1)
  390. {
  391. WorkTask_XYV3();
  392. rt_thread_delay(1000);
  393. }
  394. }
  395. /*入口函数清扫*/
  396. static void Check_Self_thread_entry(void *parameter)
  397. {
  398. static int selfcheck_cnt = 0;
  399. rt_uint32_t e;
  400. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  401. if (event_485_play == RT_NULL)
  402. {
  403. LOG_E("Event create failed!");
  404. return;
  405. }
  406. while(1)
  407. {
  408. //等待事件
  409. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  410. {
  411. DTU_uart2_init();
  412. HMI_uart4_init();
  413. }
  414. if(device_status.selfcheck_ws==1){
  415. selfcheck_cnt++;
  416. }else{
  417. selfcheck_cnt = 0;
  418. }
  419. if(selfcheck_cnt>1200) //超过3分钟
  420. {
  421. device_status.selfcheck_ws = 0;
  422. device_status.camera_sta = 1; //相机异常
  423. //上报自检结果
  424. //上报自检结果
  425. char *json_temp = makeJson_selfCheck_(1);
  426. DTU_Write("%s",json_temp);
  427. free(json_temp);
  428. }
  429. if(device_status.clear_status==10){
  430. Motor_shake(1,800);
  431. device_status.clear_status = 0;
  432. }
  433. rt_thread_delay(100);
  434. }
  435. }
  436. /*入口函数*/
  437. static void Check_gaoya_thread_entry(void *parameter)
  438. {
  439. static int gaoya_value1 = 0;
  440. static int gaoya_value2 = 0;
  441. static uint32_t gaoya_cnt = 0;
  442. gaoya_hand = FALSE;
  443. while(1)
  444. {
  445. gaoya_value1 = Read_Fuse_Voltage();
  446. rt_thread_delay(50);
  447. gaoya_value2 = Read_Fuse_Voltage();
  448. if(gaoya_value2-gaoya_value1>50)
  449. {
  450. gaoya_cnt = 0;
  451. gaoya_hand = TRUE;
  452. LOG_I("高压触发:[%d,%d]",gaoya_value1,gaoya_value2);
  453. rt_thread_delay(1000);
  454. ctr_clear();
  455. device_status.gaoya_count++; //2026-07-02
  456. }
  457. if(gaoya_hand){
  458. gaoya_cnt ++;
  459. if(gaoya_cnt==20*25)
  460. {
  461. gaoya_cnt = 0;
  462. gaoya_hand = false;
  463. }
  464. }
  465. }
  466. }
  467. /*清扫线程*/
  468. int Check_Self(void)
  469. {
  470. /* 创建线程*/
  471. tid_ctr = rt_thread_create("Check_Self_thread_entry",
  472. Check_Self_thread_entry, RT_NULL,
  473. 2048, //2048
  474. 24, //23
  475. WORK_CHECK_THREAD_TIMESLICE); //5
  476. if(tid_ctr!=RT_NULL)
  477. {
  478. rt_thread_startup(tid_ctr);
  479. }
  480. return 0;
  481. }
  482. /*恒温加热线程*/
  483. int Check_gaoya(void)
  484. {
  485. /* 创建线程*/
  486. tid_heat = rt_thread_create("Check_gaoya_thread_entry",
  487. Check_gaoya_thread_entry, RT_NULL,
  488. 1024, //2048
  489. 25, //23
  490. WORK_CHECK_THREAD_TIMESLICE); //5
  491. if(tid_heat!=RT_NULL)
  492. {
  493. rt_thread_startup(tid_heat);
  494. }
  495. return 0;
  496. }
  497. /* 创建线程 -工作状态检测*/
  498. int Workstatus_check(void)
  499. {
  500. /* 创建线程*/
  501. tid_wkstc = rt_thread_create("work_status_check_thread",
  502. Work_status_thread_entry, RT_NULL,
  503. WORK_CHECK_THREAD_STACK_SIZE, //1024
  504. WORK_CHECK_THREAD_PRIORITY, //23
  505. WORK_CHECK_THREAD_TIMESLICE); //5
  506. if(tid_wkstc!=RT_NULL)
  507. {
  508. rt_thread_startup(tid_wkstc);
  509. }
  510. return 0;
  511. }
  512. /*工作流程线程*/
  513. int Work_Loop_Task(void)
  514. {
  515. rt_err_t result_loop = 0;
  516. /* 创建线程*/
  517. tid_workloop = rt_thread_create("work_loop_thread",
  518. Work_Loop_thread_entry, RT_NULL,
  519. WORK_LOOP_THREAD_STACK_SIZE, //2048
  520. WORK_LOOP_THREAD_PRIORITY, //12
  521. WORK_LOOP_THREAD_TIMESLICE); //5
  522. if(tid_workloop!=RT_NULL)
  523. {
  524. result_loop = rt_thread_startup(tid_workloop);
  525. }
  526. return result_loop;
  527. }
  528. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  529. //INIT_APP_EXPORT(Control_heat);