2026-10-27 22:16:55 +08:00
|
|
|
|
#include "visionpage.h"
|
|
|
|
|
|
#include "../services/visionservice.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <QGridLayout>
|
|
|
|
|
|
#include <QGroupBox>
|
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
#include <QTableWidget>
|
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
|
|
|
|
VisionPage::VisionPage(VisionService *service, QWidget *parent)
|
2026-10-29 18:21:27 +08:00
|
|
|
|
: QWidget(parent), m_service(service), m_operationPending(false)
|
2026-10-27 22:16:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
|
|
|
|
layout->setContentsMargins(18, 18, 18, 18);
|
2026-10-29 18:21:27 +08:00
|
|
|
|
layout->setSpacing(12);
|
|
|
|
|
|
setStyleSheet(
|
|
|
|
|
|
"QGroupBox#visionControlBox QPushButton { min-width: 160px; }"
|
|
|
|
|
|
"QGroupBox#visionControlBox QPushButton:pressed {"
|
|
|
|
|
|
" background: #cfd8dc; border-color: #607d8b; padding-top: 7px; padding-bottom: 3px; }"
|
|
|
|
|
|
"QGroupBox#visionControlBox QPushButton:disabled {"
|
|
|
|
|
|
" color: #90a4ae; background: #eceff1; border-color: #cfd8dc; }");
|
|
|
|
|
|
|
|
|
|
|
|
QGroupBox *control = new QGroupBox("视觉程序控制", this);
|
|
|
|
|
|
control->setObjectName("visionControlBox");
|
|
|
|
|
|
QGridLayout *controlLayout = new QGridLayout(control);
|
|
|
|
|
|
controlLayout->setHorizontalSpacing(10);
|
|
|
|
|
|
controlLayout->setVerticalSpacing(8);
|
|
|
|
|
|
m_start = new QPushButton("启动视觉程序", control);
|
|
|
|
|
|
m_connect = new QPushButton("连接已有视觉程序", control);
|
|
|
|
|
|
m_initialize = new QPushButton("初始化左右相机", control);
|
|
|
|
|
|
m_show = new QPushButton("打开视觉调试窗口", control);
|
|
|
|
|
|
m_disconnect = new QPushButton("断开并释放调试控制", control);
|
|
|
|
|
|
controlLayout->addWidget(m_start, 0, 0);
|
|
|
|
|
|
controlLayout->addWidget(m_connect, 0, 1);
|
|
|
|
|
|
controlLayout->addWidget(m_initialize, 0, 2);
|
|
|
|
|
|
controlLayout->addWidget(m_show, 1, 0);
|
|
|
|
|
|
controlLayout->addWidget(m_disconnect, 1, 1);
|
|
|
|
|
|
controlLayout->setColumnStretch(0, 1);
|
|
|
|
|
|
controlLayout->setColumnStretch(1, 1);
|
|
|
|
|
|
controlLayout->setColumnStretch(2, 1);
|
|
|
|
|
|
m_operation = new QLabel("最近操作:等待操作", control);
|
|
|
|
|
|
m_operation->setObjectName("visionOperationFeedback");
|
|
|
|
|
|
m_operation->setMinimumHeight(38);
|
|
|
|
|
|
m_operation->setWordWrap(true);
|
|
|
|
|
|
controlLayout->addWidget(m_operation, 2, 0, 1, 3);
|
|
|
|
|
|
showOperation("info", "等待操作");
|
|
|
|
|
|
layout->addWidget(control);
|
|
|
|
|
|
|
|
|
|
|
|
QGroupBox *status = new QGroupBox("运行状态", this);
|
|
|
|
|
|
QVBoxLayout *statusLayout = new QVBoxLayout(status);
|
|
|
|
|
|
statusLayout->setSpacing(7);
|
|
|
|
|
|
m_status = new QLabel("视觉进程:未连接", status);
|
|
|
|
|
|
m_initialization = new QLabel("相机初始化:未执行", status);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
m_initialization->setWordWrap(true);
|
2026-10-29 18:21:27 +08:00
|
|
|
|
QLabel *parameters = new QLabel(
|
|
|
|
|
|
"左相机 · 高精细化 · 测量截止 5 秒 · MinScore 0.70 · Greediness 0.80 · 中心选择 · 模型原点 · 抓取偏移 4.40 mm",
|
|
|
|
|
|
status);
|
|
|
|
|
|
parameters->setWordWrap(true);
|
|
|
|
|
|
m_cycle = new QLabel("自动流程:未开始", status);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
m_cycle->setWordWrap(true);
|
2026-10-29 18:21:27 +08:00
|
|
|
|
m_result = new QLabel("本次结果:无", status);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
m_result->setWordWrap(true);
|
2026-10-29 18:21:27 +08:00
|
|
|
|
statusLayout->addWidget(m_status);
|
|
|
|
|
|
statusLayout->addWidget(m_initialization);
|
|
|
|
|
|
statusLayout->addWidget(parameters);
|
|
|
|
|
|
statusLayout->addWidget(m_cycle);
|
|
|
|
|
|
statusLayout->addWidget(m_result);
|
|
|
|
|
|
layout->addWidget(status);
|
|
|
|
|
|
|
|
|
|
|
|
QGroupBox *poses = new QGroupBox("位姿结果", this);
|
|
|
|
|
|
QVBoxLayout *poseLayout = new QVBoxLayout(poses);
|
|
|
|
|
|
m_poses = new QTableWidget(3, 6, poses);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
m_poses->setHorizontalHeaderLabels(QStringList() << "X" << "Y" << "Z" << "A" << "B" << "C");
|
|
|
|
|
|
m_poses->setVerticalHeaderLabels(QStringList() << "R40 拍照位姿" << "相机模型原点" << "R60 抓取目标");
|
|
|
|
|
|
m_poses->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
|
|
m_poses->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
2026-10-29 18:21:27 +08:00
|
|
|
|
m_poses->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
|
|
|
|
m_poses->setMinimumHeight(190);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
for (int row = 0; row < 3; ++row)
|
|
|
|
|
|
for (int col = 0; col < 6; ++col)
|
|
|
|
|
|
m_poses->setItem(row, col, new QTableWidgetItem("—"));
|
2026-10-29 18:21:27 +08:00
|
|
|
|
poseLayout->addWidget(m_poses);
|
|
|
|
|
|
QLabel *units = new QLabel(
|
|
|
|
|
|
"机器人坐标单位:mm / °。相机姿态沿用 VisionGuide 原始类型;显示抓取目标不代表 PLC 已写入或机器人已接收。",
|
|
|
|
|
|
poses);
|
|
|
|
|
|
units->setWordWrap(true);
|
|
|
|
|
|
poseLayout->addWidget(units);
|
|
|
|
|
|
layout->addWidget(poses, 1);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
|
2026-10-29 18:21:27 +08:00
|
|
|
|
connect(m_start, &QPushButton::clicked, this, [this]() {
|
|
|
|
|
|
beginOperation("start", "正在启动或连接视觉程序…");
|
|
|
|
|
|
m_service->startService();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(m_connect, &QPushButton::clicked, this, [this]() {
|
|
|
|
|
|
beginOperation("connect", "正在连接已有视觉程序…");
|
|
|
|
|
|
m_service->connectService();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(m_initialize, &QPushButton::clicked, this, [this]() {
|
|
|
|
|
|
beginOperation("initialize", "正在初始化左右相机…");
|
|
|
|
|
|
m_service->initialize();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(m_show, &QPushButton::clicked, this, [this]() {
|
|
|
|
|
|
beginOperation("show_window", "正在请求显示视觉调试窗口…");
|
|
|
|
|
|
m_service->showWindow();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(m_disconnect, &QPushButton::clicked, this, [this]() {
|
|
|
|
|
|
beginOperation("disconnect", "正在断开视觉控制…");
|
|
|
|
|
|
m_service->disconnectService();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(service, &VisionService::operationFeedback, this,
|
|
|
|
|
|
[this](const QString &operation, const QString &state, const QString &message) {
|
|
|
|
|
|
if (!m_operationPending || operation != m_pendingOperation)
|
|
|
|
|
|
return;
|
|
|
|
|
|
m_operationPending = false;
|
|
|
|
|
|
showOperation(state, message);
|
|
|
|
|
|
refreshControls();
|
2026-10-27 22:16:55 +08:00
|
|
|
|
});
|
|
|
|
|
|
connect(service, &VisionService::availabilityChanged, this, [this](bool ready, bool busy, const QString &session) {
|
|
|
|
|
|
m_status->setText(session.isEmpty() ? "视觉进程:未连接"
|
|
|
|
|
|
: QString("视觉进程:在线;初始化%1;资源%2").arg(ready ? "就绪" : "未就绪", busy ? "忙" : "空闲"));
|
|
|
|
|
|
if (session.isEmpty()) invalidateResult();
|
|
|
|
|
|
refreshControls();
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(service, &VisionService::statusChanged, this, [this](const QJsonObject &status) {
|
|
|
|
|
|
if (status.contains("initialization")) {
|
|
|
|
|
|
const QJsonObject initialization = status.value("initialization").toObject();
|
|
|
|
|
|
QStringList details;
|
2026-10-29 11:57:47 +08:00
|
|
|
|
const QStringList cameras = QStringList() << "left" << "right";
|
|
|
|
|
|
for (const QString &camera : cameras) {
|
2026-10-27 22:16:55 +08:00
|
|
|
|
const QJsonObject state = initialization.value(camera).toObject();
|
|
|
|
|
|
details.append(QString("%1:连接%2 / 参数%3 / 白平衡%4")
|
|
|
|
|
|
.arg(camera == "left" ? "左相机" : "右相机",
|
|
|
|
|
|
state.value("connected").toBool() ? "完成" : "未完成",
|
|
|
|
|
|
state.value("parameters_applied").toBool() ? "完成" : "未完成",
|
|
|
|
|
|
state.value("white_balance_finished").toBool() ? "完成" : "未完成"));
|
|
|
|
|
|
}
|
|
|
|
|
|
m_initialization->setText(details.join(";"));
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(service, &VisionService::requestStarted, this, [this](const QJsonObject &request) {
|
|
|
|
|
|
invalidateResult();
|
|
|
|
|
|
showPose(0, request.value("robot_pose"));
|
|
|
|
|
|
const QJsonObject context = request.value("context").toObject();
|
2026-10-29 18:21:27 +08:00
|
|
|
|
m_cycle->setText(QString("请求 %1;工件 %2;视觉料盘 %3;等待测量结果")
|
2026-10-27 22:16:55 +08:00
|
|
|
|
.arg(request.value("request_id").toString(), context.value("workpiece_id").toString())
|
2026-10-29 18:21:27 +08:00
|
|
|
|
.arg(context.value("tray_no").toInt()));
|
2026-10-27 22:16:55 +08:00
|
|
|
|
});
|
|
|
|
|
|
connect(service, &VisionService::measurementFinished, this, [this](const QJsonObject &result) {
|
|
|
|
|
|
if (result.value("status").toString() != "ok") {
|
|
|
|
|
|
invalidateResult();
|
|
|
|
|
|
m_result->setText("本次结果不可用:" + result.value("message").toString());
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
showPose(1, result.value("camera_pose"));
|
|
|
|
|
|
showPose(2, result.value("robot_pose"));
|
|
|
|
|
|
m_result->setText(QString("视觉计算成功;匹配分数 %1;测量 %2 ms;抓取计算 %3 ms;等待生产流程确认提交状态")
|
|
|
|
|
|
.arg(result.value("score").toDouble(), 0, 'f', 3)
|
|
|
|
|
|
.arg(result.value("measure_elapsed_ms").toInt()).arg(result.value("grasp_elapsed_ms").toInt()));
|
|
|
|
|
|
});
|
|
|
|
|
|
refreshControls();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-29 18:21:27 +08:00
|
|
|
|
void VisionPage::beginOperation(const QString &operation, const QString &message)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pendingOperation = operation;
|
|
|
|
|
|
m_operationPending = true;
|
|
|
|
|
|
showOperation("pending", message);
|
|
|
|
|
|
refreshControls();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionPage::showOperation(const QString &state, const QString &message)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString color = "#546e7a";
|
|
|
|
|
|
QString background = "#eceff1";
|
|
|
|
|
|
if (state == "pending") {
|
|
|
|
|
|
color = "#1565c0";
|
|
|
|
|
|
background = "#e3f2fd";
|
|
|
|
|
|
} else if (state == "success") {
|
|
|
|
|
|
color = "#2e7d32";
|
|
|
|
|
|
background = "#e8f5e9";
|
|
|
|
|
|
} else if (state == "error") {
|
|
|
|
|
|
color = "#c62828";
|
|
|
|
|
|
background = "#ffebee";
|
|
|
|
|
|
}
|
|
|
|
|
|
m_operation->setText("最近操作:" + message);
|
|
|
|
|
|
m_operation->setStyleSheet(QString(
|
|
|
|
|
|
"QLabel { color: %1; background: %2; border: 1px solid %1; border-radius: 4px; padding: 7px 10px; }")
|
|
|
|
|
|
.arg(color, background));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-27 22:16:55 +08:00
|
|
|
|
void VisionPage::showPose(int row, const QJsonValue &pose)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QJsonArray values = pose.toArray();
|
|
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
|
|
m_poses->item(row, i)->setText(values.size() == 6 ? QString::number(values.at(i).toDouble(), 'g', 12) : "—");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionPage::invalidateResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
showPose(1, QJsonArray());
|
|
|
|
|
|
showPose(2, QJsonArray());
|
|
|
|
|
|
m_result->setText("本次结果:不可用");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionPage::updateCycle(const QJsonObject &cycle)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_cycle->setText("自动流程:" + cycle.value("phase").toString() + ";" + cycle.value("message").toString());
|
|
|
|
|
|
if (cycle.contains("photo_pose")) showPose(0, cycle.value("photo_pose"));
|
|
|
|
|
|
if (cycle.contains("robot_pose")) showPose(2, cycle.value("robot_pose"));
|
|
|
|
|
|
if (cycle.value("invalidated").toBool()) invalidateResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionPage::refreshControls()
|
|
|
|
|
|
{
|
2026-10-29 18:21:27 +08:00
|
|
|
|
m_start->setEnabled(!m_operationPending || m_pendingOperation != "start");
|
|
|
|
|
|
m_connect->setEnabled(!m_operationPending || m_pendingOperation != "connect");
|
|
|
|
|
|
m_initialize->setEnabled(m_service->canInitialize()
|
|
|
|
|
|
&& (!m_operationPending || m_pendingOperation != "initialize"));
|
|
|
|
|
|
m_show->setEnabled(!m_operationPending || m_pendingOperation != "show_window");
|
|
|
|
|
|
m_disconnect->setEnabled(!m_operationPending || m_pendingOperation != "disconnect");
|
2026-10-27 22:16:55 +08:00
|
|
|
|
}
|