2026-03-11 23:40:06 +08:00

276 lines
9.1 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 IVRCONFIG_H
#define IVRCONFIG_H
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstring>
// 包含公共配置结构体
#include "VrCommonConfig.h"
/**
* @brief 孔洞检测参数(映射 SHoleDetectionParams
*/
struct VrHoleDetectionParam
{
int neighborCount = 3; // 相邻点连接数
double angleThresholdPos = 10.0; // 正角度阈值(度)
double angleThresholdNeg = -10.0; // 负角度阈值(度)
double minPitDepth = 1.0; // 最小凹坑深度mm
double angleStep = 1.0; // 径向扫描角度步长(度)
double maxScanRadius = 100.0; // 最大扫描半径mm
double clusterEps = 5.0; // DBSCAN聚类半径mm
int clusterMinPoints = 5; // DBSCAN最小点数
double minRadius = 5.0; // 最小孔半径mm
double maxRadius = 50.0; // 最大孔半径mm
int expansionSize1 = 5; // 第一环扩展大小
int expansionSize2 = 10; // 第二环扩展大小
double validZThreshold = 0.0001; // 有效Z值阈值
int minVTransitionPoints = 1; // V形端点间最小有效过渡点数
// 角点检测参数cornerMethod
double cornerScale = 2.0; // 前后搜索点距离mm
double cornerAngleThreshold = 45.0; // 最小角点角度变化(度)
double jumpCornerTh_1 = 10.0; // 跳跃检测小角度阈值(度)
double jumpCornerTh_2 = 30.0; // 跳跃检测大角度阈值(度)
double minEndingGap = 5.0; // Y方向跳跃配对距离阈值mm
double minEndingGap_z = 1.0; // Z方向跳跃验证高度阈值mm
};
/**
* @brief 孔洞过滤参数(映射 SHoleFilterParams
*/
struct VrHoleFilterParam
{
double minHoleRadius = 1.0; // 最小孔半径mm
double maxHoleRadius = 10.0; // 最大孔半径mm
double maxEccentricity = 0.99995; // 最大离心率
double maxCornerRatio = 0.15; // 最大矩形度比率
double minAngularCoverage = 10.0; // 最小角度覆盖(度)
double maxRadiusFitRatio = 0.3; // 最大半径拟合比率
double minQualityScore = 0.3; // 最小质量分数
double maxPlaneResidual = 10.0; // 最大平面残差mm
double maxAngularGap = 90.0; // 最大角度间隙(度)
double minInlierRatio = 0.3; // 最小内点比率
};
/**
* @brief 孔洞排序模式枚举(映射 ESortMode
*/
enum VrHoleSortMode
{
HOLE_SORT_NONE = 0, // 不排序
HOLE_SORT_BY_RADIUS = 1, // 按半径排序(最大优先)
HOLE_SORT_BY_DEPTH = 2, // 按深度排序(最深优先)
HOLE_SORT_BY_QUALITY = 3 // 按质量分数排序(最高优先)
};
/**
* @brief 手眼标定矩阵
*/
struct VrHandEyeCalibMatrix
{
double matrix[16] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
int eulerOrder = 11; // 欧拉角顺序默认11=sZYX外旋ZYX
};
/**
* @brief 姿态输出顺序枚举
*
* 定义机械臂姿态数据的输出顺序
*/
enum VrPoseOutputOrder
{
POSE_ORDER_RPY = 0, // Roll, Pitch, Yaw默认
POSE_ORDER_RYP = 1, // Roll, Yaw, Pitch
POSE_ORDER_PRY = 2, // Pitch, Roll, Yaw
POSE_ORDER_PYR = 3, // Pitch, Yaw, Roll
POSE_ORDER_YRP = 4, // Yaw, Roll, Pitch
POSE_ORDER_YPR = 5 // Yaw, Pitch, Roll
};
/**
* @brief 方向向量反向配置枚举
*
* 定义机械臂方向向量的反向配置
* 用于调整相机坐标系到机械臂坐标系的方向向量转换
*/
enum VrDirVectorInvert
{
DIR_INVERT_NONE = 0, // 不反向
DIR_INVERT_XY = 1, // X和Y方向反向
DIR_INVERT_XZ = 2, // X和Z方向反向
DIR_INVERT_YZ = 3 // Y和Z方向反向默认兼容原有行为
};
/**
* @brief TCP协议和坐标输出配置
*
* 默认值参考 Doc/porotol.jpg 协议文档:
* - 汇川PLC-easy320: 192.168.0.88:502
* - 配天机械臂: 192.168.0.90:502
*/
struct VrPlcRobotServerConfig
{
int tcpServerPort = 7800; // TCP协议服务端口
int poseOutputOrder = POSE_ORDER_RPY; // 姿态输出顺序默认RPY
int dirVectorInvert = DIR_INVERT_YZ; // 方向向量反向配置默认YZ反向兼容原有行为
// 显式赋值构造函数
VrPlcRobotServerConfig& operator=(const VrPlcRobotServerConfig& other) {
if (this != &other) {
tcpServerPort = other.tcpServerPort;
poseOutputOrder = other.poseOutputOrder;
dirVectorInvert = other.dirVectorInvert;
}
return *this;
}
// 显式复制构造函数
VrPlcRobotServerConfig(const VrPlcRobotServerConfig& other)
: tcpServerPort(other.tcpServerPort)
, poseOutputOrder(other.poseOutputOrder)
, dirVectorInvert(other.dirVectorInvert) {
}
// 默认构造函数
VrPlcRobotServerConfig() = default;
};
/**
* @brief 算法参数配置结构
*/
struct VrAlgorithmParams
{
VrHoleDetectionParam detectionParam; // 孔洞检测参数
VrHoleFilterParam filterParam; // 孔洞过滤参数
int sortMode = HOLE_SORT_NONE; // 排序模式
VrPlaneCalibParam planeCalibParam; // 平面校准参数
// 显式赋值构造函数,确保正确的深拷贝
VrAlgorithmParams& operator=(const VrAlgorithmParams& other) {
if (this != &other) {
detectionParam = other.detectionParam;
filterParam = other.filterParam;
sortMode = other.sortMode;
planeCalibParam = other.planeCalibParam;
}
return *this;
}
// 显式复制构造函数
VrAlgorithmParams(const VrAlgorithmParams& other)
: detectionParam(other.detectionParam)
, filterParam(other.filterParam)
, sortMode(other.sortMode)
, planeCalibParam(other.planeCalibParam) {
}
// 默认构造函数
VrAlgorithmParams() = default;
};
/**
* @brief 配置加载结果
*/
struct ConfigResult
{
std::vector<DeviceInfo> cameraList;
std::vector<DeviceInfo> deviceList;
VrAlgorithmParams algorithmParams; // 算法参数
std::vector<VrHandEyeCalibMatrix> handEyeCalibMatrixList; // 多相机手眼标定矩阵列表
VrDebugParam debugParam; // 调试参数
SerialConfig serialConfig; // 串口配置
VrPlcRobotServerConfig plcRobotServerConfig; // PLC和机械臂服务端配置
// 显式赋值构造函数,确保正确的深拷贝
ConfigResult& operator=(const ConfigResult& other) {
if (this != &other) {
cameraList = other.cameraList;
deviceList = other.deviceList;
algorithmParams = other.algorithmParams;
handEyeCalibMatrixList = other.handEyeCalibMatrixList;
debugParam = other.debugParam;
serialConfig = other.serialConfig;
plcRobotServerConfig = other.plcRobotServerConfig;
}
return *this;
}
// 显式复制构造函数
ConfigResult(const ConfigResult& other)
: cameraList(other.cameraList)
, deviceList(other.deviceList)
, algorithmParams(other.algorithmParams)
, handEyeCalibMatrixList(other.handEyeCalibMatrixList)
, debugParam(other.debugParam)
, serialConfig(other.serialConfig)
, plcRobotServerConfig(other.plcRobotServerConfig) {
}
// 默认构造函数
ConfigResult() = default;
};
/**
* @brief 配置加载错误代码
*/
enum LoadConfigErrorCode
{
LOAD_CONFIG_SUCCESS = 0, // 加载成功
LOAD_CONFIG_FILE_NOT_FOUND = -1, // 配置文件不存在
LOAD_CONFIG_PARSE_ERROR = -2, // 配置文件解析错误
LOAD_CONFIG_INVALID_FORMAT = -3, // 配置文件格式无效
LOAD_CONFIG_UNKNOWN_ERROR = -99 // 未知错误
};
/**
* @brief VrConfig接口类
*/
class IVrConfig
{
public:
/**
* @brief 虚析构函数
*/
virtual ~IVrConfig() {}
/**
* @brief 创建实例
* @return 实例
*/
static bool CreateInstance(IVrConfig** ppVrConfig);
/**
* @brief 加载配置文件
* @param filePath 配置文件路径
* @param configResult 输出参数,加载的配置结果
* @return 错误代码 (LoadConfigErrorCode): 0=成功, 负值表示错误
*/
virtual int LoadConfig(const std::string& filePath, ConfigResult& configResult) = 0;
/**
* @brief 保存配置文件
* @param filePath 配置文件路径
* @param configResult 配置结果
* @return 是否保存成功
*/
virtual bool SaveConfig(const std::string& filePath, ConfigResult& configResult) = 0;
/**
* @brief 设置配置改变通知回调
* @param notify 通知接口指针
*/
virtual void SetConfigChangeNotify(IVrConfigChangeNotify* notify) = 0;
};
#endif // IVRCONFIG_H