From ac32f66352e66b5cd87078f269ebb993a230c51a Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Tue, 15 Sep 2026 11:34:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=88=E6=8A=A4=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RBcore/daemon_task.c | 178 ++++++++++++++++++++++++++++++++++++++++++ RBcore/include/BHBF.h | 12 +++ 2 files changed, 190 insertions(+) create mode 100644 RBcore/daemon_task.c diff --git a/RBcore/daemon_task.c b/RBcore/daemon_task.c new file mode 100644 index 0000000..4586515 --- /dev/null +++ b/RBcore/daemon_task.c @@ -0,0 +1,178 @@ +/****************************************************************************** + + 版权所有 (C), 2018-2099, Radkil + + ****************************************************************************** + 文 件 名 : daemon_task.c + 版 本 号 : 初稿 + 作 者 : radkil + 生成日期 : 2026年9月14日 + 最近修改 : + 功能描述 : 守护线程,用于处理各种异常 + + 修改历史 : + 1.日 期 : 2026年9月14日 + 作 者 : radkil + 修改内容 : 创建文件 + +******************************************************************************/ +#include "BHBF.h" +#include "msg_center.h" +#include "bsp_Error.pb.h" + +/*----------------------------------------------* + * 常量/宏定义(前置,供模块级变量与函数使用) * + *----------------------------------------------*/ +#define COM_ERROR_MODULE_NUM 7 // ComError 模块数量(位 0~6) +#define COM_ERROR_TIMEOUT_THRESHOLD 50 // 连续 N 次未收到正常信号判定为模块异常 + +/*----------------------------------------------* + * 外部变量说明 * + *----------------------------------------------*/ + +/*----------------------------------------------* + * 外部函数原型说明 * + *----------------------------------------------*/ + +/*----------------------------------------------* + * 内部函数原型说明 * + *----------------------------------------------*/ + +/*----------------------------------------------* + * 全局变量 * + *----------------------------------------------*/ + +/*----------------------------------------------* + * 模块级变量 * + *----------------------------------------------*/ +static uint32_t g_uidaemonModuleID = 0; +static int32_t g_iCom_Error_Code = 0; +static uint32_t g_auiComErrorCount[COM_ERROR_MODULE_NUM] = {0}; // 各模块失联计数 +static int32_t g_iCom_Error_Status = 0; // 按位记录模块异常状态:1=异常,0=正常 + +/*----------------------------------------------* + * 常量定义 * + *----------------------------------------------*/ + +/*----------------------------------------------* + * 宏定义 * + *----------------------------------------------*/ + +void SET_BIT_1(int32_t* num,int32_t k) +{ + *num=((*num) | (1 << (k))); +} + +void SET_BIT_0(int32_t* num,int32_t k) +{ + *num=((*num) & ~(1 << (k))); +} + +int32_t Get_BIT(int32_t* num,int32_t k) +{ + return (*num >> (k)) & 1; +} + +/***************************************************************************** + 函 数 名 : daemon_PollComError + 功能描述 : 按位轮询通信错误标志,实现各模块心跳/存活检测 + 位为 1 表示本周期收到模块正常信号,消费该标志并清零失联计数; + 位为 0 表示本周期未收到正常信号,失联计数递增,达到阈值判定异常。 + 输入参数 : 无 + 输出参数 : 无 + 返 回 值 : 无 +*****************************************************************************/ +void daemon_PollComError(void) +{ + for (int32_t i = 0; i < COM_ERROR_MODULE_NUM; i++) + { + if (Get_BIT(&g_iCom_Error_Code, i) == 1) + { + // 收到正常信号:消费标志位,等待下周期模块重新置位 + SET_BIT_0(&g_iCom_Error_Code, i); + g_auiComErrorCount[i] = 0; // 失联计数清零 + SET_BIT_0(&g_iCom_Error_Status, i); // 标记模块正常 + } + else + { + // 未收到正常信号:失联计数递增 + g_auiComErrorCount[i]++; + if (g_auiComErrorCount[i] >= COM_ERROR_TIMEOUT_THRESHOLD) + { + SET_BIT_1(&g_iCom_Error_Status, i); // 判定模块异常 + g_auiComErrorCount[i] = COM_ERROR_TIMEOUT_THRESHOLD; // 计数封顶,避免溢出 + MsgCenter_SendTo(MODULE_NAME_CUSTOM, CUSTOM_GET_DAEMON_CODE, &g_iCom_Error_Status, sizeof(int32_t)); + } + } + } +} + +static void daemon_ModuleHandler(const Msg_t *pstMsg) +{ + if (NULL == pstMsg) + { + return; + } + + switch (pstMsg->m_uiMsgID) + { + case DAEMON_SET_MK32_SBUS: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_MK32_SBus); + break; + } + case DAEMON_SET_TL720D: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_TL720D); + break; + } + case DAEMON_SET_LEFT_MOTOR: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_LS_LeftMotor); + break; + } + case DAEMON_SET_RIGHT_MOTOR: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_LS_RightMotor); + break; + } + case DAEMON_SET_BUTTON_RESET: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_Remote_Button_Reset_State); + break; + } + case DAEMON_SET_MK32_SERIAL: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_MK32_Serial); + break; + } + case DAEMON_SET_MK32_UDP: + { + SET_BIT_1(&g_iCom_Error_Code, ComError_MK32_UDP); + break; + } + default: + break; + } +} + +void daemon_task(void *argument) +{ + while(1) + { + MsgCenter_ProcessWait(g_uidaemonModuleID, 2); + daemon_PollComError(); + } +} + +void daemon_Init(void) +{ + g_uidaemonModuleID = MsgCenter_Register(MODULE_NAME_DAEMON, daemon_ModuleHandler); + + const osThreadAttr_t daemon_attributes = { + .name = MODULE_NAME_DAEMON, + .stack_size = 1024, + .priority = (osPriority_t) osPriorityRealtime1, + }; + (void)osThreadNew(daemon_task, NULL, &daemon_attributes); +} diff --git a/RBcore/include/BHBF.h b/RBcore/include/BHBF.h index fab37b8..aab082e 100644 --- a/RBcore/include/BHBF.h +++ b/RBcore/include/BHBF.h @@ -71,6 +71,7 @@ extern "C"{ #define MODULE_NAME_MOTOR "motor" #define MODULE_NAME_CUSTOM "custom" #define MODULE_NAME_SENDIV "sendiv" +#define MODULE_NAME_DAEMON "daemon" typedef enum { COMMON_CMD_SHOW_INFO, // 终端信息展示 @@ -97,12 +98,22 @@ typedef enum { CUSTOM_GET_TL720D_ROLL, // 获取陀螺仪横滚角(IV显示) CUSTOM_GET_SPEED, // 获取速度(电机回码) CUSTOM_GET_FAULT_CODE, // 获取错误码(电机回码) + CUSTOM_GET_DAEMON_CODE, // 获取daemon错误码 CUSTOM_CMD_PAINTGUN, // 控制喷枪 CUSTOM_CMD_STRAIGHT_DRIVE, // RBCORE_CMD_STRAIGHT_DRIVE命令停止后回调 CUSTOM_CMD_TURN_ANGLE, // RBCORE_CMD_TURN_ANGLE命令停止后回调 SENDIV_START = 0x0400, // *IV发送线程命令(一般低优先级事项也放到这)* SENDIV_SET_IV, // 设置IV + DAEMON_START = 0x0500, // *守护线程命令 + + DAEMON_SET_MK32_SBUS, // 置位ComError_MK32_SBus + DAEMON_SET_TL720D, // 置位ComError_TL720D + DAEMON_SET_LEFT_MOTOR, // 置位ComError_LS_LeftMotor + DAEMON_SET_RIGHT_MOTOR, // 置位ComError_LS_RightMotor + DAEMON_SET_BUTTON_RESET, // 置位ComError_Remote_Button_Reset_State + DAEMON_SET_MK32_SERIAL, // 置位ComError_MK32_Serial + DAEMON_SET_MK32_UDP, // 置位ComError_MK32_UDP } BHBF_Cmd_e; typedef struct { @@ -113,6 +124,7 @@ typedef struct { /*==============================================* * project-wide global variables * *----------------------------------------------*/ +int32_t Get_BIT(int32_t* num,int32_t k); /*==============================================*