添加管控软件和相机的通讯
This commit is contained in:
@@ -5,10 +5,16 @@
|
||||
#include "orderservice.h"
|
||||
#include "trayservice.h"
|
||||
#include "workpieceservice.h"
|
||||
#include "visionservice.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QScopedValueRollback>
|
||||
#include <QTimer>
|
||||
#include <QUuid>
|
||||
#include <cmath>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -64,6 +70,22 @@ ProductionService::State ProductionService::state() const
|
||||
return m_state;
|
||||
}
|
||||
|
||||
ProductionService::~ProductionService()
|
||||
{
|
||||
m_timer->stop();
|
||||
m_state = Terminating;
|
||||
m_stopRequested.store(true);
|
||||
++m_visionGeneration;
|
||||
m_visionSuspended = true;
|
||||
m_actionCancelRequested = false;
|
||||
m_deferredCancelCallback = DeviceService::OperationCallback();
|
||||
m_shutdownCallback = DeviceService::OperationCallback();
|
||||
m_shutdownRequested = false;
|
||||
// Drain request callbacks while this object's value members still exist.
|
||||
delete m_deviceService;
|
||||
m_deviceService = nullptr;
|
||||
}
|
||||
|
||||
QString ProductionService::stateName() const
|
||||
{
|
||||
return stateNameForState(m_state);
|
||||
@@ -130,13 +152,481 @@ void ProductionService::setExternalStopFlag(std::atomic_bool *flag)
|
||||
m_externalStopRequested = flag;
|
||||
}
|
||||
|
||||
void ProductionService::setExternalPauseFlag(std::atomic_bool *flag)
|
||||
{
|
||||
m_externalPauseRequested = flag;
|
||||
}
|
||||
|
||||
void ProductionService::requestStopImmediate()
|
||||
{
|
||||
m_stopRequested.store(true);
|
||||
}
|
||||
|
||||
bool ProductionService::requiresVision(DeviceService::ProductionAction action) const
|
||||
{
|
||||
return action == DeviceService::RackPick
|
||||
&& ConfigManager::instance().visionConfig().enabled
|
||||
&& !ConfigManager::instance().simulationMode();
|
||||
}
|
||||
|
||||
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_deviceStatusValid && m_deviceStatus.plcConnected
|
||||
&& m_deviceStatus.safetyDoorClosed && !m_deviceStatus.emergencyStop;
|
||||
}
|
||||
|
||||
void ProductionService::reportVision(const QString &phase, const QString &message, bool invalidated)
|
||||
{
|
||||
QJsonObject cycle{{"phase", phase}, {"message", message}, {"invalidated", invalidated},
|
||||
{"recovery_required", m_visionNeedsReconcile},
|
||||
{"request_id", m_visionRequest.value("request_id")},
|
||||
{"context", m_visionRequest.value("context")}};
|
||||
if (!m_visionRequest.isEmpty()) cycle.insert("photo_pose", m_visionRequest.value("robot_pose"));
|
||||
if (!m_visionResult.isEmpty()) cycle.insert("robot_pose", m_visionResult.value("robot_pose"));
|
||||
if (!m_visionResult.isEmpty()) {
|
||||
cycle.insert("camera_pose", m_visionResult.value("camera_pose"));
|
||||
cycle.insert("parameters", m_visionResult.value("parameters"));
|
||||
cycle.insert("calibration_fingerprint", m_visionResult.value("calibration_fingerprint"));
|
||||
cycle.insert("measure_elapsed_ms", m_visionResult.value("measure_elapsed_ms"));
|
||||
cycle.insert("grasp_elapsed_ms", m_visionResult.value("grasp_elapsed_ms"));
|
||||
}
|
||||
emit visionCycleChanged(cycle);
|
||||
}
|
||||
|
||||
void ProductionService::setVisionStage(VisionStage stage, const QString &message)
|
||||
{
|
||||
m_visionStage = stage;
|
||||
m_visionTimer.restart();
|
||||
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::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::Fault: name = "等待人工核对"; break;
|
||||
}
|
||||
reportVision(name, message);
|
||||
}
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
++m_visionGeneration;
|
||||
m_visionWorkpieceId = m_currentWorkpiece.id;
|
||||
m_visionTrayNo = m_currentTrayNo;
|
||||
m_visionSession = m_availableVisionSession;
|
||||
m_visionRequest = QJsonObject();
|
||||
m_visionResult = QJsonObject();
|
||||
m_visionCommitStarted = false;
|
||||
m_visionIoPending = false;
|
||||
m_visionRecoveryChecking = false;
|
||||
setVisionStage(VisionStage::Preflight, "核对上一轮相机握手已全部复位");
|
||||
advanceVision();
|
||||
}
|
||||
|
||||
void ProductionService::advanceVision()
|
||||
{
|
||||
if (m_visionSuspended || m_visionStage == VisionStage::Fault) return;
|
||||
if (!visionCurrent(m_visionGeneration)) {
|
||||
visionFault("当前工件、视觉进程会话或设备状态已改变");
|
||||
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;
|
||||
if (m_visionTimer.isValid() && m_visionTimer.elapsed() > limit) {
|
||||
visionFault("视觉流程等待超时,请核对 PLC 与机器人状态后人工恢复",
|
||||
!m_visionCommitStarted);
|
||||
return;
|
||||
}
|
||||
if (m_visionIoPending) return;
|
||||
if (m_visionStage != VisionStage::Preflight && m_visionStage != VisionStage::WaitRequest
|
||||
&& m_visionStage != VisionStage::WaitAck && m_visionStage != VisionStage::WaitReset) return;
|
||||
const quint64 generation = m_visionGeneration;
|
||||
m_visionIoPending = true;
|
||||
m_deviceService->readVisionHandshake(
|
||||
[this, generation](bool success, const VisionHandshake &handshake, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (!success) { visionFault("读取视觉握手失败:" + error); return; }
|
||||
handleVisionHandshake(handshake);
|
||||
});
|
||||
}
|
||||
|
||||
void ProductionService::handleVisionHandshake(const VisionHandshake &handshake)
|
||||
{
|
||||
if (m_visionStage == VisionStage::Preflight) {
|
||||
if (handshake.robotReceived || handshake.photoRequest || handshake.comparing
|
||||
|| handshake.comparisonError || handshake.comparisonPassed) {
|
||||
visionFault("取料开始前已有相机握手信号置位,需人工核对复位");
|
||||
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,
|
||||
[this, generation](VisionWriteOutcome outcome, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (outcome != VisionWriteOutcome::Confirmed) {
|
||||
visionFault("相机比对中信号写入未确认:" + error); return;
|
||||
}
|
||||
beginVisionMeasurement();
|
||||
}, [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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProductionService::beginVisionMeasurement()
|
||||
{
|
||||
const quint64 generation = m_visionGeneration;
|
||||
setVisionStage(VisionStage::ReadingPose, "从 R40 整组读取本次拍照 XYZABC");
|
||||
m_visionIoPending = true;
|
||||
m_deviceService->readRobotPhotoPose(
|
||||
[this, generation](bool success, const RobotPose &pose, const QString &error) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
m_visionIoPending = false;
|
||||
if (!success || !pose.isValid()) {
|
||||
visionFault("R40 拍照位姿读取失败:" + error, true); return;
|
||||
}
|
||||
QJsonArray values;
|
||||
for (double value : pose.xyzabc) values.append(value);
|
||||
const VisionConfig config = ConfigManager::instance().visionConfig();
|
||||
m_visionRequest = QJsonObject{
|
||||
{"protocol_version", 1}, {"type", "measure"},
|
||||
{"request_id", QUuid::createUuid().toString(QUuid::WithoutBraces)},
|
||||
{"server_session_id", m_visionSession}, {"robot_pose", values},
|
||||
{"pose_source", "plc_r40"},
|
||||
{"pose_read_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs)},
|
||||
{"require_robot_pose", true}, {"parameters", VisionService::standardParameters()},
|
||||
{"convention", VisionService::standardConvention()},
|
||||
{"context", QJsonObject{{"workpiece_id", QString::number(m_visionWorkpieceId)},
|
||||
{"task_id", QString::number(m_runId) + "/RackPick"},
|
||||
{"tray_no", config.visionTrayNo(m_visionTrayNo)},
|
||||
{"generation", QString::number(generation)}}}};
|
||||
setVisionStage(VisionStage::Measuring, "等待左相机识别并计算机器人抓取坐标");
|
||||
emit visionMeasureRequested(m_visionRequest);
|
||||
});
|
||||
}
|
||||
|
||||
void ProductionService::onVisionAvailability(bool ready, bool busy, const QString &session)
|
||||
{
|
||||
m_visionReady = ready;
|
||||
m_visionBusy = busy;
|
||||
m_availableVisionSession = session;
|
||||
if (m_visionStage != VisionStage::Idle && m_visionStage != VisionStage::Fault
|
||||
&& (!ready || session != m_visionSession))
|
||||
visionFault("VisionGuide 已断开、未就绪或进程会话改变");
|
||||
}
|
||||
|
||||
void ProductionService::publishVisionSnapshot()
|
||||
{
|
||||
emit visionProductionActiveChanged(m_visionNeedsReconcile || (m_state != Idle && m_state != Completed));
|
||||
reportVision(m_visionNeedsReconcile ? "等待人工核对" : "待机",
|
||||
m_visionNeedsReconcile ? m_lastError : "等待下一次料架取料");
|
||||
}
|
||||
|
||||
void ProductionService::onVisionResult(const QJsonObject &result)
|
||||
{
|
||||
if (m_visionStage != VisionStage::Measuring || !visionCurrent(m_visionGeneration)
|
||||
|| 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;
|
||||
}
|
||||
RobotPose target, camera;
|
||||
for (const QJsonValue &value : result.value("robot_pose").toArray()) {
|
||||
if (!value.isDouble()) { visionFault("机器人坐标格式无效", true); return; }
|
||||
target.xyzabc.append(value.toDouble());
|
||||
}
|
||||
for (const QJsonValue &value : result.value("camera_pose").toArray()) {
|
||||
if (!value.isDouble()) { visionFault("相机坐标格式无效", true); return; }
|
||||
camera.xyzabc.append(value.toDouble());
|
||||
}
|
||||
if (result.value("status").toString() != "ok" || !result.value("robot_valid").toBool()
|
||||
|| !result.value("camera_valid").toBool() || !target.isValid() || !camera.isValid()
|
||||
|| result.value("source").toString() != "LEFT" || result.value("pose_type").toString() != "model_origin"
|
||||
|| result.value("parameters") != m_visionRequest.value("parameters")
|
||||
|| result.value("convention") != m_visionRequest.value("convention")
|
||||
|| !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;
|
||||
}
|
||||
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) {
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (!success || !handshake.photoRequest || handshake.robotReceived
|
||||
|| !handshake.comparing || handshake.comparisonPassed || handshake.comparisonError) {
|
||||
m_visionIoPending = false;
|
||||
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); });
|
||||
});
|
||||
}
|
||||
|
||||
void ProductionService::clearVisionOutputs(int index, quint64 generation)
|
||||
{
|
||||
if (!visionCurrent(generation)) return;
|
||||
if (index == 3) {
|
||||
m_visionIoPending = false;
|
||||
setVisionStage(VisionStage::Complete, "机器人接收及信号复位完成,继续等待原取料动作完成");
|
||||
return;
|
||||
}
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::ComparisonPassed,
|
||||
DeviceService::ComparisonError, DeviceService::Comparing};
|
||||
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;
|
||||
}
|
||||
clearVisionOutputs(index + 1, generation);
|
||||
}, [this, generation]() { return visionCurrent(generation); });
|
||||
}
|
||||
|
||||
void ProductionService::invalidateVision(const QString &reason)
|
||||
{
|
||||
if (m_visionStage == VisionStage::Idle && !m_visionNeedsReconcile) return;
|
||||
++m_visionGeneration;
|
||||
m_visionSuspended = true;
|
||||
m_visionNeedsReconcile = true;
|
||||
m_visionIoPending = false;
|
||||
m_visionRecoveryChecking = false;
|
||||
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);
|
||||
}
|
||||
|
||||
void ProductionService::visionFault(const QString &reason, bool reportComparisonError)
|
||||
{
|
||||
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;
|
||||
m_visionSuspended = true;
|
||||
m_visionStage = VisionStage::Fault;
|
||||
m_timer->stop();
|
||||
pauseProcessTimer();
|
||||
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;
|
||||
emit productionError("PLC 动作取消失败:" + error); return;
|
||||
}
|
||||
m_deviceService->pause([this](bool paused, const QString &error) {
|
||||
m_visionFaultCleanupPending = false;
|
||||
if (!paused) emit productionError("PLC 暂停失败:" + error);
|
||||
});
|
||||
});
|
||||
persistAlarm("VISION", reason);
|
||||
emit timeoutRecoveryRequired("VISION", reason);
|
||||
emit productionError(reason);
|
||||
}
|
||||
|
||||
bool ProductionService::recoverVision(QString *error)
|
||||
{
|
||||
if (m_visionRecoveryChecking) {
|
||||
if (error) *error = "正在核对视觉恢复状态";
|
||||
return false;
|
||||
}
|
||||
if (!checkRecoverySafety(error)) return false;
|
||||
if (!m_visionReady || m_visionBusy || m_visionFaultCleanupPending || m_deviceService->productionActionBusy()
|
||||
|| m_deviceService->productionActionActive()) {
|
||||
if (error) *error = "请等待 PLC 操作结束且 VisionGuide 就绪后恢复";
|
||||
return false;
|
||||
}
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
m_visionRecoveryChecking = true;
|
||||
m_visionRecoverySession = m_availableVisionSession;
|
||||
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_visionRecoveryChecking = false;
|
||||
return;
|
||||
}
|
||||
m_visionRecoveryChecking = false;
|
||||
if (!success || handshake.photoRequest || handshake.robotReceived) {
|
||||
emit productionError("人工恢复要求机器人接收成功和拍照请求均已复位:" + error);
|
||||
return;
|
||||
}
|
||||
m_visionRecoveryChecking = true;
|
||||
resetVisionForRecovery(0, m_visionGeneration);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
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_visionRecoveryChecking = false;
|
||||
return;
|
||||
}
|
||||
if (index < 3) {
|
||||
const DeviceService::VisionSignal outputs[] = {DeviceService::ComparisonPassed,
|
||||
DeviceService::ComparisonError, DeviceService::Comparing};
|
||||
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;
|
||||
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;
|
||||
});
|
||||
return;
|
||||
}
|
||||
m_deviceService->readVisionHandshake(
|
||||
[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) {
|
||||
emit productionError("视觉恢复核对期间握手信号未保持复位:" + error);
|
||||
return;
|
||||
}
|
||||
if (m_state != Idle && m_state != Completed) {
|
||||
State mapped = Idle;
|
||||
QString processStep;
|
||||
if (!mapPlcStepToState(&mapped, &processStep)
|
||||
|| (mapped != FrontChecking && mapped != BackChecking)) {
|
||||
m_lastError = "当前 PLC 步骤不能明确恢复视觉中断的取料动作。请先终止当前任务,核对机器人后再执行视觉恢复;不会自动重发取料或 R60。";
|
||||
setState(ManualConfirm);
|
||||
reportVision("等待人工核对", m_lastError, true);
|
||||
emit timeoutRecoveryRequired("VISION", m_lastError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
persistRecoveryAction("VISION_CONFIRMED", "Operator verified the robot and PLC; handshake is reset; old target discarded");
|
||||
m_visionNeedsReconcile = false;
|
||||
m_visionSuspended = false;
|
||||
m_visionCommitStarted = false;
|
||||
m_visionStage = VisionStage::Idle;
|
||||
m_visionRequest = QJsonObject();
|
||||
m_visionResult = QJsonObject();
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
reportVision("人工恢复完成", "现场核对及信号复位完成,已丢弃旧视觉目标", true);
|
||||
emit visionProductionActiveChanged(m_state != Idle && m_state != Completed);
|
||||
if (m_state == Idle || m_state == Completed) return;
|
||||
QString recoveryError;
|
||||
if (!recover(&recoveryError)) emit productionError(recoveryError);
|
||||
});
|
||||
}
|
||||
|
||||
bool ProductionService::start(int orderId, QString *error)
|
||||
{
|
||||
const VisionConfig vision = ConfigManager::instance().visionConfig();
|
||||
if (m_visionNeedsReconcile || (vision.enabled && !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";
|
||||
@@ -172,6 +662,7 @@ bool ProductionService::start(int orderId, QString *error)
|
||||
m_actionError.clear();
|
||||
m_frontResultState.clear();
|
||||
m_stopRequested.store(false);
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
if (m_externalStopRequested) {
|
||||
m_externalStopRequested->store(false);
|
||||
}
|
||||
@@ -271,6 +762,10 @@ bool ProductionService::pause(QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_visionStage != VisionStage::Idle) {
|
||||
invalidateVision("已请求暂停生产");
|
||||
m_timer->stop();
|
||||
}
|
||||
cancelPendingProductionAction(
|
||||
[this](bool canceled, const QString &cancelError) {
|
||||
if (!canceled) {
|
||||
@@ -294,7 +789,9 @@ bool ProductionService::pause(QString *error)
|
||||
|
||||
pauseProcessTimer();
|
||||
m_timer->stop();
|
||||
setState(Paused);
|
||||
setState(m_visionNeedsReconcile ? ManualConfirm : Paused);
|
||||
if (m_visionNeedsReconcile)
|
||||
emit timeoutRecoveryRequired("VISION", "视觉流程已中断,请核对 PLC 和机器人后恢复");
|
||||
emitProcessElapsed();
|
||||
});
|
||||
});
|
||||
@@ -303,6 +800,10 @@ bool ProductionService::pause(QString *error)
|
||||
|
||||
bool ProductionService::resume(QString *error)
|
||||
{
|
||||
if (m_visionNeedsReconcile) {
|
||||
if (error) *error = "视觉流程需人工核对恢复,不能直接继续";
|
||||
return false;
|
||||
}
|
||||
if (m_state != Paused) {
|
||||
if (error) {
|
||||
*error = "Production service is not paused";
|
||||
@@ -313,6 +814,7 @@ bool ProductionService::resume(QString *error)
|
||||
if (!checkRecoverySafety(error)) {
|
||||
return false;
|
||||
}
|
||||
if (m_externalPauseRequested) m_externalPauseRequested->store(false);
|
||||
|
||||
m_deviceService->setInspectionMode(
|
||||
m_inspectionMode,
|
||||
@@ -364,6 +866,7 @@ bool ProductionService::stop(QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
invalidateVision("操作员已终止生产");
|
||||
m_lastError = "Production stopped by operator";
|
||||
m_timer->stop();
|
||||
setState(Terminating);
|
||||
@@ -441,6 +944,7 @@ bool ProductionService::stop(QString *error)
|
||||
|
||||
void ProductionService::shutdown(const DeviceService::OperationCallback &callback)
|
||||
{
|
||||
invalidateVision("生产线程正在退出");
|
||||
if (m_shutdownRequested) {
|
||||
if (callback) {
|
||||
callback(false, "Production shutdown is already in progress");
|
||||
@@ -472,6 +976,10 @@ void ProductionService::shutdown(const DeviceService::OperationCallback &callbac
|
||||
|
||||
bool ProductionService::reset(QString *error)
|
||||
{
|
||||
if (m_visionNeedsReconcile) {
|
||||
if (error) *error = "请先人工核对视觉与 PLC 状态,再执行复位";
|
||||
return false;
|
||||
}
|
||||
if (m_state != Error) {
|
||||
if (error) {
|
||||
*error = "Only failed production can be reset";
|
||||
@@ -601,6 +1109,12 @@ bool ProductionService::restoreActiveRun(QString *error)
|
||||
m_timeoutState = persistedState;
|
||||
}
|
||||
|
||||
if (requiresVision(DeviceService::RackPick)) {
|
||||
m_visionNeedsReconcile = true;
|
||||
m_visionSuspended = true;
|
||||
m_visionStage = VisionStage::Fault;
|
||||
reportVision("等待人工核对", "恢复未完成的生产记录,需核对相机与机器人握手", true);
|
||||
}
|
||||
setState(ManualConfirm);
|
||||
m_heartbeatTimer.restart();
|
||||
m_timer->stop();
|
||||
@@ -613,6 +1127,7 @@ bool ProductionService::restoreActiveRun(QString *error)
|
||||
|
||||
bool ProductionService::recover(QString *error)
|
||||
{
|
||||
if (m_visionNeedsReconcile) return recoverVision(error);
|
||||
if (m_state != TimeoutPaused && m_state != ManualConfirm) {
|
||||
if (error) {
|
||||
*error = "Production is not waiting for recovery";
|
||||
@@ -659,6 +1174,10 @@ bool ProductionService::recover(QString *error)
|
||||
|
||||
bool ProductionService::retry(QString *error)
|
||||
{
|
||||
if (m_visionNeedsReconcile) {
|
||||
if (error) *error = "视觉结果不能自动重放,请使用人工核对恢复";
|
||||
return false;
|
||||
}
|
||||
if (m_state != TimeoutPaused && m_state != ManualConfirm) {
|
||||
if (error) {
|
||||
*error = "Production is not waiting for a retry";
|
||||
@@ -729,6 +1248,10 @@ bool ProductionService::retry(QString *error)
|
||||
|
||||
bool ProductionService::markCurrentWorkpieceNg(QString *error)
|
||||
{
|
||||
if (m_visionNeedsReconcile) {
|
||||
if (error) *error = "请先处理未完成的视觉及机器人握手,再标记工件 NG";
|
||||
return false;
|
||||
}
|
||||
if (m_state != TimeoutPaused && m_state != ManualConfirm) {
|
||||
if (error) {
|
||||
*error = "Production is not waiting for manual recovery";
|
||||
@@ -801,6 +1324,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));
|
||||
|
||||
if (m_runId > 0) {
|
||||
const QString processStep = processStepForState(state).isEmpty()
|
||||
@@ -924,6 +1448,19 @@ bool ProductionService::processProductionAction(
|
||||
return true;
|
||||
}
|
||||
|
||||
if (requiresVision(action)) {
|
||||
if (m_visionNeedsReconcile || m_visionSuspended) return true;
|
||||
if (m_visionStage == VisionStage::Idle) {
|
||||
prepareVision();
|
||||
return true;
|
||||
}
|
||||
if (m_visionStage == VisionStage::Preflight) {
|
||||
advanceVision();
|
||||
return true;
|
||||
}
|
||||
if (m_visionStage == VisionStage::Fault) return true;
|
||||
}
|
||||
|
||||
if (!m_actionPending) {
|
||||
m_pendingAction = action;
|
||||
m_actionPending = true;
|
||||
@@ -937,6 +1474,8 @@ bool ProductionService::processProductionAction(
|
||||
m_actionStarting = false;
|
||||
if (!success) {
|
||||
m_actionError = actionError;
|
||||
} else if (m_visionStage == VisionStage::Armed && !m_visionSuspended) {
|
||||
setVisionStage(VisionStage::WaitRequest, "等待机器人到位后的拍照请求 851");
|
||||
}
|
||||
tryDeferredActionCancel();
|
||||
});
|
||||
@@ -961,10 +1500,21 @@ bool ProductionService::processProductionAction(
|
||||
return true;
|
||||
}
|
||||
|
||||
if (requiresVision(action) && m_visionStage != VisionStage::Complete) {
|
||||
advanceVision();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_actionCompleted) {
|
||||
*completed = true;
|
||||
m_actionPending = false;
|
||||
m_actionCompleted = false;
|
||||
if (requiresVision(action)) {
|
||||
m_visionStage = VisionStage::Idle;
|
||||
m_visionCommitStarted = false;
|
||||
m_visionRequest = QJsonObject();
|
||||
m_visionResult = QJsonObject();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1824,6 +2374,10 @@ void ProductionService::fail(const QString &error)
|
||||
if (m_state == Error) {
|
||||
return;
|
||||
}
|
||||
if (m_visionStage != VisionStage::Idle && m_visionStage != VisionStage::Fault) {
|
||||
visionFault(error);
|
||||
return;
|
||||
}
|
||||
|
||||
m_lastError = error.isEmpty() ? "Production failed" : error;
|
||||
cancelPendingProductionAction(
|
||||
|
||||
Reference in New Issue
Block a user