#include /* * Copyright (c) 2006-2020, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-05-30 Administrator the first version * 2025-06-26 RTC闹钟使用定时开启和关闭路由器供电 */ #include "beep.h" #include "hmi.h" #include "easyflash.h" #include "stdio.h" #include "sound.h" #include "dtu_uart2.h" #include "sht30_collect.h" #include #include #ifdef RT_USING_FINSH #include /* finsh_set_device(): uart_stop_for_ota 要把 finsh 摘回 uart1 */ #endif #define DBG_ENABLE #define DBG_COLOR #define DBG_TAG "control" #define DBG_LEVEL DBG_LOG #include //must after of DBG_LVL, DBG_TAG or other options #include "sys_monitor.h" #include "param_mirror.h" //工作检测 #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级 #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小 #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片 //工作流程 #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级 #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小 #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片 #define PWM_EN 0 //0关闭,1-打开 //设备状态表结构体 /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */ /* system_status 已收敛为全工程唯一定义(在 sys_monitor.c), 见 sys_monitor.h 的 extern 声明 */ rt_thread_t tid_workloop = RT_NULL; rt_thread_t tid_wkstc = RT_NULL; rt_thread_t tid_ctr = RT_NULL; rt_thread_t tid_heat = RT_NULL; rt_thread_t tid_485_play = RT_NULL; extern rt_device_t dtuserial; /*串口设备句柄*/ rt_event_t event_485_play; extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS); /***************************************************************************/ int thread_delete_workloop(void) { rt_err_t result; if(tid_workloop!=RT_NULL) { result = rt_thread_delete(tid_workloop); } return result; } int thread_restart_workloop(void) { rt_err_t result; result = Work_Loop_Task(); return result; } /***************************************************************************/ /* ==================== OTA 升级前的线程清理 ==================== */ #define OTA_THREAD_MAX 24 /* 保留名单: 这几类线程删掉反而会让升级失败 */ static const char *g_ota_keep[] = { "syswatch", /* 喂狗线程: 删了 5s 后硬件看门狗就复位, 升级必中断 */ "tshell", /* 控制台 */ "tidle", "main", }; /* 需要停掉的业务线程前缀(写完整名即可, 比较时自动按 RT_NAME_MAX-1 截断)。 * * 为什么用"前缀匹配 + 遍历"而不是 rt_thread_find(全名): * rtconfig.h 里 RT_NAME_MAX = 8, 线程名被内核截断到 7 个字符 —— * "Control_Clear_thread_entry" / "Control_Check_thread_entry" 都变成 "Control"; * "DTU_Uart_thread_send" / "DTU_Uart_thread_recv" 都截成 "DTU_Uar", * 而 recv 正是执行升级的当前线程, 用 rt_thread_find 有可能把调用者自己删掉。 * 所以改成遍历线程对象链表, 并显式跳过 rt_thread_self()。 */ static const char *g_ota_stop[] = { "work_loop_thread", /* 工作流程, LOG_I/rt_kprintf 密集 */ "work_status_check_thread", /* WorkCheck, 每 10s 一批日志 */ "Control_Clear_thread_entry", /* 清扫, 会驱动电机 */ "Control_Check_thread_entry", /* 恒温, LOG_I("process") 每秒 */ "thread_485play", /* 485 播报 */ "adc_thread", /* ADC 采集, 每 5s 打 3 行 */ "Power Check thread", /* 每秒读电流/电压 */ "Hmi_Fresh_thread", /* 屏幕刷新 */ "Ctr motor", /* 仓门/光源, 带 rt_kprintf */ "sys_monitor", /* CPU/统计 */ "param_sv", /* 参数落盘: 会擦 flash(4KB≈100ms) 抢 CPU */ "DTU_Uart_thread_send", /* DTU 发送 (recv 是自身, 会被跳过) */ "Uart5_thread_recv", /* 485 透传 */ "md_m_poll", /* modbus 轮询 */ "md_m_send", /* modbus 发送 */ "usage", /* cpu_usage 临时线程 */ }; /* 名字前缀比较: 内核只存了前 RT_NAME_MAX-1 个字符, 所以最多比这么多 */ static int ota_name_match(const char *thread_name, const char *prefix) { rt_size_t n; if (thread_name == RT_NULL || prefix == RT_NULL || thread_name[0] == '\0') { return 0; } n = rt_strlen(prefix); if (n > RT_NAME_MAX - 1) { n = RT_NAME_MAX - 1; } if (n == 0) { return 0; } return (rt_strncmp(thread_name, prefix, n) == 0) ? 1 : 0; } static int ota_in_list(const char *thread_name, const char **list, int cnt) { int i; for (i = 0; i < cnt; i++) { if (ota_name_match(thread_name, list[i])) { return 1; } } return 0; } /** * OTA 升级前把所有业务线程停掉, 只留 tshell / idle / syswatch。 * * 为什么必须停: * 1) ymodem_ota() 会把 console 切到 uart2(ymodem_ota.c:117), 之后**任何线程** * 的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染 ymodem 字节流 -> 校验失败; * 2) adc / Power Check 每秒读 ADC, param_sv 会擦 flash(4KB 约 100ms), * 抢 CPU 和外设会让 ymodem 丢字节; * 3) Control_Clear / Control_Check 会驱动电机, 升级过程中不该动机械部件。 * * 顺序: ① adc_thread 优雅退出(它可能正持有 ADC 设备锁) * ② 置 RAM 内退出请求, 让带 `update_flag==1 break` 的线程自己退 * ③ 其余按前缀强删(先收集再删, 不能边遍历边删) * * @return 实际删除的线程数; 负值为错误 */ int thread_stop_all_for_ota(void) { struct rt_object_information *info; rt_list_t *node; rt_thread_t victims[OTA_THREAD_MAX]; rt_thread_t self; int nv = 0, i, done = 0; info = rt_object_get_information(RT_Object_Class_Thread); if (info == RT_NULL) { LOG_E("[ota] thread object information NOT found"); return -RT_ERROR; } /* ① adc_thread 必须优雅退出: 直接 rt_thread_delete 若刚好删在 rt_adc_enable/read 中间, 它持有的 ADC 设备锁永远不会释放 */ thread_delete_adc(); /* ② 置 RAM 内退出请求。Control_Clear / Control_Check / thread_485play 三处都有 `if(device_status.update_flag==1) break;`。 ★ 只改 RAM, 绝不写 flash / 参数快照 —— 一旦把 1 落盘, 下次开机会被 hmi.c 的 up_flag_* 裁决判成"有升级请求", 而带 break 的线程就会 一开机直接退出(设备不清扫、不恒温)。 因为全程不落盘, ymodem 结束复位后这个 1 自然消失, 不需要额外清理。 */ device_status.update_flag = 1; rt_thread_mdelay(300); /* ③ 收集待停线程。必须"先收集后删除": rt_thread_delete 会摘掉链表节点, 边遍历边删会把 object_list 走坏 */ self = rt_thread_self(); rt_enter_critical(); rt_list_for_each(node, &info->object_list) { rt_thread_t t = rt_list_entry(node, struct rt_thread, list); if (t == self) /* 不能删自己 */ { continue; } if (ota_in_list(t->name, g_ota_keep, sizeof(g_ota_keep) / sizeof(g_ota_keep[0]))) { continue; } if (!ota_in_list(t->name, g_ota_stop, sizeof(g_ota_stop) / sizeof(g_ota_stop[0]))) { continue; /* 不在名单里, 不动 */ } if (nv < OTA_THREAD_MAX) { victims[nv++] = t; } } rt_exit_critical(); /* ④ 逐个删除 */ for (i = 0; i < nv; i++) { if (rt_thread_delete(victims[i]) == RT_EOK) { done++; } } LOG_I("[ota] stopped %d of %d candidate thread(s)", done, nv); return done; } /** * @brief 把 double 定点格式化成 "整数.两位小数" 字符串(不依赖 libc 浮点 printf)。 * * 🔴 为什么必须有这个函数: * RT-Thread 的 rt_snprintf() 只是 rt_vsnprintf() 的薄封装, 而 rt_vsnprintf * (rt-thread/src/kservice.c:886) 的转换字符 switch 只有: * case 'c' / 's' / 'd' / 'i' / 'o' / 'u' / 'x' / 'X' / '%' * **没有 case 'f' / 'e' / 'g'**。rtconfig.h 也没打开 RT_USING_LIBC_FLOAT_PRINT。 * 于是 "%.2f" 会一路走到 default: 分支(kservice.c:1090), 该分支的行为是 * 把 '%' 和当前字符原样写进缓冲区, 然后 continue; * 结果就是缓冲区里得到字面量 **"%f"**(小数点 ".2" 也会被当普通字符写出去), * 而且**不消费对应的 double 可变参数** —— 后面若还有格式符, 参数会整体错位。 * 这就是 2026-09-28 平台侧电流/电压显示成 "%f" 的根因。 * * 也不能简单换回标准库 sprintf("%f"): newlib 的 float printf 默认不使能, * 要额外链入 _printf_float(体积大), 且原来 power[8] 装 "25000.00" 会越界。 * * 这里的做法: 纯整数定点运算, 只用 %ld/%02ld, 任何 printf 实现都支持。 * 代价是没有完整的 dtoa 舍入, 但先加 0.5 再强转已经等价于四舍五入到 0.01, * 电流电压这类量只要两位小数, 精度完全够。 * * @param buf 输出缓冲(必须是数组, 不能是指针) * @param size 缓冲大小 * @param val 待格式化的值 * @param prec 小数位数, 内部钳到 0~2 */ void fmt_fixed2(char *buf, rt_size_t size, double val, int prec) { /* 上限钳位: 防止 val*100 溢出 long。20000.00 是电流/功率物理上够不到的上限。 */ const double MAX_VAL = 20000.0; long scaled; /* val 放大 100 倍后的定点整数 */ long ip, fp; /* 整数部分 / 小数部分 */ int neg = 0; if (buf == RT_NULL || size == 0) { return; } if (prec < 0) { prec = 0; } if (prec > 2) { prec = 2; } if (val < 0.0) { neg = 1; val = -val; } if (val > MAX_VAL) { val = MAX_VAL; } /* 先统一放大 100 倍并四舍五入(加 0.5 后强转即等价于四舍五入到 0.01)。 这样 prec=0/1/2 三个分支都能直接取用, 不会出现 prec=1 时把 12.99 截断成 12.9 的问题 —— 那一档要的是四舍五入后的 13.0。 */ scaled = (long)(val * 100.0 + 0.5); ip = scaled / 100; fp = scaled % 100; if (prec == 0) { /* 四舍五入到整数: fp >= 50 则进位 */ if (fp >= 50) { ip += 1; } rt_snprintf(buf, size, "%s%ld", neg ? "-" : "", ip); } else if (prec == 1) { /* 四舍五入到 0.1: fp 的个位 >= 5 则进位到十分位 */ if (fp % 10 >= 5) { fp += 10; if (fp >= 100) { fp -= 100; ip += 1; } } rt_snprintf(buf, size, "%s%ld.%ld", neg ? "-" : "", ip, fp / 10); } else { rt_snprintf(buf, size, "%s%ld.%02ld", neg ? "-" : "", ip, fp); } } /** * @brief 把一个串口设备彻底关掉(解引用到 ref_count==0), 用于 OTA 前收尾。 * * 为什么不能只 rt_device_close() 一次: * rt_device_close() 只有在 ref_count 减到 0 时才会真正调用驱动的 close, * 否则只减计数就返回 RT_EOK。而 uart4/uart5 的引用不止一处: * uart4 → HMI_uart4_init() 开 1 次, 若现场跑过 Debug_log_ON(), * rt_console_set_device("uart4") + finsh_set_device("uart4") * 又各开 1 次(共 3 次); * uart5 → DTU_uart5_init() 开 1 次, UART_485_INIT() 再开 1 次, * freemodbus 的 xMBMasterPortSerialInit() 还会再开 1 次。 * 所以必须循环 close 到 ref_count 归零, 否则设备仍在收发、ISR 仍会触发。 * * @param dev 设备句柄, 可为 RT_NULL * @param name 名字, 仅用于日志 */ static void ota_close_uart(rt_device_t dev, const char *name) { rt_uint8_t guard = 8; if (dev == RT_NULL) { return; } /* ① 先摘掉接收回调。close() 内部也会清, 但清回调后到 close 之间 还有窗口期, 此刻若有数据到达, 回调里会 release 已经没人等的 信号量 / 往已删线程的消息队列里塞消息。 */ rt_device_set_rx_indicate(dev, RT_NULL); /* ② 循环 close 到引用计数归零。guard 兜底, 防止内核行为异常时空转。 */ while (dev->ref_count > 0 && guard-- > 0) { if (rt_device_close(dev) != RT_EOK) { break; } } LOG_I("[ota] %s closed, ref_count=%d", name, (int)dev->ref_count); } /** * @brief OTA 升级前关闭串口4(HMI屏) 和串口5(485)。 * * 为什么要关: * 1) ymodem_ota() 内部会 rt_console_set_device("uart2"), 之后 console 指向 * uart2。此时 uart4/uart5 若还在工作, 它们的 ISR、以及仍可能残留的 * 485 播报/屏幕刷新, 都会和 ymodem 抢 CPU 与中断, 导致传输丢字节; * 2) 485 是**半双工**总线, 收发器被驱动着去收 ymodem 期间还挂着一个 * 从机在等应答, 升级完复位后从机状态与 MCU 不同步, 现场表现为 * "升级完 485 读不到数据"; * 3) 屏幕在升级过程中继续刷新, 白白占用带宽, 且部分屏会因长时间收不到 * 指令而重新初始化自身。 * * ★ 本函数必须在 thread_stop_all_for_ota() **之后**调用: * 先停线程(没人再发数据) → 再关设备。反过来的话, 关设备的瞬间还有 * 线程在 HMI_WRITE/Uart5_Write, 会写到已被 disable 的 fifo 上。 * * @return 关闭的串口个数 */ int uart_stop_for_ota(void) { int n = 0; /* 置标志必须**先于**关设备: 中间还有别的线程可能正在 HMI_WRITE, 看到标志后直接 return, 不会碰到正在被拆掉的 fifo。 */ g_hmi_port_closed = 1; /* ---- 串口4: HMI 屏 ---- 先把 console / finsh 摘回 uart1, 否则它们各自持有 uart4 的一次 open (rt_console_set_device / finsh_set_device 内部都是 open 新的 + close 旧的), 引用计数降不到 0, 设备等于没关; 而且 tshell 正阻塞在 uart4 的 read 上, 设备被关后它会一直读到 0。 */ if (rt_console_get_device() != RT_NULL && rt_strcmp(rt_console_get_device()->parent.name, UART_NAME) == 0) { rt_console_set_device(RT_CONSOLE_DEVICE_NAME); } #if defined(RT_USING_FINSH) && !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE) finsh_set_device(RT_CONSOLE_DEVICE_NAME); #endif if (serial != RT_NULL) { ota_close_uart(serial, UART_NAME); n++; } /* ---- 串口5: 485 ---- */ if (serial_485 != RT_NULL) { ota_close_uart(serial_485, "uart5"); n++; } /* 把句柄置 NULL: 让上层即使漏判 g_hmi_port_closed 也不会往已废设备写。 ⚠️ 置 NULL 之后 ymodem 失败返回也不会有任何 HMI/485 输出, 这是预期的 ——升级流程本来就要求这段时间对外静默。 */ serial = RT_NULL; serial_485 = RT_NULL; LOG_I("[ota] uart4/uart5 stopped (%d)", n); return n; } uint8_t ctr_Fan_PUT(uint8_t status_ws) { rt_pin_mode(FAN_P, PIN_MODE_OUTPUT); if(status_ws==ON) { rt_pin_write(FAN_P,ON); } else { rt_pin_write(FAN_P, OFF); } return 0; } uint8_t ctr_Fan_IN(uint8_t status_ws) { rt_pin_mode(FAN_I, PIN_MODE_OUTPUT); if(status_ws==ON) { rt_pin_write(FAN_I,ON); } else { rt_pin_write(FAN_I, OFF); } return 0; } /**********************************************************************/ /*光控*/ uint8_t In_Light_status(void) { rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT); return rt_pin_read(LIGHT_PIN); } /*雨控*/ uint8_t In_Rain_status(void) { rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT); return rt_pin_read(RAIN_PIN); } /*加热*/ uint8_t ctr_Heat(uint8_t status_heat) { rt_bool_t is_currently_on; rt_tick_t now; now = rt_tick_get(); is_currently_on = (system_status.heat_start_time != 0); device_status.heat_status = status_heat; rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT); rt_pin_write(Heat_Pin,status_heat); if(status_heat==ON){ // 已经开着 → 不重复计时 if (is_currently_on) { return ON; } // 关 → 开,记录起始时间 system_status.heat_start_time = now; }else{ // 已经关着 → 不重复结算 if (!is_currently_on) { return OFF; } // 开 → 关,结算运行时间 system_status.heater_running_time += (now - system_status.heat_start_time); system_status.heat_start_time = 0; } return status_heat; } /*制冷*/ uint8_t ctr_Cold(uint8_t status_cold) { device_status.cold_status = status_cold; rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT); rt_pin_write(Cold_Pin,status_cold); return status_cold; } /*相机供电*/ void ctr_photo(uint8_t status_photo) { rt_pin_mode(PHOTO,PIN_MODE_OUTPUT); rt_pin_write(PHOTO,status_photo); } /************************************************* Function: ctr_Cool_Fan Description: 散热风机的开关 Calls: Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: ON-开,OFF-关 Output: NULL Return: ON-开,OFF-关 Others: NULL *************************************************/ uint8_t ctr_Cool_Fan(uint8_t status_cool) { device_status.cool_fan_status = status_cool; rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT); rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT); if(status_cool==OFF) { rt_pin_write(Cool_Fan_L,OFF); rt_pin_write(Cool_Fan_R,OFF); } if(status_cool==ON) { rt_pin_write(Cool_Fan_L,ON); rt_pin_write(Cool_Fan_R,OFF); } return device_status.cool_fan_status; } /************************************************* Function: ctr_Collect_Fan Description: 采集风机的开关 Calls: Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: ON-开,OFF-关 Output: NULL Return: ON-开,OFF-关 Others: NULL *************************************************/ uint8_t ctr_Collect_Fan(uint8_t status_collect) { device_status.collect_fan_status = status_collect; rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT); if(status_collect==OFF) { rt_pin_write(Collect_Fan,OFF); } if(status_collect==ON) { rt_pin_write(Collect_Fan,ON); } return device_status.collect_fan_status; } /************************************************* Function: ctr_develop Description: 培养载玻片孢子 Calls: Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: v=0(复位) v=90转90度 Output: NULL Return: NULL Others: NULL *************************************************/ /*上仓门*/ uint8_t ctr_cangmen_up(uint8_t status_up) { rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT); rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT); if(status_up==OFF) { rt_pin_write(Collect_fan_R,ON); rt_pin_write(Collect_fan_L,ON); device_status.ctr_up_motor = 0; rt_thread_delay(1000); } if(status_up==OPEN) { device_status.up_status = 1; rt_pin_write(Collect_fan_R,OFF); rt_pin_write(Collect_fan_L,OFF); #if PCB_V1 == 1 rt_pin_write(Collect_fan_R,ON); rt_pin_write(Collect_fan_L,OFF); #else rt_pin_write(Collect_fan_R,OFF); rt_pin_write(Collect_fan_L,ON); #endif } if(status_up==CLOSE) { device_status.up_status = 0; rt_pin_write(Collect_fan_R,OFF); rt_pin_write(Collect_fan_L,OFF); #if PCB_V1 == 1 rt_pin_write(Collect_fan_R,OFF); rt_pin_write(Collect_fan_L,ON); #else rt_pin_write(Collect_fan_R,ON); rt_pin_write(Collect_fan_L,OFF); #endif } return device_status.up_status; } //下仓门 uint8_t ctr_cangmen_down(uint8_t status_down) { rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT); rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT); if(status_down==OFF) { rt_pin_write(C_DOWN_R,ON); rt_pin_write(C_DOWN_L,ON); device_status.ctr_down_motor = 0; rt_thread_delay(1000); } if(status_down==OPEN) { device_status.down_status = 1; rt_pin_write(C_DOWN_R,OFF); rt_pin_write(C_DOWN_L,OFF); rt_pin_write(C_DOWN_R,ON); rt_pin_write(C_DOWN_L,OFF); } if(status_down==CLOSE) { device_status.down_status = 0; rt_pin_write(C_DOWN_R,OFF); rt_pin_write(C_DOWN_L,OFF); rt_pin_write(C_DOWN_R,OFF); rt_pin_write(C_DOWN_L,ON); } return device_status.down_status; } //镇流器 //镇流器 uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num) { rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT); rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT); rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT); rt_bool_t is_currently_on; rt_tick_t now; now = rt_tick_get(); is_currently_on = (system_status.lamp_start_time != 0); if (status_lamp == ON) { // ====== 开灯 ====== switch (lamp_num) { case 1: rt_pin_write(LAMP_SW, ON); device_status.lamp_status = ON; break; case 2: rt_pin_write(LAMP_1_SW, ON); device_status.lamp1_status = ON; break; case 3: rt_pin_write(LAMP_2_SW, ON); device_status.lamp2_status = ON; break; default: break; } // 已经开着 → 不重复计时 if (is_currently_on) { return ON; } // 关 → 开,记录起始时间 system_status.lamp_start_time = now; } else { // ====== 关灯 ====== switch (lamp_num) { case 1: rt_pin_write(LAMP_SW, OFF); device_status.lamp_status = OFF; break; case 2: rt_pin_write(LAMP_1_SW, OFF); device_status.lamp1_status = OFF; break; case 3: rt_pin_write(LAMP_2_SW, OFF); device_status.lamp2_status = OFF; break; default: break; } // 已经关着 → 不重复结算 if (!is_currently_on) { return OFF; } // 开 → 关,结算运行时间 system_status.lamp_running_time += (now - system_status.lamp_start_time); system_status.lamp_start_time = 0; } HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status); return device_status.lamp_status; } //震动电机 uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec) { rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT); #if 0 rt_pin_write(SHAKE_SW,ON); return 0; #endif if(status_shake==ON&&device_status.shake_ws==1) { if(device_status.dtype==3) { for(uint8_t shake_i = 0;shake_i 走 dtu_write_json */ dtu_write_json(makeJson_Warn("超时", "转盘")); break; } } if(sta!=0) { //保存位置 device_status.run_sta = sta; ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); /* 转盘位置变了, 同步进参数镜像(掉电后能恢复到正确仓位) */ param_mirror_sync(); } turn_cnt = 0; rt_pin_write(TURN_ZP_R, OFF); rt_pin_write(TURN_ZP_L, OFF); Motor_shake(OFF,0); HMI_WRITE("cangWei.rotateFlag.val=0"); } /* * * */ void ctr_Motor_Driver_Power(uint8_t status_ws) { rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT); rt_pin_write(SHAKE_SW, status_ws); } /************************************************* Function: ctr_Motor_Driver Description: 步进电机驱动 Calls: Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: 50000000 Output: NULL Return: shake_sec Others: NULL *************************************************/ void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out) { beep_init(); rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT); rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT); rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT); rt_pin_write(Motor_EN, EN); rt_pin_write(Motor_DIR, DIR); beep_set(pwm, pwm/2); //200kzh - 50% while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1) { if(time_out==0) { break; } rt_thread_delay(50); time_out --; } while(DIR==0&&rt_pin_read(LIMIT_UP)==1) { if(time_out==0) { break; } rt_thread_delay(50); time_out --; } beep_set(0, 0); //200kzh - 50% rt_thread_delay(50); } /************************************************* Function: ctr_Petroleum_jelly Description: 涂凡士林 Calls: Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: NULL Output: NULL Return: NULL Others: NULL *************************************************/ void ctr_Petroleum_jelly(rt_uint32_t delay) { beep_init(); pwm_on(2,50000); rt_thread_delay(delay*1000); pwm_off(2,0); rt_thread_delay(1000); pwm_on(3, 50000); rt_thread_delay(delay*1000); pwm_off(3,0); device_status.fsl_sw = 0; } //1.0转仓 void turnzp(void) { LOG_W("转仓执行!"); static uint16_t turn_timeout = 0; // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT); // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT); rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT); // rt_pin_write(CLEAR_R,OFF); // rt_pin_write(CLEAR_L,OFF); while(rt_pin_read(CLEAR_R_LIMIT)==0) { rt_thread_delay(20); while(rt_pin_read(CLEAR_R_LIMIT)==0) { ctr_Collect_Fan(ON); rt_thread_delay(50); turn_timeout++; if(turn_timeout==600) { LOG_E("检测不到限位,电机强制停止"); char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机"); DTU_Write("%s",ctr_zp_limit_warn); rt_free(ctr_zp_limit_warn); break; } } break; } while(rt_pin_read(CLEAR_R_LIMIT)==1) { rt_thread_delay(20); while(rt_pin_read(CLEAR_R_LIMIT)==1) { // rt_pin_write(CLEAR_R,ON); // rt_pin_write(CLEAR_L,OFF); ctr_Collect_Fan(ON); rt_thread_delay(50); turn_timeout++; if(turn_timeout==600) { LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止"); char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机"); DTU_Write("%s",ctr_zp_limit_warn); rt_free(ctr_zp_limit_warn); break; } } break; } while(rt_pin_read(CLEAR_R_LIMIT)==0) { rt_thread_delay(20); if(rt_pin_read(CLEAR_R_LIMIT)==0) { // rt_pin_write(CLEAR_R,OFF); // rt_pin_write(CLEAR_L,OFF); ctr_Collect_Fan(OFF); break; } else { break; } } ctr_Collect_Fan(OFF); turn_timeout = 0; HMI_WRITE("cangWei.rotateFlag.val=%d",0); rt_thread_delay(1000); } //清扫-常闭型限位开关 //新结构使用推杆电机做清扫 uint8_t ctr_clear(uint16_t status_clear) { static uint16_t clear_num = 0; #if PWM_EN == 0 rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT); rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT); #endif rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT); rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT); /*************************************************************外接清扫 rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT); rt_pin_write(POWER_CLEAR,ON); rt_thread_delay(1000*100); rt_pin_write(POWER_CLEAR,OFF); ************************************************************/ //停止 #if PWM_EN == 0 rt_pin_write(CLEAR_R,OFF); rt_pin_write(CLEAR_L,OFF); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0; rt_thread_delay(100); //如果清扫位置在中间 while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,OFF); #else Motor_Qingsao_R_PWM(ON); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(50); clear_num++; //已到达检测位置 if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(2000); break; } if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12) { rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power)); //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device")); #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_free(makeJson_Warn("Clear Motor ERR","device")); if(Read_Real_Voltage()/100<12) { HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); device_status.clear_status = 0; return 0; } break; } } clear_num = 0; //如果清扫位置在左侧 while(rt_pin_read(CLEAR_R_LIMIT)==0) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,OFF); #else Motor_Qingsao_R_PWM(ON); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(50); clear_num++; if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12) { rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power)); //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device")); #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_free(makeJson_Warn("Clear Motor ERR","device")); if(Read_Real_Voltage()/100<12) { HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); device_status.clear_status = 0; return 0; } break; } } clear_num = 0; while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif if(Read_Real_Voltage()/100<12) { HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); device_status.clear_status = 0; return 0; } rt_thread_delay(2000); break; } while(rt_pin_read(CLEAR_L_LIMIT)==0) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,OFF); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(ON); #endif rt_thread_delay(50); clear_num++; if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12) { rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power)); char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device"); //DTU_Write("%s",Clear_ERR); #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_free(Clear_ERR); if(Read_Real_Voltage()/100<12) { HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); device_status.clear_status = 0; return 0; } break; } } while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(2000); break; } while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif if(device_status.version_type==41) { rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power)); char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device"); //DTU_Write("%s",Clear_ERR); rt_free(Clear_ERR); } return 0; } clear_num = 0; rt_thread_delay(2000); #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); device_status.clear_status = 0; return 1; } //路由器供电 void ctr_router(uint8_t status_router) { rt_pin_mode(ROUTER, PIN_MODE_OUTPUT); //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT); if(status_router==ON) { rt_pin_write(ROUTER,ON); } else { rt_pin_write(ROUTER,OFF); } } //清扫-推杆电机 void ctr_TG_Clear(uint16_t clear_timeout) { #if 0 int current_clear[200]; int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流 LOG_I("清扫电机待机:%d",Electric_current); #endif static uint16_t clear_count = 0; /* 屏幕 workSta.t6「清扫结束剩余时间」用的尾段常量: * 全程 = 正向推杆(clear_timeout*100ms) + 3s 停顿 + 反向复位(clear_timeout*100ms) + 2s 收尾 */ uint16_t clear_left_tail = (uint16_t)(clear_timeout / 10) + 3 + 2; clear_count = clear_timeout; #if PWM_EN == 0 rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT); rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT); #endif while(clear_count!=0) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,OFF); rt_pin_write(CLEAR_L,ON); #else #if QINGSAO_IO == 1 Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(ON); #else Motor_Qingsao_R_PWM(ON); Motor_Qingsao_L_PWM(OFF); #endif #endif clear_count--; device_status.clear_left = (uint16_t)(clear_count / 10) + clear_left_tail; /* t6 剩余清扫时间(s) */ rt_thread_delay(100); } #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(3000); clear_count = 0; while(clear_timeout!=clear_count) { #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,OFF); #else #if QINGSAO_IO == 1 Motor_Qingsao_R_PWM(ON); Motor_Qingsao_L_PWM(OFF); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(ON); #endif #endif // if(clear_count<200) // current_clear[clear_count] = atoi(device_status.power); clear_count++; device_status.clear_left = (uint16_t)((clear_timeout - clear_count) / 10) + 2; /* t6 反向复位剩余(s) */ rt_thread_delay(100); } #if PWM_EN == 0 rt_pin_write(CLEAR_R,ON); rt_pin_write(CLEAR_L,ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif #if 0 bubblesort(current_clear,200); if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000) { LOG_E("清扫电机电流不足:%d!",current_clear[199]); char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机"); DTU_Write("%s",ctr_clear_warn); rt_free(ctr_clear_warn); } #endif rt_thread_delay(2000); device_status.clear_status = 0; device_status.clear_left = 0; /* 清扫结束,倒计时清零 */ HMI_WRITE("cangWei.rotateFlag.val=%d",0); HMI_WRITE("sysTest.switchClear.val=%d",0); } // 全局静态变量,保存上次关闭的 tick static rt_uint32_t s_last_off_tick = 0; void ctr_RTU_DTU(uint8_t status_rtu) { // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用) rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT); if (status_rtu == ON) { // 开机逻辑:必须满足冷却时间才能开机 rt_uint32_t current_tick = rt_tick_get(); rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒 if (s_last_off_tick != 0) { rt_uint32_t diff_tick = current_tick - s_last_off_tick; if (diff_tick < cool_ticks) { // 未冷却完成,计算剩余时间 rt_uint32_t wait_ticks = cool_ticks - diff_tick; rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND); LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms); // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay) rt_thread_delay(wait_ticks); } } // 冷却完成,执行开机 rt_pin_write(RTU_DTU, ON); s_last_off_tick = 0; // 清除关机时间 LOG_W("RTU电源启动!!!"); } else { // 关机逻辑:记录关机时刻tick rt_pin_write(RTU_DTU, OFF); s_last_off_tick = rt_tick_get(); LOG_W("RTU电源关闭:%d",s_last_off_tick); } } /** * @brief 安全关闭 RTU 12V 电源 (防假掉电流程) */ void RTU_Power_Off_Safe(void) { /* 2. 【关键】关闭串口设备。 * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话), * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。 */ rt_device_close(dtuserial); /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数 * extern UART_HandleTypeDef huart2; * HAL_UART_DeInit(&huart2); */ /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT) * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。 */ rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT); rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT); /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */ rt_thread_mdelay(50); /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */ rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT); rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS LOG_I("RTU 12V Power OFF safely.\n"); } /** * @brief 恢复 RTU 12V 电源并重新初始化串口 */ void RTU_Power_On_Restore(void) { rt_thread_mdelay(5000); /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */ rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS LOG_I("RTU 12V Power ON.\n"); /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来 * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒 */ rt_thread_mdelay(5000); /* 3. 【关键】重新打开串口设备。 * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为 * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。 */ GPIO_InitTypeDef gpioInitStruct; __HAL_RCC_GPIOA_CLK_ENABLE(); //PA2 TXD gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式 gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9 gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率 HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA //PA3 RXD gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式 gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10 gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率 HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA #ifdef RT_USING_SERIAL_V2 rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式 if(uart_res!=RT_EOK) { LOG_E("%s打开失败!",RTU_UART_NAME); }else{ LOG_I("%s Open Success!",RTU_UART_NAME); } #endif #ifdef RT_USING_SERIAL_V1 rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX #endif /* 4. 【关键】重新注册接收回调 * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。 * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做, * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。 */ dtu_uart2_reinit_rx(); } /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */ void cmd_rtu_restart(int argc, char** argv) { rt_kprintf("Starting RTU safe restart sequence...\n"); // 1. 安全断电 RTU_Power_Off_Safe(); // 2. 保持断电 30 秒 (确保电容彻底放电) rt_kprintf("Waiting 30s for capacitor discharge...\n"); rt_thread_mdelay(30000); // 3. 恢复供电并重建串口 RTU_Power_On_Restore(); rt_kprintf("RTU restart sequence completed.\n"); } //海康拍照 void Hk_Take_io(void) { rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT); rt_pin_mode(HK_IO, PIN_MODE_OUTPUT); rt_pin_write(HK_TAKE, 1); rt_pin_write(HK_IO, 0); if(device_status.dtype==3) Beep_Tip(1000); if(device_status.dtype==7) rt_thread_mdelay(1000); rt_pin_write(HK_TAKE, 0); rt_pin_write(HK_IO, 1); } void calorstat(uint16_t heat_time) { //uint8_t heat_up_cnt = 0; uint16_t n = 0; uint16_t delay_heat = 60; FlagStatus heat_flg = RESET; device_status.heat_time_min = 0; device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度 ctr_Heat(ON); // UpLoad_Data(); //开始加热时的第一条数据 //恒温模式 for(n=0;n < heat_time;n++) { if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET) { heat_flg = SET; ctr_Heat(OFF); while(delay_heat!=0) { device_status.C01 &= ~(1<<1); //加热 正常 delay_heat --; rt_kprintf("正在恒温加热!!!:%d",delay_heat); HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat); rt_thread_delay(1000); if(device_status.heattem>device_status.heat_tem_max + 1){ device_status.heat_tem_max = device_status.heattem; } } delay_heat = 60; //跳出FOR循环 n = n + 60; } if(device_status.heattem>device_status.run_sta + 1){ device_status.heat_tem_max = device_status.heattem; } if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET) { device_status.heat_tem_max = device_status.heattem; ctr_Heat(ON); } else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET) { // if(heat_up_cnt==0) // { // UpLoad_Data(); //加热到设定温度的数据 // heat_up_cnt = 1; // } device_status.heat_tem_max = device_status.heattem; ctr_Heat(OFF); } if((n % 60)==0) { device_status.heat_time_min = device_status.heat_time_min + 1; if(device_status.heat_time_min % 5 == 0) //加热5分钟温度低于50,判定加热故障 { if(device_status.heattem<50) { device_status.C01 |= (1 << 1); // 第1位变为1,加热故障 }else { device_status.C01 &= ~(1<<1); //加热 正常 } } } /* 屏幕 workSta.t1:剩余加热时间(秒)。恒温等待阶段 n 未推进,剩余时间按同一口径显示 */ device_status.work_heat_left = (n < heat_time) ? (uint16_t)(heat_time - n) : 0; //如果触发异常退出加热 if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1) { rt_thread_delay(1000); if(device_status.bat_status == 1) { rt_kprintf("============================触发异常退出加热=========================="); ctr_lamp(OFF,1); ctr_Heat(OFF); break; } } if(device_status.work_mode==1){ LOG_W("切换模式,退出加热!"); break; } if(device_status.timeSwitch==0&&device_status.sun_status==1) { ctr_Heat(OFF); rt_kprintf("异常退出!!!\n"); break; } rt_thread_delay(1000); rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time); HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time); } device_status.work_heat_left = 0; /* 加热结束,倒计时清零 */ ctr_Heat(OFF); //UpLoad_Data(); //加热结束的数据 } //拍照 void HK_TakePhoto(void) { //打开补光灯 //DTU_Write("takephoto.mp3"); rt_thread_delay(10000); rt_timer_start(timer_takephoto); char *takephotojson = makeJson_takephoto(); DTU_Write("%s",takephotojson); rt_free(takephotojson); //吸虫塔 if(device_status.version_type!=6) { ctr_led(ON); if(device_status.version_type==48) { rt_thread_delay(1000*60); } else { rt_thread_delay(1000*15); } } //触发海康相机拍照 Hk_Take_io(); if(device_status.version_type!=6) { rt_thread_delay(1000*45); } else { ctr_led(ON); rt_thread_delay(1000*19); } ctr_led(OFF); } //光控测试任务 void SunTest(void) { rt_uint16_t ui_timeout_l = 0; //未检测到光照,一秒扫描等待,超时30S while(Read_Light_Voltage()<500) { ui_timeout_l ++; rt_thread_delay(1000); if(ui_timeout_l==30) break; } rt_thread_delay(3000); if(Read_Light_Voltage()<500) { device_status.sun_status = 0;//光控状态 } if(Read_Light_Voltage()>500) { rt_thread_delay(1000); } rt_thread_delay(3000); if(Read_Light_Voltage()>500) { device_status.sun_status = 1; ctr_lamp(OFF,1); } HMI_WRITE("sysTest.switchSun.val=%d",0); device_status.sun_test = 0; device_status.work_status=0; } /*上电初始化*/ void ResetInit(void) { /* 复位时清掉屏幕 workSta 页的实时倒计时/进度(t1~t6),避免残留旧值 */ device_status.work_heat_left = 0; device_status.work_clt_left = 0; device_status.work_door_left = 0; device_status.work_progress = 0; device_status.photo_left = 0; device_status.clear_left = 0; device_status.work_start_tick = 0; device_status.work_total_sec = 0; if(device_status.version_type!=6&&device_status.version_type!=7) { device_status.work_done = 1; //1.打开路由器供电 ctr_router(ON); //2.打开相机供电 ctr_photo(ON); ctr_RTU_DTU(ON); //3.关闭镇流器 ctr_lamp(OFF,1); //4.关闭震动电机 ctr_shake(OFF, 0); //5.关闭加热 ctr_Heat(OFF); //6.关闭补光灯 if(device_status.photo_ws==0) ctr_led(OFF); if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1) { LOG_W("欠压!"); return; } //7.关闭上仓门 device_status.ctr_up_motor = 2; // while(device_status.ctr_up_motor!=0) // { // LOG_I("关闭上仓门..."); // rt_thread_delay(100); // } //8.关闭下仓门 device_status.ctr_down_motor = 2; while(device_status.ctr_down_motor!=0) { LOG_I("关闭下仓门...."); rt_thread_delay(100); } //9.清扫 if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1) { LOG_W("异常退出!!!\n"); return; } if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43) { if(device_status.photo_ws==0) device_status.clear_status=1; while(device_status.clear_status==1){ rt_thread_delay(1000); } #if JINGJIKUAN == 1 //再执行4次清扫 for(int i=0;i<4;i++) { if(device_status.photo_ws==0) device_status.clear_status=1; while(device_status.clear_status==1){ rt_thread_delay(1000); } } #endif } } else { if(device_status.power_status==1) { //1.打开路由器供电 #if MATCH_DEVICE ==0 ctr_router(ON); //2.打开相机供电 ctr_photo(ON); #endif ctr_RTU_DTU(ON); } } } /*三色光源*/ void Led_Check(uint8_t tp) { if(tp==3) { if(device_status.light_over_tim>2*3600) { device_status.work_status = 1; device_status.lamp_status = 1; device_status.lamp1_status = 0; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 1; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim<1*3600) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 0; device_status.lamp2_status = 1; LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim); } } else if (tp==6) { if(device_status.light_over_tim>4*3600) { device_status.work_status = 1; device_status.lamp_status = 1; device_status.lamp1_status = 0; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 1; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim<7200) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 0; device_status.lamp2_status = 1; LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim); } } else if (tp==9) { if(device_status.light_over_tim>6*3600) { device_status.work_status = 1; device_status.lamp_status = 1; device_status.lamp1_status = 0; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 1; device_status.lamp2_status = 0; LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim); } else if (device_status.light_over_tim<3*3600) { device_status.work_status = 1; device_status.lamp_status = 0; device_status.lamp1_status = 0; device_status.lamp2_status = 1; LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim); } } } void WorkCheck(void) { static rt_uint32_t light_timeout = 3600; while(device_status.ds == 1 && device_status.rain_status == 0 && device_status.temp_status == 0 && device_status.bat_status == 0) { if(device_status.timeSwitch == 1) // 时控模式 { LOG_I("***********时控*****************"); time_t now; time(&now); struct tm now_tm = *localtime(&now); if(device_status.startBar == device_status.stopBar) { // 开始结束时间相同则24小时模式 device_status.work_status = 1; rt_thread_delay(10000); LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00", device_status.work_status ? "工作" : "待机", now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec, device_status.startBar, device_status.stopBar); continue; } rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec; rt_uint32_t start_sec = device_status.startBar * 3600; rt_uint32_t end_sec = device_status.stopBar * 3600; // 修复跨天判断逻辑 if (start_sec < end_sec) { // 不跨天模式 device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0; } else { // 跨天模式 device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0; } LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00", device_status.work_status ? "工作" : "待机", now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec, device_status.startBar, device_status.stopBar); rt_thread_delay(10000); } else // 光控模式 { LOG_I("光控定时模式!"); if(device_status.sun_status == 0 && device_status.light_over_tim > 0) { device_status.light_over_tim--; light_timeout = 3600; // 重置超时计数器 device_status.work_status = 1; LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim); } else { // 仅在白天递减超时计数器 if(device_status.sun_status == 1 && light_timeout > 0) { light_timeout--; } device_status.work_status = 0; //没有在测试并且没有在自检 if(device_status.test_flag==0 && device_status.selfcheck_ws==0) { ctr_lamp(OFF,1); } // 连续1小时白天后重置 if(light_timeout == 0) { device_status.light_over_tim = 0; light_timeout = 3600; // 重置防止负数 device_status.light_over_tim = device_status.sunBar * 3600; } } } rt_thread_delay(1000); } // 异常状态处理 device_status.work_status = 0; rt_thread_delay(1000); } //工作任务流程s /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ==================== * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。 * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(85s) + 仓门动作预留(40s)。 * 实际存在仓门寻位、恒温等待、异常提前退出等偏差,故进度上限钳到 99%, * 与 t1~t3/t5/t6 的实时倒计时配合显示即可反映真实过程;非工作态返回 0。 */ void Work_Progress_Start(void) { uint32_t total; total = (uint32_t)device_status.clt_time * 60 + (uint32_t)device_status.heattime * 60 + WORK_PHOTO_SEC + WORK_CLEAR_SEC + WORK_DOOR_SEC; if (total == 0) total = 1; if (total > 0xFFFF) total = 0xFFFF; /* work_total_sec 为 uint16_t */ device_status.work_total_sec = (uint16_t)total; device_status.work_start_tick = rt_tick_get(); device_status.work_progress = 0; /* 新的一轮工作开始,清掉上一轮的实时倒计时 */ device_status.work_heat_left = 0; device_status.work_clt_left = 0; device_status.work_door_left = 0; device_status.photo_left = 0; device_status.clear_left = 0; } uint8_t Work_Progress_Get(void) { uint32_t elapsed; uint32_t total = device_status.work_total_sec; if (device_status.work_status != 1 || device_status.work_start_tick == 0 || total == 0) { return 0; } elapsed = (rt_tick_get() - device_status.work_start_tick) / RT_TICK_PER_SECOND; device_status.work_progress = (uint8_t)((elapsed >= total) ? 99 : (elapsed * 100 / total)); return device_status.work_progress; } void WorkTask(void) { static int clt_time = 0; //收集时间差秒数 static int heat_time = 0; LOG_I("工作状态:%d",device_status.work_status); LOG_I("开关机状态:%d",device_status.ds); while(device_status.clear_status==1){ LOG_I("等待清扫执行完成...."); rt_thread_delay(1000); } 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) { if(device_status.work_mode==1){ break; } device_status.work_done = 0; /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长, * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。 */ Work_Progress_Start(); if(clt_time==0) { DTU_Write("workstart.mp3"); if(device_status.debug_ws==1) { time_t stamp=time(RT_NULL); static struct tm tm; gmtime_r(&stamp, &tm); rt_sprintf(device_status.stamp,"%04d%02d%02d%02d%02d%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); dtu_write_json(makeJson_Warn("Work start!", device_status.stamp)); } } ctr_lamp(ON,1); LOG_I("打开灯管!!!"); if(device_status.version_type!=5) { rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流 //2.打开上仓门 LOG_I("打开上仓门!!!"); device_status.ctr_up_motor = 1; //3.收集 clt_time = device_status.clt_time * 60; while(clt_time!=1) { clt_time --; device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */ rt_thread_delay(1000); rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time); if(device_status.work_mode==1){ break; } //如果触发异常 if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1) { ctr_lamp(OFF,1); #if 0//LAMP_EN == 0 ctr_Fan_IN(OFF); #endif rt_kprintf("异常退出!!!\n"); break; } if(device_status.timeSwitch==0&&device_status.sun_status==1) { ctr_lamp(OFF,1); #if 0//LAMP_EN == 0 ctr_Fan_IN(OFF); #endif rt_kprintf("异常退出!!!\n"); break; } } clt_time = 1; device_status.work_clt_left = 0; /* 收集结束,倒计时清零 */ if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1) { ctr_lamp(OFF,1); #if 0//LAMP_EN == 0 ctr_Fan_IN(OFF); #endif rt_kprintf("异常退出!!!\n"); break; } if(device_status.timeSwitch==0&&device_status.sun_status==1) { ctr_lamp(OFF,1); #if 0//LAMP_EN == 0 ctr_Fan_IN(OFF); #endif rt_kprintf("异常退出!!!\n"); break; } #if 0//LAMP_EN == 0 ctr_Fan_IN(OFF); #endif //4.关闭上仓门 device_status.ctr_up_motor = 2; rt_kprintf("关闭上仓门!!!\n"); while(device_status.ctr_up_motor!=0) { rt_thread_delay(100); LOG_I("等待上仓门关闭!!!"); } //5.加热 if(device_status.heat_sw==0) { calorstat(device_status.heattime*60); } else { heat_time = device_status.heattime * 60; while(heat_time!=0) { if(device_status.work_mode==1) { break; } heat_time--; device_status.work_heat_left = (uint16_t)heat_time; /* 屏幕 workSta.t1 剩余加热时间(s) */ LOG_I("正在加热状态!!!"); rt_thread_delay(1000); } } device_status.work_heat_left = 0; #if TG_MODULE == 0 //6.打开下仓门 if(device_status.rain_status==0) { device_status.ctr_down_motor =1; } else { device_status.ctr_down_motor = 2; device_status.ctr_up_motor = 2; } //待下仓门完全打开 while(device_status.ctr_down_motor!=0) { rt_thread_delay(100); rt_kprintf("等待下仓门打开!!!\n"); } rt_thread_delay(3000); //关闭下仓门 device_status.ctr_down_motor = 2; while(device_status.ctr_down_motor!=0) { rt_thread_delay(100); rt_kprintf("等待下仓门关闭!!!\n"); } //2022-04-12add: LOG_I("打开上仓门!!!"); device_status.ctr_up_motor = 1; #else //打开下仓门 ctr_cangmen_down(OPEN); uint16_t timeout_down_open = 0; while(1) { timeout_down_open++; if(rt_pin_read(CTR_UPC_LIMIT)==1||timeout_down_open>20*20) break; rt_thread_delay(50); } ctr_cangmen_down(OFF); rt_thread_delay(1000); #endif if(device_status.work_mode==1){ break; } if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43) { //震动 ctr_shake(ON, device_status.shake_sec); //补光&&拍照 LOG_I("拍照!!!"); device_status.photo_ws = 1; } #if TG_MODULE == 1 while(device_status.photo_ws == 1) { rt_thread_delay(1000); LOG_I("等待拍照完成..."); } rt_thread_delay(1000); while(device_status.clear_status==1) { rt_thread_delay(1000); LOG_I("等待清扫完成..."); } ctr_cangmen_down(OPEN); //待下仓门完全打开 uint8_t i = 0; while(1) { i++; rt_thread_delay(100); rt_kprintf("等待下仓门打开!!!\n"); if(i>100) { ctr_cangmen_down(OFF); break; } } rt_thread_delay(2000); ctr_shake(ON, device_status.shake_sec); device_status.photo_ws = 1; //关闭下仓门 device_status.ctr_down_motor = 2; //待下仓门完全关闭 while(device_status.ctr_down_motor!=0) { rt_thread_delay(100); rt_kprintf("等待下仓门关闭!!!\n"); } device_status.ctr_up_motor = 1; //打开上仓门 #endif } } if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0) { LOG_I("-------------------not work--------------------"); device_status.work_status = 0; } //工作结束 if(device_status.work_done==0&&device_status.work_status==0) { DTU_Write("workdone"); ResetInit(); clt_time = 0; } rt_kprintf("***not work-device status:%d\n",device_status.ds); } #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用 void slidelock() { ctr_Motor_Driver_Power(ON); //电机驱动供电 rt_thread_delay(10); rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14 rt_pin_write(Motor_EN, ON); //引脚输出高使能有效 } /* 入口函数-工作状态检测 */ static void Work_status_thread_entry(void *parameter) { static uint16_t sun_status_num = 120; //如果是光控定时,进行光控监测的消抖 if(device_status.timeSwitch==0&&device_status.sun_status==0) { rt_kprintf("光控定时!!!\n"); while(sun_status_num!=0) { sun_status_num--; rt_thread_delay(1000); rt_kprintf("开机光控检测:%d\n",sun_status_num); } device_status.light_over_tim = device_status.sunBar * 3600; device_status.test_flag = 0; } while(1) { WorkCheck(); } } //指令 // /*入口函数-工作流程*/ static void Work_Loop_thread_entry(void *parameter) { rt_thread_delay(1000); ResetInit(); device_status.light_over_tim = device_status.sunBar * 3600; rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT); static struct tm tm_tg_run; static uint8_t alarm_h,alarm_m,alarm_s; while(1) { if(device_status.work_mode==0) { WorkTask(); }else{ if(device_status.work_status==1){ ctr_lamp(ON, 1); }else{ ctr_lamp(OFF, 1); } char buf[7] = {0}; /* ★ 原来用 "%-6s": 6 是"最小宽度"不是截断宽度, alarm_tg 是 char[12], 超过 6 字符时会完整写出 12+1 字节到 7 字节的 buf -> 栈溢出 6 字节。 必须写成 "%-6.6s"(精度 .6 才是截断)。 */ rt_snprintf(buf, sizeof(buf), "%-6.6s", device_status.alarm_tg); // 截取 时: 前2位 alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0'); // 截取 分: 中间2位 alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0'); // 截取 秒: 后2位 alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0'); device_status.ctr_up_motor = 2; //关闭上仓门 device_status.ctr_down_motor = 2; //关闭下仓门 LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s); //判断时间 time_t time_run_tg; time(&time_run_tg); tm_tg_run =*localtime(&time_run_tg); LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec); if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s)) { LOG_W("预制执行 开始!"); dtu_write_json(makeJson_Warn("执行完成", "预制模式")); rt_thread_delay(2000); //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1} cJSON *params = cJSON_CreateObject(); // params 子对象 cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20 cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7 /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串 -> 走 dtu_write_json */ dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1)); rt_thread_delay(20000); } } rt_thread_delay(1000); } } /*入口函数清扫*/ static void Control_Clear_thread_entry(void *parameter) { while(1) { if(device_status.update_flag==1) break; if(device_status.clear_status==1) { if(device_status.version_type==41){ ctr_TG_Clear(300); }else { ctr_TG_Clear(400); //20260924-350修改为400 } system_status.cleaning_count +=1; //清扫次数 } if(device_status.clear_status==2) { turnzp(); device_status.clear_status = 0; } rt_thread_delay(100); } } /*入口函数,自检*/ static void Control_Check_thread_entry(void *parameter) { static int total_seconds = 200; static int current_second = 0; static int process = 0; device_status.C01 = 0; rt_thread_delay(10000); while(1) { if(device_status.update_flag==1) break; if(device_status.work_status==1&&device_status.heat_sw==1) { calorstat(device_status.heattime*60); rt_thread_delay(10000); } else { if(device_status.selfcheck_ws==2) { while(current_second <= total_seconds) { process = (current_second * 100) / total_seconds; device_status.process = process / 100.0; LOG_I("process:%d",process); HMI_WRITE("selfcheck.s106.val=%d",process); rt_thread_delay(1000); current_second++; if(device_status.selfcheck_ws==0) { HMI_WRITE("selfcheck.s106.val=100"); break; } } HMI_WRITE("selfcheck.s106.val=100"); }else { process = 0; current_second = 0; #if 0 if(device_status.rain_status==1) { LOG_E("雨控!"); rt_thread_delay(10000); } //根据故障码 亮指示灯 //灯管故障 if(device_status.C01 & (1 << 0)) { LOG_E("灯管故障!"); //发送红灯常亮信号 write_single_holding_register(0x01,0x00C2,0x0011,1000); //延时10S rt_thread_delay(10000); } //加热故障 if(device_status.C01 & (1 << 1)) { LOG_E("加热故障!"); //发送红灯慢闪信号 write_single_holding_register(0x01,0x00C2,0x0021,1000); //延时10S rt_thread_delay(10000); } //相机拍照故障 if(device_status.C01 & (1 << 2)) { LOG_E("拍照故障!"); //发送红灯快闪信号 write_single_holding_register(0x01,0x00C2,0x0031,1000); //延时10S rt_thread_delay(10000); } //补光灯故障 if(device_status.C01 & (1 << 3)) { LOG_E("补光灯故障!"); //发送绿灯慢闪信号 write_single_holding_register(0x01,0x00C2,0x0023,1000); //延时10S rt_thread_delay(10000); } //上仓门故障 if(device_status.C01 & (1 << 8)) { LOG_E("上仓门故障!"); //发送黄灯常亮信号 write_single_holding_register(0x01,0x00C2,0x0012,1000); //延时10S rt_thread_delay(10000); } //下仓门故障 if(device_status.C01 & (1 << 9)) { LOG_E("下仓门故障!"); //发送黄灯慢闪信号 write_single_holding_register(0x01,0x00C2,0x0022,1000); //延时10S rt_thread_delay(10000); } //清扫故障 if(device_status.C01 & (1 << 10)) { LOG_E("清扫故障!"); //发送黄灯爆闪信号 write_single_holding_register(0x01,0x00C2,0x0032,1000); //延时10S rt_thread_delay(10000); } //通信故障 if(device_status.C01 & (1 << 13)) { LOG_E("通信故障!"); //发送绿灯爆闪信号 write_single_holding_register(0x01,0x00C2,0x0033,1000); //延时10S rt_thread_delay(10000); } if(device_status.C01==0) { LOG_W("无故障!"); //发送绿灯常亮信号 //write_single_holding_register(0x01,0x00C2,0x0013,1000); //延时10S rt_thread_delay(10000); } #endif } } rt_thread_delay(1000); } } static void thread_485_play_entry(void *param) { static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037}; rt_uint32_t e; // 创建事件控制块 event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO); if (event_485_play == RT_NULL) { LOG_E("Event create failed!"); return; } while(1) { if(device_status.update_flag==1) break; //等待事件 if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK) { LOG_I("Interrupt occurred and processed."); _485_play(sound105, 4); //发送串口消息,通知JETSON计算卡进行拍照 DTU_Write("{\"guangshan\":\"on\"}"); } } } /*清扫线程*/ int Control_clear(void) { /* 创建线程*/ tid_ctr = rt_thread_create("Control_Clear_thread_entry", Control_Clear_thread_entry, RT_NULL, 2048, //2048 24, //23 WORK_CHECK_THREAD_TIMESLICE); //5 if(tid_ctr!=RT_NULL) { rt_thread_startup(tid_ctr); } return 0; } /*恒温加热线程*/ int Control_Check(void) { /* 创建线程*/ tid_heat = rt_thread_create("Control_Check_thread_entry", Control_Check_thread_entry, RT_NULL, 1024, //2048 25, //23 WORK_CHECK_THREAD_TIMESLICE); //5 if(tid_heat!=RT_NULL) { rt_thread_startup(tid_heat); } return 0; } int thread_485_play(void) { tid_485_play = rt_thread_create("thread_485play", thread_485_play_entry, RT_NULL, 1024, //1024 26, //23 5); //5 if(tid_485_play!=RT_NULL) { rt_thread_startup(tid_485_play); } else { LOG_E("thread_485_play start failed!"); } return 0; } /* 创建线程 -工作状态检测*/ int Workstatus_check(void) { /* 创建线程*/ tid_wkstc = rt_thread_create("work_status_check_thread", Work_status_thread_entry, RT_NULL, WORK_CHECK_THREAD_STACK_SIZE, //1024 WORK_CHECK_THREAD_PRIORITY, //23 WORK_CHECK_THREAD_TIMESLICE); //5 if(tid_wkstc!=RT_NULL) { rt_thread_startup(tid_wkstc); } return 0; } /*工作流程线程*/ int Work_Loop_Task(void) { rt_err_t result_loop = 0; /* 创建线程*/ tid_workloop = rt_thread_create("work_loop_thread", Work_Loop_thread_entry, RT_NULL, WORK_LOOP_THREAD_STACK_SIZE, //2048 WORK_LOOP_THREAD_PRIORITY, //12 WORK_LOOP_THREAD_TIMESLICE); //5 if(tid_workloop!=RT_NULL) { result_loop = rt_thread_startup(tid_workloop); } return result_loop; } #ifdef RT_USING_ALARM void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp) { LOG_W("alarm_callback_router_on.\n"); ctr_router(ON); } void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp) { LOG_W("alarm_callback_router_off.\n"); ctr_router(OFF); } void alarm_app_router(void) { struct rt_alarm_setup setup; struct rt_alarm * alarm_router_on = RT_NULL; if (alarm_router_on != RT_NULL) return; setup.flag = RT_ALARM_DAILY; setup.wktime.tm_hour = 23; setup.wktime.tm_min = 0; setup.wktime.tm_sec = 0; alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup); if(RT_NULL != alarm_router_on) { LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec); rt_alarm_start(alarm_router_on); } } #endif MSH_CMD_EXPORT(ctr_lamp,ctr_lamp); MSH_CMD_EXPORT(ctr_Heat,ctr_Heat); MSH_CMD_EXPORT(ctr_clear,ctr_clear); MSH_CMD_EXPORT(ctr_led,sample:ctr_led ); MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power); //INIT_APP_EXPORT(Control_heat);