169 lines
5.1 KiB
C++
169 lines
5.1 KiB
C++
#ifndef ROBOTPROTOCOL_H
|
||
#define ROBOTPROTOCOL_H
|
||
|
||
#include <functional>
|
||
#include "IYModbusTCPServer.h"
|
||
|
||
/**
|
||
* @brief 机械臂ModbusTCP协议封装类
|
||
* 提供机械臂坐标配置、状态检测、工作信号处理等功能
|
||
*/
|
||
class RobotProtocol
|
||
{
|
||
|
||
public:
|
||
/**
|
||
* @brief 机械臂坐标结构体
|
||
*/
|
||
struct RobotCoordinate {
|
||
float x; // X坐标
|
||
float y; // Y坐标
|
||
float z; // Z坐标
|
||
float rx; // X轴旋转
|
||
float ry; // Y轴旋转
|
||
float rz; // Z轴旋转
|
||
};
|
||
|
||
/**
|
||
* @brief 机械臂状态枚举
|
||
*/
|
||
enum RobotStatus {
|
||
STATUS_DISCONNECTED = 0, // 断开连接
|
||
STATUS_CONNECTED = 1, // 已连接
|
||
STATUS_IDLE = 2, // 空闲状态
|
||
STATUS_WORKING = 3, // 工作中
|
||
STATUS_ERROR = 4 // 错误状态
|
||
};
|
||
|
||
/**
|
||
* @brief 连接状态回调函数类型
|
||
* @param connected true-连接,false-断开
|
||
*/
|
||
using ConnectionCallback = std::function<void(bool connected)>;
|
||
|
||
/**
|
||
* @brief 开始工作信号回调函数类型
|
||
* @param startWork true-开始工作,false-停止工作
|
||
*/
|
||
using WorkSignalCallback = std::function<void(bool startWork)>;
|
||
|
||
public:
|
||
RobotProtocol();
|
||
~RobotProtocol();
|
||
|
||
/**
|
||
* @brief 初始化ModbusTCP服务
|
||
* @param port TCP端口号,默认502
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int Initialize(uint16_t port = 502);
|
||
|
||
/**
|
||
* @brief 反初始化,停止服务
|
||
*/
|
||
void Deinitialize();
|
||
|
||
/**
|
||
* @brief 设置机械臂坐标配置
|
||
* @param coordinate 坐标结构体
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int SetRobotCoordinate(const RobotCoordinate& coordinate);
|
||
|
||
/**
|
||
* @brief 获取机械臂坐标配置
|
||
* @param coordinate 输出坐标结构体
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int GetRobotCoordinate(RobotCoordinate& coordinate);
|
||
|
||
/**
|
||
* @brief 获取当前检测状态
|
||
* @return 当前状态
|
||
*/
|
||
RobotStatus GetDetectionStatus() const;
|
||
|
||
/**
|
||
* @brief 设置连接状态回调
|
||
* @param callback 回调函数
|
||
*/
|
||
void SetConnectionCallback(const ConnectionCallback& callback);
|
||
|
||
/**
|
||
* @brief 设置工作信号回调
|
||
* @param callback 回调函数
|
||
*/
|
||
void SetWorkSignalCallback(const WorkSignalCallback& callback);
|
||
|
||
/**
|
||
* @brief 获取服务运行状态
|
||
* @return true-运行中,false-已停止
|
||
*/
|
||
bool IsRunning() const;
|
||
|
||
private:
|
||
/**
|
||
* @brief 停止ModbusTCP服务器
|
||
*/
|
||
void StopModbusTCPServer();
|
||
|
||
/**
|
||
* @brief 处理线圈写入请求(工作信号)
|
||
* @param unitId 单元ID
|
||
* @param startAddress 起始地址
|
||
* @param quantity 数量
|
||
* @param values 值数组
|
||
* @return 错误码
|
||
*/
|
||
IYModbusTCPServer::ErrorCode OnWriteCoils(uint8_t unitId, uint16_t startAddress,
|
||
uint16_t quantity, const uint8_t* values);
|
||
|
||
/**
|
||
* @brief 处理保持寄存器写入请求
|
||
* @param unitId 单元ID
|
||
* @param startAddress 起始地址
|
||
* @param quantity 数量
|
||
* @param values 值数组
|
||
* @return 错误码
|
||
*/
|
||
IYModbusTCPServer::ErrorCode OnWriteRegisters(uint8_t unitId, uint16_t startAddress,
|
||
uint16_t quantity, const uint16_t* values);
|
||
|
||
/**
|
||
* @brief 处理读取保持寄存器请求
|
||
* @param unitId 单元ID
|
||
* @param startAddress 起始地址
|
||
* @param quantity 数量
|
||
* @param values 输出值数组
|
||
* @return 错误码
|
||
*/
|
||
IYModbusTCPServer::ErrorCode OnReadHoldingRegisters(uint8_t unitId, uint16_t startAddress,
|
||
uint16_t quantity, uint16_t* values);
|
||
|
||
private:
|
||
// ModbusTCP相关
|
||
IYModbusTCPServer* m_pModbusServer; // ModbusTCP服务器实例
|
||
bool m_bServerRunning; // 服务器运行状态
|
||
uint16_t m_nPort; // TCP端口
|
||
|
||
// 机械臂数据
|
||
RobotCoordinate m_robotCoordinate; // 机械臂坐标
|
||
RobotStatus m_robotStatus; // 机械臂状态
|
||
|
||
// 回调函数
|
||
ConnectionCallback m_connectionCallback; // 连接状态回调
|
||
WorkSignalCallback m_workSignalCallback; // 工作信号回调
|
||
|
||
// Modbus地址映射
|
||
static const uint16_t COORD_X_ADDR = 0x0000; // X坐标地址
|
||
static const uint16_t COORD_Y_ADDR = 0x0002; // Y坐标地址
|
||
static const uint16_t COORD_Z_ADDR = 0x0004; // Z坐标地址
|
||
static const uint16_t COORD_RX_ADDR = 0x0006; // RX坐标地址
|
||
static const uint16_t COORD_RY_ADDR = 0x0008; // RY坐标地址
|
||
static const uint16_t COORD_RZ_ADDR = 0x000A; // RZ坐标地址
|
||
static const uint16_t STATUS_ADDR = 0x000C; // 状态地址
|
||
static const uint16_t WORK_SIGNAL_COIL = 0x0000; // 工作信号线圈地址
|
||
};
|
||
|
||
#endif // ROBOTPROTOCOL_H
|