You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.1 KiB

/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: paint_robot_new_motors.c
: 稿
: radkil
: 2026821
:
:
:
1. : 2026821
: radkil
:
******************************************************************************/
#include "BHBF.h"
#include "motor_manager.h"
#include "msp_motor_leisai.h"
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
static Motor_Instance_t *g_apstMotors[MOTOR_INSTANCE_MAX] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static uint32_t g_uiMotorModuleID = 0;
static int g_aiMotorSpeed[MOTOR_INSTANCE_MAX] = {0};
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
static void Motor_ModuleHandler(const Msg_t *pstMsg)
{
if (NULL == pstMsg)
{
return;
}
switch (pstMsg->m_uiMsgID)
{
case MOTOR_CMD_SET_SPEED:
{
int iSpeed = 0;
if (pstMsg->m_uiDataLen >= sizeof(int))
{
RD_MEMCPY(&iSpeed, pstMsg->m_aucData, sizeof(int));
}
//log_i("Set Speed %d", iSpeed);
g_aiMotorSpeed[0] = iSpeed;
g_aiMotorSpeed[1] = iSpeed;
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:
{
log_i("Not Support [%d]", pstMsg->m_uiMsgID);
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;
}
}
// 雷赛电机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 MotorTask(void *argument)
{
// 初始化FDCAN1,使用雷赛电机的回调
TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 32, 0);
g_ptCAN1 = rd_ComCreate(check_LeiSaiMotor, decode_LeiSaiMotor, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData);
CAN_IT_init(g_ptCAN1);
// 初始化电机管理模块
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_ptCAN1, 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_ptCAN1, LeiSai_GetProtocol(), NULL);
g_uiMotorModuleID = MsgCenter_Register(MODULE_NAME_MOTOR, Motor_ModuleHandler);
MotorMgr_ResetAll(g_apstMotors[0]);
Rd_Delay(500);
MotorMgr_ResetAll(g_apstMotors[1]);
Rd_Delay(500);
int iCount = 6;
while(iCount--)
{
MotorMgr_ActivateAll(g_apstMotors[0]);
Rd_Delay(500);
MotorMgr_ActivateAll(g_apstMotors[1]);
Rd_Delay(500);
}
MotorMgr_SetHome(g_apstMotors[0]);
MotorMgr_SetHome(g_apstMotors[1]);
MotorMgr_SpeedModeInit(g_apstMotors[0]);
MotorMgr_SpeedModeInit(g_apstMotors[1]);
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]);
MotorMgr_SetTargetSpeed(g_apstMotors[i], g_aiMotorSpeed[i]);
}
}
Rd_Delay(2); // 2ms周期
}
}