feat: 重构视觉取料流程及生产故障恢复

- 删除订单启动、暂停、复位相关 PLC 握手及无效反馈配置
- 清除报警改为仅写 clearAlarmCommand,不等待反馈
- 将拍照位移动、视觉处理和 RackPick 拆分为三个独立动作
- 增加 M850/M851 拍照位动作及 R60/M852 视觉结果流程
- 暂停改为等待当前机器人动作完成,视觉结果不确定时转人工确认
- 删除复位按钮,合并为恢复/重试按钮
- 普通 PAUSED 订单通过启动按钮继续
- 恢复前检查 PLC、安全状态、订单及动作命令反馈
- 支持在途动作接管、已完成动作收尾、安全重试和人工确认
- 故障状态保持订单 RUNNING,并持久化实际恢复阶段
- 恢复成功后仅清除本次软件故障对应的报警
- 更新小轴 PLC 地址、Modbus 配置和视觉集成文档
This commit is contained in:
lidg
2026-10-29 17:20:14 +08:00
parent 31454722a0
commit 407af2892d
17 changed files with 839 additions and 929 deletions
+99 -218
View File
@@ -8,10 +8,9 @@
namespace {
const int FeedbackPollIntervalMs = 50;
bool actionAddresses(DeviceService::ProductionAction action,
const ModbusAddressMap &addresses,
const VisionConfig &vision,
int *commandAddress, int *feedbackAddress)
{
if (!commandAddress || !feedbackAddress) {
@@ -19,6 +18,10 @@ bool actionAddresses(DeviceService::ProductionAction action,
}
switch (action) {
case DeviceService::PhotoMoveAction:
*commandAddress = vision.photoMoveCoil;
*feedbackAddress = vision.photoMoveCoilFeedback;
return true;
case DeviceService::RackPick:
*commandAddress = addresses.rackPickCommand;
*feedbackAddress = addresses.rackPickFeedback;
@@ -70,8 +73,6 @@ DeviceService::DeviceService(QObject *parent)
, m_productionFeedbackAddress(-1)
, m_terminationActive(false)
, m_terminationSignalIndex(0)
, m_controlCommandAddress(-1)
, m_controlFeedbackAddress(-1)
{
connect(m_pollTimer, &QTimer::timeout,
this, &DeviceService::pollStatus);
@@ -349,84 +350,6 @@ DeviceStatus DeviceService::simulatedStatus() const
return status;
}
void DeviceService::start(const OperationCallback &callback)
{
if (m_terminationActive) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
if (ConfigManager::instance().simulationMode()) {
if (callback) {
callback(true, QString());
}
return;
}
if (!ConfigManager::instance().productionEnabled()) {
if (callback) {
callback(false, "Production control is disabled by configuration");
}
return;
}
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
executeCommand(addresses.startCommand, addresses.startFeedback, callback);
}
void DeviceService::pause(const OperationCallback &callback)
{
if (m_terminationActive) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
if (ConfigManager::instance().simulationMode()) {
if (callback) {
callback(true, QString());
}
return;
}
if (!ConfigManager::instance().productionEnabled()) {
if (callback) {
callback(false, "Production control is disabled by configuration");
}
return;
}
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
executeCommand(addresses.pauseCommand, addresses.pauseFeedback, callback);
}
void DeviceService::reset(const OperationCallback &callback)
{
if (m_terminationActive) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
if (ConfigManager::instance().simulationMode()) {
if (callback) {
callback(true, QString());
}
return;
}
if (!ConfigManager::instance().productionEnabled()) {
if (callback) {
callback(false, "Production control is disabled by configuration");
}
return;
}
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
executeCommand(addresses.resetCommand, addresses.resetFeedback, callback);
}
void DeviceService::clearAlarm(const OperationCallback &callback)
{
if (m_terminationActive) {
@@ -449,9 +372,22 @@ void DeviceService::clearAlarm(const OperationCallback &callback)
return;
}
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
executeCommand(addresses.clearAlarmCommand,
addresses.clearAlarmFeedback, callback);
if (m_controlRequestPending || m_productionActionBusy) {
if (callback) {
callback(false, "Another PLC operation is already active");
}
return;
}
m_controlRequestPending = true;
m_modbus->writeCoil(
m_modbus->config().addressMap.clearAlarmCommand, true,
[this, callback](bool success, const QString &error) {
m_controlRequestPending = false;
if (callback) {
callback(success, error);
}
});
}
void DeviceService::setInspectionMode(const QString &mode,
@@ -517,7 +453,6 @@ void DeviceService::terminate(const OperationCallback &callback)
m_pollTimer->stop();
const QList<QPair<int, int>> currentSignals = QList<QPair<int, int>>()
<< qMakePair(m_controlCommandAddress, m_controlFeedbackAddress)
<< qMakePair(m_productionCommandAddress,
m_productionFeedbackAddress);
for (const QPair<int, int> &signalPair : currentSignals) {
@@ -546,16 +481,7 @@ void DeviceService::clearNextTerminationSignal()
return;
}
if (m_terminationSignalIndex >= m_terminationSignals.size()) {
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
executeCommand(
addresses.pauseCommand, addresses.pauseFeedback,
[this](bool success, const QString &error) {
if (!success) {
finishTermination(false, error);
return;
}
finishTermination(true, QString());
}, true);
finishTermination(true, QString());
return;
}
@@ -587,8 +513,6 @@ void DeviceService::finishTermination(bool success, const QString &error)
m_productionActionActive = false;
m_productionActionBusy = false;
if (success) {
m_controlCommandAddress = -1;
m_controlFeedbackAddress = -1;
m_productionCommandAddress = -1;
m_productionFeedbackAddress = -1;
}
@@ -597,113 +521,6 @@ void DeviceService::finishTermination(bool success, const QString &error)
}
}
void DeviceService::executeCommand(int commandAddress, int feedbackAddress,
const OperationCallback &callback,
bool allowDuringTermination)
{
if (m_terminationActive && !allowDuringTermination) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
if (m_controlRequestPending || m_productionActionBusy) {
if (callback) {
callback(false, "Another PLC operation is already active");
}
return;
}
m_controlRequestPending = true;
m_controlCommandAddress = commandAddress;
m_controlFeedbackAddress = feedbackAddress;
writeCommandAndWaitFeedback(commandAddress, feedbackAddress,
[this, commandAddress, feedbackAddress,
callback](bool success,
const QString &error) {
m_controlRequestPending = false;
if (success
&& m_controlCommandAddress
== commandAddress
&& m_controlFeedbackAddress
== feedbackAddress) {
m_controlCommandAddress = -1;
m_controlFeedbackAddress = -1;
}
if (callback) {
callback(success, error);
}
}, allowDuringTermination);
}
void DeviceService::writeCommandAndWaitFeedback(
int commandAddress, int feedbackAddress,
const OperationCallback &callback, bool allowDuringTermination)
{
if (m_terminationActive && !allowDuringTermination) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
m_modbus->writeCoil(
commandAddress, true,
[this, commandAddress, feedbackAddress, callback,
allowDuringTermination](bool success,
const QString &error) {
if (!success) {
if (callback) {
callback(false, error);
}
return;
}
pollFeedback(commandAddress, feedbackAddress, callback,
allowDuringTermination);
});
}
void DeviceService::pollFeedback(int commandAddress, int feedbackAddress,
const OperationCallback &callback,
bool allowDuringTermination)
{
if (m_terminationActive && !allowDuringTermination) {
if (callback) {
callback(false, "PLC termination is in progress");
}
return;
}
m_modbus->readCoil(
feedbackAddress,
[this, commandAddress, feedbackAddress, callback,
allowDuringTermination](
bool success, bool feedback,
const QString &error) {
if (!success) {
if (callback) {
callback(false, error);
}
return;
}
if (!feedback) {
QTimer::singleShot(
FeedbackPollIntervalMs, this,
[this, commandAddress, feedbackAddress, callback,
allowDuringTermination]() {
pollFeedback(commandAddress, feedbackAddress, callback,
allowDuringTermination);
});
return;
}
clearSignals(commandAddress, feedbackAddress,
[callback](bool cleared, const QString &clearError) {
if (callback) {
callback(cleared, clearError);
}
});
});
}
void DeviceService::beginProductionAction(
ProductionAction action, int trayNo, int slotNo,
const OperationCallback &callback, const std::function<bool()> &beforeSend)
@@ -740,7 +557,8 @@ void DeviceService::beginProductionAction(
}
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
if (!actionAddresses(action, addresses,
const VisionConfig vision = ConfigManager::instance().visionConfig();
if (!actionAddresses(action, addresses, vision,
&m_productionCommandAddress,
&m_productionFeedbackAddress)) {
if (callback) {
@@ -750,10 +568,11 @@ void DeviceService::beginProductionAction(
}
m_productionActionBusy = true;
m_modbus->readCoil(
m_productionFeedbackAddress,
inspectProductionAction(
action,
[this, action, trayNo, slotNo, addresses, callback, beforeSend](
bool success, bool feedback, const QString &error) {
bool success, const ProductionActionStatus &status,
const QString &error) {
if (!success) {
m_productionActionBusy = false;
if (callback) {
@@ -761,11 +580,11 @@ void DeviceService::beginProductionAction(
}
return;
}
if (feedback) {
if (status.command || status.feedback) {
m_productionActionBusy = false;
if (callback) {
callback(false,
"PLC feedback is still active before a new action");
"PLC command or feedback is still active before a new action");
}
return;
}
@@ -803,7 +622,8 @@ void DeviceService::beginProductionAction(
}
};
if (action == RackPick || action == RackPlace) {
if (action == PhotoMoveAction || action == RackPick
|| action == RackPlace) {
m_modbus->writeHoldingRegister(
addresses.trayNumber,
static_cast<quint16>(trayNo),
@@ -825,6 +645,67 @@ void DeviceService::beginProductionAction(
});
}
void DeviceService::inspectProductionAction(
ProductionAction action, const ActionStatusCallback &callback)
{
ProductionActionStatus emptyStatus;
if (m_terminationActive) {
if (callback) callback(false, emptyStatus,
"PLC termination is in progress");
return;
}
if (ConfigManager::instance().simulationMode()) {
if (callback) callback(true, emptyStatus, QString());
return;
}
int commandAddress = -1;
int feedbackAddress = -1;
if (!actionAddresses(action, m_modbus->config().addressMap,
ConfigManager::instance().visionConfig(),
&commandAddress, &feedbackAddress)) {
if (callback) callback(false, emptyStatus,
"Unknown PLC production action");
return;
}
m_modbus->readCoil(
commandAddress,
[this, feedbackAddress, callback](bool commandRead, bool command,
const QString &commandError) {
if (!commandRead) {
if (callback) callback(false, ProductionActionStatus(),
commandError);
return;
}
m_modbus->readCoil(
feedbackAddress,
[callback, command](bool feedbackRead, bool feedback,
const QString &feedbackError) {
ProductionActionStatus status;
status.command = command;
status.feedback = feedback;
if (callback) callback(feedbackRead, status,
feedbackError);
});
});
}
void DeviceService::adoptProductionAction(ProductionAction action)
{
int commandAddress = -1;
int feedbackAddress = -1;
if (m_productionActionBusy
|| !actionAddresses(action, m_modbus->config().addressMap,
ConfigManager::instance().visionConfig(),
&commandAddress, &feedbackAddress)) {
return;
}
m_productionCommandAddress = commandAddress;
m_productionFeedbackAddress = feedbackAddress;
m_productionActionActive = true;
}
void DeviceService::pollProductionAction(const ActionCallback &callback)
{
if (m_terminationActive) {
@@ -1069,7 +950,7 @@ void DeviceService::readVisionHandshake(const HandshakeCallback &callback)
return;
}
const VisionConfig config = ConfigManager::instance().visionConfig();
if (config.photoArrivedCoil == config.photoMoveCoil + 1
if (config.photoMoveCoilFeedback == config.photoMoveCoil + 1
&& config.comparisonPickCoil == config.photoMoveCoil + 2) {
const quint64 generation = m_visionGeneration;
m_modbus->readCoils(config.photoMoveCoil, 3,
@@ -1077,7 +958,7 @@ void DeviceService::readVisionHandshake(const HandshakeCallback &callback)
VisionHandshake state;
if (success && values.size() == 3 && generation == m_visionGeneration) {
state.photoMove = values.at(0);
state.photoArrived = values.at(1);
state.photoMoveFeedback = values.at(1);
state.comparisonPick = values.at(2);
if (callback) callback(true, state, QString());
} else if (callback) {
@@ -1101,7 +982,7 @@ void DeviceService::readVisionHandshakeStep(int index, const VisionHandshake &st
return;
}
const VisionConfig config = ConfigManager::instance().visionConfig();
const int addresses[] = {config.photoMoveCoil, config.photoArrivedCoil, config.comparisonPickCoil};
const int addresses[] = {config.photoMoveCoil, config.photoMoveCoilFeedback, config.comparisonPickCoil};
m_modbus->readVisionCoil(addresses[index],
[this, index, state, generation, callback](bool success, bool value, const QString &error) {
if (!success) {
@@ -1111,7 +992,7 @@ void DeviceService::readVisionHandshakeStep(int index, const VisionHandshake &st
VisionHandshake next = state;
switch (index) {
case 0: next.photoMove = value; break;
case 1: next.photoArrived = value; break;
case 1: next.photoMoveFeedback = value; break;
case 2: next.comparisonPick = value; break;
}
readVisionHandshakeStep(index + 1, next, generation, callback);
@@ -1131,8 +1012,8 @@ void DeviceService::writeVisionSignal(VisionSignal signal, bool value,
int address = -1;
switch (signal) {
case PhotoMove: address = config.photoMoveCoil; break;
case PhotoArrived:
if (!value) address = config.photoArrivedCoil; // Only PLC may publish arrival.
case PhotoMoveFeedback:
if (!value) address = config.photoMoveCoilFeedback; // Only PLC may publish arrival.
break;
case ComparisonPick: address = config.comparisonPickCoil; break;
default: break;
+13 -15
View File
@@ -32,6 +32,12 @@ struct DeviceStatus
Q_DECLARE_METATYPE(DeviceStatus)
struct ProductionActionStatus
{
bool command = false;
bool feedback = false;
};
class QTimer;
class DeviceService : public QObject
@@ -44,14 +50,17 @@ public:
std::function<void(bool, const DeviceStatus &, const QString &)>;
using ActionCallback =
std::function<void(bool, bool, const QString &)>;
using ActionStatusCallback =
std::function<void(bool, const ProductionActionStatus &, const QString &)>;
using PoseCallback = std::function<void(bool, const RobotPose &, const QString &)>;
using HandshakeCallback = std::function<void(bool, const VisionHandshake &, const QString &)>;
using VisionWriteCallback = std::function<void(VisionWriteOutcome, const QString &)>;
enum VisionSignal { PhotoMove, PhotoArrived, ComparisonPick };
enum VisionSignal { PhotoMove, PhotoMoveFeedback, ComparisonPick };
enum ProductionAction
{
PhotoMoveAction,
RackPick,
FrontLoad,
Inspect,
@@ -70,9 +79,6 @@ public:
bool isPolling() const;
void requestStatus(const StatusCallback &callback);
void start(const OperationCallback &callback);
void pause(const OperationCallback &callback);
void reset(const OperationCallback &callback);
void clearAlarm(const OperationCallback &callback);
void setInspectionMode(const QString &mode,
const OperationCallback &callback);
@@ -81,6 +87,9 @@ public:
int slotNo, const OperationCallback &callback,
const std::function<bool()> &beforeSend = {});
void pollProductionAction(const ActionCallback &callback);
void inspectProductionAction(ProductionAction action,
const ActionStatusCallback &callback);
void adoptProductionAction(ProductionAction action);
void cancelProductionAction(const OperationCallback &callback);
bool productionActionActive() const;
bool productionActionBusy() const;
@@ -104,15 +113,6 @@ private:
void readStatusStep(int index, const DeviceStatus &status);
void finishStatusRequest(bool success, const DeviceStatus &status,
const QString &error);
void executeCommand(int commandAddress, int feedbackAddress,
const OperationCallback &callback,
bool allowDuringTermination = false);
void writeCommandAndWaitFeedback(int commandAddress, int feedbackAddress,
const OperationCallback &callback,
bool allowDuringTermination = false);
void pollFeedback(int commandAddress, int feedbackAddress,
const OperationCallback &callback,
bool allowDuringTermination = false);
void clearSignals(int commandAddress, int feedbackAddress,
const OperationCallback &callback);
void clearActionSignals(const OperationCallback &callback);
@@ -136,8 +136,6 @@ private:
QList<QPair<int, int>> m_terminationSignals;
int m_terminationSignalIndex;
OperationCallback m_terminationCallback;
int m_controlCommandAddress;
int m_controlFeedbackAddress;
quint64 m_visionGeneration = 0;
};
File diff suppressed because it is too large Load Diff
+15 -11
View File
@@ -22,6 +22,8 @@ public:
Idle,
Preparing,
WaitingDeviceReady,
PhotoMoving,
VisionProcessing,
RackPicking,
FrontLoading,
Loading,
@@ -60,15 +62,12 @@ public:
bool pause(QString *error = nullptr);
bool resume(QString *error = nullptr);
bool stop(QString *error = nullptr);
bool reset(QString *error = nullptr);
bool clearAlarm(QString *error = nullptr);
bool recover(QString *error = nullptr);
bool retry(QString *error = nullptr);
bool markCurrentWorkpieceNg(QString *error = nullptr);
bool restoreActiveRun(QString *error = nullptr);
void shutdown(const DeviceService::OperationCallback &callback);
void setExternalStopFlag(std::atomic_bool *flag);
void setExternalPauseFlag(std::atomic_bool *flag);
void requestStopImmediate();
public slots:
@@ -107,24 +106,19 @@ public:
private:
friend class VisionProductionTest;
enum class VisionStage {
Idle, Preflight, Moving, WaitArrival, Clearing, ReadingPose,
Measuring, WritingPose, Publishing, Complete, Fault
Idle, ReadingPose, Measuring, WritingPose, Publishing, Complete, Fault
};
bool requiresVision(DeviceService::ProductionAction action) const;
void prepareVision();
void advanceVision();
void handleVisionHandshake(const VisionHandshake &handshake);
void beginVisionMeasurement();
void publishPickMode(bool useVision);
void skipVision(const QString &reason);
void clearVisionOutputs(int index, quint64 generation);
bool visionCurrent(quint64 generation) const;
void setVisionStage(VisionStage stage, const QString &message);
void reportVision(const QString &phase, const QString &message, bool invalidated = false);
void invalidateVision(const QString &reason);
void visionFault(const QString &reason);
bool recoverVision(QString *error);
void resetVisionForRecovery(int index, quint64 generation);
void publishVisionControl();
bool saveVisionRecoveryState(bool pending, QString *error);
void loadVisionRecoveryState();
@@ -144,12 +138,20 @@ private:
bool recoverFromPlcStep(QString *error);
bool processProductionAction(DeviceService::ProductionAction action,
bool *completed, QString *error);
bool processVisionAction(bool *completed, QString *error);
void cancelPendingProductionAction(
const DeviceService::OperationCallback &callback);
void tryDeferredActionCancel();
bool executeCurrentAction(DeviceService::ProductionAction action,
const QString &nextProcessStep,
State nextState, QString *error);
bool pauseAtActionBoundary(QString *error = nullptr);
void abortVisionForPause();
bool productionActionForState(State state,
DeviceService::ProductionAction *action) const;
void continueRecovery(State state,
DeviceService::ProductionAction action,
const ProductionActionStatus &status);
bool recordInspection(const QString &side, int rawResult,
const QString &resultState, QString *error);
QString resultStateForRawResult(int rawResult) const;
@@ -197,6 +199,7 @@ private:
bool m_actionStarting;
bool m_actionPollPending;
bool m_actionCompleted;
bool m_actionWasSent;
QString m_actionError;
bool m_actionCancelRequested;
DeviceService::OperationCallback m_deferredCancelCallback;
@@ -206,9 +209,12 @@ private:
bool m_starting;
bool m_plcStarted;
bool m_tickRunning;
bool m_pauseAfterCurrentAction;
bool m_recoveryCheckPending;
DeviceService::ProductionAction m_pendingAction;
QString m_inspectionMode;
QString m_frontResultState;
QString m_recoveryAlarmCode;
QString m_currentProcessStep;
QString m_lastError;
std::atomic_bool m_stopRequested;
@@ -231,9 +237,7 @@ private:
bool m_visionNeedsReconcile = false;
bool m_visionRecoveryChecking = false;
bool m_visionFaultCleanupPending = false;
bool m_visionPauseConfirmed = false;
QString m_visionStatePath;
std::atomic_bool *m_externalPauseRequested = nullptr;
};
Q_DECLARE_METATYPE(ProductionService::State)
-21
View File
@@ -4,7 +4,6 @@ ProductionWorker::ProductionWorker(QObject *parent)
: QObject(parent)
, m_service(nullptr)
, m_stopRequested(false)
, m_pauseRequested(false)
{
}
@@ -16,7 +15,6 @@ void ProductionWorker::ensureService()
m_service = new ProductionService(this);
m_service->setExternalStopFlag(&m_stopRequested);
m_service->setExternalPauseFlag(&m_pauseRequested);
connect(m_service, &ProductionService::visionMeasureRequested, this, &ProductionWorker::visionMeasureRequested);
connect(m_service, &ProductionService::visionCancelRequested, this, &ProductionWorker::visionCancelRequested);
connect(m_service, &ProductionService::visionCycleChanged, this, &ProductionWorker::visionCycleChanged);
@@ -54,11 +52,6 @@ void ProductionWorker::requestStopImmediate()
m_stopRequested.store(true);
}
void ProductionWorker::requestPauseImmediate()
{
m_pauseRequested.store(true);
}
void ProductionWorker::updateVisionAvailability(bool ready, bool busy, const QString &session)
{
ensureService();
@@ -112,13 +105,6 @@ void ProductionWorker::requestStop()
reportFailure(m_service->stop(&error), error);
}
void ProductionWorker::requestReset()
{
ensureService();
QString error;
reportFailure(m_service->reset(&error), error);
}
void ProductionWorker::requestClearAlarm()
{
ensureService();
@@ -133,13 +119,6 @@ void ProductionWorker::requestRecover()
reportFailure(m_service->recover(&error), error);
}
void ProductionWorker::requestRetry()
{
ensureService();
QString error;
reportFailure(m_service->retry(&error), error);
}
void ProductionWorker::requestMarkNg()
{
ensureService();
-4
View File
@@ -13,17 +13,14 @@ class ProductionWorker : public QObject
public:
explicit ProductionWorker(QObject *parent = nullptr);
void requestStopImmediate();
void requestPauseImmediate();
public slots:
void requestStart(int orderId);
void requestPause();
void requestResume();
void requestStop();
void requestReset();
void requestClearAlarm();
void requestRecover();
void requestRetry();
void requestMarkNg();
void requestRestore();
void requestShutdown();
@@ -57,7 +54,6 @@ private:
ProductionService *m_service;
std::atomic_bool m_stopRequested;
std::atomic_bool m_pauseRequested;
};
#endif // PRODUCTIONWORKER_H