添加管控软件和相机的通讯
This commit is contained in:
@@ -13,6 +13,7 @@ DEFKEY(K_ShapeModel, "paths/shapeModel"); // 兼容旧版(单模板)
|
||||
DEFKEY(K_ShapeModelLeft, "paths/shapeModelLeft"); // 左相机轮廓库
|
||||
DEFKEY(K_ShapeModelRight, "paths/shapeModelRight"); // 右相机轮廓库
|
||||
DEFKEY(K_ExportDir, "paths/exportDir");
|
||||
|
||||
DEFKEY(K_Caltab, "paths/caltab");
|
||||
DEFKEY(K_CalibSaveDir, "paths/calibSaveDir");
|
||||
DEFKEY(K_CalibImageDir, "paths/calibImageDir");
|
||||
|
||||
@@ -12,6 +12,12 @@
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using namespace VisionGuideApi;
|
||||
namespace {
|
||||
struct Handle {
|
||||
|
||||
@@ -329,3 +329,14 @@ bool CameraManager::applyWhiteBalanceOnce(CamIndex idx)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CameraManager::whiteBalanceFinished(CamIndex idx, bool& finished)
|
||||
{
|
||||
finished=false;
|
||||
if(!m_connected[idx]||m_device[idx].IsNull())return false;
|
||||
try {
|
||||
const auto value=m_device[idx]->GetRemoteFeatureControl()->GetEnumFeature("BalanceWhiteAuto")->GetValue();
|
||||
finished=gxToQString(value)==QStringLiteral("Off");
|
||||
return true;
|
||||
}catch(CGalaxyException& e){emit errorOccurred(idx,QString("Read white balance state failed: %1").arg(e.what()));return false;}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ public:
|
||||
// Run the camera's one-shot automatic white-balance procedure.
|
||||
// The camera performs the adjustment and returns to its normal state.
|
||||
bool applyWhiteBalanceOnce(CamIndex idx);
|
||||
bool whiteBalanceFinished(CamIndex idx, bool& finished);
|
||||
|
||||
signals:
|
||||
void leftFrameReady(const QImage& img);
|
||||
|
||||
+57
-16
@@ -5,6 +5,7 @@
|
||||
#include "AppSettings.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QComboBox>
|
||||
#include <QElapsedTimer>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
|
||||
#define CN_LEFT_CAM "左相机"
|
||||
@@ -71,6 +72,8 @@ CameraPanel::CameraPanel(Ui::MainWindowClass& u, CameraManager* mgr, QObject* pa
|
||||
ui.btnLeftFreeze->setText(QString::fromUtf8(CN_FREEZE));
|
||||
ui.btnRightFreeze->setText(QString::fromUtf8(CN_FREEZE));
|
||||
updateStates();
|
||||
connect(m_mgr,&CameraManager::cameraDisconnected,this,[this](CameraManager::CamIndex index){m_remoteInitialized[index]=false;});
|
||||
connect(m_mgr,&CameraManager::errorOccurred,this,[this](int index,const QString&){if(index>=0&&index<2)m_remoteInitialized[index]=false;});
|
||||
}
|
||||
|
||||
// ── 外部采集接口(MeasurePanel / OPC-UA 调用)────────────────────────
|
||||
@@ -230,32 +233,70 @@ void CameraPanel::onApplyParams() {
|
||||
double gain = ui.spinGain->value();
|
||||
int target = m_comboCamTarget->currentData().toInt();
|
||||
bool bwLimit = ui.chkBandwidthLimit->isChecked();
|
||||
QString bwMode = bwLimit ? "On" : "Off";
|
||||
|
||||
auto& settings = AppSettings::instance();
|
||||
settings.set(QStringLiteral("camera/exposureUs"), QString::number(exposure, 'f', 0));
|
||||
settings.set(QStringLiteral("camera/gain"), QString::number(gain, 'f', 3));
|
||||
settings.set(QStringLiteral("camera/bandwidthLimit"), bwLimit ? QStringLiteral("1") : QStringLiteral("0"));
|
||||
|
||||
auto apply = [&](CameraManager::CamIndex ci) {
|
||||
if (!m_mgr->isConnected(ci)) return;
|
||||
m_mgr->setExposureTime(ci, exposure);
|
||||
m_mgr->setGain(ci, gain);
|
||||
try {
|
||||
auto fc = m_mgr->getDevice(ci)->GetRemoteFeatureControl();
|
||||
fc->GetIntFeature("DeviceLinkSelector")->SetValue(0);
|
||||
fc->GetEnumFeature("DeviceLinkThroughputLimitMode")
|
||||
->SetValue(bwMode.toStdString().c_str());
|
||||
} catch (...) {
|
||||
emit logMessage(QString::fromUtf8("[Camera] 带宽限制模式设置失败"));
|
||||
}
|
||||
};
|
||||
if (target == 0 || target == 2) apply(CameraManager::LEFT);
|
||||
if (target == 1 || target == 2) apply(CameraManager::RIGHT);
|
||||
if (target == 0 || target == 2) {m_remoteInitialized[0]=false;applyParameters(CameraManager::LEFT);}
|
||||
if (target == 1 || target == 2) {m_remoteInitialized[1]=false;applyParameters(CameraManager::RIGHT);}
|
||||
emit logMessage(QString("Params: Exp=%1 Gain=%2")
|
||||
.arg(exposure).arg(gain));
|
||||
}
|
||||
|
||||
bool CameraPanel::applyParameters(int cameraIndex)
|
||||
{
|
||||
const auto index=static_cast<CameraManager::CamIndex>(cameraIndex);
|
||||
if(!m_mgr->isConnected(index))return false;
|
||||
const bool exposure=m_mgr->setExposureTime(index,ui.spinExposure->value());
|
||||
const bool gain=m_mgr->setGain(index,ui.spinGain->value());
|
||||
bool bandwidth=false;
|
||||
try {
|
||||
auto features=m_mgr->getDevice(index)->GetRemoteFeatureControl();
|
||||
features->GetIntFeature("DeviceLinkSelector")->SetValue(0);
|
||||
features->GetEnumFeature("DeviceLinkThroughputLimitMode")->SetValue(ui.chkBandwidthLimit->isChecked()?"On":"Off");
|
||||
bandwidth=true;
|
||||
}catch(...){emit logMessage(QString::fromUtf8("[Camera] 带宽限制模式设置失败"));}
|
||||
return exposure&&gain&&bandwidth;
|
||||
}
|
||||
|
||||
bool CameraPanel::remoteInitialized() const
|
||||
{
|
||||
return m_remoteInitialized[0]&&m_remoteInitialized[1]&&leftConnected()&&rightConnected();
|
||||
}
|
||||
|
||||
QJsonObject CameraPanel::initializeRemote()
|
||||
{
|
||||
QJsonObject result;
|
||||
for(int i=0;i<2;++i){
|
||||
const auto index=static_cast<CameraManager::CamIndex>(i);
|
||||
QJsonObject state;
|
||||
if(!m_mgr->isConnected(index))m_remoteInitialized[i]=false;
|
||||
if(m_remoteInitialized[i]){state={{"connected",true},{"parameters_applied",true},{"white_balance_finished",true},{"reused",true}};}
|
||||
else {
|
||||
const bool connected=m_mgr->isConnected(index)||m_mgr->connectCamera(index);
|
||||
state["connected"]=connected;
|
||||
const bool parameters=connected&&applyParameters(i);state["parameters_applied"]=parameters;
|
||||
bool finished=false;
|
||||
// Once must have been submitted successfully in this connection generation.
|
||||
// A fresh frame drives camera-side AWB; only its subsequent Off state confirms completion.
|
||||
if(parameters&&m_mgr->applyWhiteBalanceOnce(index)){
|
||||
QElapsedTimer deadline;deadline.start();
|
||||
while(deadline.elapsed()<10000){
|
||||
if(m_mgr->captureSingleFrame(index).isNull())break;
|
||||
if(!m_mgr->whiteBalanceFinished(index,finished))break;
|
||||
if(finished)break;
|
||||
}
|
||||
}
|
||||
state["white_balance_finished"]=finished;m_remoteInitialized[i]=connected&¶meters&&finished;
|
||||
if(!m_remoteInitialized[i])state["error"]=QStringLiteral("Camera connection, parameters or one-shot white balance incomplete");
|
||||
}
|
||||
result[i==0?"left":"right"]=state;
|
||||
}
|
||||
updateStates();result["initialized"]=remoteInitialized();return result;
|
||||
}
|
||||
|
||||
void CameraPanel::onApplyWhiteBalance()
|
||||
{
|
||||
const int target = m_comboCamTarget->currentData().toInt();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
#include <QObject>
|
||||
#include <QImage>
|
||||
#include <QJsonObject>
|
||||
namespace Ui { class MainWindowClass; }
|
||||
class CameraManager;
|
||||
class QComboBox;
|
||||
@@ -19,6 +20,8 @@ public:
|
||||
bool rightConnected() const;
|
||||
bool leftPreviewing() const { return m_lPrev; }
|
||||
bool rightPreviewing() const { return m_rPrev; }
|
||||
QJsonObject initializeRemote();
|
||||
bool remoteInitialized() const;
|
||||
|
||||
signals:
|
||||
void logMessage(const QString& msg);
|
||||
@@ -38,10 +41,12 @@ private slots:
|
||||
|
||||
private:
|
||||
void updateStates();
|
||||
bool applyParameters(int cameraIndex);
|
||||
Ui::MainWindowClass& ui;
|
||||
CameraManager* m_mgr;
|
||||
QComboBox* m_comboCamTarget = nullptr;
|
||||
bool m_lPrev = false, m_rPrev = false;
|
||||
bool m_lFrozen = false, m_rFrozen = false;
|
||||
QImage m_lastLeft, m_lastRight;
|
||||
bool m_remoteInitialized[2] = {false,false};
|
||||
};
|
||||
|
||||
+103
-3
@@ -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"));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include "ui_MainWindow.h"
|
||||
#include "StereoPoseFusion.h"
|
||||
#include <QJsonObject>
|
||||
class VisionIpcServer;
|
||||
class QThread;
|
||||
class CameraManager;
|
||||
class LightController;
|
||||
class YoloDetector;
|
||||
@@ -27,6 +30,10 @@ class MainWindow : public QMainWindow
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
~MainWindow();
|
||||
void startIpc(const QString& name);
|
||||
signals:
|
||||
void ipcStateChanged(const QJsonObject& state);
|
||||
void ipcFinished(const QJsonObject& result);
|
||||
private slots:
|
||||
void appendLog(const QString& msg);
|
||||
void onClearLog();
|
||||
@@ -39,8 +46,18 @@ private slots:
|
||||
// OPC-UA 测量请求
|
||||
void onOpcMeasureRequested(uint32_t timeoutMs);
|
||||
void onOpcResetRequested();
|
||||
void onIpcCommand(const QJsonObject& request);
|
||||
private:
|
||||
void autoLoadModels();
|
||||
void updateIpcState();
|
||||
void updateRemoteLock();
|
||||
bool resourceBusy() const;
|
||||
VisionIpcServer* m_ipc = nullptr;
|
||||
QThread* m_ipcThread = nullptr;
|
||||
bool m_controllerAttached = false;
|
||||
bool m_initializing = false;
|
||||
bool m_modalObserved = false;
|
||||
QJsonObject m_initialization;
|
||||
Ui::MainWindowClass ui;
|
||||
// ── 后端对象(MainWindow 拥有生命周期) ──
|
||||
CameraManager* m_camMgr = nullptr;
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <QPointer>
|
||||
#include <QTimer>
|
||||
#include <QCoreApplication>
|
||||
#include <QSignalBlocker>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
@@ -345,8 +346,8 @@ void MeasurementOptionsPanel::start() {
|
||||
s.finalPose=selected.pose;s.finalSide=CAM_LEFT;s.valid=selected.pose.valid;record["timings_ms"]=timings;
|
||||
if(self)QMetaObject::invokeMethod(self.data(),[self,s,record,selected,generation,clock,deadlineMs]()mutable{if(!self||self->m_generation!=generation)return;auto times=record["timings_ms"].toObject();if(clock->elapsed()>=deadlineMs){s.valid=false;s.finalPose.valid=false;selected.pose.valid=false;selected.source="NONE";selected.error=QString::fromUtf8("位姿发布前已超时。");record["timed_out"]=true;times["pose_ready_ms"]=QJsonValue();}else if(s.valid)times["pose_ready_ms"]=static_cast<double>(clock->elapsed());record["timings_ms"]=times;self->complete(s,record,selected);},Qt::QueuedConnection);
|
||||
});
|
||||
m_worker=worker;connect(worker,&QThread::finished,this,[this,worker]{if(m_worker==worker)m_worker=nullptr;m_legacy->endExternalMeasurement();setBusy(false);worker->deleteLater();});worker->start();
|
||||
QTimer::singleShot(std::max(0,deadlineMs-int(clock->elapsed())),Qt::PreciseTimer,this,[this,generation,deadlineMs]{if(!m_busy||m_generation!=generation||m_record["generation"].toDouble()==static_cast<double>(generation))return;m_timedOut=true;m_status->setText(QString::fromUtf8("达到%1秒截止,返回失败;等待视觉引导算子退出…").arg(deadlineMs/1000));m_snapshot.valid=false;m_draw->setEnabled(false);m_grasp->setEnabled(false);emit logMessage(QString::fromUtf8("[测量][超时] 迟到结果将丢弃;算子退出前保持资源锁。"));});
|
||||
m_worker=worker;connect(worker,&QThread::finished,this,[this,worker]{if(m_worker==worker)m_worker=nullptr;m_legacy->endExternalMeasurement();setBusy(false);m_remoteRequest={};worker->deleteLater();});worker->start();
|
||||
QTimer::singleShot(std::max(0,deadlineMs-int(clock->elapsed())),Qt::PreciseTimer,this,[this,generation,deadlineMs]{if(!m_busy||m_generation!=generation||m_record["generation"].toDouble()==static_cast<double>(generation))return;m_timedOut=true;m_status->setText(QString::fromUtf8("达到%1秒截止,返回失败;等待视觉引导算子退出…").arg(deadlineMs/1000));m_snapshot.valid=false;m_draw->setEnabled(false);m_grasp->setEnabled(false);remoteFailure("DEADLINE","Measurement deadline reached; waiting for the operator to exit");emit logMessage(QString::fromUtf8("[测量][超时] 迟到结果将丢弃;算子退出前保持资源锁。"));});
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::complete(MeasurementCaptureSnapshot snapshot,QJsonObject record,const MeasurementSelection& selected){
|
||||
@@ -358,10 +359,118 @@ void MeasurementOptionsPanel::complete(MeasurementCaptureSnapshot snapshot,QJson
|
||||
auto metrics=m_record["fusion"].toObject();metrics["position_limit_mm"]=1.0;metrics["reprojection_checked"]=snapshot.fusion.reprojectionChecked;metrics["reprojection_passed"]=snapshot.fusion.reprojectionPassed;metrics["left_reprojection_error_px"]=snapshot.fusion.leftReprojectionErrorPx;metrics["right_reprojection_error_px"]=snapshot.fusion.rightReprojectionErrorPx;metrics["reprojection_limit_px"]=snapshot.fusion.reprojectionLimitPx;m_record["fusion"]=metrics;
|
||||
if(!snapshot.rightImage.isNull())emit logMessage(QString::fromUtf8("[测量][双目] %1").arg(snapshot.fusion.message));
|
||||
if(!snapshot.valid)emit logMessage(QString::fromUtf8("[测量][失败原因] %1;左路:%2;右路:%3").arg(record["error"].toString(),snapshot.leftPose.errorMsg,snapshot.rightPose.errorMsg));
|
||||
// Publish structured values before drawing/archiving; neither may trigger another capture.
|
||||
if(!m_remoteRequest.isEmpty())completeRemote(snapshot,m_record);
|
||||
if(snapshot.leftPose.valid||snapshot.rightPose.valid)renderResult();
|
||||
writeArchive(m_record,m_snapshot,QImage(),false);
|
||||
}
|
||||
|
||||
bool MeasurementOptionsPanel::remoteReady(QString* error) const
|
||||
{
|
||||
try {
|
||||
if(!m_camera||!m_camera->leftConnected()||!m_camera->rightConnected())throw std::runtime_error("Both cameras must be connected");
|
||||
if(!m_matcher||!m_matcher->isReady(CAM_LEFT)||!m_yolo||!m_yolo->isLoaded())throw std::runtime_error("LEFT calibration/model or YOLO is not loaded");
|
||||
const auto cal=effectiveCalibration();
|
||||
if(cal["robot_rotation_rule"].toString()!="Rz(A) Ry(B) Rx(C)"||cal["robot_tool"].toString()!="TOOL1"||cal["robot_reference_frame_human_confirmed"].toString()!="world")throw std::runtime_error("Verified world/TOOL1 Rz(A)Ry(B)Rx(C) calibration required");
|
||||
jsonMatrix(cal["T_TOOL1_camera_mm"]);jsonMatrix(cal["T_grasp_TOOL1_from_one_teach_mm"]);
|
||||
robotTransform(cal["teach_reference_XYZABC_mm_deg"].toArray());
|
||||
const auto normal=cal["tray_normal_world"].toArray();
|
||||
if(normal.size()!=3)throw std::runtime_error("Tray normal missing");
|
||||
double norm=0;for(auto v:normal){if(!v.isDouble()||!std::isfinite(v.toDouble()))throw std::runtime_error("Invalid tray normal");norm+=v.toDouble()*v.toDouble();}
|
||||
if(norm<.81)throw std::runtime_error("Invalid tray normal");
|
||||
if(usesTeachPlane()){robotTransform(m_activeTeach);}
|
||||
return true;
|
||||
} catch(const std::exception& e){if(error)*error=QString::fromUtf8(e.what());return false;}
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::remoteFailure(const QString& code,const QString& message)
|
||||
{
|
||||
if(m_remoteRequest.isEmpty()||m_remoteTerminal)return;
|
||||
m_remoteTerminal=true;
|
||||
QJsonObject result{{"type","measure"},{"request_id",m_remoteRequest["request_id"]},
|
||||
{"context",m_remoteRequest["context"]},{"status",m_remoteCancelled?"cancelled":(code=="DEADLINE"?"timeout":"error")},
|
||||
{"camera_valid",false},{"robot_valid",false},{"error_code",code},{"message",message}};
|
||||
if(!m_remoteCancelled&&code=="GRASP")for(auto it=m_remoteCameraResult.begin();it!=m_remoteCameraResult.end();++it)result[it.key()]=it.value();
|
||||
emit remoteFinished(result);
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::startRemote(const QJsonObject& request)
|
||||
{
|
||||
if(!m_legacy||m_busy||m_legacy->isBusy()){
|
||||
emit remoteFinished(QJsonObject{{"type","measure"},{"request_id",request["request_id"]},{"context",request["context"]},{"status","error"},{"error_code","BUSY"},{"message","A measurement is already running"}});return;
|
||||
}
|
||||
m_remoteRequest=request;m_remoteTerminal=false;m_remoteCancelled=false;m_remoteCameraResult={};
|
||||
try {
|
||||
const auto parameters=request["parameters"].toObject();
|
||||
if(parameters["camera_mode"].toString()!="left_only"||parameters["refinement"].toString()!="least_squares_high"||parameters["deadline_ms"].toInt()!=5000||
|
||||
parameters["min_score"].toDouble()!=.70||parameters["greediness"].toDouble()!=.80||parameters["yolo_selection"].toString()!="center"||
|
||||
parameters["pose_display"].toString()!="model_origin"||parameters["grasp_offset_mm"].toDouble()!=4.40)throw std::runtime_error("Unsupported measurement parameters");
|
||||
const auto convention=request["convention"].toObject();
|
||||
if(convention["frame"].toString()!="world"||convention["tool"].toString()!="TOOL1"||convention["rotation"].toString()!="Rz(A)Ry(B)Rx(C)"||convention["linear_unit"].toString()!="mm"||convention["angular_unit"].toString()!="deg")throw std::runtime_error("Robot pose convention mismatch");
|
||||
if(!request["require_robot_pose"].toBool()||(request["pose_source"].toString()!="plc_r40"&&request["pose_source"].toString()!="diagnostic"))throw std::runtime_error("Robot pose source required");
|
||||
m_remoteRobot=request["robot_pose"].toArray();robotTransform(m_remoteRobot);
|
||||
const auto context=request["context"].toObject();
|
||||
if(context["workpiece_id"].toString().isEmpty()||context["task_id"].toString().isEmpty()||context["generation"].toString().isEmpty())throw std::runtime_error("Complete measurement context required");
|
||||
const int tray=context["tray_no"].toInt();
|
||||
if(tray<1||tray>99||context["tray_no"].toDouble()!=tray)throw std::runtime_error("Invalid tray number");
|
||||
if(tray!=m_currentTrayId){
|
||||
const auto profile=m_trayProfiles[QString::number(tray)];
|
||||
if(!profile.isObject())throw std::runtime_error("Requested tray has no saved setup");
|
||||
m_trayProfiles[QString::number(m_currentTrayId)]=traySetupState();
|
||||
restoreTraySetup(profile.toObject());m_currentTrayId=tray;
|
||||
QSignalBlocker blocker(m_traySelector);m_traySelector->setCurrentIndex(m_traySelector->findData(tray));
|
||||
}
|
||||
QString error;if(!remoteReady(&error))throw std::runtime_error(error.toUtf8().constData());
|
||||
// Freeze the exact robot pose and effective tray setup before capture. Display rounding is irrelevant.
|
||||
m_remoteCalibration=effectiveCalibration();m_remoteTeach=m_activeTeach;m_remoteHeightMethod=selectedHeightMethod();
|
||||
m_mode->setCurrentIndex(0);m_refinement->setCurrentIndex(m_refinement->findData("least_squares_high"));m_deadline->setValue(5);
|
||||
m_minScore->setValue(.70);m_greediness->setValue(.80);m_yoloSelection->setCurrentIndex(0);m_posePoint->setCurrentIndex(0);m_offset->setValue(4.4);
|
||||
m_remoteFingerprint=QString::fromLatin1(QCryptographicHash::hash(QJsonDocument(QJsonObject{{"calibration",m_remoteCalibration},{"tray",traySetupState()},{"camera_sha256",m_matcher->cameraParamSha256(CAM_LEFT)},{"model_sha256",shaFile(m_matcher->shapeModelFile(CAM_LEFT))},{"offset_mm",4.4}}).toJson(QJsonDocument::Compact),QCryptographicHash::Sha256).toHex());
|
||||
for(int i=0;i<6;++i)m_robot[i]->setText(QString::number(m_remoteRobot[i].toDouble(),'g',17));
|
||||
start();
|
||||
if(!m_busy&&!m_remoteTerminal)remoteFailure("START_FAILED","Measurement did not start");
|
||||
if(!m_busy)m_remoteRequest={};
|
||||
}catch(const VisionGuideApi::HException& e){remoteFailure("MEASURE",QString::fromLocal8Bit(e.ErrorMessage().TextA()));if(!m_worker){m_legacy->endExternalMeasurement();setBusy(false);m_remoteRequest={};}}
|
||||
catch(const std::exception& e){remoteFailure("VALIDATION",QString::fromUtf8(e.what()));if(!m_worker){m_legacy->endExternalMeasurement();setBusy(false);m_remoteRequest={};}}
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::cancelRemote()
|
||||
{
|
||||
if(m_remoteRequest.isEmpty())return;
|
||||
m_remoteCancelled=true;remoteFailure("CANCELLED","Measurement result invalidated");
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::completeRemote(const MeasurementCaptureSnapshot& snapshot,const QJsonObject& record)
|
||||
{
|
||||
if(m_remoteTerminal)return;
|
||||
if(m_remoteCancelled){remoteFailure("CANCELLED","Measurement result invalidated");return;}
|
||||
if(!snapshot.valid||!snapshot.leftPose.valid||record["output_source"].toString()!="LEFT"){
|
||||
remoteFailure(record["timed_out"].toBool()?"DEADLINE":"MEASURE",record["error"].toString("LEFT measurement failed"));return;
|
||||
}
|
||||
QElapsedTimer clock;clock.start();
|
||||
try {
|
||||
const auto camera=array6(snapshot.leftPose);for(auto v:camera)if(!v.isDouble()||!std::isfinite(v.toDouble()))throw std::runtime_error("Non-finite camera pose");
|
||||
if(!std::isfinite(snapshot.leftPose.score)||snapshot.leftPose.score<.70)throw std::runtime_error("LEFT score is below MinScore");
|
||||
m_remoteCameraResult={{"camera_valid",true},{"camera_pose",camera},{"source","LEFT"},{"pose_type","model_origin"},{"camera_rotation_type",snapshot.leftPose.poseType},{"score",snapshot.leftPose.score}};
|
||||
QJsonObject grasp;
|
||||
const QJsonArray reference=m_remoteTeach.size()==6?QJsonArray{m_remoteTeach[0],m_remoteTeach[1],m_remoteTeach[2]}:QJsonArray();
|
||||
if(m_remoteHeightMethod=="keep_visual_xy_solve_z")grasp=graspTargetKeepingXY(snapshot.leftPose,m_remoteRobot,m_remoteCalibration,reference,4.4);
|
||||
else if(m_remoteHeightMethod=="camera_ray_plane_intersection")grasp=graspTargetOnPlane(snapshot.leftPose,m_remoteRobot,m_remoteCalibration,reference,4.4);
|
||||
else if(m_remoteHeightMethod=="visual_3d")grasp=graspTarget(snapshot.leftPose,m_remoteRobot,m_remoteCalibration,4.4);
|
||||
else throw std::runtime_error("Unsupported tray height method");
|
||||
const auto robot=grasp["XYZABC_mm_deg"].toArray();robotTransform(robot);
|
||||
m_record["photo_robot_XYZABC_mm_deg"]=m_remoteRobot;m_record["photo_robot_pair_confirmed"]=true;m_record["photo_robot_pairing_method"]="ipc_request_snapshot";
|
||||
m_record["grasp_targets"]=QJsonObject{{"left",grasp}};
|
||||
for(int i=0;i<6;++i)m_values->setItem(3,i,new QTableWidgetItem(QString::number(robot[i].toDouble(),'f',3)));
|
||||
m_remoteTerminal=true;
|
||||
emit remoteFinished(QJsonObject{{"type","measure"},{"request_id",m_remoteRequest["request_id"]},{"context",m_remoteRequest["context"]},{"status","ok"},
|
||||
{"camera_pose",camera},{"robot_pose",robot},{"camera_valid",true},{"robot_valid",true},{"source","LEFT"},{"pose_type","model_origin"},{"camera_rotation_type",snapshot.leftPose.poseType},
|
||||
{"score",snapshot.leftPose.score},{"parameters",m_remoteRequest["parameters"]},{"convention",m_remoteRequest["convention"]},{"calibration_fingerprint",m_remoteFingerprint},{"run_id",record["run_id"]},
|
||||
{"capture_begin",record["capture_begin_utc"]},{"capture_end",record["capture_end_utc"]},{"measure_elapsed_ms",record["timings_ms"].toObject()["pose_ready_ms"]},{"grasp_elapsed_ms",double(clock.elapsed())}});
|
||||
}catch(const VisionGuideApi::HException& e){remoteFailure("GRASP",QString::fromLocal8Bit(e.ErrorMessage().TextA()));}
|
||||
catch(const std::exception& e){remoteFailure("GRASP",QString::fromUtf8(e.what()));}
|
||||
}
|
||||
|
||||
void MeasurementOptionsPanel::renderResult(){
|
||||
if(!m_matcher)return;
|
||||
auto draw=[&](VisionPoseResult pose,const QImage& input,CameraSide side,const QString& label){
|
||||
|
||||
@@ -34,6 +34,10 @@ public:
|
||||
VisionGuideMatcher* matcher, StereoPoseFusion* fusion,
|
||||
MeasurePanel* legacy, QWidget* parent = nullptr,const QString& setupFile = QString());
|
||||
~MeasurementOptionsPanel();
|
||||
bool isBusy() const { return m_busy; }
|
||||
bool remoteReady(QString* error = nullptr) const;
|
||||
void startRemote(const QJsonObject& request);
|
||||
void cancelRemote();
|
||||
static MeasurementSelection selectPose(int mode, const VisionPoseResult& left,
|
||||
const VisionPoseResult& right, const StereoPoseFusion::FusionResult& fusion);
|
||||
static MeasurementSelection enforceDeadline(MeasurementSelection selection,
|
||||
@@ -56,6 +60,7 @@ signals:
|
||||
void logMessage(const QString& message);
|
||||
void busyChanged(bool busy);
|
||||
void resultImage(const QString& name, const QImage& image);
|
||||
void remoteFinished(const QJsonObject& result);
|
||||
private:
|
||||
friend class MeasurementOptionsTest;
|
||||
void start();
|
||||
@@ -67,6 +72,8 @@ private:
|
||||
void writeArchive(QJsonObject record, const MeasurementCaptureSnapshot& snapshot,
|
||||
const QImage& result, bool images);
|
||||
void calculateGrasp();
|
||||
void completeRemote(const MeasurementCaptureSnapshot& snapshot, const QJsonObject& record);
|
||||
void remoteFailure(const QString& code, const QString& message);
|
||||
void refreshCameraPoses();
|
||||
void invalidateGraspTargets();
|
||||
void createTraySetupControls(QVBoxLayout* layout);
|
||||
@@ -124,4 +131,8 @@ private:
|
||||
QJsonArray m_normalSamples, m_activeTeach;
|
||||
MeasurementCaptureSnapshot m_boardCapture;
|
||||
bool m_normalRequired=false,m_loadingSetup=false;
|
||||
QJsonObject m_remoteRequest, m_remoteCalibration, m_remoteCameraResult;
|
||||
QJsonArray m_remoteRobot, m_remoteTeach;
|
||||
QString m_remoteHeightMethod, m_remoteFingerprint;
|
||||
bool m_remoteTerminal=false, m_remoteCancelled=false;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# ssp 独立进程接入
|
||||
|
||||
使用本目录 `VisionGuide.pro` 构建;不需要导出算法 DLL。ssp 保留原 MinGW 工程,此程序使用 Qt 5.14.1 MSVC x64 kit,运行目录必须保留部署脚本生成的 DLL、`visionguide.package.json`、`qt.conf` 和原项目资源目录。
|
||||
|
||||
启动参数:`VisionGuide.exe --ipc-name ssp-visionguide --background`。不带 `--background` 时显示原窗口;不带 `--ipc-name` 时保持手动运行方式。IPC 接入不会自动连接相机,必须收到 initialize 命令才连接左右相机。
|
||||
|
||||
协议对应 ssp 的 `docs/visionguide-ipc-protocol.md`:同用户 QLocalSocket,4字节大端长度和 UTF-8 JSON,协议版本1。通信在独立线程执行;相机/界面仍沿用原线程边界。控制端连接期间手动界面只供查看,OPC、标定和参数修改不能抢占;需断开控制端后进行手动配置。
|
||||
|
||||
初始化沿用 CameraPanel 当前曝光、增益和带宽值,两台相机分别连接、应用参数、提交一次白平衡、获取新帧并检查 BalanceWhiteAuto=Off。成功的同一连接代次不重复初始化。相机错误或断开使该路初始化失效。白平衡不完成时返回失败,不用命令提交成功冒充完成。
|
||||
|
||||
测量复用新测量页:读取指定料盘配置,冻结输入机器人位姿、标定、示教及高度策略,然后显式采用左相机、高精细化、5秒、0.70/0.80、中心选择、模型原点显示和4.40mm偏移。返回相机原点与左路抓取XYZABC两组数值;原点显示不会取消抓取偏移。取消或截止后算子仍在运行时保持 busy,迟到成功不会发布。旧分步位姿/调试和双目数据采集在整个操作期间保持资源占用,即使原进度界面处理嵌套事件,IPC初始化、测量和退出也会拒绝抢占。
|
||||
|
||||
测试工程:
|
||||
|
||||
- `tests/vision_ipc/vision_ipc.pro`:不加载相机SDK的真实本机socket测试,验证分包/粘包、会话、去重、取消、断线、busy、单客户端及非法帧。
|
||||
- `tests/vision_grasp/vision_grasp.pro`:复用实际抓取实现的无相机测试,验证已知几何、R40快照、偏移一次、部分成功和迟到结果拒绝。测试产物同样名为 VisionGuide.exe,位于独立测试构建目录,不用于生产。
|
||||
|
||||
2026-10-27 本机验证使用 Qt 5.14.1 msvc2017_64 库和已安装 VS2022 v143 14.44 编译器;本机未安装 v141,故尚未实测 MSVC2017 编译器。实际左右相机白平衡、同图识别一致性和机器人握手仍需现场验收。没有执行真实相机、PLC或机器人操作。
|
||||
@@ -1,7 +1,7 @@
|
||||
# Open this file in Qt Creator with a Qt 5.14.1 / MSVC / x64 kit.
|
||||
TEMPLATE = app
|
||||
TARGET = VisionGuide
|
||||
QT += core gui widgets serialport
|
||||
QT += core gui widgets serialport network
|
||||
CONFIG += c++17
|
||||
CONFIG -= debug_and_release debug_and_release_target
|
||||
# Qt 5.14 can miscalculate header dependencies in deeply nested shadow builds.
|
||||
@@ -65,6 +65,7 @@ SOURCES += \
|
||||
$$PWD/VisionGuidanceCapturePanel.cpp \
|
||||
$$PWD/VisionGuideMatcher.cpp \
|
||||
$$PWD/VisionGuidePanel.cpp \
|
||||
$$PWD/VisionIpcServer.cpp \
|
||||
$$PWD/YoloDetector.cpp \
|
||||
$$PWD/YoloPanel.cpp \
|
||||
$$PWD/main.cpp
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "ui_MainWindow.h"
|
||||
#include "AppSettings.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QScopeGuard>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
VisionGuidePanel::VisionGuidePanel(Ui::MainWindowClass& u, VisionGuideMatcher* m, QObject* parent)
|
||||
@@ -108,6 +109,9 @@ void VisionGuidePanel::syncParams()
|
||||
|
||||
void VisionGuidePanel::onStepSeg()
|
||||
{
|
||||
if(m_busy||m_resourceBlocked)return;
|
||||
auto release=qScopeGuard([this]{m_busy=false;updateStepButtons();emit busyChanged(false);});
|
||||
m_busy=true;emit busyChanged(true);
|
||||
if (m_frame.isNull() || !m_roi.isValid()) return;
|
||||
emit logMessage(QString::fromUtf8("═ VisionGuide Step1: 自动分割 ═"));
|
||||
ui.progressAlgoStep->setValue(10);
|
||||
@@ -133,6 +137,9 @@ void VisionGuidePanel::onStepSeg()
|
||||
|
||||
void VisionGuidePanel::onStepPose()
|
||||
{
|
||||
if(m_busy||m_resourceBlocked)return;
|
||||
auto release=qScopeGuard([this]{m_busy=false;updateStepButtons();emit busyChanged(false);});
|
||||
m_busy=true;emit busyChanged(true);
|
||||
if (m_frame.isNull() || !m_roi.isValid() || !m_hal->isReady(m_side)) return;
|
||||
QString sideStr = (m_side == CAM_LEFT) ? QString::fromUtf8("左") : QString::fromUtf8("右");
|
||||
emit logMessage(QString::fromUtf8("═ VisionGuide Step2: 位姿测量 (%1相机) ═").arg(sideStr));
|
||||
@@ -168,6 +175,9 @@ void VisionGuidePanel::onStepPose()
|
||||
|
||||
void VisionGuidePanel::onStepDebug()
|
||||
{
|
||||
if(m_busy||m_resourceBlocked)return;
|
||||
auto release=qScopeGuard([this]{m_busy=false;updateStepButtons();emit busyChanged(false);});
|
||||
m_busy=true;emit busyChanged(true);
|
||||
if (m_frame.isNull() || !m_roi.isValid() || !m_hal->isReady(m_side)) return;
|
||||
QString sideStr = (m_side == CAM_LEFT) ? QString::fromUtf8("左") : QString::fromUtf8("右");
|
||||
emit logMessage(QString::fromUtf8("═ VisionGuide Step3: 参数调试 (%1相机) ═").arg(sideStr));
|
||||
@@ -207,5 +217,5 @@ void VisionGuidePanel::onStepDebug()
|
||||
void VisionGuidePanel::onVisionGuideLog(const QString& msg) { emit logMessage(msg); }
|
||||
void VisionGuidePanel::onProgress(int pct, const QString&) {
|
||||
ui.progressAlgoStep->setValue(pct);
|
||||
QCoreApplication::processEvents();
|
||||
if(m_busy)QCoreApplication::processEvents();
|
||||
}
|
||||
|
||||
@@ -22,11 +22,14 @@ public:
|
||||
void updateStepButtons();
|
||||
// 将“位姿估计”页当前参数立即同步到共享匹配器,供“开始测量”使用。
|
||||
void applyCurrentParameters();
|
||||
bool isBusy() const { return m_busy; }
|
||||
void setResourceBlocked(bool blocked) { m_resourceBlocked=blocked; }
|
||||
|
||||
signals:
|
||||
void logMessage(const QString& msg);
|
||||
void resultImage(const QString& name, const QImage& img);
|
||||
void poseReady(const VisionPoseResult& pose);
|
||||
void busyChanged(bool busy);
|
||||
|
||||
private slots:
|
||||
void onStepSeg();
|
||||
@@ -45,4 +48,5 @@ private:
|
||||
QRect m_roi;
|
||||
VisionPoseResult m_pose;
|
||||
CameraSide m_side = CAM_LEFT; // 分步操作默认使用左相机
|
||||
bool m_busy=false, m_resourceBlocked=false;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "VisionIpcServer.h"
|
||||
#include <QLocalServer>
|
||||
#include <QLocalSocket>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
#include <QUuid>
|
||||
#include <QtEndian>
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace { const quint32 MaxMessage = 1024 * 1024; }
|
||||
|
||||
VisionIpcServer::VisionIpcServer(QObject* parent) : QObject(parent),
|
||||
m_session(QUuid::createUuid().toString(QUuid::WithoutBraces))
|
||||
{
|
||||
m_state = {{"ready",false},{"resource_busy",false}};
|
||||
}
|
||||
|
||||
void VisionIpcServer::listen(const QString& name)
|
||||
{
|
||||
if(m_server)return;
|
||||
m_server = new QLocalServer(this);
|
||||
m_server->setSocketOptions(QLocalServer::UserAccessOption);
|
||||
connect(m_server,&QLocalServer::newConnection,this,[this]{
|
||||
while(m_server->hasPendingConnections()){
|
||||
auto socket=m_server->nextPendingConnection();
|
||||
if(m_socket){socket->disconnectFromServer();socket->deleteLater();continue;}
|
||||
m_socket=socket;socket->setReadBufferSize(MaxMessage+4);m_buffer.clear();
|
||||
connect(socket,&QLocalSocket::readyRead,this,&VisionIpcServer::read);
|
||||
connect(socket,&QLocalSocket::disconnected,this,[this,socket]{
|
||||
if(m_socket!=socket)return;
|
||||
m_socket=nullptr;m_buffer.clear();socket->deleteLater();
|
||||
if(!m_active.isEmpty()&&!m_terminal){
|
||||
m_terminal=true;
|
||||
auto cancelled=reply(m_active,"cancelled","DISCONNECTED","Controller disconnected");
|
||||
send(cancelled);
|
||||
emit commandReceived(QJsonObject{{"type","cancel"},{"target_request_id",m_active["request_id"]}});
|
||||
}
|
||||
emit controllerChanged(false);
|
||||
});
|
||||
emit controllerChanged(true);
|
||||
}
|
||||
});
|
||||
// Do not remove another instance's endpoint to make a duplicate launch succeed.
|
||||
if(!m_server->listen(name))emit fatalError(m_server->errorString());
|
||||
}
|
||||
|
||||
void VisionIpcServer::close()
|
||||
{
|
||||
if(m_socket){m_socket->disconnect(this);m_socket->abort();delete m_socket;m_socket=nullptr;}
|
||||
if(m_server){m_server->close();delete m_server;m_server=nullptr;}
|
||||
}
|
||||
|
||||
QJsonObject VisionIpcServer::reply(const QJsonObject& request,const QString& status,
|
||||
const QString& code,const QString& message) const
|
||||
{
|
||||
auto out=m_state;
|
||||
out["protocol_version"]=1;out["type"]=request["type"];out["request_id"]=request["request_id"];
|
||||
out["server_session_id"]=m_session;out["status"]=status;
|
||||
out["server_process_id"]=QString::number(QCoreApplication::applicationPid());
|
||||
out["resource_busy"]=m_state["resource_busy"].toBool()||!m_active.isEmpty();
|
||||
if(request.contains("context"))out["context"]=request["context"];
|
||||
if(!code.isEmpty())out["error_code"]=code;
|
||||
if(!message.isEmpty())out["message"]=message;
|
||||
return out;
|
||||
}
|
||||
|
||||
void VisionIpcServer::send(QJsonObject out,bool remember)
|
||||
{
|
||||
const auto id=out["request_id"].toString();
|
||||
if(remember&&!id.isEmpty()){
|
||||
m_seen.insert(id);
|
||||
if(!m_replies.contains(id))m_replyOrder.enqueue(id);
|
||||
m_replies.insert(id,out);
|
||||
while(m_replyOrder.size()>64)m_replies.remove(m_replyOrder.dequeue());
|
||||
}
|
||||
if(!m_socket)return;
|
||||
const auto payload=QJsonDocument(out).toJson(QJsonDocument::Compact);
|
||||
QByteArray frame(4,'\0');qToBigEndian<quint32>(quint32(payload.size()),reinterpret_cast<uchar*>(frame.data()));
|
||||
frame.append(payload);m_socket->write(frame);
|
||||
}
|
||||
|
||||
void VisionIpcServer::read()
|
||||
{
|
||||
if(!m_socket)return;
|
||||
m_buffer+=m_socket->readAll();
|
||||
while(m_buffer.size()>=4){
|
||||
const auto size=qFromBigEndian<quint32>(reinterpret_cast<const uchar*>(m_buffer.constData()));
|
||||
if(size==0||size>MaxMessage){m_socket->abort();return;}
|
||||
if(m_buffer.size()<int(size)+4)return;
|
||||
const auto data=m_buffer.mid(4,int(size));m_buffer.remove(0,int(size)+4);
|
||||
QJsonParseError error;const auto document=QJsonDocument::fromJson(data,&error);
|
||||
if(error.error!=QJsonParseError::NoError||!document.isObject()){m_socket->abort();return;}
|
||||
dispatch(document.object());
|
||||
if(!m_socket)return;
|
||||
}
|
||||
}
|
||||
|
||||
void VisionIpcServer::dispatch(const QJsonObject& request)
|
||||
{
|
||||
const auto id=request["request_id"].toString(),type=request["type"].toString();
|
||||
if(id.isEmpty()||id.size()>128||request["protocol_version"].toDouble()!=1){
|
||||
send(reply(request,"error","PROTOCOL","Expected protocol 1 and a request ID"),false);return;
|
||||
}
|
||||
if(type!="hello"&&request["server_session_id"].toString()!=m_session){
|
||||
send(reply(request,"error","SESSION","Server session changed"),false);return;
|
||||
}
|
||||
if(m_replies.contains(id)){send(m_replies.value(id),false);return;}
|
||||
// Keep only recent payloads, but never execute a request ID again in this process session.
|
||||
if(m_seen.contains(id)){send(reply(request,"error","DUPLICATE_EXPIRED","Request already handled; cached response expired"),false);return;}
|
||||
if(type=="hello"||type=="get_status"){send(reply(request,"ok"),false);return;}
|
||||
if(type=="cancel"){
|
||||
if(request["target_request_id"]!=m_active["request_id"]||m_active.isEmpty()){
|
||||
send(reply(request,"error","NOT_ACTIVE","Target request is not active"));return;
|
||||
}
|
||||
send(reply(request,"ok"));
|
||||
if(!m_terminal){m_terminal=true;send(reply(m_active,"cancelled","CANCELLED","Request invalidated; resource remains busy until the operation exits"));emit commandReceived(request);}
|
||||
return;
|
||||
}
|
||||
if(type=="show_window"){send(reply(request,"ok"));emit commandReceived(request);return;}
|
||||
if(type!="initialize"&&type!="measure"&&type!="shutdown"){
|
||||
send(reply(request,"error","COMMAND","Unknown command"));return;
|
||||
}
|
||||
if(m_state["resource_busy"].toBool()||!m_active.isEmpty()){
|
||||
send(reply(request,"error","BUSY","Vision resource is busy"));return;
|
||||
}
|
||||
m_active=request;m_terminal=false;m_dispatchPending=true;
|
||||
send(reply(request,"accepted"));emit commandReceived(request);
|
||||
}
|
||||
|
||||
void VisionIpcServer::setBackendState(const QJsonObject& state)
|
||||
{
|
||||
m_state=state;
|
||||
if(!m_state["resource_busy"].toBool()&&m_terminal&&!m_dispatchPending)m_active={};
|
||||
send(reply(QJsonObject{{"type","status"},{"request_id",""}},"ok"),false);
|
||||
}
|
||||
|
||||
void VisionIpcServer::finish(const QJsonObject& result)
|
||||
{
|
||||
if(m_active.isEmpty()||result["request_id"]!=m_active["request_id"])return;
|
||||
m_dispatchPending=false;
|
||||
if(!m_terminal){
|
||||
auto out=reply(m_active,result["status"].toString("error"));
|
||||
for(auto it=result.begin();it!=result.end();++it)out[it.key()]=it.value();
|
||||
out["server_session_id"]=m_session;out["protocol_version"]=1;
|
||||
out["resource_busy"]=m_state["resource_busy"];
|
||||
m_terminal=true;send(out);
|
||||
}
|
||||
if(!m_state["resource_busy"].toBool())m_active={};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
#include <QObject>
|
||||
#include <QJsonObject>
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
#include <QQueue>
|
||||
#include <QByteArray>
|
||||
|
||||
class QLocalServer;
|
||||
class QLocalSocket;
|
||||
|
||||
// Lives entirely on its own QThread. Camera/UI work is delivered by queued signals.
|
||||
class VisionIpcServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit VisionIpcServer(QObject* parent = nullptr);
|
||||
public slots:
|
||||
void listen(const QString& name);
|
||||
void close();
|
||||
void setBackendState(const QJsonObject& state);
|
||||
void finish(const QJsonObject& result);
|
||||
signals:
|
||||
void commandReceived(const QJsonObject& request);
|
||||
void controllerChanged(bool attached);
|
||||
void fatalError(const QString& message);
|
||||
private:
|
||||
void read();
|
||||
void dispatch(const QJsonObject& request);
|
||||
void send(QJsonObject reply, bool remember = true);
|
||||
QJsonObject reply(const QJsonObject& request, const QString& status,
|
||||
const QString& code = QString(), const QString& message = QString()) const;
|
||||
QLocalServer* m_server = nullptr;
|
||||
QLocalSocket* m_socket = nullptr;
|
||||
QByteArray m_buffer;
|
||||
QString m_session;
|
||||
QJsonObject m_state;
|
||||
QJsonObject m_active;
|
||||
bool m_terminal = false;
|
||||
bool m_dispatchPending = false;
|
||||
QHash<QString,QJsonObject> m_replies;
|
||||
QSet<QString> m_seen;
|
||||
QQueue<QString> m_replyOrder;
|
||||
};
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <QTableWidgetItem>
|
||||
#include <QAbstractItemView>
|
||||
#include <QThread>
|
||||
#include <QScopeGuard>
|
||||
#include <QTimer>
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
@@ -180,6 +181,9 @@ void YoloPanel::updateStates()
|
||||
|
||||
void YoloPanel::onCaptureStereoDataset()
|
||||
{
|
||||
if(m_busy||m_resourceBlocked)return;
|
||||
auto release=qScopeGuard([this]{m_busy=false;updateStates();emit busyChanged(false);});
|
||||
m_busy=true;emit busyChanged(true);
|
||||
if (!m_cameraManager ||
|
||||
!m_cameraManager->isConnected(CameraManager::LEFT) ||
|
||||
!m_cameraManager->isConnected(CameraManager::RIGHT)) {
|
||||
@@ -308,6 +312,9 @@ void YoloPanel::onCaptureStereoDataset()
|
||||
|
||||
void YoloPanel::onCaptureStereoPair()
|
||||
{
|
||||
if(m_busy||m_resourceBlocked)return;
|
||||
auto release=qScopeGuard([this]{m_busy=false;updateStates();emit busyChanged(false);});
|
||||
m_busy=true;emit busyChanged(true);
|
||||
if (!m_cameraManager ||
|
||||
!m_cameraManager->isConnected(CameraManager::LEFT) ||
|
||||
!m_cameraManager->isConnected(CameraManager::RIGHT)) {
|
||||
|
||||
@@ -21,6 +21,8 @@ public:
|
||||
QRect roiRect() const { return m_roi; }
|
||||
void setROI(const QRect& r);
|
||||
void clearState();
|
||||
bool isBusy() const { return m_busy; }
|
||||
void setResourceBlocked(bool blocked) { m_resourceBlocked=blocked; }
|
||||
// 全流程用的批量接口
|
||||
QRect runYoloAndCrop(const QImage& img, int filterClass,
|
||||
const QRect& hint, const QString& tag);
|
||||
@@ -28,6 +30,7 @@ signals:
|
||||
void logMessage(const QString& msg);
|
||||
void roiChanged(const QRect& roi);
|
||||
void yoloCompleted();
|
||||
void busyChanged(bool busy);
|
||||
|
||||
private slots:
|
||||
void onLoadModel(); void onRunYolo();
|
||||
@@ -54,6 +57,7 @@ private:
|
||||
QVector<YoloResult> m_results;
|
||||
QVector<GrooveDetectionResult> m_grooveResults;
|
||||
bool m_grooveModelValid = false;
|
||||
bool m_busy=false, m_resourceBlocked=false;
|
||||
float m_confThresh = 0.25f;
|
||||
float m_nmsThresh = 0.45f;
|
||||
float m_grooveConfThresh = 0.25f;
|
||||
|
||||
+8
-2
@@ -49,12 +49,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
if (checkOnly) { const auto json = QJsonDocument(state).toJson(); std::fwrite(json.constData(), 1, json.size(), stdout); return 0; }
|
||||
} catch (const std::exception& error) {
|
||||
if (a.arguments().contains("--portable-check") || a.arguments().contains("--camera-check")) { std::fprintf(stderr, "%s\n", error.what()); return 2; }
|
||||
if (a.arguments().contains("--portable-check") || a.arguments().contains("--camera-check") || a.arguments().contains("--background")) { std::fprintf(stderr, "%s\n", error.what()); return 2; }
|
||||
QMessageBox::critical(nullptr, QString::fromUtf8("启动失败"), sanitizeUserVisibleLog(QString::fromUtf8(error.what()))); return 2;
|
||||
}
|
||||
#endif
|
||||
|
||||
MainWindow w;
|
||||
w.show();
|
||||
const auto arguments=a.arguments();const int ipcIndex=arguments.indexOf("--ipc-name");
|
||||
if(ipcIndex>=0){
|
||||
if(ipcIndex+1>=arguments.size()||arguments[ipcIndex+1].isEmpty()){std::fprintf(stderr,"--ipc-name requires a name\n");return 2;}
|
||||
w.startIpc(arguments[ipcIndex+1]);
|
||||
}
|
||||
if(!arguments.contains("--background"))w.show();
|
||||
else a.setQuitOnLastWindowClosed(false);
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ function Copy-RuntimeFile([string]$source,[string]$destination) {
|
||||
if([IO.Path]::GetFullPath($source) -ne [IO.Path]::GetFullPath($destination)){Copy-Item -LiteralPath $source -Destination $destination -Force}
|
||||
}
|
||||
$suffix=if($Configuration -eq 'Debug'){'d'}else{''}
|
||||
foreach($module in @('Core','Gui','Widgets','SerialPort')) {
|
||||
foreach($module in @('Core','Gui','Widgets','SerialPort','Network')) {
|
||||
$name="Qt5$module$suffix.dll";Copy-RuntimeFile (Join-Path $qtBin $name) (Join-Path $outputPath $name)
|
||||
}
|
||||
foreach($name in @("libEGL$suffix.dll","libGLESv2$suffix.dll",'D3Dcompiler_47.dll','opengl32sw.dll')) {
|
||||
|
||||
Reference in New Issue
Block a user