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.
 
 
 
 

835 lines
29 KiB

/******************************************************************************
版权所有 (C), 2018-2099, Radkil
******************************************************************************
文 件 名 : msp_motor_rs485.c
版 本 号 : 初稿
作 者 : radkil
生成日期 : 2026年7月13日
最近修改 :
功能描述 : RS485电机Modbus RTU协议实现(CiA402映射表)
修改历史 :
1.日 期 : 2026年7月13日
作 者 : radkil
修改内容 : 创建文件
******************************************************************************/
#define LOG_TAG "motor485"
#include "msp_motor_rs485.h"
#include "BHBF.h"
#include "msg_center.h"
/*----------------------------------------------*
* 外部变量说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 外部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 内部函数原型说明 *
*----------------------------------------------*/
static void Rs485_EnsureLock(void);
static uint16_t Rs485_CalculateCRC(const uint8_t *pucBuf, uint32_t uiLen);
static int Rs485_BuildWriteFrame(uint8_t ucAddr, uint16_t usReg, uint16_t usVal, uint8_t *pucBuf);
static int Rs485_BuildReadFrame(uint8_t ucAddr, uint16_t usReg, uint16_t usCnt, uint8_t *pucBuf);
static int Rs485_WriteReg(Motor_Instance_t *pstMotor, uint16_t usReg, uint16_t usVal);
static int Rs485_WriteReg32(Motor_Instance_t *pstMotor, uint16_t usReg, int32_t iValue);
static int Rs485_RequestRead(Motor_Instance_t *pstMotor, uint16_t usReg, uint8_t ucRegs);
static int32_t Rs485_AssembleValue(const uint8_t *pucData, uint8_t ucRegs);
static int Rs485_Init(Motor_Instance_t *pstMotor);
static int Rs485_ResetAll(Motor_Instance_t *pstMotor);
static int Rs485_ActivateAll(Motor_Instance_t *pstMotor);
static int Rs485_SetHome(Motor_Instance_t *pstMotor);
static int Rs485_SetWatchdog(Motor_Instance_t *pstMotor, uint32_t uiTimeMs);
static int Rs485_RequestPosition(Motor_Instance_t *pstMotor);
static int Rs485_RequestVelocity(Motor_Instance_t *pstMotor);
static int Rs485_RequestTorque(Motor_Instance_t *pstMotor);
static int Rs485_RequestFault(Motor_Instance_t *pstMotor);
static int Rs485_SpeedModeInit(Motor_Instance_t *pstMotor);
static int Rs485_SetTargetSpeed(Motor_Instance_t *pstMotor, int32_t iTargetSpeed);
static int Rs485_PositionModeInit(Motor_Instance_t *pstMotor, int32_t iAccel, int32_t iDecel, int32_t iTargetSpeed);
static int Rs485_SetTargetPosition(Motor_Instance_t *pstMotor, int32_t iTargetPos);
static int Rs485_SetDO(Motor_Instance_t *pstMotor, uint8_t ucDoIndex, uint8_t ucState);
static int Rs485_ParseResponse(Motor_Instance_t *pstMotor, const uint8_t *pucData, uint32_t uiLen);
static int32_t Rs485_SpeedConvert(Motor_Instance_t *pstMotor, int32_t iSpeedE01Mmin);
static int32_t Rs485_SpeedConvertex(Motor_Instance_t *pstMotor, int32_t iSpeedUnit);
/*----------------------------------------------*
* 全局变量 *
*----------------------------------------------*/
// RS485协议操作函数表
const Motor_Protocol_t g_stRs485Protocol = {
.m_szName = "ModbusRTU",
.pfInit = Rs485_Init,
.pfResetAll = Rs485_ResetAll,
.pfActivateAll = Rs485_ActivateAll,
.pfSetHome = Rs485_SetHome,
.pfSetWatchdog = Rs485_SetWatchdog,
.pfRequestPosition = Rs485_RequestPosition,
.pfRequestVelocity = Rs485_RequestVelocity,
.pfRequestTorque = Rs485_RequestTorque,
.pfRequestFault = Rs485_RequestFault,
.pfSpeedModeInit = Rs485_SpeedModeInit,
.pfSetTargetSpeed = Rs485_SetTargetSpeed,
.pfPositionModeInit = Rs485_PositionModeInit,
.pfSetTargetPosition = Rs485_SetTargetPosition,
.pfSetDO = Rs485_SetDO,
.pfParseResponse = Rs485_ParseResponse,
.pfSpeedConvert = Rs485_SpeedConvert,
.pfSpeedConvertex = Rs485_SpeedConvertex,
};
/*----------------------------------------------*
* 模块级变量 *
*----------------------------------------------*/
static rd_mutex_t g_stRs485Lock = RD_MUTEX_INITIALIZER; // Modbus事务互斥锁(发请求+打pending标签原子)
static int g_iLockReady = 0;
/*----------------------------------------------*
* 常量定义 *
*----------------------------------------------*/
/*----------------------------------------------*
* 宏定义 *
*----------------------------------------------*/
/*****************************************************************************
函 数 名 : MotorRs485_ModuleInit
功能描述 : 初始化RS485协议模块(显式创建互斥锁,须在创建实例前调用)
*****************************************************************************/
void MotorRs485_ModuleInit(void)
{
Rs485_EnsureLock();
}
/*****************************************************************************
函 数 名 : Rs485_EnsureLock
功能描述 : 惰性初始化模块互斥锁(未初始化时Rd_MutexLock为空操作,仅降级不阻塞)
*****************************************************************************/
static void Rs485_EnsureLock(void)
{
if (!g_iLockReady)
{
if (RD_SUCCESS == Rd_MutexInit(&g_stRs485Lock))
{
g_iLockReady = 1;
}
}
}
/*****************************************************************************
函 数 名 : MotorRs485_GetProtocol
功能描述 : 获取RS485协议操作函数表
返 回 值 : const Motor_Protocol_t*
*****************************************************************************/
const Motor_Protocol_t* MotorRs485_GetProtocol(void)
{
return &g_stRs485Protocol;
}
/*****************************************************************************
函 数 名 : MotorRs485_CreatePriv
功能描述 : 分配并初始化协议私有数据
返 回 值 : MotorRs485_Priv_t* 失败返回NULL
*****************************************************************************/
MotorRs485_Priv_t* MotorRs485_CreatePriv(uint8_t ucSlaveAddr)
{
if (RS485_MODBUS_BROADCAST == ucSlaveAddr)
{
return NULL;
}
MotorRs485_Priv_t *pstPriv = (MotorRs485_Priv_t *)RD_CALLOC(1, sizeof(MotorRs485_Priv_t));
if (NULL == pstPriv)
{
return NULL;
}
pstPriv->m_ucSlaveAddr = ucSlaveAddr;
pstPriv->m_usPendingReg = 0;
pstPriv->m_ucPendingRegs = 0;
pstPriv->m_uiReqTick = 0;
return pstPriv;
}
/*****************************************************************************
函 数 名 : MotorRs485_FreePriv
功能描述 : 释放私有数据
*****************************************************************************/
void MotorRs485_FreePriv(MotorRs485_Priv_t *pstPriv)
{
RD_FREE(pstPriv);
}
/*****************************************************************************
函 数 名 : Rs485_CalculateCRC
功能描述 : Modbus RTU CRC16(poly 0xA001,初值0xFFFF,低字节在前发送)
*****************************************************************************/
static uint16_t Rs485_CalculateCRC(const uint8_t *pucBuf, uint32_t uiLen)
{
uint16_t usCrc = 0xFFFF;
for (uint32_t i = 0; i < uiLen; i++)
{
usCrc ^= pucBuf[i];
for (int j = 0; j < 8; j++)
{
if (usCrc & 0x0001)
{
usCrc = (uint16_t)((usCrc >> 1) ^ 0xA001);
}
else
{
usCrc >>= 1;
}
}
}
return usCrc;
}
/*****************************************************************************
函 数 名 : Rs485_BuildWriteFrame
功能描述 : 构造FC06写单寄存器帧 [addr][06][regH][regL][valH][valL][crcL][crcH]
返 回 值 : 帧长度(8)
*****************************************************************************/
static int Rs485_BuildWriteFrame(uint8_t ucAddr, uint16_t usReg, uint16_t usVal, uint8_t *pucBuf)
{
pucBuf[0] = ucAddr;
pucBuf[1] = 0x06;
pucBuf[2] = (uint8_t)(usReg >> 8);
pucBuf[3] = (uint8_t)(usReg & 0xFF);
pucBuf[4] = (uint8_t)(usVal >> 8);
pucBuf[5] = (uint8_t)(usVal & 0xFF);
uint16_t usCrc = Rs485_CalculateCRC(pucBuf, 6);
pucBuf[6] = (uint8_t)(usCrc & 0xFF);
pucBuf[7] = (uint8_t)((usCrc >> 8) & 0xFF);
return RS485_FRAME_SIZE_WRITE;
}
/*****************************************************************************
函 数 名 : Rs485_BuildReadFrame
功能描述 : 构造FC03读保持寄存器请求帧 [addr][03][regH][regL][cntH][cntL][crcL][crcH]
返 回 值 : 帧长度(8)
*****************************************************************************/
static int Rs485_BuildReadFrame(uint8_t ucAddr, uint16_t usReg, uint16_t usCnt, uint8_t *pucBuf)
{
pucBuf[0] = ucAddr;
pucBuf[1] = 0x03;
pucBuf[2] = (uint8_t)(usReg >> 8);
pucBuf[3] = (uint8_t)(usReg & 0xFF);
pucBuf[4] = (uint8_t)(usCnt >> 8);
pucBuf[5] = (uint8_t)(usCnt & 0xFF);
uint16_t usCrc = Rs485_CalculateCRC(pucBuf, 6);
pucBuf[6] = (uint8_t)(usCrc & 0xFF);
pucBuf[7] = (uint8_t)((usCrc >> 8) & 0xFF);
return RS485_FRAME_SIZE_READ_REQ;
}
/*****************************************************************************
函 数 名 : Rs485_WriteReg
功能描述 : 写单个保持寄存器(FC06,持锁发送)
返 回 值 : RD_SUCCESS成功,非0失败
*****************************************************************************/
static int Rs485_WriteReg(Motor_Instance_t *pstMotor, uint16_t usReg, uint16_t usVal)
{
if (NULL == pstMotor || NULL == pstMotor->m_ptCAN)
{
return RD_NULL;
}
MotorRs485_Priv_t *pstPriv = (MotorRs485_Priv_t *)pstMotor->m_pstPrivData;
if (NULL == pstPriv)
{
return RD_NULL;
}
uint8_t aucFrame[RS485_FRAME_SIZE_WRITE];
Rs485_BuildWriteFrame(pstPriv->m_ucSlaveAddr, usReg, usVal, aucFrame);
Rd_MutexLock(&g_stRs485Lock, __func__, NULL);
int iRet = rd_ComSend(pstMotor->m_ptCAN, (char *)aucFrame, RS485_FRAME_SIZE_WRITE);
Rd_MutexUnlock(&g_stRs485Lock, __func__, NULL);
return iRet;
}
/*****************************************************************************
函 数 名 : Rs485_WriteReg32
功能描述 : 32位量拆两次FC06写入(字序由RS485_WORD_ORDER_HI_FIRST控制)
返 回 值 : RD_SUCCESS成功,非0失败
*****************************************************************************/
static int Rs485_WriteReg32(Motor_Instance_t *pstMotor, uint16_t usReg, int32_t iValue)
{
uint16_t usHi = (uint16_t)((uint32_t)iValue >> 16);
uint16_t usLo = (uint16_t)((uint32_t)iValue & 0xFFFF);
#if RS485_WORD_ORDER_HI_FIRST
int iRet = Rs485_WriteReg(pstMotor, usReg, usHi);
Rd_Delay(5);
if (RD_SUCCESS != iRet) return iRet;
return Rs485_WriteReg(pstMotor, (uint16_t)(usReg + 1), usLo);
#else
int iRet = Rs485_WriteReg(pstMotor, usReg, usLo);
Rd_Delay(5);
if (RD_SUCCESS != iRet) return iRet;
return Rs485_WriteReg(pstMotor, (uint16_t)(usReg + 1), usHi);
#endif
}
/*****************************************************************************
函 数 名 : Rs485_RequestRead
功能描述 : 发起FC03读请求(持锁内打pending标签+发帧原子,防迟到响应错配)
返 回 值 : RD_SUCCESS成功,非0失败
*****************************************************************************/
static int Rs485_RequestRead(Motor_Instance_t *pstMotor, uint16_t usReg, uint8_t ucRegs)
{
if (NULL == pstMotor || NULL == pstMotor->m_ptCAN)
{
return RD_NULL;
}
MotorRs485_Priv_t *pstPriv = (MotorRs485_Priv_t *)pstMotor->m_pstPrivData;
if (NULL == pstPriv)
{
return RD_NULL;
}
uint8_t aucFrame[RS485_FRAME_SIZE_READ_REQ];
Rs485_BuildReadFrame(pstPriv->m_ucSlaveAddr, usReg, ucRegs, aucFrame);
Rd_MutexLock(&g_stRs485Lock, __func__, NULL);
pstPriv->m_usPendingReg = usReg;
pstPriv->m_ucPendingRegs = ucRegs;
pstPriv->m_uiReqTick = Rd_GetTime();
int iRet = rd_ComSend(pstMotor->m_ptCAN, (char *)aucFrame, RS485_FRAME_SIZE_READ_REQ);
if (RD_SUCCESS != iRet)
{
pstPriv->m_usPendingReg = 0; // 发送失败清标签
}
Rd_MutexUnlock(&g_stRs485Lock, __func__, NULL);
// 读请求间最小间隔,配合上层调用形成半双工pacing(往返约12ms@115200)
Rd_Delay(10);
return iRet;
}
/*****************************************************************************
函 数 名 : Rs485_AssembleValue
功能描述 : 从FC03应答数据区组装寄存器值(pucData指向首数据字节)
*****************************************************************************/
static int32_t Rs485_AssembleValue(const uint8_t *pucData, uint8_t ucRegs)
{
uint16_t usWord0 = (uint16_t)((pucData[0] << 8) | pucData[1]);
if (1 == ucRegs)
{
return (int32_t)(int16_t)usWord0;
}
uint16_t usWord1 = (uint16_t)((pucData[2] << 8) | pucData[3]);
#if RS485_WORD_ORDER_HI_FIRST
return (int32_t)(((uint32_t)usWord0 << 16) | usWord1);
#else
return (int32_t)(((uint32_t)usWord1 << 16) | usWord0);
#endif
}
/*****************************************************************************
函 数 名 : Rs485_Init
功能描述 : 初始化电机(校验私有数据+惰性初始化互斥锁,无总线流量)
*****************************************************************************/
static int Rs485_Init(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor || NULL == pstMotor->m_pstPrivData)
{
return RD_NULL;
}
Rs485_EnsureLock();
pstMotor->m_eState = MOTOR_STATE_IDLE;
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_ResetAll
功能描述 : 复位节点(语义变化:Modbus无NMT广播,为单从站控制字故障复位)
*****************************************************************************/
static int Rs485_ResetAll(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_FAULT_RESET);
Rd_Delay(50);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_ActivateAll
功能描述 : 激活节点(语义变化:Modbus无NMT广播,为单从站使能序列)
*****************************************************************************/
static int Rs485_ActivateAll(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SHUTDOWN);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SWITCH_ON);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_ENABLE);
Rd_Delay(50);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SetHome
功能描述 : 设置当前位置为零点(回零模式+回零方式+使能+启动)
*****************************************************************************/
static int Rs485_SetHome(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg(pstMotor, RS485_OD_MODE_OF_OPERATION, RS485_MODE_HOMING);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_HOMING_METHOD, RS485_HOMING_METHOD);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SHUTDOWN);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SWITCH_ON);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_ENABLE);
Rd_Delay(50);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_START_MOVE);
Rd_Delay(50);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SetWatchdog
功能描述 : 设置看门狗时间(寄存器地址品牌相关,见RS485_OD_WATCHDOG_TIME)
*****************************************************************************/
static int Rs485_SetWatchdog(Motor_Instance_t *pstMotor, uint32_t uiTimeMs)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_WriteReg(pstMotor, RS485_OD_WATCHDOG_TIME, (uint16_t)uiTimeMs);
}
/*****************************************************************************
函 数 名 : Rs485_RequestPosition
功能描述 : 读取电机位置(FC03读0x6064,2寄存器)
*****************************************************************************/
static int Rs485_RequestPosition(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_RequestRead(pstMotor, RS485_OD_POSITION_ACTUAL, 2);
}
/*****************************************************************************
函 数 名 : Rs485_RequestVelocity
功能描述 : 读取电机速度(FC03读0x606C,2寄存器)
*****************************************************************************/
static int Rs485_RequestVelocity(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_RequestRead(pstMotor, RS485_OD_VELOCITY_ACTUAL, 2);
}
/*****************************************************************************
函 数 名 : Rs485_RequestTorque
功能描述 : 读取电机转矩(FC03读0x6077,1寄存器)
*****************************************************************************/
static int Rs485_RequestTorque(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_RequestRead(pstMotor, RS485_OD_TORQUE_ACTUAL, 1);
}
/*****************************************************************************
函 数 名 : Rs485_RequestFault
功能描述 : 读取故障码(FC03读0x603F,1寄存器)
*****************************************************************************/
static int Rs485_RequestFault(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_RequestRead(pstMotor, RS485_OD_FAULT_CODE, 1);
}
/*****************************************************************************
函 数 名 : Rs485_SpeedModeInit
功能描述 : 速度模式初始化(CiA402规范:模式切换在Shutdown态进行,故先写模式)
*****************************************************************************/
static int Rs485_SpeedModeInit(Motor_Instance_t *pstMotor)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SHUTDOWN);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_MODE_OF_OPERATION, RS485_MODE_PROFILE_VEL);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SWITCH_ON);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_ENABLE);
Rd_Delay(10);
Rs485_WriteReg32(pstMotor, RS485_OD_TARGET_VELOCITY, 0);
Rd_Delay(10);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SetTargetSpeed
功能描述 : 设置目标速度(驱动器单位,经pfSpeedConvert换算)
*****************************************************************************/
static int Rs485_SetTargetSpeed(Motor_Instance_t *pstMotor, int32_t iTargetSpeed)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
return Rs485_WriteReg32(pstMotor, RS485_OD_TARGET_VELOCITY, iTargetSpeed);
}
/*****************************************************************************
函 数 名 : Rs485_PositionModeInit
功能描述 : 位置模式初始化(模式切换在Shutdown态进行)
*****************************************************************************/
static int Rs485_PositionModeInit(Motor_Instance_t *pstMotor, int32_t iAccel, int32_t iDecel, int32_t iTargetSpeed)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SHUTDOWN);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_MODE_OF_OPERATION, RS485_MODE_PROFILE_POS);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_SWITCH_ON);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_ENABLE);
Rd_Delay(10);
Rs485_WriteReg32(pstMotor, RS485_OD_ACCELERATION, iAccel);
Rd_Delay(5);
Rs485_WriteReg32(pstMotor, RS485_OD_DECELERATION, iDecel);
Rd_Delay(5);
Rs485_WriteReg32(pstMotor, RS485_OD_TARGET_VELOCITY, iTargetSpeed);
Rd_Delay(5);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SetTargetPosition
功能描述 : 设置目标位置(相对运动:写目标位置+相对位+启动)
*****************************************************************************/
static int Rs485_SetTargetPosition(Motor_Instance_t *pstMotor, int32_t iTargetPos)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
Rs485_WriteReg32(pstMotor, RS485_OD_TARGET_POSITION, iTargetPos);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_RELATIVE_MOVE);
Rd_Delay(10);
Rs485_WriteReg(pstMotor, RS485_OD_CONTROL_WORD, RS485_CTRL_START_MOVE);
Rd_Delay(10);
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SetDO
功能描述 : 设置数字输出(寄存器地址品牌相关,见RS485_OD_DO1/DO2)
*****************************************************************************/
static int Rs485_SetDO(Motor_Instance_t *pstMotor, uint8_t ucDoIndex, uint8_t ucState)
{
if (NULL == pstMotor)
{
return RD_NULL;
}
uint16_t usIndex = 0;
switch (ucDoIndex)
{
case 1:
usIndex = RS485_OD_DO1;
break;
case 2:
usIndex = RS485_OD_DO2;
break;
default:
return RD_INVALUE;
}
return Rs485_WriteReg(pstMotor, usIndex, ucState ? 1 : 0);
}
/*****************************************************************************
函 数 名 : Rs485_ParseResponse
功能描述 : 解析Modbus RTU应答帧(FC03按pending标签解析,异常帧清标签)
*****************************************************************************/
static int Rs485_ParseResponse(Motor_Instance_t *pstMotor, const uint8_t *pucData, uint32_t uiLen)
{
if (NULL == pstMotor || NULL == pucData || uiLen < RS485_FRAME_SIZE_EXC)
{
return RD_NULL;
}
MotorRs485_Priv_t *pstPriv = (MotorRs485_Priv_t *)pstMotor->m_pstPrivData;
if (NULL == pstPriv)
{
return RD_NULL;
}
uint8_t ucFc = pucData[1];
if (ucFc & 0x80)
{
pstPriv->m_usPendingReg = 0;
log_w("Rs485[%d] modbus exception fc=0x%02X exc=0x%02X",
pstPriv->m_ucSlaveAddr, ucFc, pucData[2]);
return RD_SUCCESS;
}
if (0x06 == ucFc)
{
return RD_SUCCESS; // 写应答回显,无需处理
}
if (0x03 == ucFc)
{
uint16_t usReg = pstPriv->m_usPendingReg;
uint8_t ucRegs = pstPriv->m_ucPendingRegs;
uint8_t ucBc = pucData[2];
// 无论后续是否匹配,先清标签,防止迟到响应被复用
pstPriv->m_usPendingReg = 0;
if (0 == usReg || ucBc != (uint8_t)(ucRegs * 2) || uiLen < (uint32_t)(ucBc + 5))
{
return RD_FAILURE;
}
int32_t iValue = Rs485_AssembleValue(&pucData[3], ucRegs);
switch (usReg)
{
case RS485_OD_VELOCITY_ACTUAL:
{
int32_t iRealVelocity[2] = {0};
iRealVelocity[1] = MotorMgr_SpeedConvertex(pstMotor, iValue);
iRealVelocity[0] = pstMotor->m_stConfig.m_ucMotorID;
MsgCenter_SendTo(MODULE_NAME_CUSTOM, GET_VEHICLE_REAL_SPEED, iRealVelocity, sizeof(iRealVelocity));
break;
}
case RS485_OD_POSITION_ACTUAL:
{
int32_t iRealPosition[2] = {0};
iRealPosition[0] = pstMotor->m_stConfig.m_ucMotorID;
iRealPosition[1] = iValue;
MsgCenter_SendTo(MODULE_NAME_CUSTOM, GET_MOTOR_REAL_POSITION, iRealPosition, sizeof(iRealPosition));
break;
}
case RS485_OD_TORQUE_ACTUAL:
{
int32_t iRealTorque[2] = {0};
iRealTorque[0] = pstMotor->m_stConfig.m_ucMotorID;
iRealTorque[1] = iValue;
MsgCenter_SendTo(MODULE_NAME_CUSTOM, GET_MOTOR_REAL_TORQUE, iRealTorque, sizeof(iRealTorque));
break;
}
case RS485_OD_FAULT_CODE:
{
uint32_t uiFaultCode[2] = {0};
if (iValue != 0)
{
pstMotor->m_eState = MOTOR_STATE_ERROR;
}
uiFaultCode[0] = pstMotor->m_stConfig.m_ucMotorID;
uiFaultCode[1] = (uint32_t)iValue;
MsgCenter_SendTo(MODULE_NAME_CUSTOM, GET_MOTOR_FAULT_CODE, uiFaultCode, sizeof(uiFaultCode));
break;
}
default:
break;
}
}
return RD_SUCCESS;
}
/*****************************************************************************
函 数 名 : Rs485_SpeedConvert
功能描述 : 速度单位转换:0.1m/min -> 驱动器单位(0.1RPM)
*****************************************************************************/
static int32_t Rs485_SpeedConvert(Motor_Instance_t *pstMotor, int32_t iSpeedE01Mmin)
{
if (NULL == pstMotor)
{
return 0;
}
double dSpeedMMin = iSpeedE01Mmin * 0.1;
double dWheelCircumference = 3.14 * pstMotor->m_stConfig.m_fWheelDiameter;
double dRpm = dSpeedMMin / dWheelCircumference * pstMotor->m_stConfig.m_uiReductionRatio;
return (int32_t)(dRpm * RS485_SPEED_UNIT_PER_RPM);
}
/*****************************************************************************
函 数 名 : Rs485_SpeedConvertex
功能描述 : 速度单位转换:驱动器单位(0.1RPM) -> 0.1m/min
*****************************************************************************/
static int32_t Rs485_SpeedConvertex(Motor_Instance_t *pstMotor, int32_t iSpeedUnit)
{
if (NULL == pstMotor)
{
return 0;
}
double dRpm = (double)iSpeedUnit / RS485_SPEED_UNIT_PER_RPM;
double dWheelCircumference = 3.14 * pstMotor->m_stConfig.m_fWheelDiameter;
double dSpeedMMin = dRpm * dWheelCircumference / pstMotor->m_stConfig.m_uiReductionRatio;
return (int32_t)(dSpeedMMin * 10.0);
}
/*****************************************************************************
函 数 名 : MotorRs485_ComCheck
功能描述 : com层检查回调:识别本总线上的Modbus RTU应答帧
返 回 值 : >0帧长度;0=等待更多数据;负数=本协议不认领该字节
*****************************************************************************/
int MotorRs485_ComCheck(char *_pBuffer, uint32_t _iSize)
{
if (NULL == _pBuffer || _iSize < 2)
{
return -1;
}
uint8_t ucAddr = (uint8_t)_pBuffer[0];
uint8_t ucFc = (uint8_t)_pBuffer[1];
// 只认领已注册电机(MotorID==从站地址)的应答,其余按丢1字节重同步处理
if (RS485_MODBUS_BROADCAST == ucAddr || NULL == MotorMgr_FindByID(ucAddr))
{
return -1;
}
uint32_t uiExpect;
if (ucFc & 0x80)
{
uiExpect = RS485_FRAME_SIZE_EXC;
}
else if (0x06 == ucFc)
{
uiExpect = RS485_FRAME_SIZE_WRITE;
}
else if (0x03 == ucFc)
{
if (_iSize < 3)
{
return 0;
}
uint8_t ucBc = (uint8_t)_pBuffer[2];
if (ucBc < 2 || ucBc > 8 || (ucBc & 0x01))
{
return -1; // 非法字节计数
}
uiExpect = (uint32_t)ucBc + 5;
}
else
{
return -1; // 本总线不处理的功能码
}
if (_iSize < uiExpect)
{
return 0;
}
// CRC校验(低字节在前)
uint16_t usCrcCalc = Rs485_CalculateCRC((const uint8_t *)_pBuffer, uiExpect - 2);
uint16_t usCrcRecv = (uint16_t)(((uint8_t)_pBuffer[uiExpect - 1] << 8) | (uint8_t)_pBuffer[uiExpect - 2]);
if (usCrcCalc != usCrcRecv)
{
return -1;
}
return (int)uiExpect;
}
/*****************************************************************************
函 数 名 : MotorRs485_ComDecode
功能描述 : com层解码回调:CRC复验后按从站地址路由到MotorMgr_ParseResponse
*****************************************************************************/
void MotorRs485_ComDecode(const char *_pBuffer, uint32_t _iSize)
{
if (NULL == _pBuffer || _iSize < RS485_FRAME_SIZE_EXC)
{
return;
}
uint16_t usCrcCalc = Rs485_CalculateCRC((const uint8_t *)_pBuffer, _iSize - 2);
uint16_t usCrcRecv = (uint16_t)(((uint8_t)_pBuffer[_iSize - 1] << 8) | (uint8_t)_pBuffer[_iSize - 2]);
if (usCrcCalc != usCrcRecv)
{
return;
}
MotorMgr_ParseResponse((uint8_t)_pBuffer[0], (const uint8_t *)_pBuffer, _iSize);
}