From 793bf2b4663df1869ca744b765597e2bf3d9b545 Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Tue, 14 Jul 2026 14:43:29 +0800 Subject: [PATCH] =?UTF-8?q?paint=5Frobot=5Fnew=E3=80=90=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E3=80=91=E7=94=B5=E6=9C=BA=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E5=92=8C=E7=8B=AC=E7=AB=8B=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=B7=B2=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- RBcore/{BHBF_robot.c => BHBF.c} | 12 +- RBcore/CMakeLists.txt | 6 +- RBcore/drv_interface.c | 16 +- RBcore/include/{BHBF_robot.h => BHBF.h} | 44 +- RBcore/msg_example.c | 378 ------------------ motor/CMakeLists.txt | 6 +- motor/include/motor_manager.h | 2 +- .../paint_robot_new => motor}/motor_example.c | 159 +++++++- motor/msp_motor_leisai.c | 1 - optional/msp_MK32.c | 2 +- optional/msp_TI5MOTOR.c | 2 +- project/paint_robot_new/CMakeLists.txt | 12 +- .../paint_robot_new/include/paint_robot_new.h | 2 - project/paint_robot_new/paint_robot_new.c | 74 +--- 15 files changed, 212 insertions(+), 506 deletions(-) rename RBcore/{BHBF_robot.c => BHBF.c} (92%) rename RBcore/include/{BHBF_robot.h => BHBF.h} (72%) delete mode 100644 RBcore/msg_example.c rename {project/paint_robot_new => motor}/motor_example.c (54%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4932eb..15bad08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,8 +117,8 @@ add_module(ringbuffer library/ringbuffer OFF OFF) add_module(peripheral peripheral OFF ON) add_module(lua lua OFF ON) add_module(bspMCU bspMCU OFF OFF) -add_module(motor motor OFF OFF) add_module(RBcore RBcore OFF OFF) +add_module(motor motor OFF OFF) add_module(optional optional OFF OFF) add_module(Spoolend project/Spoolend OFF OFF) add_module(paint_robot_new project/paint_robot_new OFF OFF) diff --git a/RBcore/BHBF_robot.c b/RBcore/BHBF.c similarity index 92% rename from RBcore/BHBF_robot.c rename to RBcore/BHBF.c index cd11f70..a5ad215 100644 --- a/RBcore/BHBF_robot.c +++ b/RBcore/BHBF.c @@ -3,7 +3,7 @@ 版权所有 (C), 2018-2099, Radkil ****************************************************************************** - 文 件 名 : BHBF_robot.c + 文 件 名 : BHBF.c 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年6月8日 @@ -16,11 +16,9 @@ 修改内容 : 创建文件 ******************************************************************************/ -#include "BHBF_robot.h" +#include "BHBF.h" #include "common.h" -#include "cmsis_os.h" -#include "FreeRTOS.h" #include "task.h" /*----------------------------------------------* @@ -73,15 +71,13 @@ void GF_Dispatch(void *argument) { while(1) { - GF_CMD GFCmd = MK32_Task(GV.m_iWorkMode); - GF_FsmStart(GFCmd); + //GF_CMD GFCmd = MK32_Task(GV.m_iWorkMode); + GF_FsmStart(0); } } void GF_Init(void) { - Drv_InterfaceInit(); - // const osThreadAttr_t GF_Dispatch_attributes = { // .name = "GF_Task", // .stack_size = 1024, diff --git a/RBcore/CMakeLists.txt b/RBcore/CMakeLists.txt index 2723f10..3b9ceec 100644 --- a/RBcore/CMakeLists.txt +++ b/RBcore/CMakeLists.txt @@ -9,8 +9,8 @@ else() message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") endif() -if(TARGET motor) - target_link_libraries(${TARGET_NAME} PUBLIC motor) +if(TARGET bspMCU) + target_link_libraries(${TARGET_NAME} PUBLIC bspMCU) else() - message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'motor' not found.") + message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.") endif() \ No newline at end of file diff --git a/RBcore/drv_interface.c b/RBcore/drv_interface.c index 86290a3..6ef0af4 100644 --- a/RBcore/drv_interface.c +++ b/RBcore/drv_interface.c @@ -16,11 +16,12 @@ 修改内容 : 创建文件 ******************************************************************************/ -#include "BHBF_robot.h" +#include "BHBF.h" #include "cmsis_os.h" #include "FreeRTOS.h" +#include "msg_center.h" /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ @@ -255,12 +256,9 @@ void SendAll_Task(void *argument) } extern void MotorInit(void); -void MotorTask(void *argument); void Drv_InterfaceInit(void) { - //MK32_Init(); - const osThreadAttr_t Read_RS485_1_attributes = { .name = "RS485_1", .stack_size = 512, @@ -275,14 +273,6 @@ void Drv_InterfaceInit(void) }; (void)osThreadNew(SendAll_Task, NULL, &send_task_attributes); - // 初始化电机 + MsgCenter_Init(); MotorInit(); - - // 创建电机任务 - const osThreadAttr_t motor_task_attributes = { - .name = "motor_task", - .stack_size = 1024, - .priority = (osPriority_t) osPriorityHigh5, - }; - (void)osThreadNew(MotorTask, NULL, &motor_task_attributes); } diff --git a/RBcore/include/BHBF_robot.h b/RBcore/include/BHBF.h similarity index 72% rename from RBcore/include/BHBF_robot.h rename to RBcore/include/BHBF.h index 2de252f..3d8aba7 100644 --- a/RBcore/include/BHBF_robot.h +++ b/RBcore/include/BHBF.h @@ -3,12 +3,12 @@ 版权所有 (C), 2018-2099, Radkil ****************************************************************************** - 文 件 名 : BHBF_robot.h + 文 件 名 : BHBF.h 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年6月8日 最近修改 : - 功能描述 : BHBF_robot.c 的头文件 + 功能描述 : BHBF.c 的头文件 修改历史 : 1.日 期 : 2026年6月8日 @@ -45,8 +45,8 @@ * 宏定义 * *----------------------------------------------*/ -#ifndef __BHBF_ROBOT_H__ -#define __BHBF_ROBOT_H__ +#ifndef __BHBF_H__ +#define __BHBF_H__ #ifdef __cplusplus @@ -61,11 +61,41 @@ extern "C"{ *----------------------------------------------*/ #include "drv_interface.h" #include "rd_time.h" - +#include "msg_center.h" +#include "cmsis_os.h" +#include "FreeRTOS.h" /*==============================================* * constants or macros define * *----------------------------------------------*/ +#define MODULE_NAME_MOTOR "motor" + +typedef enum { + MOTOR_CMD_SET_SPEED = 0x01, // 设置速度 + MOTOR_CMD_SET_POSITION = 0x02, // 设置位置 + MOTOR_CMD_STOP = 0x03, // 停止 + MOTOR_CMD_GET_STATUS = 0x04, // 获取状态 + MOTOR_CMD_SET_HOME = 0x05, // 设置零点 + MOTOR_CMD_SPEED_MODE = 0x06, // 速度模式 + MOTOR_CMD_POSITION_MODE = 0x07, // 位置模式 +} Motor_Cmd_e; + +// 电机命令数据结构 +typedef struct { + uint8_t m_ucMotorIndex; // 电机索引(0=左轮,1=右轮) + int32_t m_iValue; // 值(速度/位置等) +} Motor_CmdData_t; + +// 电机状态数据结构 +typedef struct { + uint8_t m_ucMotorIndex; + int32_t m_iPosition; + int32_t m_iVelocity; + uint32_t m_uiFaultCode; +} Motor_StatusData_t; + +#define MODULE_NAME_RBCORE "RBcore" + typedef enum { GF_CMD_DEFAULT_NULL = 0, GF_CMD_STOP_ALL, @@ -93,10 +123,6 @@ typedef struct _GV_struct_define GV_define *GetGV(void); void SetGV(GV_define *_pGV); // 待实现接口 -void MK32_Init(void); -GF_CMD MK32_Task(int mode); -void MoveWheel_Init(void); -void MoveWheel_Task(int32_t Target_Velcity[]); #ifdef __cplusplus #if __cplusplus diff --git a/RBcore/msg_example.c b/RBcore/msg_example.c deleted file mode 100644 index 850cf02..0000000 --- a/RBcore/msg_example.c +++ /dev/null @@ -1,378 +0,0 @@ -/****************************************************************************** - - 版权所有 (C), 2018-2099, Radkil - - ****************************************************************************** - 文 件 名 : msg_example.c - 版 本 号 : 初稿 - 作 者 : radkil - 生成日期 : 2026年7月13日 - 最近修改 : - 功能描述 : 消息中心使用示例 - - 修改历史 : - 1.日 期 : 2026年7月13日 - 作 者 : radkil - 修改内容 : 创建文件 - - ******************************************************************************/ - -#include "BHBF_robot.h" -#include "msg_center.h" -#include "motor_manager.h" -#include "msp_motor_leisai.h" -#include "cmsis_os.h" -#include "FreeRTOS.h" - -/*==============================================* - * 模块定义 * - *==============================================*/ - -// 模块名称定义 -#define MODULE_NAME_MOTOR "motor" -#define MODULE_NAME_CHASSIS "chassis" -#define MODULE_NAME_UI "ui" - -/*==============================================* - * 电机模块命令字定义 * - *==============================================*/ - -typedef enum { - MOTOR_CMD_SET_SPEED = 0x01, // 设置速度 - MOTOR_CMD_SET_POSITION = 0x02, // 设置位置 - MOTOR_CMD_STOP = 0x03, // 停止 - MOTOR_CMD_GET_STATUS = 0x04, // 获取状态 - MOTOR_CMD_SET_HOME = 0x05, // 设置零点 - MOTOR_CMD_SPEED_MODE = 0x06, // 速度模式 - MOTOR_CMD_POSITION_MODE = 0x07, // 位置模式 -} Motor_Cmd_e; - -// 电机命令数据结构 -typedef struct { - uint8_t m_ucMotorIndex; // 电机索引(0=左轮,1=右轮) - int32_t m_iValue; // 值(速度/位置等) -} Motor_CmdData_t; - -// 电机状态数据结构 -typedef struct { - uint8_t m_ucMotorIndex; - int32_t m_iPosition; - int32_t m_iVelocity; - uint32_t m_uiFaultCode; -} Motor_StatusData_t; - -/*==============================================* - * 底盘模块命令字定义 * - *==============================================*/ - -typedef enum { - CHASSIS_CMD_MOVE = 0x01, // 移动 - CHASSIS_CMD_STOP = 0x02, // 停止 - CHASSIS_CMD_SET_SPEED = 0x03, // 设置速度 -} Chassis_Cmd_e; - -typedef struct { - int32_t m_iSpeedX; // X方向速度 - int32_t m_iSpeedY; // Y方向速度 - int32_t m_iSpeedW; // 旋转速度 -} Chassis_MoveData_t; - -/*==============================================* - * 模块级变量 * - *==============================================*/ - -static uint32_t g_uiMotorModuleID = 0; -static uint32_t g_uiChassisModuleID = 0; -static uint32_t g_uiUIModuleID = 0; - -static Motor_Instance_t *g_apstMotors[2] = {NULL, NULL}; - -/*==============================================* - * 电机模块消息处理 * - *==============================================*/ - -static void Motor_ModuleHandler(const Msg_t *pstMsg) -{ - if (NULL == pstMsg) - { - return; - } - - switch (pstMsg->m_uiMsgID) - { - case MOTOR_CMD_SET_SPEED: - { - if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t)) - { - Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData; - if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL) - { - MotorMgr_SetTargetSpeed(g_apstMotors[pstData->m_ucMotorIndex], pstData->m_iValue); - } - } - break; - } - case MOTOR_CMD_STOP: - { - for (int i = 0; i < 2; i++) - { - if (g_apstMotors[i] != NULL) - { - MotorMgr_SetTargetSpeed(g_apstMotors[i], 0); - } - } - break; - } - case MOTOR_CMD_SET_HOME: - { - if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t)) - { - Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData; - if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL) - { - MotorMgr_SetHome(g_apstMotors[pstData->m_ucMotorIndex]); - } - } - break; - } - case MOTOR_CMD_GET_STATUS: - { - // 回复状态给请求者 - Motor_StatusData_t stStatus; - for (int i = 0; i < 2; i++) - { - if (g_apstMotors[i] != NULL) - { - const Motor_Data_t *pstData = MotorMgr_GetData(g_apstMotors[i]); - if (pstData != NULL) - { - stStatus.m_ucMotorIndex = i; - stStatus.m_iPosition = pstData->m_iRealPosition; - stStatus.m_iVelocity = pstData->m_iRealVelocity; - stStatus.m_uiFaultCode = pstData->m_uiFaultCode; - - // 回复给请求者 - MsgCenter_SendTo("ui", 0x10, &stStatus, sizeof(stStatus)); - } - } - } - break; - } - default: - break; - } -} - -/*==============================================* - * 底盘模块消息处理 * - *==============================================*/ - -static void Chassis_ModuleHandler(const Msg_t *pstMsg) -{ - if (NULL == pstMsg) - { - return; - } - - switch (pstMsg->m_uiMsgID) - { - case CHASSIS_CMD_MOVE: - { - if (pstMsg->m_uiDataLen >= sizeof(Chassis_MoveData_t)) - { - Chassis_MoveData_t *pstData = (Chassis_MoveData_t *)pstMsg->m_aucData; - - // 转换为左右轮速度(差速模型) - int32_t iLeftSpeed = pstData->m_iSpeedX - pstData->m_iSpeedW; - int32_t iRightSpeed = pstData->m_iSpeedX + pstData->m_iSpeedW; - - // 发送给电机模块 - Motor_CmdData_t stMotorCmd; - stMotorCmd.m_ucMotorIndex = 0; - stMotorCmd.m_iValue = iLeftSpeed; - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stMotorCmd, sizeof(stMotorCmd)); - - stMotorCmd.m_ucMotorIndex = 1; - stMotorCmd.m_iValue = iRightSpeed; - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stMotorCmd, sizeof(stMotorCmd)); - } - break; - } - case CHASSIS_CMD_STOP: - { - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_STOP, NULL, 0); - break; - } - default: - break; - } -} - -/*==============================================* - * UI模块消息处理 * - *==============================================*/ - -static void UI_ModuleHandler(const Msg_t *pstMsg) -{ - if (NULL == pstMsg) - { - return; - } - - // 处理来自其他模块的回复 - switch (pstMsg->m_uiMsgID) - { - case 0x10: // 电机状态回复 - { - if (pstMsg->m_uiDataLen >= sizeof(Motor_StatusData_t)) - { - Motor_StatusData_t *pstStatus = (Motor_StatusData_t *)pstMsg->m_aucData; - RD_PRINTF("Motor[%d] Status: Pos=%ld, Vel=%ld, Fault=%ld\n", - pstStatus->m_ucMotorIndex, - pstStatus->m_iPosition, - pstStatus->m_iVelocity, - pstStatus->m_uiFaultCode); - } - break; - } - default: - break; - } -} - -/*==============================================* - * 模块任务函数 * - *==============================================*/ - -// 电机模块任务 -void Motor_ModuleTask(void *argument) -{ - // 初始化电机 - MotorMgr_Init(); - - Motor_Config_t stConfig = { - .m_ucMotorID = 1, - .m_uiPulsePerRound = 10000, - .m_uiReductionRatio = 70, - .m_fWheelDiameter = 0.26f - }; - g_apstMotors[0] = MotorMgr_Create(&stConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); - - stConfig.m_ucMotorID = 2; - g_apstMotors[1] = MotorMgr_Create(&stConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); - - // 初始化电机 - for (int i = 0; i < 2; i++) - { - if (g_apstMotors[i] != NULL) - { - MotorMgr_ResetAll(g_apstMotors[i]); - MotorMgr_ActivateAll(g_apstMotors[i]); - MotorMgr_SpeedModeInit(g_apstMotors[i]); - } - } - - while (1) - { - // 处理消息 - MsgCenter_ProcessWait(g_uiMotorModuleID, 2); - - // 周期性读取电机状态 - for (int i = 0; i < 2; i++) - { - if (g_apstMotors[i] != NULL) - { - MotorMgr_RequestPosition(g_apstMotors[i]); - MotorMgr_RequestVelocity(g_apstMotors[i]); - MotorMgr_RequestFault(g_apstMotors[i]); - } - } - } -} - -// 底盘模块任务 -void Chassis_ModuleTask(void *argument) -{ - while (1) - { - // 处理消息 - MsgCenter_ProcessWait(g_uiChassisModuleID, 10); - } -} - -// UI模块任务 -void UI_ModuleTask(void *argument) -{ - while (1) - { - // 处理消息 - MsgCenter_ProcessWait(g_uiUIModuleID, 100); - - // 定期请求电机状态 - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_GET_STATUS, NULL, 0); - } -} - -/*==============================================* - * 初始化示例 * - *==============================================*/ - -void MsgExample_Init(void) -{ - // 初始化消息中心 - MsgCenter_Init(); - - // 注册模块 - g_uiMotorModuleID = MsgCenter_Register(MODULE_NAME_MOTOR, Motor_ModuleHandler); - g_uiChassisModuleID = MsgCenter_Register(MODULE_NAME_CHASSIS, Chassis_ModuleHandler); - g_uiUIModuleID = MsgCenter_Register(MODULE_NAME_UI, UI_ModuleHandler); - - // 创建模块任务 - const osThreadAttr_t motor_task_attr = { - .name = "motor_task", - .stack_size = 1024, - .priority = (osPriority_t) osPriorityHigh5, - }; - (void)osThreadNew(Motor_ModuleTask, NULL, &motor_task_attr); - - const osThreadAttr_t chassis_task_attr = { - .name = "chassis_task", - .stack_size = 512, - .priority = (osPriority_t) osPriorityHigh4, - }; - (void)osThreadNew(Chassis_ModuleTask, NULL, &chassis_task_attr); - - const osThreadAttr_t ui_task_attr = { - .name = "ui_task", - .stack_size = 512, - .priority = (osPriority_t) osPriorityNormal3, - }; - (void)osThreadNew(UI_ModuleTask, NULL, &ui_task_attr); -} - -/*==============================================* - * 外部调用示例 * - *==============================================*/ - -// 从任何线程调用这些函数即可跨线程控制电机 -void Example_SetMotorSpeed(uint8_t ucMotorIndex, int32_t iSpeed) -{ - Motor_CmdData_t stData; - stData.m_ucMotorIndex = ucMotorIndex; - stData.m_iValue = iSpeed; - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stData, sizeof(stData)); -} - -void Example_StopAllMotors(void) -{ - MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_STOP, NULL, 0); -} - -void Example_ChassisMove(int32_t iSpeedX, int32_t iSpeedW) -{ - Chassis_MoveData_t stData; - stData.m_iSpeedX = iSpeedX; - stData.m_iSpeedY = 0; - stData.m_iSpeedW = iSpeedW; - MsgCenter_SendTo(MODULE_NAME_CHASSIS, CHASSIS_CMD_MOVE, &stData, sizeof(stData)); -} diff --git a/motor/CMakeLists.txt b/motor/CMakeLists.txt index 3b9ceec..9f262e6 100644 --- a/motor/CMakeLists.txt +++ b/motor/CMakeLists.txt @@ -9,8 +9,8 @@ else() message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") endif() -if(TARGET bspMCU) - target_link_libraries(${TARGET_NAME} PUBLIC bspMCU) +if(TARGET RBcore) + target_link_libraries(${TARGET_NAME} PUBLIC RBcore) else() - message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.") + message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'RBcore' not found.") endif() \ No newline at end of file diff --git a/motor/include/motor_manager.h b/motor/include/motor_manager.h index b78ae8f..13c9001 100644 --- a/motor/include/motor_manager.h +++ b/motor/include/motor_manager.h @@ -34,7 +34,7 @@ extern "C"{ /*==============================================* * constants or macros define * *----------------------------------------------*/ - +#define MOTOR_INSTANCE_MAX 8 //支持最大电机实例数 /*==============================================* * data structures * *----------------------------------------------*/ diff --git a/project/paint_robot_new/motor_example.c b/motor/motor_example.c similarity index 54% rename from project/paint_robot_new/motor_example.c rename to motor/motor_example.c index aa95075..0a07c64 100644 --- a/project/paint_robot_new/motor_example.c +++ b/motor/motor_example.c @@ -16,8 +16,7 @@ 修改内容 : 创建文件 ******************************************************************************/ - -#include "BHBF_robot.h" +#include "BHBF.h" #include "motor_manager.h" #include "msp_motor_leisai.h" @@ -40,7 +39,8 @@ /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ - +static Motor_Instance_t *g_apstMotors[MOTOR_INSTANCE_MAX] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; +static uint32_t g_uiMotorModuleID = 0; /*----------------------------------------------* * 常量定义 * *----------------------------------------------*/ @@ -49,6 +49,7 @@ * 宏定义 * *----------------------------------------------*/ +#if 0 /***************************************************************************** 函 数 名 : MotorExample_LeiSai 功能描述 : 雷赛电机使用示例 @@ -179,3 +180,155 @@ void MotorExample_MultiProtocol(void) RD_PRINTF("Found motor: %s\n", pstFound->m_szName); } } +#endif + +void MotorTask(void *argument) +{ + // 等待系统稳定 + Rd_Delay(1000); + + // 复位并激活所有电机 + for (int i = 0; i < MOTOR_INSTANCE_MAX; i++) + { + if (g_apstMotors[i] != NULL) + { + MotorMgr_ResetAll(g_apstMotors[i]); + Rd_Delay(500); + MotorMgr_ActivateAll(g_apstMotors[i]); + Rd_Delay(500); + MotorMgr_SetHome(g_apstMotors[i]); + Rd_Delay(100); + MotorMgr_SpeedModeInit(g_apstMotors[i]); + } + } + + while(1) + { + // 处理消息 + MsgCenter_ProcessWait(g_uiMotorModuleID, 2); + + for (int i = 0; i < MOTOR_INSTANCE_MAX; i++) + { + if (g_apstMotors[i] != NULL) + { + // 读取位置、速度、故障 + MotorMgr_RequestPosition(g_apstMotors[i]); + MotorMgr_RequestFault(g_apstMotors[i]); + MotorMgr_RequestVelocity(g_apstMotors[i]); + } + } + + Rd_Delay(2); // 2ms周期 + } +} + +/*==============================================* + * 电机模块消息处理 * + *==============================================*/ + +static void Motor_ModuleHandler(const Msg_t *pstMsg) +{ + if (NULL == pstMsg) + { + return; + } + + switch (pstMsg->m_uiMsgID) + { + case MOTOR_CMD_SET_SPEED: + { + if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t)) + { + Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData; + if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL) + { + MotorMgr_SetTargetSpeed(g_apstMotors[pstData->m_ucMotorIndex], pstData->m_iValue); + } + } + break; + } + case MOTOR_CMD_STOP: + { + for (int i = 0; i < 2; i++) + { + if (g_apstMotors[i] != NULL) + { + MotorMgr_SetTargetSpeed(g_apstMotors[i], 0); + } + } + break; + } + case MOTOR_CMD_SET_HOME: + { + if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t)) + { + Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData; + if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL) + { + MotorMgr_SetHome(g_apstMotors[pstData->m_ucMotorIndex]); + } + } + break; + } + case MOTOR_CMD_GET_STATUS: + { + // 回复状态给请求者 + Motor_StatusData_t stStatus; + for (int i = 0; i < 2; i++) + { + if (g_apstMotors[i] != NULL) + { + const Motor_Data_t *pstData = MotorMgr_GetData(g_apstMotors[i]); + if (pstData != NULL) + { + stStatus.m_ucMotorIndex = i; + stStatus.m_iPosition = pstData->m_iRealPosition; + stStatus.m_iVelocity = pstData->m_iRealVelocity; + stStatus.m_uiFaultCode = pstData->m_uiFaultCode; + + // 回复给请求者 + MsgCenter_SendTo(MODULE_NAME_RBCORE, 0x10, &stStatus, sizeof(stStatus)); + } + } + } + break; + } + default: + break; + } +} + +void MotorInit(void) +{ + // 初始化电机管理模块 + MotorMgr_Init(); + + // 配置左轮电机 + Motor_Config_t stLeftMotorConfig = { + .m_ucMotorID = 1, + .m_uiPulsePerRound = 10000, + .m_uiReductionRatio = 70, + .m_fWheelDiameter = 0.26f + }; + g_apstMotors[0] = MotorMgr_Create(&stLeftMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); + + // 配置右轮电机 + Motor_Config_t stRightMotorConfig = { + .m_ucMotorID = 2, + .m_uiPulsePerRound = 10000, + .m_uiReductionRatio = 70, + .m_fWheelDiameter = 0.26f + }; + g_apstMotors[1] = MotorMgr_Create(&stRightMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); + + g_uiMotorModuleID = MsgCenter_Register(MODULE_NAME_MOTOR, Motor_ModuleHandler); + + // 创建电机任务 + const osThreadAttr_t motor_task_attributes = { + .name = "motor_task", + .stack_size = 1024, + .priority = (osPriority_t) osPriorityHigh5, + }; + (void)osThreadNew(MotorTask, NULL, &motor_task_attributes); +} + diff --git a/motor/msp_motor_leisai.c b/motor/msp_motor_leisai.c index de37451..8c4b296 100644 --- a/motor/msp_motor_leisai.c +++ b/motor/msp_motor_leisai.c @@ -18,7 +18,6 @@ ******************************************************************************/ #include "msp_motor_leisai.h" -#include /*----------------------------------------------* * 外部变量说明 * diff --git a/optional/msp_MK32.c b/optional/msp_MK32.c index 5fc0f6a..16d0b67 100644 --- a/optional/msp_MK32.c +++ b/optional/msp_MK32.c @@ -17,7 +17,7 @@ ******************************************************************************/ #include "msp_MK32.h" -#include "BHBF_robot.h" +#include "BHBF.h" #include diff --git a/optional/msp_TI5MOTOR.c b/optional/msp_TI5MOTOR.c index 0eb29b5..217f30c 100644 --- a/optional/msp_TI5MOTOR.c +++ b/optional/msp_TI5MOTOR.c @@ -17,7 +17,7 @@ ******************************************************************************/ #include "msp_TI5MOTOR.h" -#include "BHBF_robot.h" +#include "BHBF.h" /*----------------------------------------------* * 外部变量说明 * diff --git a/project/paint_robot_new/CMakeLists.txt b/project/paint_robot_new/CMakeLists.txt index 115a83e..793f897 100644 --- a/project/paint_robot_new/CMakeLists.txt +++ b/project/paint_robot_new/CMakeLists.txt @@ -9,14 +9,8 @@ else() message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") endif() -if(TARGET RBcore) - target_link_libraries(${TARGET_NAME} PUBLIC RBcore) +if(TARGET bspMCU) + target_link_libraries(${TARGET_NAME} PUBLIC bspMCU) else() - message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'RBcore' not found.") -endif() - -if(TARGET peripheral) - target_link_libraries(${TARGET_NAME} PUBLIC peripheral) -else() - message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'peripheral' not found.") + message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.") endif() \ No newline at end of file diff --git a/project/paint_robot_new/include/paint_robot_new.h b/project/paint_robot_new/include/paint_robot_new.h index 6144c33..8af2f56 100644 --- a/project/paint_robot_new/include/paint_robot_new.h +++ b/project/paint_robot_new/include/paint_robot_new.h @@ -16,8 +16,6 @@ 修改内容 : 创建文件 ******************************************************************************/ -#include "motor_manager.h" -#include "msp_motor_leisai.h" /*----------------------------------------------* * 外部变量说明 * diff --git a/project/paint_robot_new/paint_robot_new.c b/project/paint_robot_new/paint_robot_new.c index 7378998..b8f9228 100644 --- a/project/paint_robot_new/paint_robot_new.c +++ b/project/paint_robot_new/paint_robot_new.c @@ -17,12 +17,10 @@ ******************************************************************************/ #include "paint_robot_new.h" -#include "com.h" -#include "rd_time.h" + /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ -extern TComCtrl *g_ptFDCAN1; /*----------------------------------------------* * 外部函数原型说明 * @@ -35,8 +33,6 @@ extern TComCtrl *g_ptFDCAN1; /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ -static Motor_Instance_t *g_pstLeftMotor; // 左轮电机实例 -static Motor_Instance_t *g_pstRightMotor; // 右轮电机实例 /*----------------------------------------------* * 模块级变量 * @@ -50,73 +46,5 @@ static Motor_Instance_t *g_pstRightMotor; // 右轮电机实例 * 宏定义 * *----------------------------------------------*/ -void MotorInit(void) -{ - // 初始化电机管理模块 - MotorMgr_Init(); - - - // 配置左轮电机 - Motor_Config_t stLeftMotorConfig = { - .m_ucMotorID = 1, - .m_uiPulsePerRound = 10000, - .m_uiReductionRatio = 70, - .m_fWheelDiameter = 0.26f - }; - g_pstLeftMotor = MotorMgr_Create(&stLeftMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); - - // 配置右轮电机 - Motor_Config_t stRightMotorConfig = { - .m_ucMotorID = 2, - .m_uiPulsePerRound = 10000, - .m_uiReductionRatio = 70, - .m_fWheelDiameter = 0.26f - }; - g_pstRightMotor = MotorMgr_Create(&stRightMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL); -} - -void MotorTask(void *argument) -{ - // 等待系统稳定 - Rd_Delay(1000); - - // 复位并激活所有电机 - if (g_pstLeftMotor != NULL) - { - MotorMgr_ResetAll(g_pstLeftMotor); - Rd_Delay(500); - MotorMgr_ActivateAll(g_pstLeftMotor); - Rd_Delay(500); - MotorMgr_SetHome(g_pstLeftMotor); - Rd_Delay(100); - MotorMgr_SpeedModeInit(g_pstLeftMotor); - } - - while(1) - { - if (g_pstLeftMotor != NULL) - { - // 读取位置、速度、故障 - MotorMgr_RequestPosition(g_pstLeftMotor); - MotorMgr_RequestFault(g_pstLeftMotor); - MotorMgr_RequestVelocity(g_pstLeftMotor); - - // 设置目标速度 - MotorMgr_SetTargetSpeed(g_pstLeftMotor, 0); - } - - if (g_pstRightMotor != NULL) - { - // 读取位置、速度、故障 - MotorMgr_RequestPosition(g_pstRightMotor); - MotorMgr_RequestFault(g_pstRightMotor); - MotorMgr_RequestVelocity(g_pstRightMotor); - - // 设置目标速度 - MotorMgr_SetTargetSpeed(g_pstRightMotor, 0); - } - Rd_Delay(2); // 2ms周期 - } -}