Browse Source

电机的CAN线初始化挪到模块内部

master
Lizongdi 1 week ago
parent
commit
46e201ebf5
  1. 9
      RBcore/BHBF.c
  2. 66
      RBcore/drv_interface.c
  3. 10
      RBcore/include/BHBF.h
  4. 50
      motor/motor_example.c

9
RBcore/BHBF.c

@ -57,22 +57,22 @@ static void RBcore_ModuleHandler(const Msg_t *pstMsg)
switch (pstMsg->m_uiMsgID) switch (pstMsg->m_uiMsgID)
{ {
case MOTOR_CMD_SET_SPEED: case RBCORE_CMD_MANUAL_FORWARD:
{ {
break; break;
} }
case MOTOR_CMD_STOP: case RBCORE_CMD_MANUAL_BACKWARD:
{ {
break; break;
} }
case MOTOR_CMD_SET_HOME: case RBCORE_CMD_MANUAL_TURNLEFT:
{ {
break; break;
} }
case MOTOR_CMD_GET_STATUS: case RBCORE_CMD_MANUAL_TURNRIGHT:
{ {
break; break;
@ -82,7 +82,6 @@ static void RBcore_ModuleHandler(const Msg_t *pstMsg)
} }
} }
void RBcore_Task(void *argument) void RBcore_Task(void *argument)
{ {
while(1) while(1)

66
RBcore/drv_interface.c

@ -226,75 +226,17 @@ void Read_RS485_1(void *argument)
} }
extern void MotorInit(void); 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) void Drv_InterfaceInit(void)
{ {
MsgCenter_Init();
MotorInit();
RBcore_Init();
const osThreadAttr_t Read_RS485_1_attributes = { const osThreadAttr_t Read_RS485_1_attributes = {
.name = "RS485_1", .name = "RS485_1",
.stack_size = 512, .stack_size = 512,
.priority = (osPriority_t) osPriorityHigh6, .priority = (osPriority_t) osPriorityHigh6,
}; };
(void)osThreadNew(Read_RS485_1, NULL, &Read_RS485_1_attributes); (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();
} }

10
RBcore/include/BHBF.h

@ -79,10 +79,12 @@ typedef enum {
MOTOR_CMD_SET_HOME, // 设置零点 MOTOR_CMD_SET_HOME, // 设置零点
MOTOR_CMD_SPEED_MODE, // 速度模式 MOTOR_CMD_SPEED_MODE, // 速度模式
MOTOR_CMD_POSITION_MODE, // 位置模式 MOTOR_CMD_POSITION_MODE, // 位置模式
MOTOR_CMD_END = 0x0100, 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; } Motor_Cmd_e;
// 电机命令数据结构 // 电机命令数据结构
@ -117,7 +119,7 @@ typedef struct _GV_struct_define
/*==============================================* /*==============================================*
* project-wide global variables * * project-wide global variables *
*----------------------------------------------*/ *----------------------------------------------*/
void RBcore_Init(void);
/*==============================================* /*==============================================*

50
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) 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(); MotorMgr_Init();

Loading…
Cancel
Save