/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : motor_protocol.h 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年7月13日 最近修改 : 功能描述 : 电机协议抽象层接口定义 修改历史 : 1.日 期 : 2026年7月13日 作 者 : radkil 修改内容 : 创建文件 ******************************************************************************/ #ifndef __MOTOR_PROTOCOL_H__ #define __MOTOR_PROTOCOL_H__ #ifdef __cplusplus #if __cplusplus extern "C"{ #endif #endif /* __cplusplus */ /*==============================================* * include header files * *----------------------------------------------*/ #include "common.h" #include "slist.h" #include "com.h" /*==============================================* * constants or macros define * *----------------------------------------------*/ #define MOTOR_MAX_NAME_LEN 16 /*==============================================* * data structures * *----------------------------------------------*/ // 电机状态枚举 typedef enum { MOTOR_STATE_IDLE = 0, // 空闲 MOTOR_STATE_INIT, // 初始化中 MOTOR_STATE_READY, // 就绪 MOTOR_STATE_RUNNING, // 运行中 MOTOR_STATE_ERROR, // 错误 } Motor_State_e; // 电机运行数据 typedef struct { int32_t m_iRealPosition; // 实际位置(脉冲) int32_t m_iRealVelocity; // 实际速度(脉冲/秒) int32_t m_iRealTorque; // 实际转矩 uint32_t m_uiFaultCode; // 故障码 double m_dRealDistance; // 实际距离(米) int32_t m_iStartMeasurePos; // 测量起始位置 int32_t m_iLastPosition; // 上次位置 int32_t m_iRoundCount; // 圈数计数 uint8_t m_ucStartMeasureFlag; // 开始测量标志 } Motor_Data_t; // 电机物理配置 typedef struct { uint8_t m_ucMotorID; // 电机节点ID uint32_t m_uiPulsePerRound; // 每圈脉冲数 uint32_t m_uiReductionRatio; // 减速比 float m_fWheelDiameter; // 轮直径(米) } Motor_Config_t; // 前向声明 struct Motor_Instance; // 协议操作函数表(虚函数表) typedef struct { // 协议名称 char m_szName[MOTOR_MAX_NAME_LEN]; // 初始化电机(协议特定的初始化序列) int (*pfInit)(struct Motor_Instance *pstMotor); // 复位所有节点 int (*pfResetAll)(struct Motor_Instance *pstMotor); // 激活所有节点 int (*pfActivateAll)(struct Motor_Instance *pstMotor); // 设置当前位置为零点 int (*pfSetHome)(struct Motor_Instance *pstMotor); // 设置看门狗时间 int (*pfSetWatchdog)(struct Motor_Instance *pstMotor, uint32_t uiTimeMs); // 读取电机位置 int (*pfRequestPosition)(struct Motor_Instance *pstMotor); // 读取电机速度 int (*pfRequestVelocity)(struct Motor_Instance *pstMotor); // 读取电机电流/转矩 int (*pfRequestTorque)(struct Motor_Instance *pstMotor); // 读取故障码 int (*pfRequestFault)(struct Motor_Instance *pstMotor); // 速度模式初始化 int (*pfSpeedModeInit)(struct Motor_Instance *pstMotor); // 设置目标速度 int (*pfSetTargetSpeed)(struct Motor_Instance *pstMotor, int32_t iTargetSpeed); // 位置模式初始化 int (*pfPositionModeInit)(struct Motor_Instance *pstMotor, int32_t iAccel, int32_t iDecel, int32_t iTargetSpeed); // 设置目标位置(相对运动) int (*pfSetTargetPosition)(struct Motor_Instance *pstMotor, int32_t iTargetPos); // 设置数字输出 int (*pfSetDO)(struct Motor_Instance *pstMotor, uint8_t ucDoIndex, uint8_t ucState); // 解析CAN响应数据 int (*pfParseResponse)(struct Motor_Instance *pstMotor, const uint8_t *pucData, uint32_t uiLen); // 速度单位转换:用户单位 -> 脉冲/秒 int32_t (*pfSpeedConvert)(struct Motor_Instance *pstMotor, int32_t iSpeedUserUnit); } Motor_Protocol_t; // 电机实例结构体 typedef struct Motor_Instance { rd_slist_t node; // slist链表节点 char m_szName[MOTOR_MAX_NAME_LEN]; // 实例名称 Motor_Config_t m_stConfig; // 配置 Motor_Data_t m_stData; // 运行数据 Motor_State_e m_eState; // 状态 TComCtrl *m_ptCAN; // 绑定的CAN控制器 uint32_t m_uiLastHeartbeat; // 上次心跳时间 const Motor_Protocol_t *m_pstProtocol; // 协议操作函数表 void *m_pstPrivData; // 协议私有数据 } Motor_Instance_t; /*==============================================* * project-wide global variables * *----------------------------------------------*/ /*==============================================* * routines' or functions' implementations * *----------------------------------------------*/ #ifdef __cplusplus #if __cplusplus } #endif #endif /* __cplusplus */ #endif /* __MOTOR_PROTOCOL_H__ */