156 lines
5.6 KiB
C++
156 lines
5.6 KiB
C++
#ifndef DEVICESERVICE_H
|
|
#define DEVICESERVICE_H
|
|
|
|
#include <QString>
|
|
#include <QtGlobal>
|
|
#include <QList>
|
|
#include <QObject>
|
|
#include <QPair>
|
|
#include <functional>
|
|
#include "../models/visionresult.h"
|
|
|
|
struct DeviceStatus
|
|
{
|
|
bool statusValid = false;
|
|
bool plcConnected = false;
|
|
bool plcReady = false;
|
|
bool plcRunning = false;
|
|
bool robotIdle = false;
|
|
bool robotRunning = false;
|
|
bool robotAlarm = false;
|
|
bool trayPresent[6] = {};
|
|
bool safetyDoorClosed = false;
|
|
bool emergencyStop = false;
|
|
bool inspectionMachineReady = false;
|
|
bool visionReady = false;
|
|
bool robotControlCabinetReady = false;
|
|
bool mainControlCabinetReady = false;
|
|
bool safetyDoor1Closed = false;
|
|
bool safetyDoor2Closed = false;
|
|
bool safetyDoor3Closed = false;
|
|
bool activeAlarm = false;
|
|
quint16 currentStep = 0;
|
|
quint16 checkResult = 0;
|
|
quint16 errorCode = 0;
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(DeviceStatus)
|
|
|
|
struct ProductionActionStatus
|
|
{
|
|
bool command = false;
|
|
bool feedback = false;
|
|
};
|
|
|
|
class QTimer;
|
|
|
|
class DeviceService : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
using OperationCallback = std::function<void(bool, const QString &)>;
|
|
using StatusCallback =
|
|
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, PhotoMoveFeedback, ComparisonPick };
|
|
|
|
enum ProductionAction
|
|
{
|
|
PhotoMoveAction,
|
|
RackPick,
|
|
FrontLoad,
|
|
Inspect,
|
|
FrontUnload,
|
|
Flip,
|
|
ReverseFlip,
|
|
BackLoad,
|
|
BackUnload,
|
|
RackPlace
|
|
};
|
|
|
|
explicit DeviceService(QObject *parent = nullptr);
|
|
~DeviceService() override;
|
|
|
|
void startPolling();
|
|
void stopPolling();
|
|
bool isPolling() const;
|
|
|
|
void requestStatus(const StatusCallback &callback);
|
|
void clearAlarm(const OperationCallback &callback);
|
|
void setInspectionMode(const QString &mode,
|
|
const OperationCallback &callback);
|
|
void terminate(const OperationCallback &callback);
|
|
void resetProductionSignals(const OperationCallback &callback);
|
|
void beginProductionAction(ProductionAction action, int trayNo,
|
|
int slotNo, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend = {});
|
|
void pollProductionAction(const ActionCallback &callback,
|
|
const std::function<bool(QString *)> &beforeClear);
|
|
void finishProductionAction(const OperationCallback &callback,
|
|
const std::function<bool(QString *)> &beforeClear);
|
|
void inspectProductionAction(ProductionAction action,
|
|
const ActionStatusCallback &callback);
|
|
void adoptProductionAction(ProductionAction action);
|
|
void cancelProductionAction(const OperationCallback &callback);
|
|
bool productionActionActive() const;
|
|
bool productionActionBusy() const;
|
|
void moveToPhotoPosition(int trayNo, int slotNo, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend);
|
|
void readRobotPhotoPose(const PoseCallback &callback);
|
|
void writeRobotGraspPose(const RobotPose &pose, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend = {});
|
|
void readVisionHandshake(const HandshakeCallback &callback);
|
|
void writeVisionSignal(VisionSignal signal, bool value, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend = {});
|
|
void cancelVisionRequests(const QString &reason);
|
|
|
|
signals:
|
|
void statusChanged(const DeviceStatus &status);
|
|
void communicationError(const QString &error);
|
|
|
|
private:
|
|
void pollStatus();
|
|
DeviceStatus simulatedStatus() const;
|
|
void readStatusStep(int index, const DeviceStatus &status);
|
|
void finishStatusRequest(bool success, const DeviceStatus &status,
|
|
const QString &error);
|
|
void clearSignals(int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback);
|
|
void clearActionSignals(const OperationCallback &callback);
|
|
void clearNextTerminationSignal();
|
|
void clearNextResetSignal(quint64 generation);
|
|
void finishTermination(bool success, const QString &error);
|
|
void finishActionPoll(bool completed, const ActionCallback &callback);
|
|
bool checkVisionAccess(QString *error) const;
|
|
void readVisionHandshakeStep(int index, const VisionHandshake &state,
|
|
quint64 generation, const HandshakeCallback &callback);
|
|
|
|
QTimer *m_pollTimer;
|
|
class ModbusTcpManager *m_modbus;
|
|
bool m_statusRequestPending;
|
|
QList<StatusCallback> m_statusCallbacks;
|
|
bool m_controlRequestPending;
|
|
bool m_productionActionActive;
|
|
bool m_productionActionBusy;
|
|
int m_productionCommandAddress;
|
|
int m_productionFeedbackAddress;
|
|
bool m_terminationActive;
|
|
QList<QPair<int, int>> m_terminationSignals;
|
|
int m_terminationSignalIndex;
|
|
OperationCallback m_terminationCallback;
|
|
QList<int> m_resetSignals;
|
|
int m_resetSignalIndex = 0;
|
|
quint64 m_resetGeneration = 0;
|
|
quint64 m_visionGeneration = 0;
|
|
};
|
|
|
|
#endif // DEVICESERVICE_H
|