Files
VisionGuide/source/OpcUaComm.h
T

150 lines
5.7 KiB
C++
Raw Normal View History

2026-10-26 10:56:56 +08:00
#pragma once
// ════════════════════════════════════════════════════════════════════════════
// OpcUaComm.h — OPC UA 通信模块 (最终修复版)
// ════════════════════════════════════════════════════════════════════════════
#include <QObject>
#include <QString>
#include <QTimer>
#include <cstdint>
// ── 系统状态 ────────────────────────────────────────────────────────
enum class VisionSystemState : uint32_t {
Idle = 0,
Measuring = 1,
MeasureSuccess = 2,
MeasureFailed = 3,
Error = 99
};
// ── 测量结果码 ──────────────────────────────────────────────────────
enum class MeasureResultCode : uint32_t {
None = 0,
Success = 1,
Failed_TargetNotFound = 2,
Failed_ImageAcquisition = 3,
Failed_PoseCalculation = 4,
Failed_Timeout = 5,
Failed_ModelNotLoaded = 6,
Failed_CameraOffline = 7
};
// ── 零件类型 ────────────────────────────────────────────────────────
enum class PartType : uint32_t {
Unknown = 0,
Gold = 1, // 镀金
NonGold = 2 // 非镀金
};
// ── 指令返回码 ──────────────────────────────────────────────────────
enum class CmdStatusCode : uint32_t {
OK = 0,
Busy = 1,
NotReady = 2,
InvalidParam = 3,
InternalError = 99
};
// ── 位姿数据 ────────────────────────────────────────────────────────
struct PoseData {
float X = 0, Y = 0, Z = 0; // mm
float Rx = 0, Ry = 0, Rz = 0; // deg
uint32_t partType = 0; // PartType
bool valid = false;
void clear() { X = Y = Z = Rx = Ry = Rz = 0; partType = 0; valid = false; }
};
// ════════════════════════════════════════════════════════════════════════════
// OPC UA Server (双目 → 机械臂)
// ════════════════════════════════════════════════════════════════════════════
class OpcUaServer : public QObject
{
Q_OBJECT
public:
explicit OpcUaServer(QObject* parent = nullptr);
~OpcUaServer();
bool start(uint16_t port = 4840);
void stop();
bool isRunning() const;
void setSystemState(VisionSystemState s);
void setMeasureResult(MeasureResultCode c);
void setPoseData(const PoseData& p);
void setPoseValid(bool v);
void setErrorMessage(const QString& msg);
VisionSystemState systemState() const;
uint32_t handleStartMeasurementCommand(uint32_t timeoutMs);
void handleResetCommand();
signals:
void startMeasurementRequested(uint32_t timeoutMs);
void resetSystemRequested();
void logMessage(const QString& msg);
private slots:
void iterate();
private:
void createNodes();
struct Impl;
Impl* d;
QTimer* m_timer = nullptr;
};
// ════════════════════════════════════════════════════════════════════════════
// OPC UA Client (双目 → 机械臂)
// ════════════════════════════════════════════════════════════════════════════
class OpcUaClient : public QObject
{
Q_OBJECT
public:
explicit OpcUaClient(QObject* parent = nullptr);
~OpcUaClient();
bool connectToServer(const QString& url);
void disconnect();
bool isConnected() const;
bool readRobotPose(PoseData& out);
void setRobotPoseNodeIds(const QString& x, const QString& y, const QString& z,
const QString& rx, const QString& ry, const QString& rz);
signals:
void logMessage(const QString& msg);
private:
struct Impl;
Impl* d;
};
// ════════════════════════════════════════════════════════════════════════════
// 通信管理器
// ════════════════════════════════════════════════════════════════════════════
class OpcUaCommManager : public QObject
{
Q_OBJECT
public:
explicit OpcUaCommManager(QObject* parent = nullptr);
~OpcUaCommManager();
OpcUaServer* server() const { return m_server; }
OpcUaClient* client() const { return m_client; }
bool startServer(uint16_t port = 4840);
void stopServer();
bool connectToRobot(const QString& url);
void disconnectFromRobot();
void publishMeasureResult(MeasureResultCode code, const PoseData& pose, const QString& errMsg = {});
void resetToIdle();
signals:
void measurementRequested(uint32_t timeoutMs);
void resetRequested();
void logMessage(const QString& msg);
private:
OpcUaServer* m_server;
OpcUaClient* m_client;
};