182 lines
7.4 KiB
C++
182 lines
7.4 KiB
C++
// OpcUaPanel.cpp
|
|||
|
|
#include "OpcUaPanel.h"
|
||
|
|
#include "ui_MainWindow.h"
|
||
|
|
#include "VisionGuideMatcher.h"
|
||
|
|
#include "StereoPoseFusion.h"
|
||
|
|
#include "AppSettings.h"
|
||
|
|
#include <QGroupBox>
|
||
|
|
#include <QVBoxLayout>
|
||
|
|
#include <QHBoxLayout>
|
||
|
|
#include <QFormLayout>
|
||
|
|
#include <QPushButton>
|
||
|
|
#include <QLabel>
|
||
|
|
#include <QLineEdit>
|
||
|
|
#include <QSpinBox>
|
||
|
|
#include <QScrollArea>
|
||
|
|
#include <QFrame>
|
||
|
|
|
||
|
|
OpcUaPanel::OpcUaPanel(Ui::MainWindowClass& u, OpcUaCommManager* opcua,
|
||
|
|
QWidget* parentWidget, QObject* parent)
|
||
|
|
: QObject(parent), ui(u), m_opcua(opcua)
|
||
|
|
{
|
||
|
|
buildTab(parentWidget);
|
||
|
|
|
||
|
|
connect(m_btnStart, &QPushButton::clicked, this, &OpcUaPanel::onStartServer);
|
||
|
|
connect(m_btnStop, &QPushButton::clicked, this, &OpcUaPanel::onStopServer);
|
||
|
|
connect(m_btnConnect, &QPushButton::clicked, this, &OpcUaPanel::onConnectRobot);
|
||
|
|
connect(m_btnDisconn, &QPushButton::clicked, this, &OpcUaPanel::onDisconnectRobot);
|
||
|
|
connect(m_opcua, &OpcUaCommManager::measurementRequested,
|
||
|
|
this, [this](uint32_t t){ emit measureRequested(t); }, Qt::QueuedConnection);
|
||
|
|
connect(m_opcua, &OpcUaCommManager::resetRequested,
|
||
|
|
this, [this]{ emit resetRequested(); }, Qt::QueuedConnection);
|
||
|
|
connect(m_opcua, &OpcUaCommManager::logMessage,
|
||
|
|
this, &OpcUaPanel::onOpcLog, Qt::QueuedConnection);
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::buildTab(QWidget* parentWidget)
|
||
|
|
{
|
||
|
|
auto* tab = new QWidget;
|
||
|
|
auto* tabLay = new QVBoxLayout(tab);
|
||
|
|
tabLay->setContentsMargins(0, 0, 0, 0);
|
||
|
|
auto* scrollArea = new QScrollArea;
|
||
|
|
scrollArea->setWidgetResizable(true);
|
||
|
|
scrollArea->setFrameShape(QFrame::NoFrame);
|
||
|
|
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||
|
|
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||
|
|
auto* scrollContent = new QWidget;
|
||
|
|
auto* mainLay = new QVBoxLayout(scrollContent);
|
||
|
|
mainLay->setContentsMargins(8, 8, 8, 8);
|
||
|
|
mainLay->setSpacing(8);
|
||
|
|
scrollArea->setWidget(scrollContent);
|
||
|
|
tabLay->addWidget(scrollArea);
|
||
|
|
|
||
|
|
// ── 服务端 ──
|
||
|
|
auto* svrGrp = new QGroupBox(QString::fromUtf8("OPC UA 服务端 (双目视觉)"));
|
||
|
|
auto* svrLay = new QVBoxLayout(svrGrp);
|
||
|
|
auto* row1 = new QHBoxLayout;
|
||
|
|
row1->addWidget(new QLabel(QString::fromUtf8("端口:")));
|
||
|
|
auto& settings = AppSettings::instance();
|
||
|
|
m_spinPort = new QSpinBox; m_spinPort->setRange(1,65535);
|
||
|
|
m_spinPort->setValue(settings.getRaw(QStringLiteral("opcua/serverPort"), QStringLiteral("4840")).toInt());
|
||
|
|
row1->addWidget(m_spinPort);
|
||
|
|
m_btnStart = new QPushButton(QString::fromUtf8("启动服务"));
|
||
|
|
m_btnStop = new QPushButton(QString::fromUtf8("停止服务")); m_btnStop->setEnabled(false);
|
||
|
|
row1->addWidget(m_btnStart); row1->addWidget(m_btnStop); row1->addStretch();
|
||
|
|
svrLay->addLayout(row1);
|
||
|
|
m_lblSvrStat = new QLabel(QString::fromUtf8("● 未启动"));
|
||
|
|
m_lblSvrStat->setStyleSheet("color:gray;");
|
||
|
|
svrLay->addWidget(m_lblSvrStat);
|
||
|
|
mainLay->addWidget(svrGrp);
|
||
|
|
|
||
|
|
// ── 客户端 ──
|
||
|
|
auto* cliGrp = new QGroupBox(QString::fromUtf8("OPC UA 客户端 (连接机械臂)"));
|
||
|
|
auto* cliLay = new QVBoxLayout(cliGrp);
|
||
|
|
auto* row2 = new QHBoxLayout;
|
||
|
|
row2->addWidget(new QLabel(QString::fromUtf8("机械臂地址:")));
|
||
|
|
m_editUrl = new QLineEdit(settings.getRaw(QStringLiteral("opcua/robotUrl"), QStringLiteral("opc.tcp://192.168.1.100:4840")));
|
||
|
|
row2->addWidget(m_editUrl);
|
||
|
|
m_btnConnect = new QPushButton(QString::fromUtf8("连接"));
|
||
|
|
m_btnDisconn = new QPushButton(QString::fromUtf8("断开")); m_btnDisconn->setEnabled(false);
|
||
|
|
row2->addWidget(m_btnConnect); row2->addWidget(m_btnDisconn);
|
||
|
|
cliLay->addLayout(row2);
|
||
|
|
m_lblCliStat = new QLabel(QString::fromUtf8("● 未连接"));
|
||
|
|
m_lblCliStat->setStyleSheet("color:gray;");
|
||
|
|
cliLay->addWidget(m_lblCliStat);
|
||
|
|
mainLay->addWidget(cliGrp);
|
||
|
|
|
||
|
|
// ── 状态 ──
|
||
|
|
auto* statGrp = new QGroupBox(QString::fromUtf8("通信状态"));
|
||
|
|
auto* statLay = new QFormLayout(statGrp);
|
||
|
|
m_lblState = new QLabel("Idle (0)");
|
||
|
|
m_lblResult = new QLabel("None (0)");
|
||
|
|
m_lblPose = new QLabel("---");
|
||
|
|
statLay->addRow(QString::fromUtf8("系统状态:"), m_lblState);
|
||
|
|
statLay->addRow(QString::fromUtf8("测量结果:"), m_lblResult);
|
||
|
|
statLay->addRow(QString::fromUtf8("当前位姿:"), m_lblPose);
|
||
|
|
mainLay->addWidget(statGrp);
|
||
|
|
|
||
|
|
mainLay->addStretch();
|
||
|
|
|
||
|
|
ui.tabWidget->addTab(tab, QString::fromUtf8("通信"));
|
||
|
|
connect(m_spinPort, QOverload<int>::of(&QSpinBox::valueChanged), this, [](int port) {
|
||
|
|
AppSettings::instance().set(QStringLiteral("opcua/serverPort"), QString::number(port));
|
||
|
|
});
|
||
|
|
connect(m_editUrl, &QLineEdit::textChanged, this, [](const QString& url) {
|
||
|
|
AppSettings::instance().set(QStringLiteral("opcua/robotUrl"), url);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::onStartServer()
|
||
|
|
{
|
||
|
|
uint16_t port = m_spinPort->value();
|
||
|
|
if (m_opcua->startServer(port)) {
|
||
|
|
m_btnStart->setEnabled(false); m_btnStop->setEnabled(true);
|
||
|
|
m_lblSvrStat->setText(QString::fromUtf8("● 已启动 (端口: %1)").arg(port));
|
||
|
|
m_lblSvrStat->setStyleSheet("color:#00cc66;");
|
||
|
|
emit logMessage(QString::fromUtf8("[OPC UA] 服务端已启动, 端口: %1").arg(port));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::onStopServer()
|
||
|
|
{
|
||
|
|
m_opcua->stopServer();
|
||
|
|
m_btnStart->setEnabled(true); m_btnStop->setEnabled(false);
|
||
|
|
m_lblSvrStat->setText(QString::fromUtf8("● 未启动"));
|
||
|
|
m_lblSvrStat->setStyleSheet("color:gray;");
|
||
|
|
emit logMessage(QString::fromUtf8("[OPC UA] 服务端已停止"));
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::onConnectRobot()
|
||
|
|
{
|
||
|
|
if (m_opcua->connectToRobot(m_editUrl->text())) {
|
||
|
|
m_btnConnect->setEnabled(false); m_btnDisconn->setEnabled(true);
|
||
|
|
m_lblCliStat->setText(QString::fromUtf8("● 已连接: %1").arg(m_editUrl->text()));
|
||
|
|
m_lblCliStat->setStyleSheet("color:#00cc66;");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::onDisconnectRobot()
|
||
|
|
{
|
||
|
|
m_opcua->disconnectFromRobot();
|
||
|
|
m_btnConnect->setEnabled(true); m_btnDisconn->setEnabled(false);
|
||
|
|
m_lblCliStat->setText(QString::fromUtf8("● 未连接"));
|
||
|
|
m_lblCliStat->setStyleSheet("color:gray;");
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::onOpcLog(const QString& msg)
|
||
|
|
{
|
||
|
|
emit logMessage(msg);
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpcUaPanel::handleMeasureResult(bool success,
|
||
|
|
const VisionPoseResult& camPose, const VisionPoseResult& robotPose,
|
||
|
|
bool robotReady, const QString& partClass,
|
||
|
|
const StereoPoseFusion::FusionResult& /*fusion*/)
|
||
|
|
{
|
||
|
|
PoseData pose;
|
||
|
|
MeasureResultCode code = MeasureResultCode::None;
|
||
|
|
|
||
|
|
if (success && camPose.valid) {
|
||
|
|
code = MeasureResultCode::Success;
|
||
|
|
const auto& src = robotReady ? robotPose : camPose;
|
||
|
|
pose.X = float(src.tx*1000); pose.Y = float(src.ty*1000); pose.Z = float(src.tz*1000);
|
||
|
|
pose.Rx = float(src.rx); pose.Ry = float(src.ry); pose.Rz = float(src.rz);
|
||
|
|
pose.valid = true;
|
||
|
|
if (partClass.contains(QString::fromUtf8("镀金")))
|
||
|
|
pose.partType = uint32_t(PartType::Gold);
|
||
|
|
else if (partClass.contains(QString::fromUtf8("非镀金")))
|
||
|
|
pose.partType = uint32_t(PartType::NonGold);
|
||
|
|
} else {
|
||
|
|
code = MeasureResultCode::Failed_PoseCalculation;
|
||
|
|
}
|
||
|
|
|
||
|
|
m_opcua->publishMeasureResult(code, pose,
|
||
|
|
camPose.valid ? "" : camPose.errorMsg);
|
||
|
|
|
||
|
|
m_lblState->setText(success ? "MeasureSuccess (2)" : "MeasureFailed (3)");
|
||
|
|
m_lblResult->setText(QString("Code=%1").arg(int(code)));
|
||
|
|
m_lblPose->setText(pose.valid
|
||
|
|
? QString("X=%1 Y=%2 Z=%3").arg(pose.X,0,'f',2).arg(pose.Y,0,'f',2).arg(pose.Z,0,'f',2)
|
||
|
|
: "---");
|
||
|
|
}
|