2025-08-27 23:10:36 +08:00
|
|
|
|
#ifndef BELTTEARINGPRESENTER_H
|
|
|
|
|
|
#define BELTTEARINGPRESENTER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
#include <QMap>
|
2025-10-24 23:19:44 +08:00
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
|
#include <QDataStream>
|
2025-09-21 22:20:24 +08:00
|
|
|
|
#include <deque>
|
|
|
|
|
|
#include <mutex>
|
2025-09-29 00:56:53 +08:00
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
#include <atomic>
|
2025-08-27 23:10:36 +08:00
|
|
|
|
#include <opencv2/opencv.hpp>
|
2025-09-10 00:31:27 +08:00
|
|
|
|
#include "IVrEyeDevice.h"
|
2025-10-12 16:46:46 +08:00
|
|
|
|
#include "beltTearingDetection_Export.h"
|
2025-09-10 00:31:27 +08:00
|
|
|
|
#include "IVrBeltTearingConfig.h"
|
|
|
|
|
|
#include "VZNL_Common.h"
|
|
|
|
|
|
#include "VZNL_Types.h"
|
|
|
|
|
|
#include "VrLog.h"
|
2025-09-21 22:20:24 +08:00
|
|
|
|
#include "IYTCPServer.h"
|
2025-10-24 23:19:44 +08:00
|
|
|
|
#include "RobotProtocol.h" // 添加RobotProtocol头文件
|
|
|
|
|
|
#include "ModbusRTUMaster.h" // 添加ModbusRTUMaster头文件
|
2025-09-29 00:56:53 +08:00
|
|
|
|
#include "ProtocolCommon.h" // 添加ProtocolCommon头文件
|
2025-11-19 00:23:09 +08:00
|
|
|
|
#include "TearingTcpProtocol.h" // 添加TearingTcpProtocol头文件
|
2025-08-27 23:10:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
class BeltTearingPresenter : public QObject, public IVrBeltTearingConfigChangeNotify
|
2025-08-27 23:10:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit BeltTearingPresenter(QObject *parent = nullptr);
|
|
|
|
|
|
~BeltTearingPresenter();
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// 配置相关方法
|
|
|
|
|
|
bool loadConfiguration(const QString& configFilePath);
|
|
|
|
|
|
void applyAlgorithmParameters(const BeltTearingAlgorithmParams& params);
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-08-27 23:10:36 +08:00
|
|
|
|
// 初始化TCP服务器
|
2025-09-10 00:31:27 +08:00
|
|
|
|
bool startServer(quint16 port = 0); // port = 0 表示使用配置文件中的端口
|
2025-08-27 23:10:36 +08:00
|
|
|
|
void stopServer();
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// 相机相关方法
|
|
|
|
|
|
bool initializeCamera();
|
2025-11-26 22:44:38 +08:00
|
|
|
|
int startCamera();
|
|
|
|
|
|
int stopCamera();
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-09-29 00:56:53 +08:00
|
|
|
|
void ResetDetect();
|
|
|
|
|
|
|
2025-11-10 00:19:20 +08:00
|
|
|
|
// 系统控制方法(供ModbusTCP调用)
|
2025-11-26 22:44:38 +08:00
|
|
|
|
int StopWork(); // 停止工作
|
|
|
|
|
|
int StartWork(); // 开始工作
|
2025-11-10 00:19:20 +08:00
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// 获取配置信息
|
|
|
|
|
|
quint16 getServerPort() const { return m_configResult.serverPort; }
|
|
|
|
|
|
const BeltTearingConfigResult& getConfig() const { return m_configResult; }
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// IVrBeltTearingConfigChangeNotify接口实现
|
|
|
|
|
|
void OnConfigChanged(const BeltTearingConfigResult& configResult) override;
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-09-29 00:56:53 +08:00
|
|
|
|
// 发送模拟数据
|
2025-09-21 22:20:24 +08:00
|
|
|
|
void sendTestData(std::string fileName);
|
2025-09-29 00:56:53 +08:00
|
|
|
|
void sendSimulationData();
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-08-27 23:10:36 +08:00
|
|
|
|
private slots:
|
2025-09-10 00:31:27 +08:00
|
|
|
|
void onCameraInitTimer();
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
2025-08-27 23:10:36 +08:00
|
|
|
|
private:
|
2025-09-21 22:20:24 +08:00
|
|
|
|
// TCP服务器回调函数
|
|
|
|
|
|
static void OnServerRecv(const TCPClient* pClient, const char* pData, const unsigned int nLen);
|
|
|
|
|
|
static void OnServerEvent(const TCPClient* pClient, TCPServerEventType eventType);
|
|
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// TCP服务器相关
|
2025-11-26 22:44:38 +08:00
|
|
|
|
IYTCPServer *m_tcpServer; // 内部对接服务器(ReadConfig/WriteConfig)
|
2025-11-19 00:23:09 +08:00
|
|
|
|
TearingTcpProtocol *m_tearingProtocol; // 撕裂TCP通信协议处理器
|
2025-09-21 22:20:24 +08:00
|
|
|
|
QMap<QString, const TCPClient*> m_clients;
|
2025-08-27 23:10:36 +08:00
|
|
|
|
quint16 m_port;
|
2025-11-19 00:23:09 +08:00
|
|
|
|
quint16 m_tcpPort; // 新协议端口
|
|
|
|
|
|
|
2025-09-10 00:31:27 +08:00
|
|
|
|
// 配置相关
|
|
|
|
|
|
IVrBeltTearingConfig *m_config;
|
|
|
|
|
|
BeltTearingConfigResult m_configResult;
|
|
|
|
|
|
QString m_configFilePath;
|
|
|
|
|
|
|
|
|
|
|
|
// 相机相关
|
2025-11-10 00:19:20 +08:00
|
|
|
|
IVrEyeDevice *m_eyeDevice = nullptr;
|
2025-09-10 00:31:27 +08:00
|
|
|
|
QTimer *m_cameraInitTimer;
|
|
|
|
|
|
QString m_cameraIP;
|
|
|
|
|
|
bool m_cameraInitialized;
|
|
|
|
|
|
bool m_cameraDetecting;
|
|
|
|
|
|
|
2025-09-21 22:20:24 +08:00
|
|
|
|
// SDK算法相关
|
|
|
|
|
|
std::atomic<bool> m_bInitAlgo{false};
|
|
|
|
|
|
std::vector<SSG_hLineProInfo> m_hLineWorkers;
|
2025-09-24 22:36:13 +08:00
|
|
|
|
|
2025-09-21 22:20:24 +08:00
|
|
|
|
SSG_beltTearingParam m_algorithmParam;
|
2025-09-24 22:36:13 +08:00
|
|
|
|
std::vector<SSG_beltTearingInfo> m_beltTearings_new;
|
|
|
|
|
|
std::vector<SSG_beltTearingInfo> m_beltTearings_growing;
|
|
|
|
|
|
std::vector<SSG_beltTearingInfo> m_beltTearings_ended;
|
|
|
|
|
|
std::vector<SSG_beltTearingInfo> m_beltTearings_unknown;
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 激光线队列管理
|
|
|
|
|
|
std::deque<SVzLaserLineData> m_laserLineQueue; // 激光线数据队列
|
|
|
|
|
|
std::mutex m_queueMutex; // 队列访问互斥锁
|
2025-09-29 00:56:53 +08:00
|
|
|
|
unsigned long long m_lineCounter; // 新增线条计数器
|
2025-11-19 00:23:09 +08:00
|
|
|
|
int m_maxQueueSize; // 最大队列大小(从配置读取)
|
|
|
|
|
|
int m_generationInterval; // 图像生成间隔(从配置读取)
|
2025-09-29 00:56:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 检测数据缓存和算法检测线程
|
|
|
|
|
|
std::mutex m_detectionDataMutex; // 检测数据缓存访问互斥锁
|
|
|
|
|
|
std::atomic<bool> m_bAlgoDetectThreadRunning{false}; // 算法检测线程运行标志
|
|
|
|
|
|
|
|
|
|
|
|
std::mutex m_algoDetectMutex; // 算法检测互斥锁
|
|
|
|
|
|
std::condition_variable m_algoDetectCondition; // 算法检测条件变量
|
|
|
|
|
|
std::thread m_algoDetectThread; // 算法检测线程
|
2025-09-10 00:31:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 相机状态回调函数
|
|
|
|
|
|
static void OnStatusCallback(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
|
|
|
|
|
|
|
|
|
|
|
|
// 点云数据回调函数
|
|
|
|
|
|
static void OnPointCloudCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
|
|
|
|
|
|
|
|
|
|
|
|
// 发送算法结果
|
|
|
|
|
|
void sendTearingResults(const std::vector<SSG_beltTearingInfo>& results);
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 激光线队列管理方法
|
|
|
|
|
|
void addLaserLineToQueue(const SVzLaserLineData* laserLine);
|
2025-09-29 00:56:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 算法检测线程方法
|
|
|
|
|
|
void _AlgoDetectThread();
|
|
|
|
|
|
int _DetectTask();
|
|
|
|
|
|
// 合并撕裂结果的独立函数
|
|
|
|
|
|
void _MergeAndReplace(std::vector<SSG_beltTearingInfo>& allTearings, const std::vector<SSG_beltTearingInfo>& source);
|
2025-09-21 22:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 图像生成和发送
|
|
|
|
|
|
void sendImageToClients(const QImage& image);
|
2025-09-10 00:31:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 工具方法
|
2025-09-21 22:20:24 +08:00
|
|
|
|
QString generateClientId(const TCPClient* client);
|
|
|
|
|
|
|
|
|
|
|
|
// 实例方法(非静态)
|
|
|
|
|
|
void handleServerRecv(const TCPClient* pClient, const char* pData, const unsigned int nLen);
|
|
|
|
|
|
void handleServerEvent(const TCPClient* pClient, TCPServerEventType eventType);
|
|
|
|
|
|
|
2025-09-29 00:56:53 +08:00
|
|
|
|
// 处理算法参数更新
|
|
|
|
|
|
void handleAlgorithmParameterUpdate(const QByteArray& paramData);
|
|
|
|
|
|
void handleSetAlgorithmParams(const QJsonObject& paramObj);
|
|
|
|
|
|
void handleGetServerInfo(const QJsonObject& requestObj);
|
|
|
|
|
|
void handleReadConfig(const TCPClient* pClient); // 添加ReadConfig处理函数声明
|
|
|
|
|
|
void handleWriteConfig(const QByteArray& paramData); // 添加WriteConfig处理函数声明
|
|
|
|
|
|
|
2025-09-21 22:20:24 +08:00
|
|
|
|
// 获取单例实例(用于静态回调)
|
|
|
|
|
|
static BeltTearingPresenter* s_instance;
|
|
|
|
|
|
|
|
|
|
|
|
// 配置与SDK参数转换函数
|
|
|
|
|
|
SSG_beltTearingParam configToSDKParam(const BeltTearingConfigResult& config) const;
|
|
|
|
|
|
BeltTearingConfigResult sdkToConfigParam(const SSG_beltTearingParam& sdkParam) const;
|
2025-09-29 00:56:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 添加RobotProtocol相关成员变量和方法
|
|
|
|
|
|
RobotProtocol* m_pRobotProtocol = nullptr; // 机械臂协议实例
|
|
|
|
|
|
bool m_bRobotConnected = false; // 机械臂连接状态
|
|
|
|
|
|
|
2025-10-24 23:19:44 +08:00
|
|
|
|
// 添加ModbusRTUMaster相关成员变量和方法
|
|
|
|
|
|
ModbusRTUMaster* m_pModbusRTUMaster = nullptr; // Modbus-RTU主端实例
|
|
|
|
|
|
bool m_bModbusRTUConnected = false; // Modbus-RTU连接状态
|
|
|
|
|
|
|
2025-09-29 00:56:53 +08:00
|
|
|
|
// 初始化机械臂协议
|
|
|
|
|
|
int InitRobotProtocol();
|
|
|
|
|
|
|
2025-10-24 23:19:44 +08:00
|
|
|
|
// 初始化Modbus-RTU主端
|
|
|
|
|
|
int InitModbusRTUMaster();
|
|
|
|
|
|
|
2025-09-29 00:56:53 +08:00
|
|
|
|
// 机械臂协议回调函数
|
|
|
|
|
|
void OnRobotConnectionChanged(bool connected);
|
|
|
|
|
|
bool OnRobotWorkSignal(bool startWork, int cameraIndex);
|
|
|
|
|
|
|
|
|
|
|
|
// 发送检测结果给机械臂
|
|
|
|
|
|
void SendDetectionResultToRobot(const std::vector<SSG_beltTearingInfo>& detectionResults);
|
|
|
|
|
|
|
2025-10-24 23:19:44 +08:00
|
|
|
|
// 发送温度数据给所有客户端
|
|
|
|
|
|
void SendTemperatureData(float temperature);
|
2025-09-29 00:56:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-27 23:10:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // BELTTEARINGPRESENTER_H
|