332 lines
11 KiB
C
Raw Normal View History

2026-02-23 18:22:38 +08:00
#ifndef IVRCONFIG_H
#define IVRCONFIG_H
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstring>
// 包含公共配置结构体
#include "VrCommonConfig.h"
/**
* @brief 离群点滤波参数
*/
struct VrOutlierFilterParam
{
double continuityTh = 20.0; // 连续性阈值,噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声
double outlierTh = 5.0; // 离群点判断阈值,若长度小于此值,视为噪声
};
/**
* @brief 定子物理参数
*/
struct VrStatorParam
{
double statorOuterD = 85.0; // 定子外直径 (mm)
double statorInnerD = 50.0; // 定子内孔直径 (mm)
double statorHeight = 100.0; // 定子高度 (mm)
double plateThickness = 8.0; // 隔板厚度 (mm)
double plateW = 500.0; // 隔板长 (mm)
double plateH = 300.0; // 隔板宽 (mm)
};
/**
* @brief 角点检测参数
*/
struct VrCornerParam
{
double minEndingGap = 20.0; // Y方向最小间隔
double minEndingGap_z = 20.0; // Z方向最小间隔
double scale = 5.0; // 角度计算窗口标度
double cornerTh = 75.0; // 角点门限
double jumpCornerTh_1 = 20.0; // 跳跃角点判别门限1
double jumpCornerTh_2 = 60.0; // 跳跃角点判别门限2
};
/**
* @brief 树生长参数
*/
struct VrTreeGrowParam
{
int maxLineSkipNum = 10; // 生长时允许跳过的最大线条数
double yDeviation_max = 10.0; // 生长时允许的最大Y偏差
double maxSkipDistance = 10.0; // 最大跳跃距离
double zDeviation_max = 10.0; // 生长时允许的最大Z偏差
double minLTypeTreeLen = 100.0; // L型树的最小长度
double minVTypeTreeLen = 100.0; // V型树的最小长度
};
/**
* @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 PLC Modbus 寄存器地址配置(0-based 协议)
*
* 统一地址定义:
* 地址0: 触发开始工作(边沿检测 0→1 触发)
* 地址1: 工作状态(0=正在工作, 1=完成正常, 2=错误)
* 地址2+: 检测结果数据(每点6个float = 12个寄存器)
*/
struct VrPlcRegisterConfig
{
int addrTrigger = 0; // 触发寄存器地址(读:边沿 0→1 触发工作)
int addrWorkStatus = 1; // 工作状态寄存器地址(写:0=工作中, 1=完成, 2=错误)
int addrCoordDataStart = 2; // 坐标数据起始寄存器地址
// 显式赋值构造函数
VrPlcRegisterConfig& operator=(const VrPlcRegisterConfig& other) {
if (this != &other) {
addrTrigger = other.addrTrigger;
addrWorkStatus = other.addrWorkStatus;
addrCoordDataStart = other.addrCoordDataStart;
}
return *this;
}
// 显式复制构造函数
VrPlcRegisterConfig(const VrPlcRegisterConfig& other)
: addrTrigger(other.addrTrigger)
, addrWorkStatus(other.addrWorkStatus)
, addrCoordDataStart(other.addrCoordDataStart) {
}
// 默认构造函数
VrPlcRegisterConfig() = default;
};
/**
* @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 数据字节序枚举
*
* 定义 PLC 通信时浮点数的字节序
* 不同 PLC 厂商可能使用不同的字节序
*/
enum VrByteOrder
{
BYTE_ORDER_BIG_ENDIAN = 0, // 大端序 (ABCD) - 高字节在前
BYTE_ORDER_LITTLE_ENDIAN = 1 // 小端序 (DCBA) - 低字节在前
};
/**
* @brief PLC和机械臂服务端配置
*
* 默认值参考 Doc/porotol.jpg 协议文档:
* - 汇川PLC-easy320: 192.168.0.88:502
* - 配天机械臂: 192.168.0.90:502
*/
struct VrPlcRobotServerConfig
{
std::string plcServerIp = "192.168.0.88"; // PLC服务端IP地址(汇川PLC-easy320)
int plcServerPort = 502; // PLC服务端端口(Modbus TCP默认502)
std::string robotServerIp = "192.168.0.90"; // 机械臂服务端IP地址(配天机械臂)
int robotServerPort = 502; // 机械臂服务端端口(Modbus TCP默认502)
VrPlcRegisterConfig registerConfig; // PLC 寄存器地址配置
int poseOutputOrder = POSE_ORDER_RPY; // 姿态输出顺序,默认RPY
int dirVectorInvert = DIR_INVERT_YZ; // 方向向量反向配置,默认YZ反向(兼容原有行为)
int byteOrder = BYTE_ORDER_BIG_ENDIAN; // 数据字节序,默认大端序
// 显式赋值构造函数
VrPlcRobotServerConfig& operator=(const VrPlcRobotServerConfig& other) {
if (this != &other) {
plcServerIp = other.plcServerIp;
plcServerPort = other.plcServerPort;
robotServerIp = other.robotServerIp;
robotServerPort = other.robotServerPort;
registerConfig = other.registerConfig;
poseOutputOrder = other.poseOutputOrder;
dirVectorInvert = other.dirVectorInvert;
byteOrder = other.byteOrder;
}
return *this;
}
// 显式复制构造函数
VrPlcRobotServerConfig(const VrPlcRobotServerConfig& other)
: plcServerIp(other.plcServerIp)
, plcServerPort(other.plcServerPort)
, robotServerIp(other.robotServerIp)
, robotServerPort(other.robotServerPort)
, registerConfig(other.registerConfig)
, poseOutputOrder(other.poseOutputOrder)
, dirVectorInvert(other.dirVectorInvert)
, byteOrder(other.byteOrder) {
}
// 默认构造函数
VrPlcRobotServerConfig() = default;
};
/**
* @brief 算法参数配置结构
*/
struct VrAlgorithmParams
{
VrOutlierFilterParam filterParam; // 离群点滤波参数
VrCornerParam cornerParam; // 角点检测参数
VrTreeGrowParam growParam; // 树生长参数
VrPlaneCalibParam planeCalibParam; // 平面校准参数
VrStatorParam statorParam; // 定子物理参数
// 显式赋值构造函数,确保正确的深拷贝
VrAlgorithmParams& operator=(const VrAlgorithmParams& other) {
if (this != &other) {
filterParam = other.filterParam;
cornerParam = other.cornerParam;
growParam = other.growParam;
planeCalibParam = other.planeCalibParam;
statorParam = other.statorParam;
}
return *this;
}
// 显式复制构造函数
VrAlgorithmParams(const VrAlgorithmParams& other)
: filterParam(other.filterParam)
, cornerParam(other.cornerParam)
, growParam(other.growParam)
, planeCalibParam(other.planeCalibParam)
, statorParam(other.statorParam) {
}
// 默认构造函数
VrAlgorithmParams() = default;
};
/**
* @brief 配置加载结果
*/
struct ConfigResult
{
std::vector<DeviceInfo> cameraList;
std::vector<DeviceInfo> deviceList;
VrAlgorithmParams algorithmParams; // 算法参数
VrHandEyeCalibMatrix handEyeCalibMatrix; // 手眼标定矩阵
VrDebugParam debugParam; // 调试参数
SerialConfig serialConfig; // 串口配置
VrPlcRobotServerConfig plcRobotServerConfig; // PLC和机械臂服务端配置
// 显式赋值构造函数,确保正确的深拷贝
ConfigResult& operator=(const ConfigResult& other) {
if (this != &other) {
cameraList = other.cameraList;
deviceList = other.deviceList;
algorithmParams = other.algorithmParams;
handEyeCalibMatrix = other.handEyeCalibMatrix;
debugParam = other.debugParam;
serialConfig = other.serialConfig;
plcRobotServerConfig = other.plcRobotServerConfig;
}
return *this;
}
// 显式复制构造函数
ConfigResult(const ConfigResult& other)
: cameraList(other.cameraList)
, deviceList(other.deviceList)
, algorithmParams(other.algorithmParams)
, handEyeCalibMatrix(other.handEyeCalibMatrix)
, 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