From d7060c968d801567e5e3ace00c6cbc2c5abffae2 Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Wed, 18 Nov 2026 08:49:25 +0800 Subject: [PATCH] =?UTF-8?q?motor=E3=80=90=E5=BE=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E3=80=91=E5=A2=9E=E5=8A=A0485=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- motor/include/msp_motor_rs485.h | 143 ++++++ motor/motor_example.c | 61 +++ motor/motor_manager.c | 7 +- motor/msp_motor_rs485.c | 835 ++++++++++++++++++++++++++++++++ 4 files changed, 1043 insertions(+), 3 deletions(-) create mode 100644 motor/include/msp_motor_rs485.h create mode 100644 motor/msp_motor_rs485.c diff --git a/motor/include/msp_motor_rs485.h b/motor/include/msp_motor_rs485.h new file mode 100644 index 0000000..d4c778f --- /dev/null +++ b/motor/include/msp_motor_rs485.h @@ -0,0 +1,143 @@ +/****************************************************************************** + + 版权所有 (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__ */ diff --git a/motor/motor_example.c b/motor/motor_example.c index cb323b7..bb7e3cd 100644 --- a/motor/motor_example.c +++ b/motor/motor_example.c @@ -176,4 +176,65 @@ void MotorExample_MultiProtocol(void) RD_PRINTF("Found motor: %s\n", pstFound->m_szName); } } + +/***************************************************************************** + 函 数 名 : MotorExample_Rs485Modbus + 功能描述 : RS485电机(Modbus RTU)使用示例 + 输入参数 : 无 + 输出参数 : 无 + 返 回 值 : void + + 说明: + 1. 总线:传入已初始化的TComCtrl*(方向控制在RS485x_Send回调内完成) + 2. 接收:rd_ComCreate注册MotorRs485_ComCheck/ComDecode,应答自动路由到协议解析 + 3. 路由约定:Motor_Config_t.m_ucMotorID == Modbus从站地址 + 4. 时序:半双工应答式,读请求pacing>=20ms(本协议内部每次请求自带10ms间隔) +*****************************************************************************/ +void MotorExample_Rs485Modbus(void) +{ + // 1. 初始化485总线(以USART3/RS485_2为例,RS485_2_Send由工程BSP提供) + TUartUserData *ptUartUserData = UART_userdata_init(3, 115200, 512); + TComCtrl *g_ptRs485Bus = rd_ComCreate(MotorRs485_ComCheck, MotorRs485_ComDecode, + RS485_2_Send, ptUartUserData->m_buf_size, ptUartUserData); + UART_IT_init(g_ptRs485Bus); + + // 2. 初始化电机管理模块与RS485协议模块 + MotorMgr_Init(); + MotorRs485_ModuleInit(); + + // 3. 配置电机(MotorID即Modbus从站地址) + Motor_Config_t stConfig = { + .m_ucMotorID = 1, + .m_uiReductionRatio = 70, + .m_fWheelDiameter = 0.26f + }; + MotorRs485_Priv_t *pstPriv = MotorRs485_CreatePriv(1); + Motor_Instance_t *pstMotor = MotorMgr_Create(&stConfig, g_ptRs485Bus, MotorRs485_GetProtocol(), pstPriv); + if (pstMotor == NULL) + { + return; + } + + // 4. 初始化序列(故障复位→使能→速度模式) + MotorMgr_ResetAll(pstMotor); + Rd_Delay(200); + MotorMgr_ActivateAll(pstMotor); + Rd_Delay(200); + MotorMgr_SpeedModeInit(pstMotor); + + // 5. 设置目标速度(0.1m/min,经pfSpeedConvert转为驱动器单位) + MotorMgr_SetTargetSpeed(pstMotor, MotorMgr_SpeedConvert(pstMotor, 500)); + + // 6. 周期读取状态(应答由ComDecode自动解析并经MsgCenter上报) + while (1) + { + char pcBuf[64]; + rd_ComRead(g_ptRs485Bus, pcBuf, sizeof(pcBuf)); + + MotorMgr_RequestVelocity(pstMotor); + MotorMgr_RequestFault(pstMotor); + + Rd_Delay(100); + } +} #endif diff --git a/motor/motor_manager.c b/motor/motor_manager.c index f386f04..5455b66 100644 --- a/motor/motor_manager.c +++ b/motor/motor_manager.c @@ -449,16 +449,17 @@ int MotorMgr_SetDO(Motor_Instance_t *pstMotor, uint8_t ucDoIndex, uint8_t ucStat /***************************************************************************** 函 数 名 : MotorMgr_ParseResponse - 功能描述 : 解析CAN响应数据(遍历所有电机实例尝试解析) - 输入参数 : uint8_t ucMotorID 电机节点ID + 功能描述 : 解析协议响应数据(按MotorID路由到对应实例的协议解析函数) + 输入参数 : uint8_t ucMotorID 电机节点ID(485协议下为Modbus从站地址) const uint8_t *pucData 数据指针 uint32_t uiLen 数据长度 输出参数 : 无 返 回 值 : void + 说明 : 长度下限5字节(Modbus最短应答帧),CAN 8字节帧与协议内部各自校验 *****************************************************************************/ void MotorMgr_ParseResponse(uint8_t ucMotorID, const uint8_t *pucData, uint32_t uiLen) { - if (NULL == pucData || uiLen < 8) + if (NULL == pucData || uiLen < 5) { return; } diff --git a/motor/msp_motor_rs485.c b/motor/msp_motor_rs485.c new file mode 100644 index 0000000..6479827 --- /dev/null +++ b/motor/msp_motor_rs485.c @@ -0,0 +1,835 @@ +/****************************************************************************** + + 版权所有 (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); +}