diff --git a/RBcore/BHBF.c b/RBcore/BHBF.c index 76e082d..1870814 100644 --- a/RBcore/BHBF.c +++ b/RBcore/BHBF.c @@ -57,32 +57,31 @@ static void RBcore_ModuleHandler(const Msg_t *pstMsg) switch (pstMsg->m_uiMsgID) { - case MOTOR_CMD_SET_SPEED: + case RBCORE_CMD_MANUAL_FORWARD: { break; } - case MOTOR_CMD_STOP: + case RBCORE_CMD_MANUAL_BACKWARD: { break; } - case MOTOR_CMD_SET_HOME: - { + case RBCORE_CMD_MANUAL_TURNLEFT: + { - break; - } - case MOTOR_CMD_GET_STATUS: - { + break; + } + case RBCORE_CMD_MANUAL_TURNRIGHT: + { - break; - } + break; + } default: break; } } - void RBcore_Task(void *argument) { while(1) diff --git a/RBcore/drv_interface.c b/RBcore/drv_interface.c index 02ba487..06d4413 100644 --- a/RBcore/drv_interface.c +++ b/RBcore/drv_interface.c @@ -226,75 +226,17 @@ void Read_RS485_1(void *argument) } extern void MotorInit(void); -extern void MotorMgr_ParseResponse(uint8_t ucMotorID, const uint8_t *pucData, uint32_t uiLen); - -// 雷赛电机CAN响应检查函数 -// 雷赛响应ID范围:0x581 - 0x5FF (0x580 + MotorID) -static int check_LeiSaiMotor(char *_pBuffer, uint32_t _iSize) -{ - if (NULL == _pBuffer || _iSize < 4) - { - return 0; - } - - // 从buffer中提取CAN ID(前4字节) - uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) - | ((_pBuffer[1] & 0xFF) << 8) - | ((_pBuffer[2] & 0xFF) << 16) - | ((_pBuffer[3] & 0xFF) << 24); - - // 雷赛响应ID范围:0x581 - 0x5FF - if (uiFrameID >= 0x581 && uiFrameID <= 0x5FF) - { - return 8 + 4; // 数据长度8 + CAN ID长度4 - } - - return 0; -} - -// 雷赛电机CAN响应解码函数 -static void decode_LeiSaiMotor(const char *_pBuffer, uint32_t _iSize) -{ - if (NULL == _pBuffer || _iSize < 12) // 4字节CAN ID + 8字节数据 - { - return; - } - - // 从buffer中提取CAN ID - uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) - | ((_pBuffer[1] & 0xFF) << 8) - | ((_pBuffer[2] & 0xFF) << 16) - | ((_pBuffer[3] & 0xFF) << 24); - - // 提取电机ID (0x580 + MotorID) - uint8_t ucMotorID = (uint8_t)(uiFrameID - 0x580); - - // 调用电机管理模块解析响应 - MotorMgr_ParseResponse(ucMotorID, (const uint8_t *)&_pBuffer[4], 8); -} void Drv_InterfaceInit(void) { + MsgCenter_Init(); + MotorInit(); + RBcore_Init(); + const osThreadAttr_t Read_RS485_1_attributes = { .name = "RS485_1", .stack_size = 512, .priority = (osPriority_t) osPriorityHigh6, }; (void)osThreadNew(Read_RS485_1, NULL, &Read_RS485_1_attributes); - - const osThreadAttr_t send_task_attributes = { - .name = "send_task", - .stack_size = 512, - .priority = (osPriority_t) osPriorityHigh7, - }; - (void)osThreadNew(SendAll_Task, NULL, &send_task_attributes); - - MsgCenter_Init(); - - // 初始化FDCAN1,使用雷赛电机的回调 - TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 8, 0); - g_ptFDCAN1 = rd_ComCreate(check_LeiSaiMotor, decode_LeiSaiMotor, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData); - CAN_IT_init(g_ptFDCAN1); - - MotorInit(); } diff --git a/RBcore/include/BHBF.h b/RBcore/include/BHBF.h index 2f295db..f568a8f 100644 --- a/RBcore/include/BHBF.h +++ b/RBcore/include/BHBF.h @@ -79,10 +79,12 @@ typedef enum { MOTOR_CMD_SET_HOME, // 设置零点 MOTOR_CMD_SPEED_MODE, // 速度模式 MOTOR_CMD_POSITION_MODE, // 位置模式 - MOTOR_CMD_END = 0x0100, - RBCORE_CMD_MANUAL_FORWARD, - RBCORE_CMD_MANUAL_BACKWARD, + + RBCORE_CMD_MANUAL_FORWARD, // 机器人手动前进 + RBCORE_CMD_MANUAL_BACKWARD, // 机器人手动后退 + RBCORE_CMD_MANUAL_TURNLEFT, // 机器人手动左转 + RBCORE_CMD_MANUAL_TURNRIGHT, // 机器人手动右转 } Motor_Cmd_e; // 电机命令数据结构 @@ -117,7 +119,7 @@ typedef struct _GV_struct_define /*==============================================* * project-wide global variables * *----------------------------------------------*/ - +void RBcore_Init(void); /*==============================================* diff --git a/motor/motor_example.c b/motor/motor_example.c index 01046cd..e9400b7 100644 --- a/motor/motor_example.c +++ b/motor/motor_example.c @@ -298,8 +298,58 @@ static void Motor_ModuleHandler(const Msg_t *pstMsg) } } +// 雷赛电机CAN响应检查函数 +// 雷赛响应ID范围:0x581 - 0x5FF (0x580 + MotorID) +static int check_LeiSaiMotor(char *_pBuffer, uint32_t _iSize) +{ + if (NULL == _pBuffer || _iSize < 4) + { + return 0; + } + + // 从buffer中提取CAN ID(前4字节) + uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) + | ((_pBuffer[1] & 0xFF) << 8) + | ((_pBuffer[2] & 0xFF) << 16) + | ((_pBuffer[3] & 0xFF) << 24); + + // 雷赛响应ID范围:0x581 - 0x5FF + if (uiFrameID >= 0x581 && uiFrameID <= 0x5FF) + { + return 8 + 4; // 数据长度8 + CAN ID长度4 + } + + return 0; +} + +// 雷赛电机CAN响应解码函数 +static void decode_LeiSaiMotor(const char *_pBuffer, uint32_t _iSize) +{ + if (NULL == _pBuffer || _iSize < 12) // 4字节CAN ID + 8字节数据 + { + return; + } + + // 从buffer中提取CAN ID + uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) + | ((_pBuffer[1] & 0xFF) << 8) + | ((_pBuffer[2] & 0xFF) << 16) + | ((_pBuffer[3] & 0xFF) << 24); + + // 提取电机ID (0x580 + MotorID) + uint8_t ucMotorID = (uint8_t)(uiFrameID - 0x580); + + // 调用电机管理模块解析响应 + MotorMgr_ParseResponse(ucMotorID, (const uint8_t *)&_pBuffer[4], 8); +} + void MotorInit(void) { + // 初始化FDCAN1,使用雷赛电机的回调 + TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 8, 0); + g_ptFDCAN1 = rd_ComCreate(check_LeiSaiMotor, decode_LeiSaiMotor, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData); + CAN_IT_init(g_ptFDCAN1); + // 初始化电机管理模块 MotorMgr_Init();