添加管控软件和相机的通讯
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
#include "visionpage.h"
|
||||
#include "../services/visionservice.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QHeaderView>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QSpinBox>
|
||||
#include <QTableWidget>
|
||||
#include <QUuid>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
VisionPage::VisionPage(VisionService *service, QWidget *parent)
|
||||
: QWidget(parent), m_service(service), m_productionActive(false)
|
||||
{
|
||||
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(18, 18, 18, 18);
|
||||
QHBoxLayout *buttons = new QHBoxLayout;
|
||||
QPushButton *start = new QPushButton("启动视觉程序", this);
|
||||
QPushButton *connectButton = new QPushButton("连接已有视觉程序", this);
|
||||
m_initialize = new QPushButton("初始化左右相机", this);
|
||||
QPushButton *show = new QPushButton("打开视觉调试窗口", this);
|
||||
QPushButton *disconnectButton = new QPushButton("断开并释放调试控制", this);
|
||||
buttons->addWidget(start);
|
||||
buttons->addWidget(connectButton);
|
||||
buttons->addWidget(m_initialize);
|
||||
buttons->addWidget(show);
|
||||
buttons->addWidget(disconnectButton);
|
||||
buttons->addStretch();
|
||||
layout->addLayout(buttons);
|
||||
m_status = new QLabel("视觉进程:未连接", this);
|
||||
m_initialization = new QLabel("相机初始化:未执行", this);
|
||||
m_initialization->setWordWrap(true);
|
||||
m_cycle = new QLabel("自动流程:未开始", this);
|
||||
m_cycle->setWordWrap(true);
|
||||
m_result = new QLabel("本次结果:无", this);
|
||||
m_result->setWordWrap(true);
|
||||
layout->addWidget(m_status);
|
||||
layout->addWidget(m_initialization);
|
||||
layout->addWidget(new QLabel("左相机 · 高精细化 · 测量截止 5 秒 · MinScore 0.70 · Greediness 0.80 · 中心选择 · 模型原点 · 抓取偏移 4.40 mm", this));
|
||||
layout->addWidget(m_cycle);
|
||||
layout->addWidget(m_result);
|
||||
m_poses = new QTableWidget(3, 6, this);
|
||||
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);
|
||||
for (int row = 0; row < 3; ++row)
|
||||
for (int col = 0; col < 6; ++col)
|
||||
m_poses->setItem(row, col, new QTableWidgetItem("—"));
|
||||
layout->addWidget(m_poses);
|
||||
layout->addWidget(new QLabel("机器人坐标单位:mm / °。相机姿态沿用 VisionGuide 原始类型;显示抓取目标不代表 PLC 已写入或机器人已接收。", this));
|
||||
|
||||
QGroupBox *diagnostic = new QGroupBox("手动诊断(只测量,不写 R60)", this);
|
||||
QGridLayout *form = new QGridLayout(diagnostic);
|
||||
form->addWidget(new QLabel("视觉料盘", diagnostic), 0, 0);
|
||||
m_tray = new QSpinBox(diagnostic);
|
||||
m_tray->setRange(1, 99);
|
||||
form->addWidget(m_tray, 0, 1);
|
||||
const QStringList axes = QStringList() << "X" << "Y" << "Z" << "A" << "B" << "C";
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
form->addWidget(new QLabel(axes.at(i), diagnostic), 1, i);
|
||||
m_diagnosticPose[i] = new QDoubleSpinBox(diagnostic);
|
||||
m_diagnosticPose[i]->setRange(-1000000, 1000000);
|
||||
m_diagnosticPose[i]->setDecimals(6);
|
||||
form->addWidget(m_diagnosticPose[i], 2, i);
|
||||
}
|
||||
m_diagnose = new QPushButton("使用上述拍照位姿测量", diagnostic);
|
||||
form->addWidget(m_diagnose, 3, 0, 1, 3);
|
||||
layout->addWidget(diagnostic);
|
||||
layout->addStretch();
|
||||
connect(start, &QPushButton::clicked, service, &VisionService::startService);
|
||||
connect(connectButton, &QPushButton::clicked, service, &VisionService::connectService);
|
||||
connect(m_initialize, &QPushButton::clicked, service, &VisionService::initialize);
|
||||
connect(show, &QPushButton::clicked, service, &VisionService::showWindow);
|
||||
connect(disconnectButton, &QPushButton::clicked, service, &VisionService::disconnectService);
|
||||
connect(m_diagnose, &QPushButton::clicked, this, [this]() {
|
||||
QJsonArray pose;
|
||||
for (QDoubleSpinBox *axis : m_diagnosticPose) pose.append(axis->value());
|
||||
const QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||
QJsonObject request{{"request_id", id}, {"robot_pose", pose},
|
||||
{"context", QJsonObject{{"workpiece_id", "diagnostic"}, {"task_id", "diagnostic"},
|
||||
{"tray_no", m_tray->value()}, {"generation", id}}},
|
||||
{"pose_source", "diagnostic"}, {"pose_read_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs)},
|
||||
{"parameters", VisionService::standardParameters()}, {"convention", VisionService::standardConvention()},
|
||||
{"require_robot_pose", true}};
|
||||
m_service->diagnose(request);
|
||||
});
|
||||
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;
|
||||
for (const QString &camera : QStringList() << "left" << "right") {
|
||||
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();
|
||||
m_poses->verticalHeaderItem(0)->setText(request.value("pose_source").toString() == "diagnostic"
|
||||
? "诊断拍照位姿" : "R40 拍照位姿");
|
||||
showPose(0, request.value("robot_pose"));
|
||||
const QJsonObject context = request.value("context").toObject();
|
||||
m_cycle->setText(QString("请求 %1;工件 %2;视觉料盘 %3;等待测量结果%4")
|
||||
.arg(request.value("request_id").toString(), context.value("workpiece_id").toString())
|
||||
.arg(context.value("tray_no").toInt())
|
||||
.arg(request.value("pose_source").toString() == "diagnostic" ? "(诊断)" : ""));
|
||||
});
|
||||
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()));
|
||||
if (result.value("context").toObject().value("task_id").toString() == "diagnostic")
|
||||
m_result->setText(m_result->text().replace("等待生产流程确认提交状态", "诊断结果,未写入 PLC"));
|
||||
});
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
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::setProductionActive(bool active)
|
||||
{
|
||||
m_productionActive = active;
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void VisionPage::refreshControls()
|
||||
{
|
||||
m_initialize->setEnabled(!m_productionActive && !m_service->isBusy() && !m_service->sessionId().isEmpty());
|
||||
m_diagnose->setEnabled(!m_productionActive && m_service->isReady() && !m_service->isBusy());
|
||||
m_tray->setEnabled(!m_productionActive);
|
||||
for (QDoubleSpinBox *axis : m_diagnosticPose) axis->setEnabled(!m_productionActive);
|
||||
}
|
||||
Reference in New Issue
Block a user