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.

182 lines
5.5 KiB

/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: motor_example.c
: 稿
: radkil
: 2026713
:
: 使
:
1. : 2026713
: radkil
:
******************************************************************************/
#include "BHBF_robot.h"
#include "motor_manager.h"
#include "msp_motor_leisai.h"
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*****************************************************************************
: MotorExample_LeiSai
: 使
:
:
: void
*****************************************************************************/
void MotorExample_LeiSai(void)
{
// 1. 初始化电机管理模块
MotorMgr_Init();
// 2. 配置左轮电机
Motor_Config_t stLeftConfig = {
.m_ucMotorID = 1,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
// 3. 创建电机实例(使用雷赛协议)
Motor_Instance_t *pstLeftMotor = MotorMgr_Create(&stLeftConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 4. 配置右轮电机
Motor_Config_t stRightConfig = {
.m_ucMotorID = 2,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
Motor_Instance_t *pstRightMotor = MotorMgr_Create(&stRightConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 5. 复位并激活所有电机
if (pstLeftMotor != NULL)
{
MotorMgr_ResetAll(pstLeftMotor);
Rd_Delay(1000);
MotorMgr_ActivateAll(pstLeftMotor);
Rd_Delay(1000);
}
// 6. 设置当前位置为零点
if (pstLeftMotor != NULL)
{
MotorMgr_SetHome(pstLeftMotor);
Rd_Delay(100);
}
if (pstRightMotor != NULL)
{
MotorMgr_SetHome(pstRightMotor);
Rd_Delay(100);
}
// 7. 初始化速度模式
if (pstLeftMotor != NULL)
{
MotorMgr_SpeedModeInit(pstLeftMotor);
}
if (pstRightMotor != NULL)
{
MotorMgr_SpeedModeInit(pstRightMotor);
}
// 8. 设置目标速度
if (pstLeftMotor != NULL)
{
MotorMgr_SetTargetSpeed(pstLeftMotor, 100000);
}
if (pstRightMotor != NULL)
{
MotorMgr_SetTargetSpeed(pstRightMotor, -100000);
}
// 9. 定期读取电机状态
while(1)
{
if (pstLeftMotor != NULL)
{
MotorMgr_RequestPosition(pstLeftMotor);
MotorMgr_RequestVelocity(pstLeftMotor);
MotorMgr_RequestFault(pstLeftMotor);
const Motor_Data_t *pstData = MotorMgr_GetData(pstLeftMotor);
if (pstData != NULL)
{
RD_PRINTF("Left Motor: Pos=%ld, Vel=%ld, Dist=%.3f m\n",
pstData->m_iRealPosition,
pstData->m_iRealVelocity,
pstData->m_dRealDistance);
}
}
Rd_Delay(100);
}
}
/*****************************************************************************
: MotorExample_MultiProtocol
: 使
:
:
: void
1. Motor_Protocol_t
2. MotorMgr_Create
*****************************************************************************/
void MotorExample_MultiProtocol(void)
{
MotorMgr_Init();
// 使用雷赛协议的电机
Motor_Config_t stConfig1 = { .m_ucMotorID = 1 };
Motor_Instance_t *pstMotor1 = MotorMgr_Create(&stConfig1, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 假设未来有其他协议
// Motor_Config_t stConfig2 = { .m_ucMotorID = 2 };
// Motor_Instance_t *pstMotor2 = MotorMgr_Create(&stConfig2, g_ptFDCAN2, OtherProtocol_GetProtocol(), NULL);
// 统一的管理接口
MotorMgr_SpeedModeInit(pstMotor1);
MotorMgr_SetTargetSpeed(pstMotor1, 50000);
// 通过名称查找
Motor_Instance_t *pstFound = MotorMgr_FindByName("Motor_1");
if (pstFound != NULL)
{
RD_PRINTF("Found motor: %s\n", pstFound->m_szName);
}
}