添加管控软件和相机的通讯

This commit is contained in:
lidg
2026-10-27 22:14:53 +08:00
parent c8cd533d9b
commit 069eb902ed
21 changed files with 572 additions and 26 deletions
+103 -3
View File
@@ -22,6 +22,7 @@
#include "GroovePanel.h"
#include "VisionGuidanceCapturePanel.h"
#include "MeasurementOptionsPanel.h"
#include "VisionIpcServer.h"
#include "StereoCalibDialog.h"
#include "HandEyeCalibDialog.h"
@@ -38,6 +39,7 @@
#include <QCoreApplication>
#include <QTimer>
#include <QMetaObject>
#include <QThread>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
{
@@ -90,7 +92,12 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
connect(m_panelMeasurementOptions,&MeasurementOptionsPanel::busyChanged,this,[this](bool busy){
for(int i=0;i<ui.tabWidget->count();++i)ui.tabWidget->setTabEnabled(i,!busy||ui.tabWidget->widget(i)==m_panelMeasurementOptions);
ui.actionStereoCalib->setEnabled(!busy);ui.actionHandEyeCalib->setEnabled(!busy);ui.actionShapeModelGen->setEnabled(!busy);
updateRemoteLock();updateIpcState();
});
connect(m_panelMeasurementOptions,&MeasurementOptionsPanel::remoteFinished,this,&MainWindow::ipcFinished);
connect(m_panelMeas,&MeasurePanel::measureCompleted,this,[this](bool){QTimer::singleShot(0,this,[this]{updateRemoteLock();updateIpcState();});});
connect(m_panelVisionGuide,&VisionGuidePanel::busyChanged,this,[this](bool){updateRemoteLock();updateIpcState();});
connect(m_panelYolo,&YoloPanel::busyChanged,this,[this](bool){updateRemoteLock();updateIpcState();});
// 日志汇聚
connect(m_panelCam, &CameraPanel::logMessage, this, &MainWindow::appendLog);
@@ -107,9 +114,12 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
connect(m_panelOpc, &OpcUaPanel::logMessage, this, &MainWindow::appendLog);
connect(m_fusion, &StereoPoseFusion::logMessage, this, &MainWindow::appendLog);
connect(m_camMgr, &CameraManager::cameraConnected, this,
[this](CameraManager::CamIndex){ m_panelMeas->updateActionButtons(); });
[this](CameraManager::CamIndex){ m_panelMeas->updateActionButtons();updateIpcState(); });
connect(m_camMgr, &CameraManager::cameraDisconnected, this,
[this](CameraManager::CamIndex){ m_panelMeas->updateActionButtons(); });
[this](CameraManager::CamIndex){ m_panelMeas->updateActionButtons();updateIpcState(); });
// CameraPanel invalidates its initialization first; publish that new state even
// when no measurement is running and the controller only polls cached status.
connect(m_camMgr,&CameraManager::errorOccurred,this,[this](int,const QString&){updateIpcState();},Qt::QueuedConnection);
// 菜单
connect(ui.btnClearLog, &QPushButton::clicked, this, &MainWindow::onClearLog);
@@ -124,13 +134,98 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
connect(m_panelOpc, &OpcUaPanel::measureRequested, this, &MainWindow::onOpcMeasureRequested);
connect(m_panelOpc, &OpcUaPanel::resetRequested, this, &MainWindow::onOpcResetRequested);
// Native/file/calibration dialogs run nested event loops. Refresh on their exit as
// well as entry, even when they did not belong to a particular panel's busy signal.
auto modalWatch=new QTimer(this);modalWatch->setInterval(200);
connect(modalWatch,&QTimer::timeout,this,[this]{
if(m_modalObserved!=(QApplication::activeModalWidget()!=nullptr)){updateRemoteLock();updateIpcState();}
});
modalWatch->start();
// ★ 延迟加载: 窗口先显示, 模型后台加载, 启动秒开
QTimer::singleShot(0, this, &MainWindow::autoLoadModels);
appendLog(QString::fromUtf8("系统已启动"));
}
MainWindow::~MainWindow() { delete m_panelMeasurementOptions; m_panelMeasurementOptions=nullptr; }
MainWindow::~MainWindow() {
if(m_ipcThread){QMetaObject::invokeMethod(m_ipc,"close",Qt::BlockingQueuedConnection);m_ipcThread->quit();m_ipcThread->wait();}
delete m_panelMeasurementOptions;m_panelMeasurementOptions=nullptr;
}
void MainWindow::startIpc(const QString& name)
{
if(m_ipcThread)return;
m_ipcThread=new QThread(this);m_ipc=new VisionIpcServer;m_ipc->moveToThread(m_ipcThread);
connect(m_ipcThread,&QThread::finished,m_ipc,&QObject::deleteLater);
connect(m_ipc,&VisionIpcServer::commandReceived,this,&MainWindow::onIpcCommand,Qt::QueuedConnection);
connect(this,&MainWindow::ipcStateChanged,m_ipc,&VisionIpcServer::setBackendState,Qt::QueuedConnection);
connect(this,&MainWindow::ipcFinished,m_ipc,&VisionIpcServer::finish,Qt::QueuedConnection);
connect(m_ipc,&VisionIpcServer::controllerChanged,this,[this](bool attached){m_controllerAttached=attached;updateRemoteLock();updateIpcState();},Qt::QueuedConnection);
connect(m_ipc,&VisionIpcServer::fatalError,this,[this](const QString& error){qCritical("IPC listen failed: %s",qPrintable(error));QCoreApplication::exit(4);},Qt::QueuedConnection);
m_ipcThread->start();QMetaObject::invokeMethod(m_ipc,[this,name]{m_ipc->listen(name);},Qt::QueuedConnection);
updateIpcState();
}
void MainWindow::updateRemoteLock()
{
m_modalObserved=QApplication::activeModalWidget()!=nullptr;
// Modality itself already blocks user input; leave native/file dialogs enabled.
// It still counts as busy for incoming commands in resourceBusy().
const bool locked=m_controllerAttached||m_initializing||m_panelMeasurementOptions->isBusy()||
m_panelMeas->isBusy()||m_panelVisionGuide->isBusy()||m_panelYolo->isBusy();
m_panelVisionGuide->setResourceBlocked(locked);m_panelYolo->setResourceBlocked(locked);
ui.centralWidget->setEnabled(!locked);
ui.actionStereoCalib->setEnabled(!locked);ui.actionHandEyeCalib->setEnabled(!locked);ui.actionShapeModelGen->setEnabled(!locked);
}
bool MainWindow::resourceBusy() const
{
return m_initializing||m_panelMeasurementOptions->isBusy()||m_panelMeas->isBusy()||
m_panelVisionGuide->isBusy()||m_panelYolo->isBusy()||QApplication::activeModalWidget();
}
void MainWindow::updateIpcState()
{
m_modalObserved=QApplication::activeModalWidget()!=nullptr;
if(!m_ipc)return;
QString error;
const bool ready=m_panelCam->remoteInitialized()&&m_panelMeasurementOptions->remoteReady(&error);
emit ipcStateChanged(QJsonObject{{"ready",ready},{"resource_busy",resourceBusy()},
{"initialization",m_initialization},{"message",error}});
}
void MainWindow::onIpcCommand(const QJsonObject& request)
{
const auto type=request["type"].toString();
if(type=="show_window"){show();raise();activateWindow();return;}
if(type=="cancel"){m_panelMeasurementOptions->cancelRemote();return;}
if(resourceBusy()){
emit ipcFinished(QJsonObject{{"type",type},{"request_id",request["request_id"]},{"context",request["context"]},{"status","error"},{"error_code","BUSY"},{"message","A local operation is already active"}});updateIpcState();return;
}
if(type=="shutdown"){
emit ipcFinished(QJsonObject{{"type",type},{"request_id",request["request_id"]},{"status","ok"}});
QCoreApplication::quit();return;
}
if(type=="initialize"){
m_initializing=true;updateRemoteLock();updateIpcState();
try {m_initialization=m_panelCam->initializeRemote();}
catch(const std::exception& e){m_initialization={{"initialized",false},{"error",QString::fromUtf8(e.what())}};}
catch(...){m_initialization={{"initialized",false},{"error","Unhandled camera initialization failure"}};}
QString error;const bool ready=m_panelCam->remoteInitialized()&&m_panelMeasurementOptions->remoteReady(&error);
m_initializing=false;
// Keep the transport's request active until its terminal response has been delivered.
emit ipcFinished(QJsonObject{{"type",type},{"request_id",request["request_id"]},{"status",ready?"ok":"error"},{"ready",ready},
{"initialization",m_initialization},{"error_code",ready?"":"INITIALIZATION"},{"message",error.isEmpty()&&!ready?QStringLiteral("Camera initialization incomplete"):error}});
updateRemoteLock();updateIpcState();return;
}
if(type=="measure"){
if(!m_panelCam->remoteInitialized()){
emit ipcFinished(QJsonObject{{"type",type},{"request_id",request["request_id"]},{"context",request["context"]},{"status","error"},{"error_code","NOT_INITIALIZED"},{"message","Initialize both cameras first"}});
}else m_panelMeasurementOptions->startRemote(request);
updateRemoteLock();updateIpcState();
}
}
void MainWindow::autoLoadModels()
{
@@ -193,6 +288,7 @@ void MainWindow::onAbout()
// ── 工具菜单 ──────────────────────────────────────────────────
void MainWindow::onStereoCalibration()
{
if(m_controllerAttached||resourceBusy())return;
StereoCalibDialog dlg(m_camMgr, this);
if (dlg.exec() == QDialog::Accepted && dlg.hasResult())
appendLog(QString::fromUtf8("[双目标定] 标定完成"));
@@ -200,6 +296,7 @@ void MainWindow::onStereoCalibration()
void MainWindow::onHandEyeCalibration()
{
if(m_controllerAttached||resourceBusy())return;
HandEyeCalibDialog dlg(m_camMgr, this);
connect(&dlg, &HandEyeCalibDialog::calibrationDone,
[this](const QString& f){ m_panelCoord->loadFromFile(f); });
@@ -208,6 +305,7 @@ void MainWindow::onHandEyeCalibration()
void MainWindow::onShapeModelGenerator()
{
if(m_controllerAttached||resourceBusy())return;
ShapeModelGenDialog dlg(this);
connect(&dlg, &ShapeModelGenDialog::modelApplied,
[this](const QString& leftPath, const QString& rightPath){
@@ -231,6 +329,7 @@ void MainWindow::onShapeModelGenerator()
// ── OPC-UA ───────────────────────────────────────────────────
void MainWindow::onOpcMeasureRequested(uint32_t timeoutMs)
{
if(m_controllerAttached||resourceBusy()){appendLog(QString::fromUtf8("[OPC UA] 视觉由 ssp 控制或正在运行,拒绝并发测量。"));return;}
appendLog(QString::fromUtf8("[OPC UA] 收到测量指令, timeout=%1ms").arg(timeoutMs));
m_opcua->server()->setSystemState(VisionSystemState::Measuring);
auto* doneConn = new QMetaObject::Connection;
@@ -248,6 +347,7 @@ void MainWindow::onOpcMeasureRequested(uint32_t timeoutMs)
void MainWindow::onOpcResetRequested()
{
if(m_controllerAttached||resourceBusy())return;
m_opcua->resetToIdle();
appendLog(QString::fromUtf8("[OPC UA] 系统已重置为Idle"));
}