GrabBag/GrabBagApp/Presenter/Inc/RobotProtocol.h

141 lines
4.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef ROBOTPROTOCOL_H
#define ROBOTPROTOCOL_H
#include <functional>
#include <vector>
#include "IYModbusTCPServer.h"
#include "ProtocolCommon.h"
/**
* @brief 机械臂ModbusTCP协议封装类
* 提供机械臂坐标配置、状态检测、工作信号处理等功能
*/
class RobotProtocol
{
public:
/**
* @brief 机械臂状态枚举(使用公共状态定义)
*/
using RobotStatus = ConnectionStatus;
public:
RobotProtocol();
~RobotProtocol();
/**
* @brief 初始化ModbusTCP服务
* @param port TCP端口号默认502
* @return 0-成功,其他-错误码
*/
int Initialize(uint16_t port = 502);
/**
* @brief 反初始化,停止服务
*/
void Deinitialize();
/**
* @brief 设置多目标检测结果数据
* @param multiTargetData 多目标数据结构
* @param cameraId 相机ID从1开始编号
* @return 0-成功,其他-错误码
*/
int SetMultiTargetData(const MultiTargetData& multiTargetData, uint16_t cameraId);
/**
* @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;
/**
* @brief 设置工作状态
* @param status 工作状态0-空闲1-相机1工作完成2-相机2工作完成3-忙碌
* @return 0-成功,其他-错误码
*/
int SetWorkStatus(uint16_t status);
// 使用公共工作状态定义
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;
private:
/**
* @brief 停止ModbusTCP服务器
*/
void StopModbusTCPServer();
/**
* @brief 处理ModbusTCP连接状态变化
* @param connected true-连接false-断开
*/
void OnModbusTCPConnectionChanged(bool connected);
/**
* @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地址映射
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; // 相机二的检测开始地址
};
#endif // ROBOTPROTOCOL_H