129 lines
4.8 KiB
C++
129 lines
4.8 KiB
C++
#ifndef WELDSEAMPRESENTER_H
|
|
#define WELDSEAMPRESENTER_H
|
|
|
|
#include <atomic>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
#include <QObject>
|
|
#include <QString>
|
|
|
|
#include "BasePresenter.h"
|
|
#include "CommonDialogCameraLevel.h"
|
|
#include "ConfigManager.h"
|
|
#include "IYWeldSeamStatus.h"
|
|
#include "LaserDataLoader.h"
|
|
#include "SG_baseDataType.h"
|
|
#include "WeldSeamTCPProtocol.h"
|
|
|
|
class DetectPresenter;
|
|
|
|
class WeldSeamPresenter : public BasePresenter,
|
|
public IVrConfigChangeNotify,
|
|
public ICameraLevelCalculator,
|
|
public ICameraLevelResultSaver
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum class ScanState : uint16_t { Ready = 0, Scanning = 1, Calculating = 2 };
|
|
|
|
explicit WeldSeamPresenter(QObject* parent = nullptr);
|
|
~WeldSeamPresenter() override;
|
|
|
|
int InitApp() override;
|
|
void DeinitApp();
|
|
|
|
QString StartDynamicScan(int cameraIndex = -1);
|
|
QString StopDynamicScanAndDetect();
|
|
void CancelDynamicScan();
|
|
ScanState GetScanState() const { return m_scanState.load(); }
|
|
|
|
int LoadAndDetect(const QString& fileName);
|
|
void ReconnectCamera();
|
|
VrAlgorithmParams GetAlgorithmParams() const;
|
|
void SetAlgorithmParams(const VrAlgorithmParams& params);
|
|
QString GetAlgoVersion() const;
|
|
ConfigManager* GetConfigManager() { return m_pConfigManager; }
|
|
|
|
void OnConfigChanged(const ConfigResult& configResult) override;
|
|
|
|
bool CalculatePlaneCalibration(
|
|
const std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& scanData,
|
|
double planeCalib[9], double& planeHeight, double invRMatrix[9]) override;
|
|
bool SaveLevelingResults(double planeCalib[9], double planeHeight, double invRMatrix[9],
|
|
int cameraIndex, const QString& cameraName) override;
|
|
bool LoadLevelingResults(int cameraIndex, const QString& cameraName,
|
|
double planeCalib[9], double& planeHeight, double invRMatrix[9]) override;
|
|
|
|
protected:
|
|
int InitAlgoParams() override;
|
|
int ProcessAlgoDetection(
|
|
std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& detectionDataCache) override;
|
|
bool OnDetectionData(EVzResultDataType dataType,
|
|
SVzLaserLineData* laserData) override;
|
|
bool HasPendingDetectionData() const override;
|
|
void ClearDetectionDataCache() override;
|
|
EVzResultDataType GetDetectionDataType() override { return keResultDataType_Position; }
|
|
VzNL_OnNotifyStatusCBEx GetCameraStatusCallback() override;
|
|
void OnCameraStatusChanged(int cameraIndex, bool isConnected) override;
|
|
void OnWorkStatusChanged(WorkStatus status) override;
|
|
void OnCameraCountChanged(int count) override;
|
|
void OnStatusUpdate(const std::string& statusMessage) override;
|
|
void OnModbusServerStatusChanged(bool isConnected) override;
|
|
void OnModbusWriteCallback(uint16_t startAddress, const uint16_t* data, uint16_t count) override;
|
|
|
|
private:
|
|
enum class ResultCacheState : uint8_t
|
|
{
|
|
Empty = 0,
|
|
Pending = 1,
|
|
Ready = 2,
|
|
Failed = 3
|
|
};
|
|
|
|
struct CachedDetectionResult
|
|
{
|
|
ResultCacheState state = ResultCacheState::Empty;
|
|
DetectionResult result;
|
|
};
|
|
|
|
int InitTCPServer();
|
|
void StopTCPServer();
|
|
void CompleteFailedScan(const QString& message, int errorCode);
|
|
void OnTCPConnectionChanged(bool connected);
|
|
void NotifyDetectionCompleted();
|
|
void SendTCPResult(const DetectionResult& result);
|
|
void SendCachedTCPResult(int cameraIndex);
|
|
void MarkCachedResultPending(int cameraIndex);
|
|
void ClearCachedResult(int cameraIndex);
|
|
void StoreCachedResult(const DetectionResult& result);
|
|
bool TransformDetectionResult(const DetectionResult& eyeResult, int cameraIndex,
|
|
DetectionResult& robotResult) const;
|
|
int GetLogicalCameraCount();
|
|
void PublishModbusResult(const DetectionResult& result);
|
|
void ResetModbusResults();
|
|
void UpdateModbusStatus(uint16_t status);
|
|
SSG_planeCalibPara GetCameraCalibParam(int cameraIndex) const;
|
|
void CheckAndUpdateWorkStatus();
|
|
static void DynamicCameraStatusCallback(EVzDeviceWorkStatus status, void* extData,
|
|
unsigned int dataLength, void* userData);
|
|
|
|
private:
|
|
ConfigManager* m_pConfigManager = nullptr;
|
|
WeldSeamTCPProtocol* m_pTCPServer = nullptr;
|
|
DetectPresenter* m_pDetectPresenter = nullptr;
|
|
std::atomic<ScanState> m_scanState{ScanState::Ready};
|
|
std::atomic<bool> m_deinitialized{false};
|
|
bool m_bTCPConnected = false;
|
|
bool m_modbusRegistersInitialized = false;
|
|
uint16_t m_tcpPort = 7800;
|
|
std::mutex m_cameraStatusMutex;
|
|
std::array<bool, 4> m_cameraConnectionStates{};
|
|
int m_logicalCameraCount = 1;
|
|
std::mutex m_resultCacheMutex;
|
|
std::array<CachedDetectionResult, 2> m_cachedDetectionResults{};
|
|
};
|
|
|
|
#endif // WELDSEAMPRESENTER_H
|