2025-06-08 12:48:04 +08:00
|
|
|
|
#ifndef ROBOTPROTOCOL_H
|
|
|
|
|
|
#define ROBOTPROTOCOL_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
2025-06-22 14:08:15 +08:00
|
|
|
|
#include <vector>
|
2025-06-08 12:48:04 +08:00
|
|
|
|
#include "IYModbusTCPServer.h"
|
2025-07-14 00:17:41 +08:00
|
|
|
|
#include "ProtocolCommon.h"
|
2025-06-08 12:48:04 +08:00
|
|
|
|
|
2025-06-22 14:08:15 +08:00
|
|
|
|
|
2025-06-08 12:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 机械臂ModbusTCP协议封装类
|
|
|
|
|
|
* 提供机械臂坐标配置、状态检测、工作信号处理等功能
|
|
|
|
|
|
*/
|
|
|
|
|
|
class RobotProtocol
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
/**
|
2025-07-14 00:17:41 +08:00
|
|
|
|
* @brief 机械臂状态枚举(使用公共状态定义)
|
2025-06-08 12:48:04 +08:00
|
|
|
|
*/
|
2025-07-14 00:17:41 +08:00
|
|
|
|
using RobotStatus = ConnectionStatus;
|
2025-06-08 12:48:04 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
RobotProtocol();
|
|
|
|
|
|
~RobotProtocol();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化ModbusTCP服务
|
|
|
|
|
|
* @param port TCP端口号,默认502
|
|
|
|
|
|
* @return 0-成功,其他-错误码
|
|
|
|
|
|
*/
|
|
|
|
|
|
int Initialize(uint16_t port = 502);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 反初始化,停止服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
void Deinitialize();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-22 14:08:15 +08:00
|
|
|
|
* @brief 设置多目标检测结果数据
|
|
|
|
|
|
* @param multiTargetData 多目标数据结构
|
|
|
|
|
|
* @param cameraId 相机ID(从1开始编号)
|
2025-06-08 12:48:04 +08:00
|
|
|
|
* @return 0-成功,其他-错误码
|
|
|
|
|
|
*/
|
2025-06-22 14:08:15 +08:00
|
|
|
|
int SetMultiTargetData(const MultiTargetData& multiTargetData, uint16_t cameraId);
|
2025-06-08 12:48:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
2025-06-22 14:08:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 设置工作状态
|
|
|
|
|
|
* @param status 工作状态:0-空闲,1-相机1工作完成,2-相机2工作完成,3-忙碌
|
|
|
|
|
|
* @return 0-成功,其他-错误码
|
|
|
|
|
|
*/
|
|
|
|
|
|
int SetWorkStatus(uint16_t status);
|
|
|
|
|
|
|
2025-07-14 00:17:41 +08:00
|
|
|
|
// 使用公共工作状态定义
|
|
|
|
|
|
static const uint16_t WORK_STATUS_IDLE = ::WORK_STATUS_IDLE;
|
|
|
|
|
|
static const uint16_t WORK_STATUS_CAMERA1_DONE = ::WORK_STATUS_CAMERA1_DONE;
|
|
|
|
|
|
static const uint16_t WORK_STATUS_CAMERA2_DONE = ::WORK_STATUS_CAMERA2_DONE;
|
|
|
|
|
|
static const uint16_t WORK_STATUS_BUSY = ::WORK_STATUS_BUSY;
|
2025-06-22 14:08:15 +08:00
|
|
|
|
|
2025-06-08 12:48:04 +08:00
|
|
|
|
private:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 停止ModbusTCP服务器
|
|
|
|
|
|
*/
|
|
|
|
|
|
void StopModbusTCPServer();
|
|
|
|
|
|
|
2025-06-08 23:51:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理ModbusTCP连接状态变化
|
|
|
|
|
|
* @param connected true-连接,false-断开
|
|
|
|
|
|
*/
|
|
|
|
|
|
void OnModbusTCPConnectionChanged(bool connected);
|
|
|
|
|
|
|
2025-06-08 12:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
// ModbusTCP相关
|
|
|
|
|
|
IYModbusTCPServer* m_pModbusServer; // ModbusTCP服务器实例
|
|
|
|
|
|
bool m_bServerRunning; // 服务器运行状态
|
|
|
|
|
|
uint16_t m_nPort; // TCP端口
|
|
|
|
|
|
|
|
|
|
|
|
// 机械臂数据
|
|
|
|
|
|
RobotStatus m_robotStatus; // 机械臂状态
|
|
|
|
|
|
|
|
|
|
|
|
// 回调函数
|
|
|
|
|
|
ConnectionCallback m_connectionCallback; // 连接状态回调
|
|
|
|
|
|
WorkSignalCallback m_workSignalCallback; // 工作信号回调
|
|
|
|
|
|
|
|
|
|
|
|
// Modbus地址映射
|
2025-06-22 14:08:15 +08:00
|
|
|
|
static const uint16_t WORK_SIGNAL_ADDR = 0; // 工作信号线圈地址 (所有相机)
|
|
|
|
|
|
static const uint16_t STATUS_ADDR = 1; // 状态地址
|
|
|
|
|
|
static const uint16_t COORD_ONE_START_ADDR = 4; // 相机一的检测开始地址
|
|
|
|
|
|
static const uint16_t COORD_TWO_START_ADDR = 46; // 相机二的检测开始地址
|
2025-06-08 12:48:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ROBOTPROTOCOL_H
|