feat(vision): 新增拍照到位前置流程并支持视觉失败降级取料
- 在普通 RackPick 前写托盘号、格位并置 M850,收到 M851 后清除两位 - 使用 vision/enabled 控制识别,关闭时仍执行拍照到位流程 - 识别成功且 R60 写入确认后写 M852=1,视觉关闭或失败时写 M852=0 - 确认 M852 写入后启动 RackPick,动作结束保留 M852 - 移除旧 850–854 五信号握手,更新配置和协议文档 - 允许未就绪的视觉服务接收测量,扩展超时以覆盖初始化重试 - 保留 PLC 写入失败阻断、暂停发送门控及迟到结果拒绝 - 持久化未完成事务,支持 PLC 暂停后的视觉维护与人工恢复 验证:MinGW 32/64 位编译通过;生产流程47、IPC14、PLC11项测试通过
This commit is contained in:
+241
-150
@@ -11,10 +11,16 @@
|
||||
#include <QDateTime>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QSaveFile>
|
||||
#include <QScopedValueRollback>
|
||||
#include <QTimer>
|
||||
#include <QUuid>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -57,6 +63,11 @@ ProductionService::ProductionService(QObject *parent)
|
||||
, m_externalStopRequested(nullptr)
|
||||
, m_shutdownRequested(false)
|
||||
{
|
||||
const QString configPath = ConfigManager::instance().filePath();
|
||||
m_visionStatePath = QFileInfo(configPath.isEmpty()
|
||||
? QDir(QCoreApplication::applicationDirPath()).filePath("config.ini") : configPath)
|
||||
.absoluteFilePath() + ".vision-state.json";
|
||||
loadVisionRecoveryState();
|
||||
connect(m_timer, &QTimer::timeout, this, &ProductionService::tick);
|
||||
connect(m_deviceService, &DeviceService::statusChanged,
|
||||
this, &ProductionService::onDeviceStatusChanged);
|
||||
@@ -165,17 +176,55 @@ void ProductionService::requestStopImmediate()
|
||||
bool ProductionService::requiresVision(DeviceService::ProductionAction action) const
|
||||
{
|
||||
return action == DeviceService::RackPick
|
||||
&& ConfigManager::instance().visionConfig().enabled
|
||||
&& !ConfigManager::instance().simulationMode();
|
||||
}
|
||||
|
||||
bool ProductionService::saveVisionRecoveryState(bool pending, QString *error)
|
||||
{
|
||||
const QJsonObject state{{"version", 1}, {"pending", pending},
|
||||
{"run_id", m_runId}, {"workpiece_id", m_currentWorkpiece.id},
|
||||
{"updated_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs)}};
|
||||
const QByteArray bytes = QJsonDocument(state).toJson(QJsonDocument::Compact);
|
||||
QSaveFile file(m_visionStatePath);
|
||||
if (!file.open(QIODevice::WriteOnly) || file.write(bytes) != bytes.size() || !file.commit()) {
|
||||
if (error) *error = "无法保存视觉恢复状态,禁止继续生产:" + file.errorString();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ProductionService::loadVisionRecoveryState()
|
||||
{
|
||||
if (!QFileInfo::exists(m_visionStatePath)) return;
|
||||
QFile file(m_visionStatePath);
|
||||
const bool opened = file.open(QIODevice::ReadOnly);
|
||||
const QJsonObject state = opened ? QJsonDocument::fromJson(file.readAll()).object() : QJsonObject();
|
||||
const bool valid = state.value("version").toDouble() == 1 && state.value("pending").isBool();
|
||||
if (valid && !state.value("pending").toBool()) return;
|
||||
m_visionNeedsReconcile = true;
|
||||
m_visionSuspended = true;
|
||||
m_visionStage = VisionStage::Fault;
|
||||
m_lastError = valid ? "上次视觉事务尚未完成,请核对机器人和 PLC 后恢复"
|
||||
: "视觉恢复记录无法读取或已损坏,请核对机器人和 PLC 后恢复";
|
||||
}
|
||||
|
||||
void ProductionService::publishVisionControl()
|
||||
{
|
||||
const bool locked = m_visionNeedsReconcile || (m_state != Idle && m_state != Completed);
|
||||
const bool maintenance = m_visionNeedsReconcile && !m_shutdownRequested
|
||||
&& !m_visionRecoveryChecking && !m_visionFaultCleanupPending
|
||||
&& (m_state == Idle || (m_state == ManualConfirm && m_visionPauseConfirmed))
|
||||
&& !m_actionStarting && !m_actionPollPending && !m_actionCancelRequested
|
||||
&& !m_deviceService->productionActionActive();
|
||||
emit visionProductionActiveChanged(locked, maintenance);
|
||||
}
|
||||
|
||||
bool ProductionService::visionCurrent(quint64 generation) const
|
||||
{
|
||||
return generation == m_visionGeneration && !m_visionSuspended && !stopRequested()
|
||||
&& !(m_externalPauseRequested && m_externalPauseRequested->load())
|
||||
&& isActive() && m_currentWorkpiece.id == m_visionWorkpieceId
|
||||
&& m_currentTrayNo == m_visionTrayNo && m_visionReady
|
||||
&& m_availableVisionSession == m_visionSession
|
||||
&& m_currentTrayNo == m_visionTrayNo
|
||||
&& m_deviceStatusValid && m_deviceStatus.plcConnected
|
||||
&& m_deviceStatus.safetyDoorClosed && !m_deviceStatus.emergencyStop;
|
||||
}
|
||||
@@ -205,18 +254,15 @@ void ProductionService::setVisionStage(VisionStage stage, const QString &message
|
||||
QString name;
|
||||
switch (stage) {
|
||||
case VisionStage::Idle: name = "待机"; break;
|
||||
case VisionStage::Preflight: name = "检查握手复位"; break;
|
||||
case VisionStage::Armed: name = "允许料架取料"; break;
|
||||
case VisionStage::WaitRequest: name = "等待拍照请求"; break;
|
||||
case VisionStage::SettingBusy: name = "开始比对"; break;
|
||||
case VisionStage::Preflight: name = "检查拍照请求复位"; break;
|
||||
case VisionStage::Moving: name = "请求移动到拍照位"; break;
|
||||
case VisionStage::WaitArrival: name = "等待到达拍照位"; break;
|
||||
case VisionStage::Clearing: name = "清除拍照请求和到位信号"; break;
|
||||
case VisionStage::ReadingPose: name = "读取 R40 位姿"; break;
|
||||
case VisionStage::Measuring: name = "识别及抓取计算"; break;
|
||||
case VisionStage::WritingPose: name = "写入 R60"; break;
|
||||
case VisionStage::Publishing: name = "发布比对通过"; break;
|
||||
case VisionStage::WaitAck: name = "等待机器人接收"; break;
|
||||
case VisionStage::WaitReset: name = "等待请求及接收复位"; break;
|
||||
case VisionStage::Clearing: name = "清除本轮输出"; break;
|
||||
case VisionStage::Complete: name = "视觉握手完成"; break;
|
||||
case VisionStage::Publishing: name = "写入是否比对取料"; break;
|
||||
case VisionStage::Complete: name = "取料准备完成"; break;
|
||||
case VisionStage::Fault: name = "等待人工核对"; break;
|
||||
}
|
||||
reportVision(name, message);
|
||||
@@ -226,9 +272,8 @@ void ProductionService::prepareVision()
|
||||
{
|
||||
QString error;
|
||||
const VisionConfig config = ConfigManager::instance().visionConfig();
|
||||
if (!config.isValid(&error) || config.visionTrayNo(m_currentTrayNo) < 1
|
||||
|| !m_visionReady || m_visionBusy || m_availableVisionSession.isEmpty()) {
|
||||
visionFault(error.isEmpty() ? "视觉未就绪或缺少料盘配置对应关系" : error);
|
||||
if (!config.isValid(&error)) {
|
||||
visionFault(error);
|
||||
return;
|
||||
}
|
||||
++m_visionGeneration;
|
||||
@@ -238,8 +283,15 @@ void ProductionService::prepareVision()
|
||||
m_visionRequest = QJsonObject();
|
||||
m_visionResult = QJsonObject();
|
||||
m_visionCommitStarted = false;
|
||||
m_visionEnabled = config.enabled;
|
||||
m_visionIoPending = false;
|
||||
m_visionRecoveryChecking = false;
|
||||
m_visionPauseConfirmed = false;
|
||||
// Persist before any pickup motion, so a crash cannot erase an in-flight transaction.
|
||||
if (!saveVisionRecoveryState(true, &error)) {
|
||||
visionFault(error);
|
||||
return;
|
||||
}
|
||||
setVisionStage(VisionStage::Preflight, "核对上一轮相机握手已全部复位");
|
||||
advanceVision();
|
||||
}
|
||||
@@ -252,17 +304,18 @@ void ProductionService::advanceVision()
|
||||
return;
|
||||
}
|
||||
const VisionConfig config = ConfigManager::instance().visionConfig();
|
||||
const int limit = m_visionStage == VisionStage::Measuring ? config.requestTimeoutMs
|
||||
: (m_visionStage == VisionStage::WaitAck || m_visionStage == VisionStage::WaitReset)
|
||||
? config.robotAckTimeoutMs : ConfigManager::instance().productionConfig().stateTimeoutMs;
|
||||
const qint64 limit = m_visionStage == VisionStage::Measuring
|
||||
? qint64(config.requestTimeoutMs) + 2LL * config.initializeTimeoutMs
|
||||
: ConfigManager::instance().productionConfig().stateTimeoutMs;
|
||||
if (m_visionTimer.isValid() && m_visionTimer.elapsed() > limit) {
|
||||
visionFault("视觉流程等待超时,请核对 PLC 与机器人状态后人工恢复",
|
||||
!m_visionCommitStarted);
|
||||
if (m_visionStage == VisionStage::Measuring)
|
||||
skipVision("视觉等待超时");
|
||||
else
|
||||
visionFault("拍照到位或 PLC 操作等待超时,请核对机器人状态");
|
||||
return;
|
||||
}
|
||||
if (m_visionIoPending) return;
|
||||
if (m_visionStage != VisionStage::Preflight && m_visionStage != VisionStage::WaitRequest
|
||||
&& m_visionStage != VisionStage::WaitAck && m_visionStage != VisionStage::WaitReset) return;
|
||||
if (m_visionStage != VisionStage::Preflight && m_visionStage != VisionStage::WaitArrival) return;
|
||||
const quint64 generation = m_visionGeneration;
|
||||
m_visionIoPending = true;
|
||||
m_deviceService->readVisionHandshake(
|
||||
@@ -277,51 +330,43 @@ void ProductionService::advanceVision()
|
||||
void ProductionService::handleVisionHandshake(const VisionHandshake &handshake)
|
||||
{
|
||||
if (m_visionStage == VisionStage::Preflight) {
|
||||
if (handshake.robotReceived || handshake.photoRequest || handshake.comparing
|
||||
|| handshake.comparisonError || handshake.comparisonPassed) {
|
||||
visionFault("取料开始前已有相机握手信号置位,需人工核对复位");
|
||||
// M852 deliberately retains the previous pick mode.
|
||||
if (handshake.photoMove || handshake.photoArrived) {
|
||||
visionFault("拍照移动开始前 M850/M851 尚未复位,需人工核对");
|
||||
return;
|
||||
}
|
||||
setVisionStage(VisionStage::Armed, "握手已复位,允许发送料架取料命令使机器人到达拍照位");
|
||||
} else if (m_visionStage == VisionStage::WaitRequest) {
|
||||
if (handshake.robotReceived || handshake.comparing || handshake.comparisonError
|
||||
|| handshake.comparisonPassed) {
|
||||
visionFault("新拍照请求前出现旧接收确认或非预期比对输出");
|
||||
return;
|
||||
}
|
||||
if (!handshake.photoRequest) return;
|
||||
const quint64 generation = m_visionGeneration;
|
||||
setVisionStage(VisionStage::SettingBusy, "机器人已到位停稳,置位相机比对中");
|
||||
m_visionIoPending = true;
|
||||
m_deviceService->writeVisionSignal(DeviceService::Comparing, true,
|
||||
setVisionStage(VisionStage::Moving, "写托盘号和格位,然后置 M850");
|
||||
m_deviceService->moveToPhotoPosition(m_currentTrayNo, m_currentSlotNo,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
visionFault("相机比对中信号写入未确认:" + error); return;
|
||||
visionFault("移动到拍照位置请求未确认:" + error);
|
||||
return;
|
||||
}
|
||||
beginVisionMeasurement();
|
||||
setVisionStage(VisionStage::WaitArrival, "等待机器人 M851 到位反馈");
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
} else if (m_visionStage == VisionStage::WaitAck) {
|
||||
if (!handshake.photoRequest || !handshake.comparisonPassed || handshake.comparisonError) {
|
||||
visionFault("机器人接收确认前,PLC 已改变拍照请求或结果握手");
|
||||
return;
|
||||
}
|
||||
if (handshake.robotReceived)
|
||||
setVisionStage(VisionStage::WaitReset, "机器人已接收目标,等待 850、851 复位");
|
||||
} else if (m_visionStage == VisionStage::WaitReset) {
|
||||
if (handshake.comparisonError || !handshake.comparisonPassed) {
|
||||
visionFault("请求及接收复位完成前,结果握手被改变"); return;
|
||||
}
|
||||
if (!handshake.robotReceived && !handshake.photoRequest) {
|
||||
setVisionStage(VisionStage::Clearing, "接收及请求已复位,清除 ssp 本轮比对输出");
|
||||
clearVisionOutputs(0, m_visionGeneration);
|
||||
}
|
||||
} else if (m_visionStage == VisionStage::WaitArrival) {
|
||||
if (!handshake.photoArrived) return;
|
||||
setVisionStage(VisionStage::Clearing, "机器人已到位,清 M850/M851");
|
||||
clearVisionOutputs(0, m_visionGeneration);
|
||||
}
|
||||
}
|
||||
|
||||
void ProductionService::beginVisionMeasurement()
|
||||
{
|
||||
const VisionConfig config = ConfigManager::instance().visionConfig();
|
||||
if (!m_visionEnabled) {
|
||||
publishPickMode(false);
|
||||
return;
|
||||
}
|
||||
if (config.visionTrayNo(m_currentTrayNo) < 1 || m_availableVisionSession.isEmpty()) {
|
||||
skipVision("缺少视觉料盘映射或视觉进程未连接");
|
||||
return;
|
||||
}
|
||||
m_visionSession = m_availableVisionSession;
|
||||
const quint64 generation = m_visionGeneration;
|
||||
setVisionStage(VisionStage::ReadingPose, "从 R40 整组读取本次拍照 XYZABC");
|
||||
m_visionIoPending = true;
|
||||
@@ -330,7 +375,7 @@ void ProductionService::beginVisionMeasurement()
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (!success || !pose.isValid()) {
|
||||
visionFault("R40 拍照位姿读取失败:" + error, true); return;
|
||||
visionFault("R40 拍照位姿读取失败:" + error); return;
|
||||
}
|
||||
QJsonArray values;
|
||||
for (double value : pose.xyzabc) values.append(value);
|
||||
@@ -354,17 +399,17 @@ void ProductionService::beginVisionMeasurement()
|
||||
|
||||
void ProductionService::onVisionAvailability(bool ready, bool busy, const QString &session)
|
||||
{
|
||||
m_visionReady = ready;
|
||||
m_visionBusy = busy;
|
||||
Q_UNUSED(ready)
|
||||
Q_UNUSED(busy)
|
||||
m_availableVisionSession = session;
|
||||
if (m_visionStage != VisionStage::Idle && m_visionStage != VisionStage::Fault
|
||||
&& (!ready || session != m_visionSession))
|
||||
visionFault("VisionGuide 已断开、未就绪或进程会话改变");
|
||||
if (m_visionStage == VisionStage::Measuring && session != m_visionSession
|
||||
&& visionCurrent(m_visionGeneration))
|
||||
skipVision("VisionGuide 连接中断或进程会话改变");
|
||||
}
|
||||
|
||||
void ProductionService::publishVisionSnapshot()
|
||||
{
|
||||
emit visionProductionActiveChanged(m_visionNeedsReconcile || (m_state != Idle && m_state != Completed));
|
||||
publishVisionControl();
|
||||
reportVision(m_visionNeedsReconcile ? "等待人工核对" : "待机",
|
||||
m_visionNeedsReconcile ? m_lastError : "等待下一次料架取料");
|
||||
}
|
||||
@@ -375,15 +420,18 @@ void ProductionService::onVisionResult(const QJsonObject &result)
|
||||
|| result.value("request_id") != m_visionRequest.value("request_id")) return;
|
||||
if (result.value("context") != m_visionRequest.value("context")
|
||||
|| result.value("server_session_id").toString() != m_visionSession) {
|
||||
visionFault("视觉结果与本次取料上下文不一致", true); return;
|
||||
skipVision("视觉结果与本次取料上下文不一致"); return;
|
||||
}
|
||||
RobotPose target, camera;
|
||||
for (const QJsonValue &value : result.value("robot_pose").toArray()) {
|
||||
if (!value.isDouble()) { visionFault("机器人坐标格式无效", true); return; }
|
||||
if (!value.isDouble() || !std::isfinite(value.toDouble())
|
||||
|| qAbs(value.toDouble()) > std::numeric_limits<float>::max()) {
|
||||
skipVision("机器人坐标无效或超出 PLC float 范围"); return;
|
||||
}
|
||||
target.xyzabc.append(value.toDouble());
|
||||
}
|
||||
for (const QJsonValue &value : result.value("camera_pose").toArray()) {
|
||||
if (!value.isDouble()) { visionFault("相机坐标格式无效", true); return; }
|
||||
if (!value.isDouble()) { skipVision("相机坐标格式无效"); return; }
|
||||
camera.xyzabc.append(value.toDouble());
|
||||
}
|
||||
if (result.value("status").toString() != "ok" || !result.value("robot_valid").toBool()
|
||||
@@ -394,61 +442,74 @@ void ProductionService::onVisionResult(const QJsonObject &result)
|
||||
|| !result.value("score").isDouble() || !std::isfinite(result.value("score").toDouble())
|
||||
|| result.value("score").toDouble() < .70 || result.value("score").toDouble() > 1.0
|
||||
|| result.value("calibration_fingerprint").toString().isEmpty()) {
|
||||
visionFault("视觉结果校验失败,禁止写入 R60:" + result.value("message").toString(), true); return;
|
||||
skipVision("视觉识别失败或结果无效:" + result.value("message").toString()); return;
|
||||
}
|
||||
m_visionResult = result;
|
||||
const quint64 generation = m_visionGeneration;
|
||||
// Re-read the request/ack before committing, while the robot holds the photo pose.
|
||||
m_visionIoPending = true;
|
||||
setVisionStage(VisionStage::WritingPose, "提交 R60 前再次核对拍照请求和接收信号");
|
||||
m_deviceService->readVisionHandshake(
|
||||
[this, generation, target](bool success, const VisionHandshake &handshake, const QString &error) {
|
||||
m_visionCommitStarted = true;
|
||||
setVisionStage(VisionStage::WritingPose, "整组写入 R60,确认后选择比对取料");
|
||||
m_deviceService->writeRobotGraspPose(target,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (!success || !handshake.photoRequest || handshake.robotReceived
|
||||
|| !handshake.comparing || handshake.comparisonPassed || handshake.comparisonError) {
|
||||
m_visionIoPending = false;
|
||||
visionFault("提交 R60 前机器人请求发生变化:" + error); return;
|
||||
m_visionIoPending = false;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
visionFault("R60 写入未确认,禁止继续取料:" + error);
|
||||
return;
|
||||
}
|
||||
m_visionCommitStarted = true;
|
||||
reportVision("写入 R60", "整组写入抓取坐标,尚未收到机器人接收确认");
|
||||
m_deviceService->writeRobotGraspPose(target,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
m_visionIoPending = false;
|
||||
visionFault("R60 写入未确认,禁止自动重发目标:" + error); return;
|
||||
}
|
||||
setVisionStage(VisionStage::Publishing, "R60 写入已确认,置位比对通过");
|
||||
m_deviceService->writeVisionSignal(DeviceService::ComparisonPassed, true,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
visionFault("比对通过信号发布未确认:" + error); return;
|
||||
}
|
||||
setVisionStage(VisionStage::WaitAck, "结果已发布,等待机器人接收成功信号 850");
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
});
|
||||
publishPickMode(true);
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
}
|
||||
|
||||
void ProductionService::publishPickMode(bool useVision)
|
||||
{
|
||||
const quint64 generation = m_visionGeneration;
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = true;
|
||||
setVisionStage(VisionStage::Publishing, useVision ? "写 M852=1,使用本次 R60" : "写 M852=0,使用普通取料");
|
||||
m_deviceService->writeVisionSignal(DeviceService::ComparisonPick, useVision,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
visionFault("M852 写入未确认,禁止启动 RackPick:" + error);
|
||||
return;
|
||||
}
|
||||
setVisionStage(VisionStage::Complete, "取料模式已确认,允许普通 RackPick");
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
}
|
||||
|
||||
void ProductionService::skipVision(const QString &reason)
|
||||
{
|
||||
if (!visionCurrent(m_visionGeneration)) return;
|
||||
const QString requestId = m_visionRequest.value("request_id").toString();
|
||||
m_visionResult = QJsonObject();
|
||||
// Change stage before cancellation can deliver a synchronous terminal callback.
|
||||
setVisionStage(VisionStage::Publishing, reason + ";本次改为普通取料");
|
||||
reportVision("视觉跳过", reason + ";本次不使用 R60", true);
|
||||
qWarning().noquote() << "Vision fallback:" << reason;
|
||||
persistRecoveryAction("VISION_FALLBACK", reason);
|
||||
emit visionCancelRequested(requestId);
|
||||
publishPickMode(false);
|
||||
}
|
||||
|
||||
void ProductionService::clearVisionOutputs(int index, quint64 generation)
|
||||
{
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (index == 3) {
|
||||
if (index == 2) {
|
||||
m_visionIoPending = false;
|
||||
setVisionStage(VisionStage::Complete, "机器人接收及信号复位完成,继续等待原取料动作完成");
|
||||
beginVisionMeasurement();
|
||||
return;
|
||||
}
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::ComparisonPassed,
|
||||
DeviceService::ComparisonError, DeviceService::Comparing};
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::PhotoMove, DeviceService::PhotoArrived};
|
||||
m_visionIoPending = true;
|
||||
m_deviceService->writeVisionSignal(outputs[index], false,
|
||||
[this, index, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
m_visionIoPending = false;
|
||||
visionFault("比对输出复位未确认:" + error); return;
|
||||
visionFault("M850/M851 清除未确认:" + error);
|
||||
return;
|
||||
}
|
||||
clearVisionOutputs(index + 1, generation);
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
@@ -462,49 +523,45 @@ void ProductionService::invalidateVision(const QString &reason)
|
||||
m_visionNeedsReconcile = true;
|
||||
m_visionIoPending = false;
|
||||
m_visionRecoveryChecking = false;
|
||||
m_visionPauseConfirmed = false;
|
||||
QString persistenceError;
|
||||
if (!saveVisionRecoveryState(true, &persistenceError)) {
|
||||
m_lastError = persistenceError;
|
||||
emit productionError(persistenceError);
|
||||
}
|
||||
emit visionCancelRequested(m_visionRequest.value("request_id").toString());
|
||||
m_deviceService->cancelVisionRequests(reason);
|
||||
m_visionStage = VisionStage::Fault;
|
||||
reportVision("等待人工核对", reason + (m_visionCommitStarted
|
||||
? ";R60 或有效信号可能已经到达机器人,需现场核对" : QString()), true);
|
||||
emit visionProductionActiveChanged(true);
|
||||
emit visionProductionActiveChanged(true, false);
|
||||
}
|
||||
|
||||
void ProductionService::visionFault(const QString &reason, bool reportComparisonError)
|
||||
void ProductionService::visionFault(const QString &reason)
|
||||
{
|
||||
if (m_visionStage == VisionStage::Fault) return;
|
||||
const bool mayReport = reportComparisonError && !m_visionCommitStarted
|
||||
&& m_visionStage != VisionStage::Idle && m_visionStage != VisionStage::Preflight;
|
||||
m_timeoutState = m_state;
|
||||
m_lastError = reason;
|
||||
invalidateVision(reason);
|
||||
// A preflight rejection also requires an explicit operator recovery.
|
||||
m_visionNeedsReconcile = true;
|
||||
invalidateVision(reason);
|
||||
m_visionSuspended = true;
|
||||
m_visionStage = VisionStage::Fault;
|
||||
m_timer->stop();
|
||||
pauseProcessTimer();
|
||||
m_visionFaultCleanupPending = true;
|
||||
setState(ManualConfirm);
|
||||
reportVision("等待人工核对", reason, true);
|
||||
if (mayReport) {
|
||||
const quint64 generation = m_visionGeneration;
|
||||
m_deviceService->writeVisionSignal(DeviceService::ComparisonError, true,
|
||||
[this](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (outcome != VisionWriteOutcome::Confirmed)
|
||||
reportVision("等待人工核对", "比对错误输出未确认:" + error, true);
|
||||
}, [this, generation]() {
|
||||
return generation == m_visionGeneration && !stopRequested()
|
||||
&& !(m_externalPauseRequested && m_externalPauseRequested->load());
|
||||
});
|
||||
}
|
||||
m_visionFaultCleanupPending = true;
|
||||
cancelPendingProductionAction([this](bool success, const QString &error) {
|
||||
if (!success) {
|
||||
m_visionFaultCleanupPending = false;
|
||||
publishVisionControl();
|
||||
emit productionError("PLC 动作取消失败:" + error); return;
|
||||
}
|
||||
m_deviceService->pause([this](bool paused, const QString &error) {
|
||||
m_visionFaultCleanupPending = false;
|
||||
m_visionPauseConfirmed = paused;
|
||||
publishVisionControl();
|
||||
if (!paused) emit productionError("PLC 暂停失败:" + error);
|
||||
});
|
||||
});
|
||||
@@ -520,26 +577,27 @@ bool ProductionService::recoverVision(QString *error)
|
||||
return false;
|
||||
}
|
||||
if (!checkRecoverySafety(error)) return false;
|
||||
if (!m_visionReady || m_visionBusy || m_visionFaultCleanupPending || m_deviceService->productionActionBusy()
|
||||
if (m_visionFaultCleanupPending || m_deviceService->productionActionBusy()
|
||||
|| m_deviceService->productionActionActive()) {
|
||||
if (error) *error = "请等待 PLC 操作结束且 VisionGuide 就绪后恢复";
|
||||
if (error) *error = "请等待 PLC 操作结束后恢复";
|
||||
return false;
|
||||
}
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
m_visionRecoveryChecking = true;
|
||||
m_visionRecoverySession = m_availableVisionSession;
|
||||
publishVisionControl();
|
||||
const quint64 generation = m_visionGeneration;
|
||||
m_deviceService->readVisionHandshake(
|
||||
[this, generation](bool success, const VisionHandshake &handshake, const QString &error) {
|
||||
if (generation != m_visionGeneration || stopRequested()
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())
|
||||
|| !m_visionReady || m_visionBusy || m_availableVisionSession != m_visionRecoverySession) {
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())) {
|
||||
m_visionRecoveryChecking = false;
|
||||
publishVisionControl();
|
||||
return;
|
||||
}
|
||||
m_visionRecoveryChecking = false;
|
||||
if (!success || handshake.photoRequest || handshake.robotReceived) {
|
||||
emit productionError("人工恢复要求机器人接收成功和拍照请求均已复位:" + error);
|
||||
if (!success || handshake.photoMove || handshake.photoArrived) {
|
||||
publishVisionControl();
|
||||
emit productionError("人工恢复要求M850 移动请求和 M851 到位反馈均已复位:" + error);
|
||||
return;
|
||||
}
|
||||
m_visionRecoveryChecking = true;
|
||||
@@ -551,27 +609,26 @@ bool ProductionService::recoverVision(QString *error)
|
||||
void ProductionService::resetVisionForRecovery(int index, quint64 generation)
|
||||
{
|
||||
if (generation != m_visionGeneration || stopRequested()
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())
|
||||
|| !m_visionReady || m_visionBusy || m_availableVisionSession != m_visionRecoverySession) {
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())) {
|
||||
m_visionRecoveryChecking = false;
|
||||
publishVisionControl();
|
||||
return;
|
||||
}
|
||||
if (index < 3) {
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::ComparisonPassed,
|
||||
DeviceService::ComparisonError, DeviceService::Comparing};
|
||||
if (index < 2) {
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::PhotoMove, DeviceService::PhotoArrived};
|
||||
m_deviceService->writeVisionSignal(outputs[index], false,
|
||||
[this, index, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (generation != m_visionGeneration) return;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
m_visionRecoveryChecking = false;
|
||||
publishVisionControl();
|
||||
emit productionError("视觉恢复时输出复位未确认:" + error);
|
||||
return;
|
||||
}
|
||||
resetVisionForRecovery(index + 1, generation);
|
||||
}, [this, generation]() {
|
||||
return generation == m_visionGeneration && !stopRequested()
|
||||
&& !(m_externalPauseRequested && m_externalPauseRequested->load())
|
||||
&& m_visionReady && !m_visionBusy && m_availableVisionSession == m_visionRecoverySession;
|
||||
&& !(m_externalPauseRequested && m_externalPauseRequested->load());
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -579,10 +636,12 @@ void ProductionService::resetVisionForRecovery(int index, quint64 generation)
|
||||
[this, generation](bool success, const VisionHandshake &handshake, const QString &error) {
|
||||
m_visionRecoveryChecking = false;
|
||||
if (generation != m_visionGeneration || stopRequested()
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())
|
||||
|| !m_visionReady || m_visionBusy || m_availableVisionSession != m_visionRecoverySession) return;
|
||||
if (!success || handshake.photoRequest || handshake.robotReceived || handshake.comparing
|
||||
|| handshake.comparisonError || handshake.comparisonPassed) {
|
||||
|| (m_externalPauseRequested && m_externalPauseRequested->load())) {
|
||||
publishVisionControl();
|
||||
return;
|
||||
}
|
||||
if (!success || handshake.photoMove || handshake.photoArrived) {
|
||||
publishVisionControl();
|
||||
emit productionError("视觉恢复核对期间握手信号未保持复位:" + error);
|
||||
return;
|
||||
}
|
||||
@@ -593,12 +652,24 @@ void ProductionService::resetVisionForRecovery(int index, quint64 generation)
|
||||
|| (mapped != FrontChecking && mapped != BackChecking)) {
|
||||
m_lastError = "当前 PLC 步骤不能明确恢复视觉中断的取料动作。请先终止当前任务,核对机器人后再执行视觉恢复;不会自动重发取料或 R60。";
|
||||
setState(ManualConfirm);
|
||||
publishVisionControl();
|
||||
reportVision("等待人工核对", m_lastError, true);
|
||||
emit timeoutRecoveryRequired("VISION", m_lastError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
QString persistenceError;
|
||||
if (!saveVisionRecoveryState(false, &persistenceError)) {
|
||||
publishVisionControl();
|
||||
emit productionError(persistenceError);
|
||||
return;
|
||||
}
|
||||
persistRecoveryAction("VISION_CONFIRMED", "Operator verified the robot and PLC; handshake is reset; old target discarded");
|
||||
if (stopRequested() || (m_externalPauseRequested && m_externalPauseRequested->load())) {
|
||||
if (!saveVisionRecoveryState(true, &persistenceError)) emit productionError(persistenceError);
|
||||
publishVisionControl();
|
||||
return;
|
||||
}
|
||||
m_visionNeedsReconcile = false;
|
||||
m_visionSuspended = false;
|
||||
m_visionCommitStarted = false;
|
||||
@@ -607,7 +678,7 @@ void ProductionService::resetVisionForRecovery(int index, quint64 generation)
|
||||
m_visionResult = QJsonObject();
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
reportVision("人工恢复完成", "现场核对及信号复位完成,已丢弃旧视觉目标", true);
|
||||
emit visionProductionActiveChanged(m_state != Idle && m_state != Completed);
|
||||
publishVisionControl();
|
||||
if (m_state == Idle || m_state == Completed) return;
|
||||
QString recoveryError;
|
||||
if (!recover(&recoveryError)) emit productionError(recoveryError);
|
||||
@@ -617,16 +688,11 @@ void ProductionService::resetVisionForRecovery(int index, quint64 generation)
|
||||
bool ProductionService::start(int orderId, QString *error)
|
||||
{
|
||||
const VisionConfig vision = ConfigManager::instance().visionConfig();
|
||||
if (m_visionNeedsReconcile || (vision.enabled && !vision.isValid(error))) {
|
||||
if (m_visionNeedsReconcile || (!ConfigManager::instance().simulationMode() && !vision.isValid(error))) {
|
||||
if (m_visionNeedsReconcile && error)
|
||||
*error = "请先人工核对并恢复上次视觉握手,再启动生产";
|
||||
return false;
|
||||
}
|
||||
if (vision.enabled && !ConfigManager::instance().simulationMode()
|
||||
&& (!m_visionReady || m_visionBusy)) {
|
||||
if (error) *error = "请先初始化 VisionGuide,等待视觉就绪后启动生产";
|
||||
return false;
|
||||
}
|
||||
if (orderId <= 0) {
|
||||
if (error) {
|
||||
*error = "Order id must be greater than 0";
|
||||
@@ -789,6 +855,7 @@ bool ProductionService::pause(QString *error)
|
||||
|
||||
pauseProcessTimer();
|
||||
m_timer->stop();
|
||||
m_visionPauseConfirmed = true;
|
||||
setState(m_visionNeedsReconcile ? ManualConfirm : Paused);
|
||||
if (m_visionNeedsReconcile)
|
||||
emit timeoutRecoveryRequired("VISION", "视觉流程已中断,请核对 PLC 和机器人后恢复");
|
||||
@@ -1113,11 +1180,29 @@ bool ProductionService::restoreActiveRun(QString *error)
|
||||
m_visionNeedsReconcile = true;
|
||||
m_visionSuspended = true;
|
||||
m_visionStage = VisionStage::Fault;
|
||||
QString stateError;
|
||||
if (!saveVisionRecoveryState(true, &stateError)) emit productionError(stateError);
|
||||
reportVision("等待人工核对", "恢复未完成的生产记录,需核对相机与机器人握手", true);
|
||||
}
|
||||
setState(ManualConfirm);
|
||||
m_heartbeatTimer.restart();
|
||||
m_timer->stop();
|
||||
if (m_visionNeedsReconcile) {
|
||||
// Startup recovery must confirm a PLC pause before camera maintenance is offered.
|
||||
const quint64 generation = m_visionGeneration;
|
||||
m_visionFaultCleanupPending = true;
|
||||
publishVisionControl();
|
||||
m_deviceService->pause([this, generation](bool paused, const QString &pauseError) {
|
||||
m_visionFaultCleanupPending = false;
|
||||
if (generation != m_visionGeneration || m_state != ManualConfirm) {
|
||||
publishVisionControl();
|
||||
return;
|
||||
}
|
||||
m_visionPauseConfirmed = paused;
|
||||
publishVisionControl();
|
||||
if (!paused) emit productionError("启动恢复时 PLC 暂停未确认:" + pauseError);
|
||||
});
|
||||
}
|
||||
emit timeoutRecoveryRequired(
|
||||
"STARTUP_RECOVERY",
|
||||
QString("Unfinished production run %1 requires recovery confirmation")
|
||||
@@ -1324,7 +1409,7 @@ void ProductionService::setState(State state)
|
||||
m_state = state;
|
||||
m_stateTimer.restart();
|
||||
emit stateChanged(m_state, stateName());
|
||||
emit visionProductionActiveChanged(m_visionNeedsReconcile || (m_state != Idle && m_state != Completed));
|
||||
publishVisionControl();
|
||||
|
||||
if (m_runId > 0) {
|
||||
const QString processStep = processStepForState(state).isEmpty()
|
||||
@@ -1454,11 +1539,11 @@ bool ProductionService::processProductionAction(
|
||||
prepareVision();
|
||||
return true;
|
||||
}
|
||||
if (m_visionStage == VisionStage::Preflight) {
|
||||
if (m_visionStage != VisionStage::Complete) {
|
||||
advanceVision();
|
||||
return true;
|
||||
}
|
||||
if (m_visionStage == VisionStage::Fault) return true;
|
||||
if (!visionCurrent(m_visionGeneration)) return true;
|
||||
}
|
||||
|
||||
if (!m_actionPending) {
|
||||
@@ -1468,17 +1553,20 @@ bool ProductionService::processProductionAction(
|
||||
m_actionPollPending = false;
|
||||
m_actionCompleted = false;
|
||||
m_actionError.clear();
|
||||
const quint64 generation = m_visionGeneration;
|
||||
const std::function<bool()> beforeSend = requiresVision(action)
|
||||
? std::function<bool()>([this, generation]() {
|
||||
return visionCurrent(generation) && m_visionStage == VisionStage::Complete;
|
||||
}) : std::function<bool()>();
|
||||
m_deviceService->beginProductionAction(
|
||||
action, m_currentTrayNo, m_currentSlotNo,
|
||||
[this](bool success, const QString &actionError) {
|
||||
m_actionStarting = false;
|
||||
if (!success) {
|
||||
m_actionError = actionError;
|
||||
} else if (m_visionStage == VisionStage::Armed && !m_visionSuspended) {
|
||||
setVisionStage(VisionStage::WaitRequest, "等待机器人到位后的拍照请求 851");
|
||||
}
|
||||
tryDeferredActionCancel();
|
||||
});
|
||||
}, beforeSend);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1500,12 +1588,15 @@ bool ProductionService::processProductionAction(
|
||||
return true;
|
||||
}
|
||||
|
||||
if (requiresVision(action) && m_visionStage != VisionStage::Complete) {
|
||||
advanceVision();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_actionCompleted) {
|
||||
if (requiresVision(action)) {
|
||||
QString persistenceError;
|
||||
if (!visionCurrent(m_visionGeneration) || !saveVisionRecoveryState(false, &persistenceError)
|
||||
|| !visionCurrent(m_visionGeneration)) {
|
||||
visionFault(persistenceError.isEmpty() ? "取料完成期间状态改变,需人工核对" : persistenceError);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
*completed = true;
|
||||
m_actionPending = false;
|
||||
m_actionCompleted = false;
|
||||
|
||||
Reference in New Issue
Block a user