control.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. #include <shell.h> /* finsh_set_device(): uart_stop_for_ota 要把 finsh 摘回 uart1 */
  11. #define DBG_ENABLE
  12. #define DBG_COLOR
  13. #define DBG_TAG "control"
  14. #define DBG_LEVEL DBG_LOG
  15. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  16. //工作检测
  17. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  18. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  19. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  20. //工作流程
  21. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  22. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  23. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  24. #define PWM_EN 1 //0关闭,1-打开
  25. //设备状态表结构体
  26. Device_Status_typedef device_status;
  27. rt_thread_t tid_workloop = RT_NULL;
  28. rt_thread_t tid_wkstc = RT_NULL;
  29. rt_thread_t tid_ctr = RT_NULL;
  30. rt_thread_t tid_heat = RT_NULL;
  31. rt_thread_t tid_485_play = RT_NULL;
  32. rt_event_t event_485_play;
  33. BOOL gaoya_hand;
  34. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  35. /***************************************************************************/
  36. int thread_delete_workloop(void)
  37. {
  38. rt_err_t result;
  39. if(tid_workloop!=RT_NULL)
  40. {
  41. result = rt_thread_delete(tid_workloop);
  42. }
  43. return result;
  44. }
  45. int thread_restart_workloop(void)
  46. {
  47. rt_err_t result;
  48. result = Work_Loop_Task();
  49. return result;
  50. }
  51. /* ==================== OTA 升级前的整机静默(与 PCB_V2 同一机制) ====================
  52. * 为什么必须停线程: ymodem_ota() 会把 console 切到 uart2(ymodem 数据通道),
  53. * 之后**任何**线程的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染固件字节流。
  54. * 本分支线程名清单按实际创建名给出; 内核只保留前 RT_NAME_MAX-1(=7) 个字符,
  55. * 所以下面的前缀都是 7 字符 —— 正好等价于全名匹配。
  56. * ⚠️ DTU_Uart_thread_send / DTU_Uart_thread_recv 截断后同为 "DTU_Uar",
  57. * 无法用名字区分; 发送线程一律由 dtu_stop_send_thread()(显式句柄)处理,
  58. * 接收线程是执行 upgrade 的自身, 由下面的 self 判断跳过。 */
  59. #define OTA_THREAD_MAX 16
  60. /* 只动这些线程(前缀, 7 字符以内) */
  61. static const char *g_ota_stop[] =
  62. {
  63. "work_lo", /* work_loop_thread 工作流程, LOG 密集 */
  64. "work_st", /* work_status_check_thread 工作状态, 每 10s 一批日志 */
  65. "Check_S", /* Check_Self_thread_entry 清扫, 驱动电机 */
  66. "Check_g", /* Check_gaoya_thread_entry 恒温, 每秒 LOG_I */
  67. "Hmi_Fre", /* Hmi_Fresh_thread 屏幕刷新 */
  68. "Ctr mot", /* Ctr motor 仓门/光源, 带 rt_kprintf */
  69. "adc_thr", /* adc_thread ADC 采集(已先由 thread_delete_adc 优雅停) */
  70. "Power C", /* Power Check thread 每秒读电流/电压 */
  71. "md_m_po", /* md_m_poll modbus 轮询 */
  72. "md_m_se", /* md_m_send modbus 发送 */
  73. "thread_", /* thread_485play 485 播报 */
  74. };
  75. /* 这些绝对不能删 */
  76. static const char *g_ota_keep[] =
  77. {
  78. "tshell", /* finsh 控制台 */
  79. "tidle", /* 空闲线程 */
  80. "main", /* 主线程: Check_ParamConfig 之后只剩 LED 心跳 */
  81. "timer", /* 软件定时器线程 */
  82. "syswatch", /* 看门狗守护 */
  83. "DTU_Ua", /* DTU 收发线程(recv 是自身, 由 self 判断再兜一层) */
  84. };
  85. /* 名字前缀比较: 内核只存了前 RT_NAME_MAX-1 个字符, 所以最多比这么多 */
  86. static int ota_name_match(const char *thread_name, const char *prefix)
  87. {
  88. rt_size_t n;
  89. if (thread_name == RT_NULL || prefix == RT_NULL || thread_name[0] == '\0')
  90. {
  91. return 0;
  92. }
  93. n = rt_strlen(prefix);
  94. if (n > RT_NAME_MAX - 1)
  95. {
  96. n = RT_NAME_MAX - 1;
  97. }
  98. if (n == 0)
  99. {
  100. return 0;
  101. }
  102. return (rt_strncmp(thread_name, prefix, n) == 0) ? 1 : 0;
  103. }
  104. static int ota_in_list(const char *thread_name, const char **list, int cnt)
  105. {
  106. int i;
  107. for (i = 0; i < cnt; i++)
  108. {
  109. if (ota_name_match(thread_name, list[i]))
  110. {
  111. return 1;
  112. }
  113. }
  114. return 0;
  115. }
  116. /**
  117. * OTA 升级前把所有业务线程停掉, 只留 tshell / idle / main / timer / DTU 收发。
  118. *
  119. * 顺序: ① adc_thread 优雅退出(它可能正持有 ADC 设备锁)
  120. * ② 收集待停线程(先收集再删, 不能边遍历边删)
  121. * ③ 逐个 rt_thread_delete
  122. *
  123. * @return 实际删除的线程数; 负值为错误
  124. */
  125. int thread_stop_all_for_ota(void)
  126. {
  127. struct rt_object_information *info;
  128. rt_list_t *node;
  129. rt_thread_t victims[OTA_THREAD_MAX];
  130. rt_thread_t self;
  131. int nv = 0, i, done = 0;
  132. info = rt_object_get_information(RT_Object_Class_Thread);
  133. if (info == RT_NULL)
  134. {
  135. LOG_E("[ota] thread object information NOT found");
  136. return -RT_ERROR;
  137. }
  138. /* ① adc_thread 必须优雅退出: 直接 rt_thread_delete 若刚好删在
  139. rt_adc_enable/read 中间, 它持有的 ADC 设备锁永远不会释放 */
  140. thread_delete_adc();
  141. /* ② 先收集后删除: rt_thread_delete 会摘掉链表节点,
  142. 边遍历边删会把 object_list 走坏 */
  143. self = rt_thread_self();
  144. rt_enter_critical();
  145. rt_list_for_each(node, &info->object_list)
  146. {
  147. rt_thread_t t = rt_list_entry(node, struct rt_thread, list);
  148. if (t == self) /* 不能删自己 */
  149. {
  150. continue;
  151. }
  152. if (ota_in_list(t->name, g_ota_keep, sizeof(g_ota_keep) / sizeof(g_ota_keep[0])))
  153. {
  154. continue;
  155. }
  156. if (!ota_in_list(t->name, g_ota_stop, sizeof(g_ota_stop) / sizeof(g_ota_stop[0])))
  157. {
  158. continue; /* 不在名单里, 不动 */
  159. }
  160. if (nv < OTA_THREAD_MAX)
  161. {
  162. victims[nv++] = t;
  163. }
  164. }
  165. rt_exit_critical();
  166. /* ③ 逐个删除 */
  167. for (i = 0; i < nv; i++)
  168. {
  169. if (rt_thread_delete(victims[i]) == RT_EOK)
  170. {
  171. done++;
  172. }
  173. }
  174. LOG_I("[ota] stopped %d of %d candidate thread(s)", done, nv);
  175. return done;
  176. }
  177. /* 关闭一个串口: 先摘接收回调, 再循环 close 到引用计数归零 */
  178. static void ota_close_uart(rt_device_t dev, const char *name)
  179. {
  180. rt_uint8_t guard = 8;
  181. if (dev == RT_NULL)
  182. {
  183. return;
  184. }
  185. /* ① 先摘掉接收回调。close() 内部也会清, 但清回调后到 close 之间
  186. 还有窗口期, 此刻若有数据到达, 回调里会 release 已经没人等的
  187. 信号量 / 往已删线程的消息队列里塞消息。 */
  188. rt_device_set_rx_indicate(dev, RT_NULL);
  189. /* ② 循环 close 到引用计数归零。guard 兜底, 防止内核行为异常时空转。 */
  190. while (dev->ref_count > 0 && guard-- > 0)
  191. {
  192. if (rt_device_close(dev) != RT_EOK)
  193. {
  194. break;
  195. }
  196. }
  197. LOG_I("[ota] %s closed, ref_count=%d", name, (int)dev->ref_count);
  198. }
  199. /**
  200. * OTA 升级前关闭串口4(HMI屏) 和串口5(485)。
  201. *
  202. * 为什么要关:
  203. * 1) ymodem_ota() 内部会 rt_console_set_device("uart2"), 之后 console 指向
  204. * uart2。此时 uart4/uart5 若还在工作, 它们的 ISR、以及仍可能残留的
  205. * 485 播报/屏幕刷新, 都会和 ymodem 抢 CPU 与中断, 导致传输丢字节;
  206. * 2) 485 是**半双工**总线, 升级完复位后从机状态与 MCU 不同步, 现场表现为
  207. * "升级完 485 读不到数据";
  208. * 3) 屏幕在升级过程中继续刷新, 白白占用带宽, 且部分屏会因长时间收不到
  209. * 指令而重新初始化自身。
  210. *
  211. * ★ 本函数必须在 thread_stop_all_for_ota() **之后**调用:
  212. * 先停线程(没人再发数据) → 再关设备。反过来的话, 关设备的瞬间还有
  213. * 线程在 HMI_WRITE/Uart5_Write, 会写到已被 disable 的 fifo 上。
  214. *
  215. * @return 关闭的串口个数
  216. */
  217. int uart_stop_for_ota(void)
  218. {
  219. int n = 0;
  220. /* 置标志必须**先于**关设备: 中间还有别的线程可能正在 HMI_WRITE,
  221. 看到标志后直接 return, 不会碰到正在被拆掉的 fifo。 */
  222. g_hmi_port_closed = 1;
  223. /* ---- 串口4: HMI 屏 ----
  224. 先把 console / finsh 摘回 uart1, 否则它们各自持有 uart4 的一次 open
  225. (rt_console_set_device / finsh_set_device 内部都是 open 新的 + close 旧的),
  226. 引用计数降不到 0, 设备等于没关; 而且 tshell 正阻塞在 uart4 的 read 上。 */
  227. if (rt_console_get_device() != RT_NULL &&
  228. rt_strcmp(rt_console_get_device()->parent.name, UART_NAME) == 0)
  229. {
  230. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  231. }
  232. #if defined(RT_USING_FINSH) && !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  233. finsh_set_device(RT_CONSOLE_DEVICE_NAME);
  234. #endif
  235. if (serial != RT_NULL)
  236. {
  237. ota_close_uart(serial, UART_NAME);
  238. n++;
  239. }
  240. /* ---- 串口5: 485 ---- */
  241. if (serial_485 != RT_NULL)
  242. {
  243. ota_close_uart(serial_485, UART_485);
  244. n++;
  245. }
  246. /* 把句柄置 NULL: 让上层即使漏判 g_hmi_port_closed 也不会往已废设备写。
  247. ⚠️ 置 NULL 之后 ymodem 失败返回也不会有任何 HMI/485 输出, 这是预期的
  248. ——升级流程本来就要求这段时间对外静默。 */
  249. serial = RT_NULL;
  250. serial_485 = RT_NULL;
  251. LOG_I("[ota] uart4/uart5 stopped (%d)", n);
  252. return n;
  253. }
  254. /**********************************************************************/
  255. /*光控*/
  256. uint8_t In_Light_status(void)
  257. {
  258. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  259. return rt_pin_read(LIGHT_PIN);
  260. }
  261. /*雨控*/
  262. uint8_t In_Rain_status(void)
  263. {
  264. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  265. return rt_pin_read(RAIN_PIN);
  266. }
  267. //补光
  268. uint8_t ctr_led(uint8_t status_led)
  269. {
  270. rt_pin_mode(LED_5V, PIN_MODE_OUTPUT);
  271. if(status_led==ON)
  272. {
  273. rt_pin_write(LED_5V,ON);
  274. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  275. }
  276. else if(status_led==OFF){
  277. rt_pin_write(LED_5V,OFF);
  278. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  279. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  280. }
  281. return status_led;
  282. }
  283. //路由器供电
  284. void ctr_router(uint8_t status_router)
  285. {
  286. rt_pin_mode(FourFaith_12V, PIN_MODE_OUTPUT);
  287. if(status_router==ON)
  288. {
  289. rt_pin_write(FourFaith_12V,ON);
  290. LOG_W("打开路由器电源!");
  291. }
  292. else {
  293. rt_pin_write(FourFaith_12V,OFF);
  294. LOG_W("关闭路由器电源!");
  295. }
  296. }
  297. //屏幕供电
  298. void ctr_hmi_5v(uint8_t sw)
  299. {
  300. rt_pin_mode(POWER_5V, PIN_MODE_OUTPUT);
  301. rt_pin_mode(SIGNAL_OUTPUT_LED, PIN_MODE_OUTPUT);
  302. if(sw==ON)
  303. {
  304. rt_pin_write(POWER_5V,ON);
  305. rt_pin_write(SIGNAL_OUTPUT_LED,OFF);
  306. LOG_W("打开5V电源!");
  307. device_status.hmi_status = 1;
  308. }
  309. else {
  310. rt_pin_write(POWER_5V,OFF);
  311. rt_pin_write(SIGNAL_OUTPUT_LED,ON);
  312. LOG_W("关闭5V电源!");
  313. device_status.hmi_status = 0;
  314. }
  315. }
  316. //高压包供电
  317. void ctr_gaoya_12v(uint8_t sw)
  318. {
  319. rt_pin_mode(GAOYA_12V, PIN_MODE_OUTPUT);
  320. if(sw==ON)
  321. {
  322. rt_pin_write(GAOYA_12V,ON);
  323. LOG_W("打开高压包12V电源!");
  324. }
  325. else {
  326. rt_pin_write(GAOYA_12V,OFF);
  327. LOG_W("关闭高压包12V电源!");
  328. }
  329. }
  330. //DTU,RTU供电
  331. void ctr_RTU_12V(uint8_t sw)
  332. {
  333. rt_pin_mode(RTU_12V, PIN_MODE_OUTPUT);
  334. if(sw==ON)
  335. {
  336. LOG_W("RTU电源启动!!!");
  337. rt_pin_write(RTU_12V,ON);
  338. device_status.rtu_status = 1;
  339. }
  340. else
  341. {
  342. LOG_W("RTU电源关闭!!!");
  343. rt_pin_write(RTU_12V,OFF);
  344. device_status.rtu_status = 0;
  345. }
  346. }
  347. void ctr_clear(void)
  348. {
  349. rt_pin_mode(DC_M1, PIN_MODE_OUTPUT);
  350. rt_pin_mode(DC_M2, PIN_MODE_OUTPUT);
  351. rt_pin_write(DC_M1,ON);
  352. rt_pin_write(DC_M2,OFF);
  353. rt_thread_delay(2000);
  354. rt_pin_write(DC_M1,OFF);
  355. rt_pin_write(DC_M2,OFF);
  356. rt_thread_delay(1000);
  357. rt_pin_write(DC_M1,OFF);
  358. rt_pin_write(DC_M2,ON);
  359. rt_thread_delay(2000);
  360. rt_pin_write(DC_M1,OFF);
  361. rt_pin_write(DC_M2,OFF);
  362. }
  363. void ctr_TG_Motor(uint8_t sw)
  364. {
  365. rt_pin_mode(DC_M21, PIN_MODE_OUTPUT);
  366. rt_pin_mode(DC_M22, PIN_MODE_OUTPUT);
  367. if(sw==OFF){
  368. rt_pin_write(DC_M21,OFF);
  369. rt_pin_write(DC_M22,OFF);
  370. }else if(sw==OPEN){
  371. rt_pin_write(DC_M21,ON);
  372. rt_pin_write(DC_M22,OFF);
  373. }else if(sw==CLOSE){
  374. rt_pin_write(DC_M21,OFF);
  375. rt_pin_write(DC_M22,ON);
  376. }
  377. }
  378. /*上电初始化*/
  379. void ResetInit(void)
  380. {
  381. LOG_I("ResetInit...");
  382. //1.打开路由器供电
  383. #if V2000W_EN == 0
  384. ctr_router(ON);
  385. #else
  386. ctr_gaoya_12v(ON); //相机供电
  387. ctr_TG_Motor(CLOSE);
  388. #endif
  389. ctr_hmi_5v(ON);
  390. //2. RTU供电默认是开的,不能调用ctr_RTU_12V(ON);
  391. device_status.rtu_status = 1;
  392. //3.打开光栅供电
  393. ctr_led(OFF);
  394. }
  395. void WorkCheck(void)
  396. {
  397. static rt_uint32_t light_timeout = 3600;
  398. while(device_status.ds == 1 &&
  399. device_status.rain_status == 0 &&
  400. device_status.temp_status == 0 &&
  401. device_status.bat_status == 0)
  402. {
  403. if(device_status.timeSwitch == 1) // 时控模式
  404. {
  405. LOG_I("***********时控*****************");
  406. if(device_status.startBar == device_status.stopBar)
  407. {
  408. // 开始结束时间相同则24小时模式
  409. device_status.work_status = 1;
  410. rt_thread_delay(10000);
  411. continue;
  412. }
  413. time_t now;
  414. time(&now);
  415. struct tm now_tm = *localtime(&now);
  416. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  417. rt_uint32_t start_sec = device_status.startBar * 3600;
  418. rt_uint32_t end_sec = device_status.stopBar * 3600;
  419. // 修复跨天判断逻辑
  420. if (start_sec < end_sec)
  421. {
  422. // 不跨天模式
  423. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  424. }
  425. else
  426. {
  427. // 跨天模式
  428. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  429. }
  430. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  431. device_status.work_status ? "工作" : "待机",
  432. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  433. device_status.startBar, device_status.stopBar);
  434. rt_thread_delay(10000);
  435. }
  436. else // 光控模式
  437. {
  438. LOG_I("光控定时模式!");
  439. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  440. {
  441. device_status.light_over_tim--;
  442. light_timeout = 3600; // 重置超时计数器
  443. device_status.work_status = 1;
  444. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  445. }
  446. else
  447. {
  448. // 仅在白天递减超时计数器
  449. if(device_status.sun_status == 1 && light_timeout > 0) {
  450. light_timeout--;
  451. }
  452. device_status.work_status = 0;
  453. //没有在测试并且没有在自检
  454. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  455. {
  456. }
  457. // 连续1小时白天后重置
  458. if(light_timeout == 0) {
  459. device_status.light_over_tim = 0;
  460. light_timeout = 3600; // 重置防止负数
  461. }
  462. // 白天且倒计时结束:重置夜晚时长
  463. if(device_status.light_over_tim == 0 &&
  464. device_status.sun_status == 1)
  465. {
  466. device_status.light_over_tim = device_status.sunBar * 3600;
  467. }
  468. }
  469. }
  470. rt_thread_delay(1000);
  471. }
  472. // 异常状态处理
  473. device_status.work_status = 0;
  474. rt_thread_delay(1000);
  475. }
  476. void guanshan_on(void *args)
  477. {
  478. LOG_W("guanshan_on!");
  479. //if(gaoya_hand){
  480. device_status.all_ycct++;
  481. // gaoya_hand = FALSE;
  482. // }
  483. //rt_event_send(event_485_play, 0x01);
  484. }
  485. void button_on(void *args)
  486. {
  487. LOG_W("button_on!");
  488. if(device_status.sleep_mode==1&&device_status.rtu_status==0) ctr_RTU_12V(ON);
  489. if(device_status.hmi_status==0) ctr_hmi_5v(ON);
  490. //rt_event_send(event_485_play, 0x01);
  491. }
  492. void pin_button_bind(void)
  493. {
  494. LOG_W("pin_button_bind");
  495. /* PA11引脚为输入模式 */
  496. rt_pin_mode(SIGNAL_INPUT_KEY, PIN_MODE_INPUT);
  497. /* 绑定中断,上升沿沿模式,回调函数名为guanshan_on */
  498. rt_pin_attach_irq(SIGNAL_INPUT_KEY, PIN_IRQ_MODE_FALLING, button_on, RT_NULL);
  499. /* 使能中断 */
  500. rt_pin_irq_enable(SIGNAL_INPUT_KEY, PIN_IRQ_ENABLE);
  501. }
  502. void pin_guangshan_bind(void)
  503. {
  504. LOG_W("pin_guangshan_bind");
  505. /* PC14引脚为输入模式 */
  506. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  507. /* 绑定中断,下降沿模式,回调函数名为guanshan_on */
  508. rt_pin_attach_irq(SIGNAL_GuangShan, PIN_IRQ_MODE_RISING, guanshan_on, RT_NULL);
  509. /* 使能中断 */
  510. rt_pin_irq_enable(SIGNAL_GuangShan, PIN_IRQ_ENABLE);
  511. }
  512. void pin_guangshan_unbind(void)
  513. {
  514. LOG_D("pin_guangshan_unbind");
  515. rt_pin_detach_irq(SIGNAL_GuangShan);
  516. }
  517. //拍照性诱工作任务流程
  518. void WorkTask_XYV3(void)
  519. {
  520. static uint8_t gaoya_on = 0;
  521. uint8_t tick_100ms = 0;
  522. rt_uint32_t cal_n = device_status.cul_time * 60;
  523. LOG_I("工作状态:%d",device_status.work_status);
  524. LOG_I("开关机状态:%d",device_status.ds);
  525. LOG_I("温控状态:%d",device_status.temp_status);
  526. LOG_I("雨控状态:%d",device_status.rain_status);
  527. while(device_status.work_status==1&&
  528. device_status.bat_status==0&&
  529. device_status.ds==1&&
  530. device_status.temp_status==0)
  531. {
  532. //高压通电
  533. ctr_gaoya_12v(ON);
  534. gaoya_on = 1;
  535. //光栅传感器通电
  536. pin_guangshan_bind();
  537. //拍照(?触发条件)计数?没有虫呢?定时
  538. tick_100ms = 0;
  539. while(1){
  540. //高压1Hz输出:通电100ms,断电900ms(每100ms为一个节拍)
  541. if(tick_100ms==0){
  542. if(gaoya_on==0){
  543. ctr_gaoya_12v(ON);
  544. gaoya_on = 1;
  545. }
  546. }
  547. else if(gaoya_on==1){
  548. ctr_gaoya_12v(OFF);
  549. gaoya_on = 0;
  550. }
  551. tick_100ms++;
  552. if(tick_100ms>=10){ //满1S,处理秒级任务
  553. tick_100ms = 0;
  554. cal_n--;
  555. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  556. if(cal_n==0){
  557. //拍照时关闭高压
  558. //发送拍照计数指令
  559. device_status.photo_ws = 2;
  560. cal_n = device_status.cul_time * 60;
  561. }
  562. if(device_status.sleep_mode==1){
  563. if(cal_n==100){
  564. ctr_RTU_12V(ON);
  565. }
  566. else if (cal_n==3400) {
  567. ctr_RTU_12V(OFF);
  568. }
  569. }
  570. }
  571. if(device_status.temp_status==1 || device_status.bat_status==1 || device_status.work_status==0){
  572. ctr_gaoya_12v(OFF);
  573. gaoya_on = 0;
  574. device_status.photo_ws = 2;
  575. break;
  576. }
  577. rt_thread_delay(100);
  578. }
  579. rt_thread_delay(10000);
  580. }
  581. }
  582. /* 入口函数-工作状态检测 */
  583. static void Work_status_thread_entry(void *parameter)
  584. {
  585. while(1)
  586. {
  587. WorkCheck();
  588. }
  589. }
  590. /*入口函数-工作流程*/
  591. static void Work_Loop_thread_entry(void *parameter)
  592. {
  593. rt_thread_delay(1000);
  594. ResetInit();
  595. pin_button_bind();
  596. while(1)
  597. {
  598. WorkTask_XYV3();
  599. rt_thread_delay(1000);
  600. }
  601. }
  602. /*入口函数清扫*/
  603. static void Check_Self_thread_entry(void *parameter)
  604. {
  605. static int selfcheck_cnt = 0;
  606. rt_uint32_t e;
  607. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  608. if (event_485_play == RT_NULL)
  609. {
  610. LOG_E("Event create failed!");
  611. return;
  612. }
  613. while(1)
  614. {
  615. //等待事件
  616. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  617. {
  618. DTU_uart2_init();
  619. HMI_uart4_init();
  620. }
  621. if(device_status.selfcheck_ws==1){
  622. selfcheck_cnt++;
  623. }else{
  624. selfcheck_cnt = 0;
  625. }
  626. if(selfcheck_cnt>1200) //超过3分钟
  627. {
  628. device_status.selfcheck_ws = 0;
  629. device_status.camera_sta = 1; //相机异常
  630. //上报自检结果
  631. //上报自检结果
  632. char *json_temp = makeJson_selfCheck_(1);
  633. DTU_Write("%s",json_temp);
  634. free(json_temp);
  635. }
  636. if(device_status.clear_status==10){
  637. Motor_shake(1,800);
  638. device_status.clear_status = 0;
  639. }
  640. rt_thread_delay(100);
  641. }
  642. }
  643. /*入口函数*/
  644. static void Check_gaoya_thread_entry(void *parameter)
  645. {
  646. static int gaoya_value1 = 0;
  647. static int gaoya_value2 = 0;
  648. static uint32_t gaoya_cnt = 0;
  649. gaoya_hand = FALSE;
  650. while(1)
  651. {
  652. gaoya_value1 = Read_Fuse_Voltage();
  653. rt_thread_delay(50);
  654. gaoya_value2 = Read_Fuse_Voltage();
  655. if(gaoya_value2-gaoya_value1>50)
  656. {
  657. gaoya_cnt = 0;
  658. gaoya_hand = TRUE;
  659. LOG_I("高压触发:[%d,%d]",gaoya_value1,gaoya_value2);
  660. ctr_clear();
  661. device_status.gaoya_count++; //2026-07-02
  662. }
  663. if(gaoya_hand){
  664. gaoya_cnt ++;
  665. if(gaoya_cnt==20*25)
  666. {
  667. gaoya_cnt = 0;
  668. gaoya_hand = false;
  669. }
  670. }
  671. }
  672. }
  673. /*清扫线程*/
  674. int Check_Self(void)
  675. {
  676. /* 创建线程*/
  677. tid_ctr = rt_thread_create("Check_Self_thread_entry",
  678. Check_Self_thread_entry, RT_NULL,
  679. 2048, //2048
  680. 24, //23
  681. WORK_CHECK_THREAD_TIMESLICE); //5
  682. if(tid_ctr!=RT_NULL)
  683. {
  684. rt_thread_startup(tid_ctr);
  685. }
  686. return 0;
  687. }
  688. /*恒温加热线程*/
  689. int Check_gaoya(void)
  690. {
  691. /* 创建线程*/
  692. tid_heat = rt_thread_create("Check_gaoya_thread_entry",
  693. Check_gaoya_thread_entry, RT_NULL,
  694. 1024, //2048
  695. 25, //23
  696. WORK_CHECK_THREAD_TIMESLICE); //5
  697. if(tid_heat!=RT_NULL)
  698. {
  699. rt_thread_startup(tid_heat);
  700. }
  701. return 0;
  702. }
  703. /* 创建线程 -工作状态检测*/
  704. int Workstatus_check(void)
  705. {
  706. /* 创建线程*/
  707. tid_wkstc = rt_thread_create("work_status_check_thread",
  708. Work_status_thread_entry, RT_NULL,
  709. WORK_CHECK_THREAD_STACK_SIZE, //1024
  710. WORK_CHECK_THREAD_PRIORITY, //23
  711. WORK_CHECK_THREAD_TIMESLICE); //5
  712. if(tid_wkstc!=RT_NULL)
  713. {
  714. rt_thread_startup(tid_wkstc);
  715. }
  716. return 0;
  717. }
  718. /*工作流程线程*/
  719. int Work_Loop_Task(void)
  720. {
  721. rt_err_t result_loop = 0;
  722. /* 创建线程*/
  723. tid_workloop = rt_thread_create("work_loop_thread",
  724. Work_Loop_thread_entry, RT_NULL,
  725. WORK_LOOP_THREAD_STACK_SIZE, //2048
  726. WORK_LOOP_THREAD_PRIORITY, //12
  727. WORK_LOOP_THREAD_TIMESLICE); //5
  728. if(tid_workloop!=RT_NULL)
  729. {
  730. result_loop = rt_thread_startup(tid_workloop);
  731. }
  732. return result_loop;
  733. }
  734. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  735. //INIT_APP_EXPORT(Control_heat);