- production_runs 新增动作阶段、尝试次数、版本及人工确认字段 - 提供兼容 PostgreSQL 9.5 的幂等升级脚本和数据库结构检查 - 工序推进、动作完成检查点和审计记录使用同一事务 - 机器人命令单次发送,完成反馈先持久化再清握手 - 已完成动作恢复仅补握手或数据库记录,避免重复执行 - 恢复/重试按钮固定显示在清除报警前,加入明确人工处理选项 - 记录人工决定、操作人及现场核对信息,拒绝过期确认 - 区分普通暂停继续、故障恢复与终止后的视觉锁解除 - 补齐视觉目标跨重启校验、异步失败及迟到回调防护 - 检测结果关联动作和尝试次数,重试清除旧结果缓存
1068 lines
37 KiB
C++
1068 lines
37 KiB
C++
#include "deviceservice.h"
|
|
|
|
#include "../config/configmanager.h"
|
|
#include "../modbus/modbustcpmanager.h"
|
|
|
|
#include <QTimer>
|
|
#include <QDebug>
|
|
|
|
namespace {
|
|
|
|
bool actionAddresses(DeviceService::ProductionAction action,
|
|
const ModbusAddressMap &addresses,
|
|
const VisionConfig &vision,
|
|
int *commandAddress, int *feedbackAddress)
|
|
{
|
|
if (!commandAddress || !feedbackAddress) {
|
|
return false;
|
|
}
|
|
|
|
switch (action) {
|
|
case DeviceService::PhotoMoveAction:
|
|
*commandAddress = vision.photoMoveCoil;
|
|
*feedbackAddress = vision.photoMoveCoilFeedback;
|
|
return true;
|
|
case DeviceService::RackPick:
|
|
*commandAddress = addresses.rackPickCommand;
|
|
*feedbackAddress = addresses.rackPickFeedback;
|
|
return true;
|
|
case DeviceService::FrontLoad:
|
|
*commandAddress = addresses.frontLoadCommand;
|
|
*feedbackAddress = addresses.frontLoadFeedback;
|
|
return true;
|
|
case DeviceService::Inspect:
|
|
*commandAddress = addresses.inspectionCommand;
|
|
*feedbackAddress = addresses.inspectionFeedback;
|
|
return true;
|
|
case DeviceService::FrontUnload:
|
|
*commandAddress = addresses.frontUnloadCommand;
|
|
*feedbackAddress = addresses.frontUnloadFeedback;
|
|
return true;
|
|
case DeviceService::Flip:
|
|
*commandAddress = addresses.flipCommand;
|
|
*feedbackAddress = addresses.flipFeedback;
|
|
return true;
|
|
case DeviceService::BackLoad:
|
|
*commandAddress = addresses.backLoadCommand;
|
|
*feedbackAddress = addresses.backLoadFeedback;
|
|
return true;
|
|
case DeviceService::BackUnload:
|
|
*commandAddress = addresses.backUnloadCommand;
|
|
*feedbackAddress = addresses.backUnloadFeedback;
|
|
return true;
|
|
case DeviceService::RackPlace:
|
|
*commandAddress = addresses.rackPlaceCommand;
|
|
*feedbackAddress = addresses.rackPlaceFeedback;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
DeviceService::DeviceService(QObject *parent)
|
|
: QObject(parent)
|
|
, m_pollTimer(new QTimer(this))
|
|
, m_modbus(new ModbusTcpManager(
|
|
ConfigManager::instance().modbusConfig(), this))
|
|
, m_statusRequestPending(false)
|
|
, m_controlRequestPending(false)
|
|
, m_productionActionActive(false)
|
|
, m_productionActionBusy(false)
|
|
, m_productionCommandAddress(-1)
|
|
, m_productionFeedbackAddress(-1)
|
|
, m_terminationActive(false)
|
|
, m_terminationSignalIndex(0)
|
|
{
|
|
connect(m_pollTimer, &QTimer::timeout,
|
|
this, &DeviceService::pollStatus);
|
|
}
|
|
|
|
void DeviceService::startPolling()
|
|
{
|
|
if (m_terminationActive) {
|
|
return;
|
|
}
|
|
|
|
if (m_pollTimer->isActive()) {
|
|
return;
|
|
}
|
|
|
|
const int interval = qMax(100, m_modbus->config().pollIntervalMs);
|
|
m_pollTimer->start(interval);
|
|
pollStatus();
|
|
}
|
|
|
|
DeviceService::~DeviceService()
|
|
{
|
|
m_pollTimer->stop();
|
|
m_terminationActive = true;
|
|
m_terminationCallback = OperationCallback();
|
|
++m_visionGeneration;
|
|
// Modbus abort invokes pending callbacks; keep our state alive until they drain.
|
|
m_modbus->abortAll("Device service is shutting down");
|
|
delete m_modbus;
|
|
m_modbus = nullptr;
|
|
}
|
|
|
|
void DeviceService::stopPolling()
|
|
{
|
|
m_pollTimer->stop();
|
|
}
|
|
|
|
bool DeviceService::isPolling() const
|
|
{
|
|
return m_pollTimer->isActive();
|
|
}
|
|
|
|
void DeviceService::requestStatus(const StatusCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
DeviceStatus status;
|
|
callback(false, status, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
const DeviceStatus status = simulatedStatus();
|
|
emit statusChanged(status);
|
|
if (callback) {
|
|
callback(true, status, QString());
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (m_statusRequestPending) {
|
|
if (callback) {
|
|
m_statusCallbacks.append(callback);
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_statusRequestPending = true;
|
|
m_statusCallbacks.clear();
|
|
if (callback) {
|
|
m_statusCallbacks.append(callback);
|
|
}
|
|
DeviceStatus status;
|
|
status.plcConnected = true;
|
|
readStatusStep(0, status);
|
|
}
|
|
|
|
void DeviceService::finishStatusRequest(bool success,
|
|
const DeviceStatus &status,
|
|
const QString &error)
|
|
{
|
|
DeviceStatus finalStatus = status;
|
|
finalStatus.statusValid = success;
|
|
if (success) {
|
|
finalStatus.safetyDoorClosed = finalStatus.safetyDoorClosed
|
|
&& finalStatus.safetyDoor1Closed
|
|
&& finalStatus.safetyDoor2Closed
|
|
&& finalStatus.safetyDoor3Closed;
|
|
}
|
|
|
|
m_statusRequestPending = false;
|
|
const QList<StatusCallback> callbacks = m_statusCallbacks;
|
|
m_statusCallbacks.clear();
|
|
emit statusChanged(finalStatus);
|
|
if (!success && !error.isEmpty()) {
|
|
emit communicationError(error);
|
|
}
|
|
for (const StatusCallback &callback : callbacks) {
|
|
if (callback) {
|
|
callback(success, finalStatus, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceService::readStatusStep(int index, const DeviceStatus &status)
|
|
{
|
|
const bool useErrorCode =
|
|
ConfigManager::instance().productionConfig().usePlcErrorCode;
|
|
if (index >= (useErrorCode ? 15 : 14)) {
|
|
finishStatusRequest(true, status, QString());
|
|
return;
|
|
}
|
|
|
|
auto failed = [this](const QString &error) {
|
|
DeviceStatus status;
|
|
status.plcConnected = false;
|
|
finishStatusRequest(
|
|
false, status,
|
|
error.isEmpty() ? "Failed to read PLC status" : error);
|
|
};
|
|
|
|
auto next = [this, index, status, failed](
|
|
bool success, quint16 value, const QString &error) {
|
|
if (!success) {
|
|
failed(error);
|
|
return;
|
|
}
|
|
|
|
DeviceStatus nextStatus = status;
|
|
switch (index) {
|
|
case 0: nextStatus.plcReady = value != 0; break;
|
|
case 1: nextStatus.robotReady = value != 0; break;
|
|
case 2: nextStatus.safetyDoorClosed = value != 0; break;
|
|
case 3: nextStatus.emergencyStop = value != 0; break;
|
|
case 4: nextStatus.inspectionMachineReady = value != 0; break;
|
|
case 5: nextStatus.visionReady = value != 0; break;
|
|
case 6: nextStatus.robotControlCabinetReady = value != 0; break;
|
|
case 7: nextStatus.mainControlCabinetReady = value != 0; break;
|
|
case 8: nextStatus.safetyDoor1Closed = value != 0; break;
|
|
case 9: nextStatus.safetyDoor2Closed = value != 0; break;
|
|
case 10: nextStatus.safetyDoor3Closed = value != 0; break;
|
|
case 11: nextStatus.activeAlarm = value != 0; break;
|
|
case 12: nextStatus.currentStep = value; break;
|
|
case 13: nextStatus.checkResult = value; break;
|
|
case 14: nextStatus.errorCode = value; break;
|
|
default: break;
|
|
}
|
|
readStatusStep(index + 1, nextStatus);
|
|
};
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
switch (index) {
|
|
case 0:
|
|
m_modbus->readCoil(addresses.plcReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 1:
|
|
m_modbus->readCoil(addresses.robotReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 2:
|
|
m_modbus->readCoil(addresses.safetyDoorClosed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 3:
|
|
m_modbus->readCoil(addresses.emergencyStop,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 4:
|
|
m_modbus->readCoil(addresses.inspectionMachineReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 5:
|
|
m_modbus->readCoil(addresses.visionReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 6:
|
|
m_modbus->readCoil(addresses.robotControlCabinetReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 7:
|
|
m_modbus->readCoil(addresses.mainControlCabinetReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 8:
|
|
m_modbus->readCoil(addresses.safetyDoor1Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 9:
|
|
m_modbus->readCoil(addresses.safetyDoor2Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 10:
|
|
m_modbus->readCoil(addresses.safetyDoor3Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 11:
|
|
m_modbus->readCoil(addresses.activeAlarm,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 12:
|
|
m_modbus->readHoldingRegister(addresses.currentStep, next);
|
|
break;
|
|
case 13:
|
|
m_modbus->readHoldingRegister(addresses.checkResult, next);
|
|
break;
|
|
case 14:
|
|
if (useErrorCode) {
|
|
m_modbus->readHoldingRegister(addresses.errorCode, next);
|
|
} else {
|
|
finishStatusRequest(true, status, QString());
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void DeviceService::pollStatus()
|
|
{
|
|
if (m_terminationActive || m_statusRequestPending || m_controlRequestPending
|
|
|| m_productionActionBusy) {
|
|
return;
|
|
}
|
|
|
|
requestStatus([this](bool success, const DeviceStatus &, const QString &error) {
|
|
if (!success && !error.isEmpty()) {
|
|
emit communicationError(error);
|
|
}
|
|
});
|
|
}
|
|
|
|
DeviceStatus DeviceService::simulatedStatus() const
|
|
{
|
|
DeviceStatus status;
|
|
status.statusValid = true;
|
|
status.plcConnected = true;
|
|
status.plcReady = true;
|
|
status.robotReady = true;
|
|
status.safetyDoorClosed = true;
|
|
status.emergencyStop = false;
|
|
status.inspectionMachineReady = true;
|
|
status.visionReady = true;
|
|
status.robotControlCabinetReady = true;
|
|
status.mainControlCabinetReady = true;
|
|
status.safetyDoor1Closed = true;
|
|
status.safetyDoor2Closed = true;
|
|
status.safetyDoor3Closed = true;
|
|
status.activeAlarm = false;
|
|
status.currentStep = 0;
|
|
status.checkResult = 0;
|
|
return status;
|
|
}
|
|
|
|
void DeviceService::clearAlarm(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;
|
|
}
|
|
|
|
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,
|
|
const OperationCallback &callback)
|
|
{
|
|
if (mode != "BEFORE_PLATING" && mode != "AFTER_PLATING") {
|
|
if (callback) {
|
|
callback(false,
|
|
"Inspection mode must be BEFORE_PLATING or AFTER_PLATING");
|
|
}
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
if (m_controlRequestPending || m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "Another PLC operation is already active");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_controlRequestPending = true;
|
|
const bool afterPlating = mode == "AFTER_PLATING";
|
|
m_modbus->writeCoil(
|
|
m_modbus->config().addressMap.inspectionMode, afterPlating,
|
|
[this, callback](bool success, const QString &error) {
|
|
m_controlRequestPending = false;
|
|
if (callback) {
|
|
callback(success, error);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DeviceService::terminate(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is already in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_terminationActive = true;
|
|
m_terminationCallback = callback;
|
|
m_terminationSignals.clear();
|
|
m_terminationSignalIndex = 0;
|
|
m_pollTimer->stop();
|
|
|
|
const QList<QPair<int, int>> currentSignals = QList<QPair<int, int>>()
|
|
<< qMakePair(m_productionCommandAddress,
|
|
m_productionFeedbackAddress);
|
|
for (const QPair<int, int> &signalPair : currentSignals) {
|
|
if (signalPair.first >= 0 && signalPair.second >= 0
|
|
&& !m_terminationSignals.contains(signalPair)) {
|
|
m_terminationSignals.append(signalPair);
|
|
}
|
|
}
|
|
|
|
m_modbus->abortAll("PLC termination requested");
|
|
m_statusRequestPending = false;
|
|
m_controlRequestPending = false;
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (ConfigManager::instance().simulationMode()
|
|
|| !ConfigManager::instance().productionEnabled()) {
|
|
finishTermination(true, QString());
|
|
return;
|
|
}
|
|
clearNextTerminationSignal();
|
|
}
|
|
|
|
void DeviceService::clearNextTerminationSignal()
|
|
{
|
|
if (!m_terminationActive) {
|
|
return;
|
|
}
|
|
if (m_terminationSignalIndex >= m_terminationSignals.size()) {
|
|
finishTermination(true, QString());
|
|
return;
|
|
}
|
|
|
|
const QPair<int, int> signalPair =
|
|
m_terminationSignals.at(m_terminationSignalIndex++);
|
|
clearSignals(
|
|
signalPair.first, signalPair.second,
|
|
[this](bool success, const QString &error) {
|
|
if (!success) {
|
|
finishTermination(false, error);
|
|
return;
|
|
}
|
|
clearNextTerminationSignal();
|
|
});
|
|
}
|
|
|
|
void DeviceService::finishTermination(bool success, const QString &error)
|
|
{
|
|
if (!m_terminationActive) {
|
|
return;
|
|
}
|
|
|
|
const OperationCallback callback = m_terminationCallback;
|
|
m_terminationCallback = OperationCallback();
|
|
m_terminationActive = false;
|
|
m_terminationSignals.clear();
|
|
m_terminationSignalIndex = 0;
|
|
m_controlRequestPending = false;
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (success) {
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
}
|
|
if (callback) {
|
|
callback(success, error);
|
|
}
|
|
}
|
|
|
|
void DeviceService::beginProductionAction(
|
|
ProductionAction action, int trayNo, int slotNo,
|
|
const VisionWriteCallback &callback, const std::function<bool()> &beforeSend)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
const bool permitted = !beforeSend || beforeSend();
|
|
if (callback) {
|
|
callback(permitted ? VisionWriteOutcome::Confirmed : VisionWriteOutcome::NotSent,
|
|
permitted ? QString() : "Production action invalidated before transmission");
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
if (m_productionActionActive || m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, "A PLC production action is already active");
|
|
}
|
|
return;
|
|
}
|
|
if (trayNo <= 0 || slotNo < 1 || slotNo > 49) {
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, "PLC tray or slot position is invalid");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
const VisionConfig vision = ConfigManager::instance().visionConfig();
|
|
if (!actionAddresses(action, addresses, vision,
|
|
&m_productionCommandAddress,
|
|
&m_productionFeedbackAddress)) {
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, "Unknown PLC production action");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
inspectProductionAction(
|
|
action,
|
|
[this, action, trayNo, slotNo, addresses, callback, beforeSend](
|
|
bool success, const ProductionActionStatus &status,
|
|
const QString &error) {
|
|
if (!success) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent, error);
|
|
}
|
|
return;
|
|
}
|
|
if (status.command || status.feedback) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(VisionWriteOutcome::NotSent,
|
|
"PLC command or feedback is still active before a new action");
|
|
}
|
|
return;
|
|
}
|
|
|
|
auto sendCommand =
|
|
[this, callback](VisionWriteOutcome outcome, const QString &error) {
|
|
// An unacknowledged command may already be running. Do not
|
|
// permit another action until it has been reconciled.
|
|
m_productionActionActive = outcome != VisionWriteOutcome::NotSent;
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(outcome, error);
|
|
}
|
|
};
|
|
|
|
auto writeCommand = [this, sendCommand, beforeSend](bool success,
|
|
const QString &error) {
|
|
if (!success) {
|
|
sendCommand(VisionWriteOutcome::NotSent, error);
|
|
return;
|
|
}
|
|
// Action commands must never use the legacy automatic retry
|
|
// path, even when no persistence hook is supplied.
|
|
m_modbus->writeVisionCoil(m_productionCommandAddress, true,
|
|
sendCommand, beforeSend);
|
|
};
|
|
|
|
if (action == PhotoMoveAction || action == RackPick
|
|
|| action == RackPlace) {
|
|
m_modbus->writeHoldingRegister(
|
|
addresses.trayNumber,
|
|
static_cast<quint16>(trayNo),
|
|
[this, addresses, slotNo, writeCommand](
|
|
bool traySuccess, const QString &trayError) {
|
|
if (!traySuccess) {
|
|
writeCommand(false, trayError);
|
|
return;
|
|
}
|
|
m_modbus->writeHoldingRegister(
|
|
addresses.slotNumber,
|
|
static_cast<quint16>(slotNo),
|
|
writeCommand);
|
|
});
|
|
return;
|
|
}
|
|
|
|
writeCommand(true, QString());
|
|
});
|
|
}
|
|
|
|
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,
|
|
const std::function<bool(QString *)> &beforeClear)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
finishProductionAction([callback](bool success, const QString &error) {
|
|
if (callback) callback(success, success, error);
|
|
}, beforeClear);
|
|
return;
|
|
}
|
|
if (m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(true, false, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!m_productionActionActive) {
|
|
if (callback) {
|
|
callback(false, false, "No PLC production action is active");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
m_modbus->readCoil(
|
|
m_productionFeedbackAddress,
|
|
[this, callback, beforeClear](bool success, bool feedback,
|
|
const QString &error) {
|
|
if (!success) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(false, false, error);
|
|
}
|
|
return;
|
|
}
|
|
if (!feedback) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(true, false, QString());
|
|
}
|
|
return;
|
|
}
|
|
|
|
QString checkpointError;
|
|
const bool saved = beforeClear && beforeClear(&checkpointError);
|
|
m_productionActionBusy = false;
|
|
if (!saved) {
|
|
if (!beforeClear)
|
|
checkpointError = "Action completion checkpoint hook is missing";
|
|
// An empty error explicitly defers completion while the
|
|
// caller gathers asynchronous inspection data.
|
|
if (callback) callback(checkpointError.isEmpty(), false, checkpointError);
|
|
return;
|
|
}
|
|
finishProductionAction(
|
|
[callback](bool cleared, const QString &clearError) {
|
|
if (callback) callback(cleared, cleared, clearError);
|
|
}, [](QString *) { return true; });
|
|
});
|
|
}
|
|
|
|
void DeviceService::finishProductionAction(
|
|
const OperationCallback &callback,
|
|
const std::function<bool(QString *)> &beforeClear)
|
|
{
|
|
if (m_terminationActive || m_productionActionBusy) {
|
|
if (callback) callback(false, "PLC production action is busy or terminating");
|
|
return;
|
|
}
|
|
const bool simulation = ConfigManager::instance().simulationMode();
|
|
if (!simulation && !m_productionActionActive) {
|
|
if (callback) callback(false, "No PLC production action is active");
|
|
return;
|
|
}
|
|
m_productionActionBusy = true;
|
|
QString checkpointError;
|
|
if (!beforeClear || !beforeClear(&checkpointError)) {
|
|
m_productionActionBusy = false;
|
|
if (callback) callback(false, checkpointError.isEmpty()
|
|
? "Action completion checkpoint was not saved"
|
|
: checkpointError);
|
|
return;
|
|
}
|
|
auto finish = [this, callback](bool success, const QString &error) {
|
|
m_productionActionBusy = false;
|
|
if (success) {
|
|
m_productionActionActive = false;
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
}
|
|
if (callback) callback(success, error);
|
|
};
|
|
if (simulation) {
|
|
finish(true, QString());
|
|
return;
|
|
}
|
|
clearActionSignals(finish);
|
|
}
|
|
|
|
void DeviceService::cancelProductionAction(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()
|
|
|| (!m_productionActionActive && !m_productionActionBusy)) {
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "PLC production action is busy");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
clearActionSignals(
|
|
[this, callback](bool cleared, const QString &error) {
|
|
m_productionActionBusy = false;
|
|
if (cleared) {
|
|
m_productionActionActive = false;
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
}
|
|
if (callback) {
|
|
callback(cleared, error);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DeviceService::clearActionSignals(const OperationCallback &callback)
|
|
{
|
|
if (m_productionCommandAddress < 0
|
|
|| m_productionFeedbackAddress < 0) {
|
|
if (callback) {
|
|
callback(false, "PLC production action addresses are invalid");
|
|
}
|
|
return;
|
|
}
|
|
|
|
clearSignals(m_productionCommandAddress, m_productionFeedbackAddress,
|
|
callback);
|
|
}
|
|
|
|
void DeviceService::clearSignals(int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback)
|
|
{
|
|
m_modbus->writeCoil(
|
|
commandAddress, false,
|
|
[this, feedbackAddress, callback](bool commandCleared,
|
|
const QString &commandError) {
|
|
if (!commandCleared) {
|
|
if (callback) callback(false, commandError);
|
|
return;
|
|
}
|
|
m_modbus->writeCoil(
|
|
feedbackAddress, false,
|
|
[callback](
|
|
bool feedbackCleared, const QString &feedbackError) {
|
|
if (callback) {
|
|
callback(feedbackCleared, feedbackError);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
bool DeviceService::productionActionActive() const
|
|
{
|
|
return m_productionActionActive;
|
|
}
|
|
|
|
bool DeviceService::productionActionBusy() const
|
|
{
|
|
return m_productionActionBusy || m_modbus->isBusy();
|
|
}
|
|
|
|
bool DeviceService::checkVisionAccess(QString *error) const
|
|
{
|
|
if (m_terminationActive || ConfigManager::instance().simulationMode()
|
|
|| !ConfigManager::instance().productionEnabled()) {
|
|
if (error) *error = "Vision PLC access is disabled during termination or simulation";
|
|
return false;
|
|
}
|
|
if (m_modbus->config().serverAddress == 0) {
|
|
if (error) *error = "Vision PLC writes require an addressed response, not broadcast";
|
|
return false;
|
|
}
|
|
return ConfigManager::instance().visionConfig().isValid(error);
|
|
}
|
|
|
|
void DeviceService::moveToPhotoPosition(int trayNo, int slotNo,
|
|
const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend)
|
|
{
|
|
QString error;
|
|
if (!checkVisionAccess(&error) || trayNo < 1 || trayNo > 65535 || slotNo < 1 || slotNo > 49) {
|
|
if (callback) callback(VisionWriteOutcome::NotSent,
|
|
error.isEmpty() ? "Invalid photo-position tray/slot" : error);
|
|
return;
|
|
}
|
|
const ModbusAddressMap addresses = m_modbus->config().addressMap;
|
|
m_modbus->writeVisionHoldingRegister(addresses.trayNumber, quint16(trayNo),
|
|
[this, addresses, slotNo, callback, beforeSend](VisionWriteOutcome outcome, const QString &error) {
|
|
if (outcome != VisionWriteOutcome::Confirmed) {
|
|
if (callback) callback(outcome, error);
|
|
return;
|
|
}
|
|
m_modbus->writeVisionHoldingRegister(addresses.slotNumber, quint16(slotNo),
|
|
[this, callback, beforeSend](VisionWriteOutcome outcome, const QString &error) {
|
|
if (outcome != VisionWriteOutcome::Confirmed) {
|
|
if (callback) callback(outcome, error);
|
|
return;
|
|
}
|
|
writeVisionSignal(PhotoMove, true, callback, beforeSend);
|
|
}, beforeSend);
|
|
}, beforeSend);
|
|
}
|
|
|
|
void DeviceService::readRobotPhotoPose(const PoseCallback &callback)
|
|
{
|
|
QString error;
|
|
if (!checkVisionAccess(&error)) {
|
|
if (callback) callback(false, RobotPose(), error);
|
|
return;
|
|
}
|
|
const VisionConfig config = ConfigManager::instance().visionConfig();
|
|
const quint64 generation = m_visionGeneration;
|
|
if (!m_modbus->readRReals(config.photoPoseR, 6,
|
|
[this, callback, generation](bool success, const QVector<double> &values, const QString &text) {
|
|
RobotPose pose;
|
|
pose.xyzabc = values;
|
|
if (callback) {
|
|
callback(success && generation == m_visionGeneration && pose.isValid(), pose,
|
|
generation == m_visionGeneration ? text : "Vision pose read canceled");
|
|
}
|
|
}, config.lowWordFirst)) {
|
|
if (callback) callback(false, RobotPose(), "Invalid R40 read request");
|
|
}
|
|
}
|
|
|
|
void DeviceService::writeRobotGraspPose(const RobotPose &pose,
|
|
const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend)
|
|
{
|
|
QString error;
|
|
if (!checkVisionAccess(&error) || !pose.isValid()) {
|
|
if (callback) callback(VisionWriteOutcome::NotSent,
|
|
error.isEmpty() ? "Invalid robot grasp pose" : error);
|
|
return;
|
|
}
|
|
const VisionConfig config = ConfigManager::instance().visionConfig();
|
|
if (!m_modbus->writeRReals(config.graspPoseR, pose.xyzabc, callback, config.lowWordFirst, beforeSend)) {
|
|
if (callback) callback(VisionWriteOutcome::NotSent, "Invalid or overflowing R60 REAL request");
|
|
return;
|
|
}
|
|
for (int axis = 0; axis < pose.xyzabc.size(); ++axis) {
|
|
const double rounded = double(float(pose.xyzabc.at(axis)));
|
|
if (rounded != pose.xyzabc.at(axis)) {
|
|
qInfo().nospace() << "Vision R60 axis " << axis << " REAL rounding: "
|
|
<< QString::number(pose.xyzabc.at(axis), 'g', 17) << " -> "
|
|
<< QString::number(rounded, 'g', 17);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceService::readVisionHandshake(const HandshakeCallback &callback)
|
|
{
|
|
QString error;
|
|
if (!checkVisionAccess(&error)) {
|
|
if (callback) callback(false, VisionHandshake(), error);
|
|
return;
|
|
}
|
|
const VisionConfig config = ConfigManager::instance().visionConfig();
|
|
if (config.photoMoveCoilFeedback == config.photoMoveCoil + 1
|
|
&& config.comparisonPickCoil == config.photoMoveCoil + 2) {
|
|
const quint64 generation = m_visionGeneration;
|
|
m_modbus->readCoils(config.photoMoveCoil, 3,
|
|
[this, callback, generation](bool success, const QVector<bool> &values, const QString &text) {
|
|
VisionHandshake state;
|
|
if (success && values.size() == 3 && generation == m_visionGeneration) {
|
|
state.photoMove = values.at(0);
|
|
state.photoMoveFeedback = values.at(1);
|
|
state.comparisonPick = values.at(2);
|
|
if (callback) callback(true, state, QString());
|
|
} else if (callback) {
|
|
callback(false, state, text.isEmpty() ? "Vision handshake read canceled or incomplete" : text);
|
|
}
|
|
}, true);
|
|
return;
|
|
}
|
|
readVisionHandshakeStep(0, VisionHandshake(), m_visionGeneration, callback);
|
|
}
|
|
|
|
void DeviceService::readVisionHandshakeStep(int index, const VisionHandshake &state,
|
|
quint64 generation, const HandshakeCallback &callback)
|
|
{
|
|
if (generation != m_visionGeneration) {
|
|
if (callback) callback(false, VisionHandshake(), "Vision handshake read canceled");
|
|
return;
|
|
}
|
|
if (index == 3) {
|
|
if (callback) callback(true, state, QString());
|
|
return;
|
|
}
|
|
const VisionConfig config = ConfigManager::instance().visionConfig();
|
|
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) {
|
|
if (callback) callback(false, VisionHandshake(), error);
|
|
return;
|
|
}
|
|
VisionHandshake next = state;
|
|
switch (index) {
|
|
case 0: next.photoMove = value; break;
|
|
case 1: next.photoMoveFeedback = value; break;
|
|
case 2: next.comparisonPick = value; break;
|
|
}
|
|
readVisionHandshakeStep(index + 1, next, generation, callback);
|
|
});
|
|
}
|
|
|
|
void DeviceService::writeVisionSignal(VisionSignal signal, bool value,
|
|
const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend)
|
|
{
|
|
QString error;
|
|
if (!checkVisionAccess(&error)) {
|
|
if (callback) callback(VisionWriteOutcome::NotSent, error);
|
|
return;
|
|
}
|
|
const VisionConfig config = ConfigManager::instance().visionConfig();
|
|
int address = -1;
|
|
switch (signal) {
|
|
case PhotoMove: address = config.photoMoveCoil; break;
|
|
case PhotoMoveFeedback:
|
|
if (!value) address = config.photoMoveCoilFeedback; // Only PLC may publish arrival.
|
|
break;
|
|
case ComparisonPick: address = config.comparisonPickCoil; break;
|
|
default: break;
|
|
}
|
|
if (address < 0) {
|
|
if (callback) callback(VisionWriteOutcome::NotSent, "Unsupported photo-position output");
|
|
return;
|
|
}
|
|
m_modbus->writeVisionCoil(address, value, callback, beforeSend);
|
|
}
|
|
|
|
void DeviceService::cancelVisionRequests(const QString &reason)
|
|
{
|
|
++m_visionGeneration;
|
|
m_modbus->cancelVisionRequests(reason);
|
|
}
|