diff --git a/.gitignore b/.gitignore index 7b92f9e..ae38382 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /tests/ /build/ +/docs/ *.vision-state.json diff --git a/config.ini b/config.ini index 78fafc5..23cf84e 100644 --- a/config.ini +++ b/config.ini @@ -21,6 +21,12 @@ reconnectAttempts=2 ; Coils clearAlarmCommand=815 plcRunning=817 +trayPresent1=820 +trayPresent2=821 +trayPresent3=822 +trayPresent4=823 +trayPresent5=824 +trayPresent6=825 safetyDoorClosed=826 safetyDoor1Closed=827 safetyDoor2Closed=828 diff --git a/config/configmanager.cpp b/config/configmanager.cpp index dbe32f3..60cb534 100644 --- a/config/configmanager.cpp +++ b/config/configmanager.cpp @@ -86,6 +86,12 @@ bool ConfigManager::load(const QString &filePath) ModbusAddressMap &addresses = m_modbusConfig.addressMap; addresses.clearAlarmCommand = settings.value("modbus/clearAlarmCommand", addresses.clearAlarmCommand).toInt(); addresses.plcRunning = settings.value("modbus/plcRunning", addresses.plcRunning).toInt(); + addresses.trayPresent1 = settings.value("modbus/trayPresent1", addresses.trayPresent1).toInt(); + addresses.trayPresent2 = settings.value("modbus/trayPresent2", addresses.trayPresent2).toInt(); + addresses.trayPresent3 = settings.value("modbus/trayPresent3", addresses.trayPresent3).toInt(); + addresses.trayPresent4 = settings.value("modbus/trayPresent4", addresses.trayPresent4).toInt(); + addresses.trayPresent5 = settings.value("modbus/trayPresent5", addresses.trayPresent5).toInt(); + addresses.trayPresent6 = settings.value("modbus/trayPresent6", addresses.trayPresent6).toInt(); addresses.plcReady = settings.value("modbus/plcReady", addresses.plcReady).toInt(); addresses.robotIdle = settings.value("modbus/robotIdle", addresses.robotIdle).toInt(); addresses.robotRunning = settings.value("modbus/robotRunning", addresses.robotRunning).toInt(); diff --git a/modbus/modbusaddress.cpp b/modbus/modbusaddress.cpp index ca4153a..75bcac0 100644 --- a/modbus/modbusaddress.cpp +++ b/modbus/modbusaddress.cpp @@ -18,6 +18,12 @@ bool ModbusAddressMap::isValid(QString *error) const const QList > coilAddresses = QList >() << qMakePair(QString("clearAlarmCommand"), clearAlarmCommand) << qMakePair(QString("plcRunning"), plcRunning) + << qMakePair(QString("trayPresent1"), trayPresent1) + << qMakePair(QString("trayPresent2"), trayPresent2) + << qMakePair(QString("trayPresent3"), trayPresent3) + << qMakePair(QString("trayPresent4"), trayPresent4) + << qMakePair(QString("trayPresent5"), trayPresent5) + << qMakePair(QString("trayPresent6"), trayPresent6) << qMakePair(QString("plcReady"), plcReady) << qMakePair(QString("robotIdle"), robotIdle) << qMakePair(QString("robotRunning"), robotRunning) diff --git a/modbus/modbusaddress.h b/modbus/modbusaddress.h index 65ee119..9cb493c 100644 --- a/modbus/modbusaddress.h +++ b/modbus/modbusaddress.h @@ -8,6 +8,12 @@ struct ModbusAddressMap int clearAlarmCommand = 3; int plcRunning = 817; + int trayPresent1 = 820; + int trayPresent2 = 821; + int trayPresent3 = 822; + int trayPresent4 = 823; + int trayPresent5 = 824; + int trayPresent6 = 825; int safetyDoorClosed = 826; int safetyDoor1Closed = 827; int safetyDoor2Closed = 828; diff --git a/pages/dashboardpage.cpp b/pages/dashboardpage.cpp index 706331a..5ee96ef 100644 --- a/pages/dashboardpage.cpp +++ b/pages/dashboardpage.cpp @@ -1152,6 +1152,7 @@ void DashboardPage::updateCurrentProcessDuration(int elapsedMs) void DashboardPage::onDeviceStatusChanged(const DeviceStatus &status) { + deviceStatus = status; emit deviceStatusChanged(status); const bool offline = !status.statusValid || !status.plcConnected; const QString plcText = offline ? "离线" @@ -1254,14 +1255,24 @@ void DashboardPage::updateRackSummary() } const Tray &tray = trays.at(index); - const QString status = tray.present ? tray.status : "离位"; + const bool sensorAvailable = deviceStatus.statusValid + && deviceStatus.plcConnected; + const bool present = !sensorAvailable || deviceStatus.trayPresent[index]; + if (!present) { + updateStatusCell(cell, + QString("托盘 %1\n不在位:0/49").arg(index + 1), + "#90a4ae", + highlightedTrayIds.contains(tray.id)); + continue; + } + + const QString status = tray.status; const QString statusText = status == "OK" ? "已完成" : status == "NG" ? "有 NG" : status == "PROCESSING" ? "加工中" : status == "PENDING" ? "待检测" - : tray.present ? "在位" : "离位"; - const QString color = !tray.present ? "#90a4ae" - : status == "NG" ? "#d32f2f" + : "有托盘"; + const QString color = status == "NG" ? "#d32f2f" : status == "PROCESSING" ? "#1e88e5" : status == "OK" ? "#2e7d32" : "#78909c"; updateStatusCell(cell, diff --git a/pages/dashboardpage.h b/pages/dashboardpage.h index f1aa3c1..e6ec20a 100644 --- a/pages/dashboardpage.h +++ b/pages/dashboardpage.h @@ -113,6 +113,7 @@ private: int currentOrderId; bool currentOrderStartable; bool currentOrderResumable; + DeviceStatus deviceStatus; Workpiece productionWorkpiece; int productionTrayNo; int productionSlotNo; diff --git a/pages/rackpage.cpp b/pages/rackpage.cpp index a7aa782..a8408b0 100644 --- a/pages/rackpage.cpp +++ b/pages/rackpage.cpp @@ -4,6 +4,7 @@ #include "squaretraybutton.h" #include "uihelpers.h" +#include "../services/deviceservice.h" #include "../services/trayservice.h" #include @@ -14,6 +15,19 @@ #include #include +namespace { + +QString displayTrayStatus(const QString &status) +{ + if (status == "OK") return "已完成"; + if (status == "NG") return "有 NG"; + if (status == "PROCESSING") return "加工中"; + if (status == "PENDING") return "待检测"; + return "有托盘"; +} + +} + RackPage::RackPage(QWidget *parent) : QWidget(parent) , ui(new Ui::RackPage) @@ -106,6 +120,15 @@ void RackPage::refresh() loadTrays(); } +void RackPage::updateDeviceStatus(const DeviceStatus &status) +{ + trayPresenceAvailable = status.statusValid && status.plcConnected; + for (int index = 0; index < 6; ++index) { + trayPresent[index] = status.trayPresent[index]; + } + updateTrayButtons(); +} + void RackPage::loadTrays() { QString error; @@ -148,15 +171,17 @@ void RackPage::updateTrayButtons() for (int index = 0; index < rackTrayButtons.size(); ++index) { SquareTrayButton *button = rackTrayButtons.at(index); if (index >= trays.size()) { - button->setTrayInfo(0, QString(), 0, "无数据", false); + button->setTrayInfo(0, QString(), 0, "无数据", false, true); button->setPlanned(false); button->setChecked(false); continue; } const Tray &tray = trays.at(index); + const bool sensorPresent = !trayPresenceAvailable || trayPresent[index]; button->setTrayInfo(tray.id, tray.code, tray.loadedCount, - tray.status, tray.present); + displayTrayStatus(tray.status), tray.present, + sensorPresent); bool planned = false; QString error; const QList traySlots = diff --git a/pages/rackpage.h b/pages/rackpage.h index 75c6089..c386d1f 100644 --- a/pages/rackpage.h +++ b/pages/rackpage.h @@ -11,6 +11,7 @@ class QLabel; class QButtonGroup; class QResizeEvent; class SquareTrayButton; +struct DeviceStatus; namespace Ui { class RackPage; } @@ -24,6 +25,7 @@ public: public slots: void refresh(); + void updateDeviceStatus(const DeviceStatus &status); signals: void logRequested(const QString &type, const QString &module, const QString &action, @@ -48,6 +50,8 @@ private: QList trays; int selectedTrayId; int selectedTrayIndex; + bool trayPresenceAvailable = false; + bool trayPresent[6] = {}; }; #endif // RACKPAGE_H diff --git a/pages/squaretraybutton.cpp b/pages/squaretraybutton.cpp index 872243a..570d52c 100644 --- a/pages/squaretraybutton.cpp +++ b/pages/squaretraybutton.cpp @@ -7,6 +7,7 @@ SquareTrayButton::SquareTrayButton(int trayIndex, int loadedCount, QWidget *pare : QAbstractButton(parent) , m_trayId(0) , m_planned(false) + , m_sensorPresent(true) { setCheckable(true); setCursor(Qt::PointingHandCursor); @@ -14,16 +15,17 @@ SquareTrayButton::SquareTrayButton(int trayIndex, int loadedCount, QWidget *pare } void SquareTrayButton::setTrayInfo(int trayId, const QString &code, int loadedCount, - const QString &status, bool present) + const QString &status, bool databasePresent, + bool sensorPresent) { m_trayId = trayId; + m_sensorPresent = sensorPresent; const QString displayCode = code.isEmpty() ? QString("托盘 %1").arg(trayId) : code; - const QString displayStatus = present ? status : "离位"; - setText(QString("%1\n7 x 7\n%2 %3/49") - .arg(displayCode) - .arg(displayStatus) - .arg(loadedCount)); - setEnabled(present); + setText(sensorPresent + ? QString("%1\n7 x 7\n%2:%3/49") + .arg(displayCode).arg(status).arg(loadedCount) + : QString("%1\n7 x 7\n不在位:0/49").arg(displayCode)); + setEnabled(databasePresent); update(); } @@ -51,11 +53,14 @@ void SquareTrayButton::paintEvent(QPaintEvent *event) painter.setRenderHint(QPainter::Antialiasing, true); const QRectF rect = QRectF(1, 1, width() - 2, height() - 2); - const QColor background = isChecked() ? QColor("#00897b") + const QColor background = !m_sensorPresent ? QColor("#90a4ae") + : isChecked() ? QColor("#00897b") : m_planned ? QColor("#f9a825") : QColor("#eceff1"); - const QColor border = isChecked() ? QColor("#004d40") + const QColor border = !m_sensorPresent ? QColor("#78909c") + : isChecked() ? QColor("#004d40") : m_planned ? QColor("#ef6c00") : QColor("#b0bec5"); - const QColor textColor = isChecked() ? QColor("#ffffff") : QColor("#263238"); + const QColor textColor = !m_sensorPresent || isChecked() + ? QColor("#ffffff") : QColor("#263238"); painter.setPen(QPen(border, 2)); painter.setBrush(background); diff --git a/pages/squaretraybutton.h b/pages/squaretraybutton.h index d8c9faa..696dd68 100644 --- a/pages/squaretraybutton.h +++ b/pages/squaretraybutton.h @@ -9,7 +9,8 @@ public: explicit SquareTrayButton(int trayIndex, int loadedCount, QWidget *parent = nullptr); void setTrayInfo(int trayId, const QString &code, int loadedCount, - const QString &status, bool present); + const QString &status, bool databasePresent, + bool sensorPresent); void setPlanned(bool planned); QSize sizeHint() const override; @@ -21,6 +22,7 @@ protected: private: int m_trayId; bool m_planned; + bool m_sensorPresent; }; #endif // SQUARETRAYBUTTON_H diff --git a/services/deviceservice.cpp b/services/deviceservice.cpp index f7e18a9..43a44c6 100644 --- a/services/deviceservice.cpp +++ b/services/deviceservice.cpp @@ -182,7 +182,7 @@ void DeviceService::readStatusStep(int index, const DeviceStatus &status) { const bool useErrorCode = ConfigManager::instance().productionConfig().usePlcErrorCode; - if (index >= (useErrorCode ? 19 : 18)) { + if (index >= (useErrorCode ? 25 : 24)) { finishStatusRequest(true, status, QString()); return; } @@ -205,23 +205,29 @@ void DeviceService::readStatusStep(int index, const DeviceStatus &status) DeviceStatus nextStatus = status; switch (index) { case 1: nextStatus.plcRunning = value != 0; break; - case 2: nextStatus.safetyDoorClosed = value != 0; break; - case 3: nextStatus.safetyDoor1Closed = value != 0; break; - case 4: nextStatus.safetyDoor2Closed = value != 0; break; - case 5: nextStatus.safetyDoor3Closed = value != 0; break; - case 6: nextStatus.emergencyStop = value != 0; break; - case 7: nextStatus.robotIdle = value != 0; break; - case 8: nextStatus.robotRunning = value != 0; break; - case 9: nextStatus.robotAlarm = value != 0; break; - case 10: nextStatus.plcReady = value != 0; break; - case 11: nextStatus.inspectionMachineReady = value != 0; break; - case 12: nextStatus.visionReady = value != 0; break; - case 13: nextStatus.robotControlCabinetReady = value != 0; break; - case 14: nextStatus.mainControlCabinetReady = value != 0; break; - case 15: nextStatus.activeAlarm = value != 0; break; - case 16: nextStatus.currentStep = value; break; - case 17: nextStatus.checkResult = value; break; - case 18: nextStatus.errorCode = value; break; + case 2: nextStatus.trayPresent[0] = value != 0; break; + case 3: nextStatus.trayPresent[1] = value != 0; break; + case 4: nextStatus.trayPresent[2] = value != 0; break; + case 5: nextStatus.trayPresent[3] = value != 0; break; + case 6: nextStatus.trayPresent[4] = value != 0; break; + case 7: nextStatus.trayPresent[5] = value != 0; break; + case 8: nextStatus.safetyDoorClosed = value != 0; break; + case 9: nextStatus.safetyDoor1Closed = value != 0; break; + case 10: nextStatus.safetyDoor2Closed = value != 0; break; + case 11: nextStatus.safetyDoor3Closed = value != 0; break; + case 12: nextStatus.emergencyStop = value != 0; break; + case 13: nextStatus.robotIdle = value != 0; break; + case 14: nextStatus.robotRunning = value != 0; break; + case 15: nextStatus.robotAlarm = value != 0; break; + case 16: nextStatus.plcReady = value != 0; break; + case 17: nextStatus.inspectionMachineReady = value != 0; break; + case 18: nextStatus.visionReady = value != 0; break; + case 19: nextStatus.robotControlCabinetReady = value != 0; break; + case 20: nextStatus.mainControlCabinetReady = value != 0; break; + case 21: nextStatus.activeAlarm = value != 0; break; + case 22: nextStatus.currentStep = value; break; + case 23: nextStatus.checkResult = value; break; + case 24: nextStatus.errorCode = value; break; default: break; } readStatusStep(index + 1, nextStatus); @@ -236,96 +242,132 @@ void DeviceService::readStatusStep(int index, const DeviceStatus &status) }); break; case 2: - m_modbus->readCoil(addresses.safetyDoorClosed, + m_modbus->readCoil(addresses.trayPresent1, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 3: - m_modbus->readCoil(addresses.safetyDoor1Closed, + m_modbus->readCoil(addresses.trayPresent2, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 4: - m_modbus->readCoil(addresses.safetyDoor2Closed, + m_modbus->readCoil(addresses.trayPresent3, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 5: - m_modbus->readCoil(addresses.safetyDoor3Closed, + m_modbus->readCoil(addresses.trayPresent4, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 6: - m_modbus->readCoil(addresses.emergencyStop, + m_modbus->readCoil(addresses.trayPresent5, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 7: - m_modbus->readCoil(addresses.robotIdle, + m_modbus->readCoil(addresses.trayPresent6, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 8: - m_modbus->readCoil(addresses.robotRunning, + m_modbus->readCoil(addresses.safetyDoorClosed, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 9: - m_modbus->readCoil(addresses.robotAlarm, + m_modbus->readCoil(addresses.safetyDoor1Closed, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 10: - m_modbus->readCoil(addresses.plcReady, + m_modbus->readCoil(addresses.safetyDoor2Closed, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 11: - m_modbus->readCoil(addresses.inspectionMachineReady, + m_modbus->readCoil(addresses.safetyDoor3Closed, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 12: - m_modbus->readCoil(addresses.visionReady, + m_modbus->readCoil(addresses.emergencyStop, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 13: - m_modbus->readCoil(addresses.robotControlCabinetReady, + m_modbus->readCoil(addresses.robotIdle, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 14: - m_modbus->readCoil(addresses.mainControlCabinetReady, + m_modbus->readCoil(addresses.robotRunning, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 15: - m_modbus->readCoil(addresses.activeAlarm, + m_modbus->readCoil(addresses.robotAlarm, [next](bool ok, bool value, const QString &error) { next(ok, value ? 1 : 0, error); }); break; case 16: - m_modbus->readHoldingRegister(addresses.currentStep, next); + m_modbus->readCoil(addresses.plcReady, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); break; case 17: - m_modbus->readHoldingRegister(addresses.checkResult, next); + m_modbus->readCoil(addresses.inspectionMachineReady, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); break; case 18: + m_modbus->readCoil(addresses.visionReady, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); + break; + case 19: + m_modbus->readCoil(addresses.robotControlCabinetReady, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); + break; + case 20: + m_modbus->readCoil(addresses.mainControlCabinetReady, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); + break; + case 21: + m_modbus->readCoil(addresses.activeAlarm, + [next](bool ok, bool value, const QString &error) { + next(ok, value ? 1 : 0, error); + }); + break; + case 22: + m_modbus->readHoldingRegister(addresses.currentStep, next); + break; + case 23: + m_modbus->readHoldingRegister(addresses.checkResult, next); + break; + case 24: m_modbus->readHoldingRegister(addresses.errorCode, next); break; } @@ -355,6 +397,7 @@ DeviceStatus DeviceService::simulatedStatus() const status.robotIdle = true; status.robotRunning = false; status.robotAlarm = false; + for (int index = 0; index < 6; ++index) status.trayPresent[index] = true; status.safetyDoorClosed = true; status.emergencyStop = false; status.inspectionMachineReady = true; diff --git a/services/deviceservice.h b/services/deviceservice.h index 28e910a..384d3aa 100644 --- a/services/deviceservice.h +++ b/services/deviceservice.h @@ -18,6 +18,7 @@ struct DeviceStatus bool robotIdle = false; bool robotRunning = false; bool robotAlarm = false; + bool trayPresent[6] = {}; bool safetyDoorClosed = false; bool emergencyStop = false; bool inspectionMachineReady = false; diff --git a/widget.cpp b/widget.cpp index 4cc1146..886be32 100644 --- a/widget.cpp +++ b/widget.cpp @@ -176,6 +176,8 @@ void Widget::setupMainArea(QVBoxLayout *rootLayout) }); connect(dashboardPage, &DashboardPage::deviceStatusChanged, devicePage, &DevicePage::updateStatus); + connect(dashboardPage, &DashboardPage::deviceStatusChanged, + rackPage, &RackPage::updateDeviceStatus); connect(dashboardPage, &DashboardPage::deviceStatusChanged, this, [this](const DeviceStatus &status) { const bool offline = !status.statusValid || !status.plcConnected;