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.

144 lines
5.5 KiB

/******************************************************************************
版权所有 (C), 2018-2099, Radkil
******************************************************************************
文 件 名 : msp_motor_rs485.h
版 本 号 : 初稿
作 者 : radkil
生成日期 : 2026年7月13日
最近修改 :
功能描述 : RS485电机Modbus RTU协议实现(CiA402映射表)
修改历史 :
1.日 期 : 2026年7月13日
作 者 : radkil
修改内容 : 创建文件
******************************************************************************/
#ifndef __MOTOR_RS485_H__
#define __MOTOR_RS485_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/*==============================================*
* include header files *
*----------------------------------------------*/
#include "motor_manager.h"
#include "com.h"
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
// CiA402 对象字典 -> Modbus 保持寄存器地址(换品牌驱动只改这张表)
#define RS485_OD_CONTROL_WORD 0x6040 // 控制字
#define RS485_OD_STATUS_WORD 0x6041 // 状态字
#define RS485_OD_MODE_OF_OPERATION 0x6060 // 操作模式
#define RS485_OD_POSITION_ACTUAL 0x6064 // 实际位置(2寄存器)
#define RS485_OD_VELOCITY_ACTUAL 0x606C // 实际速度(2寄存器)
#define RS485_OD_TORQUE_ACTUAL 0x6077 // 实际转矩(1寄存器)
#define RS485_OD_TARGET_VELOCITY 0x60FF // 目标速度(2寄存器)
#define RS485_OD_TARGET_POSITION 0x607A // 目标位置(2寄存器)
#define RS485_OD_ACCELERATION 0x6083 // 加速度(2寄存器)
#define RS485_OD_DECELERATION 0x6084 // 减速度(2寄存器)
#define RS485_OD_FAULT_CODE 0x603F // 故障码(2寄存器)
#define RS485_OD_HOMING_METHOD 0x6098 // 回零方式(1寄存器)
// 品牌相关寄存器(CiA402无标准定义,接具体驱动前按手册修改)
#define RS485_OD_WATCHDOG_TIME 0x5004 // 通讯看门狗时间(1寄存器)
#define RS485_OD_DO1 0x2410 // 数字输出1
#define RS485_OD_DO2 0x2411 // 数字输出2
// 控制字命令(CiA402状态机)
#define RS485_CTRL_SHUTDOWN 0x06
#define RS485_CTRL_SWITCH_ON 0x07
#define RS485_CTRL_ENABLE 0x0F
#define RS485_CTRL_START_MOVE 0x1F
#define RS485_CTRL_RELATIVE_MOVE 0x4F
#define RS485_CTRL_FAULT_RESET 0x86
// 操作模式
#define RS485_MODE_PROFILE_POS 0x01
#define RS485_MODE_PROFILE_VEL 0x03
#define RS485_MODE_HOMING 0x06
#define RS485_HOMING_METHOD 0x23 // 回零方式(品牌相关,默认单/双限位回零)
// 速度单位换算常数
#define RS485_SPEED_UNIT_PER_RPM 10 // 驱动器速度单位:1RPM = 10个单位(0.1RPM制)
#define RS485_WORD_ORDER_HI_FIRST 1 // 32位量寄存器字序:1=高字在前,0=低字在前
// Modbus RTU 帧长度
#define RS485_FRAME_SIZE_WRITE 8 // FC06: addr+fc+regH+regL+valH+valL+crcL+crcH
#define RS485_FRAME_SIZE_READ_REQ 8 // FC03 请求
#define RS485_FRAME_SIZE_EXC 5 // 异常帧: addr+fc|0x80+exc+crcL+crcH
#define RS485_MODBUS_BROADCAST 0x00
// 本模块MsgCenter上报消息ID(BHBF_Cmd_e 枚举仅占0~29,这里沿用GET_MOTOR_xxx惯例扩展)
#define GET_MOTOR_REAL_POSITION 0x30 // 位置回码,payload int32[2]={motorID, 位置原始值}
#define GET_MOTOR_REAL_TORQUE 0x31 // 转矩回码,payload int32[2]={motorID, 转矩原始值}
/*==============================================*
* data structures *
*----------------------------------------------*/
// RS485协议私有数据(每实例独立,生命周期由调用方管理)
typedef struct {
uint8_t m_ucSlaveAddr; // Modbus从站地址(==Motor_Config_t.m_ucMotorID)
volatile uint16_t m_usPendingReg; // 未决FC03读请求的寄存器地址(0=无未决请求)
uint8_t m_ucPendingRegs; // 未决请求的寄存器数量(1或2)
uint32_t m_uiReqTick; // 请求发出时刻(预留超时扫描用)
} MotorRs485_Priv_t;
/*==============================================*
* routines' or functions' implementations *
*----------------------------------------------*/
/**
* @brief 初始化RS485协议模块(创建模块级互斥锁,须在创建实例前调用)
*/
void MotorRs485_ModuleInit(void);
/**
* @brief 获取RS485协议操作函数表
* @return 协议操作函数表指针
*/
const Motor_Protocol_t* MotorRs485_GetProtocol(void);
/**
* @brief 分配并初始化协议私有数据
* @param ucSlaveAddr Modbus从站地址
* @return 私有数据指针,失败返回NULL(用完调用MotorRs485_FreePriv释放)
*/
MotorRs485_Priv_t* MotorRs485_CreatePriv(uint8_t ucSlaveAddr);
/**
* @brief 释放私有数据
*/
void MotorRs485_FreePriv(MotorRs485_Priv_t *pstPriv);
/**
* @brief com层检查回调:识别Modbus RTU应答帧(注册到rd_ComCreate)
* @return >0帧长度;0=等待更多数据;负数=本协议不认领该字节
*/
int MotorRs485_ComCheck(char *_pBuffer, uint32_t _iSize);
/**
* @brief com层解码回调:CRC复验后按从站地址路由到MotorMgr_ParseResponse
*/
void MotorRs485_ComDecode(const char *_pBuffer, uint32_t _iSize);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __MOTOR_RS485_H__ */