311 lines
15 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.

#include "VrConfig.h"
#include <iostream>
#include <vector>
#include <string>
#include "VrLog.h"
#include "ConfigXmlUtils.h"
using namespace tinyxml2;
CVrConfig::CVrConfig() : m_pNotify(nullptr)
{
// 构造函数
}
CVrConfig::~CVrConfig()
{
// 析构函数
}
int CVrConfig::LoadConfig(const std::string& filePath, ConfigResult& configResult)
{
// 使用tinyxml2库加载XML文件
XMLDocument doc;
XMLError err = doc.LoadFile(filePath.c_str());
if (err != XML_SUCCESS)
{
LOG_ERR("open config file failed: %s\n", filePath.c_str());
return LOAD_CONFIG_FILE_NOT_FOUND;
}
// 获取根元素
XMLElement* root = doc.RootElement();
if (!root || std::string(root->Name()) != "WorkpieceHoleConfig")
{
std::cerr << "config file format error: root element is not WorkpieceHoleConfig" << std::endl;
return LOAD_CONFIG_INVALID_FORMAT;
}
// 解析摄像头列表
ConfigXmlUtils::LoadCameraList(root, configResult.cameraList);
// 解析设备列表
XMLElement* devicesElement = root->FirstChildElement("Devices");
if (devicesElement)
{
XMLElement* deviceElement = devicesElement->FirstChildElement("Device");
while (deviceElement)
{
DeviceInfo device;
if (deviceElement->Attribute("name"))
device.name = deviceElement->Attribute("name");
if (deviceElement->Attribute("ip"))
device.ip = deviceElement->Attribute("ip");
configResult.deviceList.push_back(device);
deviceElement = deviceElement->NextSiblingElement("Device");
}
}
// 解析算法参数
XMLElement* algoParamsElement = root->FirstChildElement("AlgorithmParams");
if (algoParamsElement)
{
// 解析工件孔参数
XMLElement* workpieceHoleParamElement = algoParamsElement->FirstChildElement("WorkpieceHoleParam");
if (workpieceHoleParamElement)
{
if (workpieceHoleParamElement->Attribute("workpieceType"))
configResult.algorithmParams.workpieceHoleParam.workpieceType = workpieceHoleParamElement->IntAttribute("workpieceType");
if (workpieceHoleParamElement->Attribute("holeDiameter"))
configResult.algorithmParams.workpieceHoleParam.holeDiameter = workpieceHoleParamElement->DoubleAttribute("holeDiameter");
if (workpieceHoleParamElement->Attribute("holeDist_L"))
configResult.algorithmParams.workpieceHoleParam.holeDist_L = workpieceHoleParamElement->DoubleAttribute("holeDist_L");
if (workpieceHoleParamElement->Attribute("holeDist_W"))
configResult.algorithmParams.workpieceHoleParam.holeDist_W = workpieceHoleParamElement->DoubleAttribute("holeDist_W");
if (workpieceHoleParamElement->Attribute("xLen"))
configResult.algorithmParams.workpieceHoleParam.xLen = workpieceHoleParamElement->DoubleAttribute("xLen");
if (workpieceHoleParamElement->Attribute("yLen"))
configResult.algorithmParams.workpieceHoleParam.yLen = workpieceHoleParamElement->DoubleAttribute("yLen");
if (workpieceHoleParamElement->Attribute("H"))
configResult.algorithmParams.workpieceHoleParam.H = workpieceHoleParamElement->DoubleAttribute("H");
}
// 解析滤波参数
XMLElement* filterParamElement = algoParamsElement->FirstChildElement("FilterParam");
if (filterParamElement)
{
if (filterParamElement->Attribute("continuityTh"))
configResult.algorithmParams.filterParam.continuityTh = filterParamElement->DoubleAttribute("continuityTh");
if (filterParamElement->Attribute("outlierTh"))
configResult.algorithmParams.filterParam.outlierTh = filterParamElement->DoubleAttribute("outlierTh");
}
// 解析线段分割参数
XMLElement* lineSegParamElement = algoParamsElement->FirstChildElement("LineSegParam");
if (lineSegParamElement)
{
if (lineSegParamElement->Attribute("distScale"))
configResult.algorithmParams.lineSegParam.distScale = lineSegParamElement->DoubleAttribute("distScale");
if (lineSegParamElement->Attribute("segGapTh_y"))
configResult.algorithmParams.lineSegParam.segGapTh_y = lineSegParamElement->DoubleAttribute("segGapTh_y");
if (lineSegParamElement->Attribute("segGapTh_z"))
configResult.algorithmParams.lineSegParam.segGapTh_z = lineSegParamElement->DoubleAttribute("segGapTh_z");
}
// 解析增长参数
XMLElement* growParamElement = algoParamsElement->FirstChildElement("GrowParam");
if (growParamElement)
{
if (growParamElement->Attribute("maxLineSkipNum"))
configResult.algorithmParams.growParam.maxLineSkipNum = growParamElement->IntAttribute("maxLineSkipNum");
if (growParamElement->Attribute("yDeviation_max"))
configResult.algorithmParams.growParam.yDeviation_max = growParamElement->DoubleAttribute("yDeviation_max");
if (growParamElement->Attribute("maxSkipDistance"))
configResult.algorithmParams.growParam.maxSkipDistance = growParamElement->DoubleAttribute("maxSkipDistance");
if (growParamElement->Attribute("zDeviation_max"))
configResult.algorithmParams.growParam.zDeviation_max = growParamElement->DoubleAttribute("zDeviation_max");
if (growParamElement->Attribute("minLTypeTreeLen"))
configResult.algorithmParams.growParam.minLTypeTreeLen = growParamElement->DoubleAttribute("minLTypeTreeLen");
if (growParamElement->Attribute("minVTypeTreeLen"))
configResult.algorithmParams.growParam.minVTypeTreeLen = growParamElement->DoubleAttribute("minVTypeTreeLen");
}
// 解析多相机平面校准参数
ConfigXmlUtils::LoadPlaneCalibParams(algoParamsElement, configResult.algorithmParams.planeCalibParam);
}
// 解析调试参数
ConfigXmlUtils::LoadDebugParam(root, configResult.debugParam);
// 解析串口配置
ConfigXmlUtils::LoadSerialConfig(root, configResult.serialConfig);
// 解析手眼标定矩阵列表(支持多相机)
ConfigXmlUtils::LoadHandEyeCalibMatrixs(root, configResult.handEyeCalibMatrixList);
// 解析TCP服务端配置PLC和机械臂
XMLElement* tcpServerConfigElement = root->FirstChildElement("TcpServerConfig");
if (tcpServerConfigElement)
{
if (tcpServerConfigElement->Attribute("plcServerIp"))
configResult.plcRobotServerConfig.plcServerIp = tcpServerConfigElement->Attribute("plcServerIp");
if (tcpServerConfigElement->Attribute("plcServerPort"))
configResult.plcRobotServerConfig.plcServerPort = tcpServerConfigElement->IntAttribute("plcServerPort");
if (tcpServerConfigElement->Attribute("robotServerIp"))
configResult.plcRobotServerConfig.robotServerIp = tcpServerConfigElement->Attribute("robotServerIp");
if (tcpServerConfigElement->Attribute("robotServerPort"))
configResult.plcRobotServerConfig.robotServerPort = tcpServerConfigElement->IntAttribute("robotServerPort");
// 解析PLC寄存器地址配置
if (tcpServerConfigElement->Attribute("addrPhotoRequest"))
configResult.plcRobotServerConfig.registerConfig.addrPhotoRequest = tcpServerConfigElement->IntAttribute("addrPhotoRequest");
if (tcpServerConfigElement->Attribute("addrDataComplete"))
configResult.plcRobotServerConfig.registerConfig.addrDataComplete = tcpServerConfigElement->IntAttribute("addrDataComplete");
if (tcpServerConfigElement->Attribute("addrCoordDataStart"))
configResult.plcRobotServerConfig.registerConfig.addrCoordDataStart = tcpServerConfigElement->IntAttribute("addrCoordDataStart");
// 解析姿态输出顺序配置
if (tcpServerConfigElement->Attribute("poseOutputOrder"))
configResult.plcRobotServerConfig.poseOutputOrder = tcpServerConfigElement->IntAttribute("poseOutputOrder");
else
configResult.plcRobotServerConfig.poseOutputOrder = POSE_ORDER_RPY; // 默认RPY
// 解析方向向量反向配置
if (tcpServerConfigElement->Attribute("dirVectorInvert"))
configResult.plcRobotServerConfig.dirVectorInvert = tcpServerConfigElement->IntAttribute("dirVectorInvert");
else
configResult.plcRobotServerConfig.dirVectorInvert = DIR_INVERT_YZ; // 默认YZ反向兼容原有行为
// 解析数据字节序配置
if (tcpServerConfigElement->Attribute("byteOrder"))
configResult.plcRobotServerConfig.byteOrder = tcpServerConfigElement->IntAttribute("byteOrder");
else
configResult.plcRobotServerConfig.byteOrder = BYTE_ORDER_BIG_ENDIAN; // 默认大端序
}
return LOAD_CONFIG_SUCCESS;
}
bool CVrConfig::SaveConfig(const std::string& filePath, ConfigResult& configResult)
{
// 创建XML文档
XMLDocument doc;
// 添加声明
XMLDeclaration* declaration = doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
doc.InsertFirstChild(declaration);
// 创建根元素
XMLElement* root = doc.NewElement("WorkpieceHoleConfig");
doc.InsertEndChild(root);
// 添加摄像头列表
ConfigXmlUtils::SaveCameraList(doc, root, configResult.cameraList);
// 添加设备列表
XMLElement* devicesElement = doc.NewElement("Devices");
root->InsertEndChild(devicesElement);
for (const auto& device : configResult.deviceList)
{
XMLElement* deviceElement = doc.NewElement("Device");
deviceElement->SetAttribute("name", device.name.c_str());
deviceElement->SetAttribute("ip", device.ip.c_str());
devicesElement->InsertEndChild(deviceElement);
}
// 添加算法参数
XMLElement* algoParamsElement = doc.NewElement("AlgorithmParams");
root->InsertEndChild(algoParamsElement);
// 添加工件孔参数
XMLElement* workpieceHoleParamElement = doc.NewElement("WorkpieceHoleParam");
workpieceHoleParamElement->SetAttribute("workpieceType", configResult.algorithmParams.workpieceHoleParam.workpieceType);
workpieceHoleParamElement->SetAttribute("holeDiameter", configResult.algorithmParams.workpieceHoleParam.holeDiameter);
workpieceHoleParamElement->SetAttribute("holeDist_L", configResult.algorithmParams.workpieceHoleParam.holeDist_L);
workpieceHoleParamElement->SetAttribute("holeDist_W", configResult.algorithmParams.workpieceHoleParam.holeDist_W);
workpieceHoleParamElement->SetAttribute("xLen", configResult.algorithmParams.workpieceHoleParam.xLen);
workpieceHoleParamElement->SetAttribute("yLen", configResult.algorithmParams.workpieceHoleParam.yLen);
workpieceHoleParamElement->SetAttribute("H", configResult.algorithmParams.workpieceHoleParam.H);
algoParamsElement->InsertEndChild(workpieceHoleParamElement);
// 添加滤波参数
XMLElement* filterParamElement = doc.NewElement("FilterParam");
filterParamElement->SetAttribute("continuityTh", configResult.algorithmParams.filterParam.continuityTh);
filterParamElement->SetAttribute("outlierTh", configResult.algorithmParams.filterParam.outlierTh);
algoParamsElement->InsertEndChild(filterParamElement);
// 添加线段分割参数
XMLElement* lineSegParamElement = doc.NewElement("LineSegParam");
lineSegParamElement->SetAttribute("distScale", configResult.algorithmParams.lineSegParam.distScale);
lineSegParamElement->SetAttribute("segGapTh_y", configResult.algorithmParams.lineSegParam.segGapTh_y);
lineSegParamElement->SetAttribute("segGapTh_z", configResult.algorithmParams.lineSegParam.segGapTh_z);
algoParamsElement->InsertEndChild(lineSegParamElement);
// 添加增长参数
XMLElement* growParamElement = doc.NewElement("GrowParam");
growParamElement->SetAttribute("maxLineSkipNum", configResult.algorithmParams.growParam.maxLineSkipNum);
growParamElement->SetAttribute("yDeviation_max", configResult.algorithmParams.growParam.yDeviation_max);
growParamElement->SetAttribute("maxSkipDistance", configResult.algorithmParams.growParam.maxSkipDistance);
growParamElement->SetAttribute("zDeviation_max", configResult.algorithmParams.growParam.zDeviation_max);
growParamElement->SetAttribute("minLTypeTreeLen", configResult.algorithmParams.growParam.minLTypeTreeLen);
growParamElement->SetAttribute("minVTypeTreeLen", configResult.algorithmParams.growParam.minVTypeTreeLen);
algoParamsElement->InsertEndChild(growParamElement);
// 添加多相机平面校准参数
ConfigXmlUtils::SavePlaneCalibParams(doc, algoParamsElement, configResult.algorithmParams.planeCalibParam);
// 添加调试参数
ConfigXmlUtils::SaveDebugParam(doc, root, configResult.debugParam);
// 添加串口配置
ConfigXmlUtils::SaveSerialConfig(doc, root, configResult.serialConfig);
// 添加手眼标定矩阵列表(支持多相机)
ConfigXmlUtils::SaveHandEyeCalibMatrixs(doc, root, configResult.handEyeCalibMatrixList);
// 添加TCP服务端配置PLC和机械臂
XMLElement* tcpServerConfigElement = doc.NewElement("TcpServerConfig");
tcpServerConfigElement->SetAttribute("plcServerIp", configResult.plcRobotServerConfig.plcServerIp.c_str());
tcpServerConfigElement->SetAttribute("plcServerPort", configResult.plcRobotServerConfig.plcServerPort);
tcpServerConfigElement->SetAttribute("robotServerIp", configResult.plcRobotServerConfig.robotServerIp.c_str());
tcpServerConfigElement->SetAttribute("robotServerPort", configResult.plcRobotServerConfig.robotServerPort);
// 保存PLC寄存器地址配置
tcpServerConfigElement->SetAttribute("addrPhotoRequest", configResult.plcRobotServerConfig.registerConfig.addrPhotoRequest);
tcpServerConfigElement->SetAttribute("addrDataComplete", configResult.plcRobotServerConfig.registerConfig.addrDataComplete);
tcpServerConfigElement->SetAttribute("addrCoordDataStart", configResult.plcRobotServerConfig.registerConfig.addrCoordDataStart);
// 保存姿态输出顺序配置
tcpServerConfigElement->SetAttribute("poseOutputOrder", configResult.plcRobotServerConfig.poseOutputOrder);
// 保存方向向量反向配置
tcpServerConfigElement->SetAttribute("dirVectorInvert", configResult.plcRobotServerConfig.dirVectorInvert);
// 保存数据字节序配置
tcpServerConfigElement->SetAttribute("byteOrder", configResult.plcRobotServerConfig.byteOrder);
root->InsertEndChild(tcpServerConfigElement);
// 保存到文件
XMLError err = doc.SaveFile(filePath.c_str());
if (err != XML_SUCCESS)
{
std::cerr << "无法保存配置文件: " << filePath << std::endl;
return false;
}
// 触发配置改变通知
if (m_pNotify)
{
m_pNotify->OnConfigChanged(configResult);
}
return true;
}
// 设置配置改变通知回调
void CVrConfig::SetConfigChangeNotify(IVrConfigChangeNotify* notify)
{
m_pNotify = notify;
}
/**
* @brief 创建实例
* @return 实例
*/
bool IVrConfig::CreateInstance(IVrConfig** ppVrConfig)
{
*ppVrConfig = new CVrConfig();
return *ppVrConfig != nullptr;
}