diff --git a/CmakeLists.txt b/CmakeLists.txt index 0f111c7..7c7cc8b 100644 --- a/CmakeLists.txt +++ b/CmakeLists.txt @@ -13,6 +13,9 @@ file(GLOB_RECURSE SRC_LIST "source/*.cpp") include_directories("source") include_directories("source/ATP") include_directories("source/Logger") +include_directories("source/ZZ_SDK") +include_directories("source/Settings") +include_directories("source/Thread") add_executable( TowerOptoSifAndSpectral main.cpp diff --git a/main.cpp b/main.cpp index e643763..60ed99f 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,7 @@ #include "ATPControl_Serial_QT.h" #include "Logger.h" using namespace std; - +#pragma once int main(int argc, char *argv[]) @@ -12,6 +12,7 @@ int main(int argc, char *argv[]) //QT_LOG::ZZ_InitLogger(QCoreApplication::applicationDirPath() + "/Log/"); QString qstrTest="This is a test message 2"; qDebug() << qstrTest; + cout << "This is a test message"; //////////////////////////////////////////////////////////////////////////test ZZ_ATPControl_Serial_Qt m_ctrlATP; QByteArray qbTest; diff --git a/source/ATP/ATPControl_Serial_QT.cpp b/source/ATP/ATPControl_Serial_QT.cpp index 23682a0..ff6ecc1 100644 --- a/source/ATP/ATPControl_Serial_QT.cpp +++ b/source/ATP/ATPControl_Serial_QT.cpp @@ -1,5 +1,6 @@ #include "pch.h" #include "ATPControl_Serial_QT.h" +#include "ZZ_Math_HDRONLY.h" ZZ_ATPControl_Serial_Qt::ZZ_ATPControl_Serial_Qt() { @@ -62,17 +63,47 @@ int ZZ_ATPControl_Serial_Qt::GetDeviceInfo() qbSend.clear(); qbRecv.clear(); qbSend.append(GET_PN_NUMBER); - SendCommand(qbSend); - RecvData(qbRecv); - ParseData(qbRecv); + int iRes = SendCommand(qbSend); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = RecvData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = ParseData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } m_adiDeviceInfo.strPN = qbRecv.data(); qbSend.clear(); qbRecv.clear(); qbSend.append(GET_SN_NUMBER); - SendCommand(qbSend); - RecvData(qbRecv); - ParseData(qbRecv); + iRes = SendCommand(qbSend); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = RecvData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = ParseData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1"; + return 1; + } m_adiDeviceInfo.strSN = qbRecv.data(); @@ -86,22 +117,62 @@ int ZZ_ATPControl_Serial_Qt::GetDeviceAttribute() qbSend.clear(); qbRecv.clear(); qbSend.append(GET_MIN_INTEGRATION_TIME); - SendCommand(qbSend); - RecvData(qbRecv); - ParseData(qbRecv); + int iRes = SendCommand(qbSend); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = RecvData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = ParseData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } m_adaDeviceAttr.iMinIntegrationTime = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256; qbSend.clear(); qbRecv.clear(); qbSend.append(GET_MAX_INTEGRATION_TIME); - SendCommand(qbSend); - RecvData(qbRecv); - ParseData(qbRecv); + iRes = SendCommand(qbSend); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = RecvData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = ParseData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } m_adaDeviceAttr.iMaxIntegrationTime = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256; - SetExposureTime(m_adaDeviceAttr.iMinIntegrationTime); - SingleShot(m_adaDeviceAttr.iPixels); + iRes = SetExposureTime(m_adaDeviceAttr.iMinIntegrationTime); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Call SetExposureTime error.Exit Code:2"; + return 2; + } + iRes = SingleShot(m_adaDeviceAttr.iPixels); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Call SingleShot error.Exit Code:3"; + return 3; + } qbSend.clear(); @@ -110,15 +181,30 @@ int ZZ_ATPControl_Serial_Qt::GetDeviceAttribute() qbSend.resize(3); qbSend[1] = 0x00; qbSend[2] = 0x01; - SendCommand(qbSend); - RecvData(qbRecv); - ParseData(qbRecv); - + iRes = SendCommand(qbSend); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = RecvData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } + iRes = ParseData(qbRecv); + if (iRes != 0) + { + qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1"; + return 1; + } float fWaveLengthCoef[4]; memcpy(fWaveLengthCoef, qbRecv.data()+16, 4 * sizeof(float)); -// for () -// { -// } + for (int i=0;i< m_adaDeviceAttr.iPixels;i++) + { + m_adaDeviceAttr.fWaveLength[i] = fWaveLengthCoef[0] * i*i*i + fWaveLengthCoef[1] * i*i + fWaveLengthCoef[2] * i + fWaveLengthCoef[3]; + } return 0; } @@ -144,7 +230,7 @@ int ZZ_ATPControl_Serial_Qt::SendCommand(QByteArray qbCommand) qint64 qi64Write= m_pSerialPort->write(qbSend); if (qi64Write != qbSend.size()) { - qDebug() << "Err:write Failed.Exit Code:1"; + qDebug() << "Err:write Failed.Exit Code:1"<< qi64Write; return 1; } @@ -165,7 +251,7 @@ int ZZ_ATPControl_Serial_Qt::RecvData(QByteArray &qbData) if (iCounter > 10) { - qDebug() << "Err:RecvData Failed,Not Enough Data.Exit Code:1"< 100) { - qDebug() << "Err:RecvData Failed,Incomplete Data.Exit Code:3" << qbData.size(); + qDebug() << "Err:RecvData Failed,Incomplete Data.Exit Code:3" << qbData.size(); return 3; } iCounter++; @@ -206,7 +292,7 @@ int ZZ_ATPControl_Serial_Qt::RecvData(QByteArray &qbData) ZZ_U8 ucTemp = qbData[qbData.size() - 1]; if ((ZZ_U8)usCheckSum != ucTemp) { - qDebug() << "Err:RecvData Failed,Incorrect Check Sum.Exit Code:4" << qbData.size(); + qDebug() << "Err:RecvData Failed,Incorrect Check Sum.Exit Code:4" << qbData.size(); qbData.clear(); return 4; } @@ -218,7 +304,7 @@ int ZZ_ATPControl_Serial_Qt::ParseData(QByteArray &qbData) { if (qbData.size() < 6) { - qDebug() << "Err:ParseData Failed,Not Enough Data.Exit Code:1" << qbData.size(); + qDebug() << "Err:ParseData Failed,Not Enough Data.Exit Code:1" << qbData.size(); return 1; } qbData.remove(0, 5); @@ -228,6 +314,136 @@ int ZZ_ATPControl_Serial_Qt::ParseData(QByteArray &qbData) int ZZ_ATPControl_Serial_Qt::PerformAutoExposure(float fMinScaleFactor, float fMaxScaleFactor, float &fPredictedExposureTime) { + using namespace ZZ_MATH; + int iDeviceDepth = 65535; + + bool bFlagIsOverTrying = false; + bool bFlagIsLowerMinExposureTime = false; + bool bFlagIsOverMaxExposureTime = false; + bool bFlagIsAutoExposureOK = false; + bool bFlagIsAutoExposureFailed = false; + + bool bIsValueOverflow = false; + bool bIsLastValueOverflow = false; + + float fExposureTime = 0; + float fTempExposureTime = 0; + double fLastExposureTime = 0.1; + int iRepeatCount = 0; + + int iRes = SetExposureTime(2000);//need change to load from files + if (iRes != 0) + { + qDebug() << "Err:PerformAutoExposure Failed.Exit Code:1"; + return 1; + } + + while (!bFlagIsAutoExposureOK && !bFlagIsAutoExposureFailed) + { + ATPDataFrame dfTemp; + + if (iRepeatCount++ > 30) + { + bFlagIsAutoExposureFailed = true; + bFlagIsOverTrying = true; + break; + } + + fExposureTime = (float)m_adaDeviceAttr.iMinIntegrationTime; + fTempExposureTime = fExposureTime; + + iRes = SingleShot(dfTemp); + if (iRes != 0) + { + qDebug() << "Err:PerformAutoExposure Failed.Exit Code:2"; + return 2; + } + + HeapSort(dfTemp.usData, m_adaDeviceAttr.iPixels); + + double dSum = 0; + int iCount = m_adaDeviceAttr.iPixels / 100; + for (int i = 0; i < iCount; i++) + { + dSum += dfTemp.usData[i]; + } + double dTemp = dSum / iCount; + + if (dTemp >= iDeviceDepth * 0.99) + { + bIsValueOverflow = true; + if (!bIsLastValueOverflow) + { + fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2; + } + else + { + fExposureTime = fExposureTime / 2; + } + } + + else if (iDeviceDepth * fMaxScaleFactor >= dTemp && dTemp >= iDeviceDepth * fMinScaleFactor) + { + bFlagIsAutoExposureOK = 1; + } + else if (dTemp > iDeviceDepth * fMaxScaleFactor) + { + bIsValueOverflow = true; + if (!bIsLastValueOverflow) + { + fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2; + } + else + { + fExposureTime = fExposureTime * 3 / 4; + } + } + else if (dTemp < iDeviceDepth * fMinScaleFactor) + { + bIsValueOverflow = false; + if (bIsLastValueOverflow) + { + fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2; + } + else + { + double dFactor; + dFactor = dTemp / (iDeviceDepth * fMaxScaleFactor); + fExposureTime = (float)(fExposureTime / dFactor); + } + if (/*fExposureTime > 100 || */fExposureTime < 10) + { + bFlagIsAutoExposureOK = false; + bFlagIsAutoExposureFailed = true; + bFlagIsLowerMinExposureTime = true; + } + } + bIsLastValueOverflow = bIsValueOverflow; + fLastExposureTime = fTempExposureTime; + + if (fExposureTime > 13000) + { + bFlagIsAutoExposureOK = false; + bFlagIsAutoExposureFailed = true; + fPredictedExposureTime = 13000; + iRes = SetExposureTime(13000); + if (iRes != 0) + { + qDebug() << "Err:PerformAutoExposure Failed.Exit Code:3"; + return 3; + } + bFlagIsOverMaxExposureTime = true; + break; + } + + iRes = SetExposureTime((int)fExposureTime); + if (iRes != 0) + { + qDebug() << "Err:PerformAutoExposure Failed.Exit Code:4"; + return 3; + } + } + fPredictedExposureTime = fExposureTime; return 0; } @@ -279,11 +495,8 @@ int ZZ_ATPControl_Serial_Qt::SingleShot(ATPDataFrame &dfData) { int iDataSizeInPixel = (qbRecv.size() - 1) / 2; memcpy(dfData.usData, qbRecv.data() + 1, iDataSizeInPixel * 2); - - } - //int isize = qbRecv.size(); - //m_adaDeviceAttr.iPixels = isize / 2; + } return 0; } diff --git a/source/ATP/ATPControl_Serial_QT.h b/source/ATP/ATPControl_Serial_QT.h index a0b07c9..de34574 100644 --- a/source/ATP/ATPControl_Serial_QT.h +++ b/source/ATP/ATPControl_Serial_QT.h @@ -13,9 +13,6 @@ public: ZZ_ATPControl_Serial_Qt(); virtual ~ZZ_ATPControl_Serial_Qt(); - //////////////////////////////////////////////////////////////////////////shutter control stub code s - int SetExtShutter(int iShutterA,int iShutterB); //0:close 1:open - //////////////////////////////////////////////////////////////////////////shutter control stub code e public: int SetBaudRate(int iBaud); @@ -28,6 +25,7 @@ public: int SingleShotDeducted(ATPDataFrame &dfData); int SetExposureTime(int iExposureTimeInMS); + int GetExposureTime(int &iExposureTimeInMS); //int GetWaveLength(float *pfWaveLength); int GetDeviceInfo(); @@ -49,6 +47,10 @@ private: //ATP ATPDeviceInfo m_adiDeviceInfo; ATPDeviceAttribute m_adaDeviceAttr; + + //////////////////////////////////////////////////////////////////////////shutter control stub code s + int SetExtShutter(int iShutterA, int iShutterB); //0:close 1:open + //////////////////////////////////////////////////////////////////////////shutter control stub code e int SendCommand(QByteArray qbCommand); int RecvData(QByteArray &qbData); int ParseData(QByteArray &qbData); diff --git a/source/ATP/ATPDataFileProcessor.cpp b/source/ATP/ATPDataFileProcessor.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/ATP/ATPDataFileProcessor.h b/source/ATP/ATPDataFileProcessor.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/source/ATP/ATPDataFileProcessor.h @@ -0,0 +1 @@ +#pragma once diff --git a/source/ATP/ZZ_Types.h b/source/ATP/ZZ_Types.h index 1b22f9f..00a3007 100644 --- a/source/ATP/ZZ_Types.h +++ b/source/ATP/ZZ_Types.h @@ -39,6 +39,7 @@ namespace ZZ_MISCDEF const int SET_GPIO = 0x61; //const int SYNCHRONIZATION_GET_DARK = 0x23 + //////////////////////////////////////////////////////////////////////////device enum Model { ATP1010 = 0, @@ -67,6 +68,37 @@ namespace ZZ_MISCDEF float fWaveLength[4096]; }ATPDeviceAttribute; + //////////////////////////////////////////////////////////////////////////config file + + + + } + + namespace ZZ_DATAFILE + { + typedef struct tagEnvironmentalContext + { + + + }EnvironmentalContext; + + typedef struct tagIS1Information + { + + + }IS1Info; + + typedef struct tagISIFInformation + { + + + }ISIFInfo; + + typedef struct tagATPDataHeader + { + + + }ATPDataHeader; } } diff --git a/source/Settings/SystemConfigger.cpp b/source/Settings/SystemConfigger.cpp new file mode 100644 index 0000000..4147a52 --- /dev/null +++ b/source/Settings/SystemConfigger.cpp @@ -0,0 +1,25 @@ +#include "SystemConfigger.h" + +ZZ_SysConfigger::ZZ_SysConfigger(QObject *parent /*= nullptr*/) +{ + m_qstrFilePath = QCoreApplication::applicationDirPath()+ "/Settings/DeviceSettings.ini"; +} + +ZZ_SysConfigger::~ZZ_SysConfigger() +{ +} + +int ZZ_SysConfigger::LoadSettings_IS1() +{ + return 0; +} + +int ZZ_SysConfigger::LoadSettings_iSIF() +{ + return 0; +} + +int ZZ_SysConfigger::LoadSettings_System() +{ + return 0; +} diff --git a/source/Settings/SystemConfigger.h b/source/Settings/SystemConfigger.h new file mode 100644 index 0000000..bf7b9c1 --- /dev/null +++ b/source/Settings/SystemConfigger.h @@ -0,0 +1,21 @@ +#pragma once +#include "pch.h" + + + + +class ZZ_SysConfigger :public QObject +{ + Q_OBJECT + +public: + ZZ_SysConfigger(QObject *parent = nullptr); + virtual ~ZZ_SysConfigger(); +public: + int LoadSettings_IS1(); + int LoadSettings_iSIF(); + int LoadSettings_System(); + +private: + QString m_qstrFilePath; +}; \ No newline at end of file diff --git a/source/Thread/ThreadDataGrabber.cpp b/source/Thread/ThreadDataGrabber.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/Thread/ThreadDataGrabber.h b/source/Thread/ThreadDataGrabber.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/source/Thread/ThreadDataGrabber.h @@ -0,0 +1 @@ +#pragma once diff --git a/source/Thread/ThreadIS1Controller.cpp b/source/Thread/ThreadIS1Controller.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/Thread/ThreadIS1Controller.h b/source/Thread/ThreadIS1Controller.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/source/Thread/ThreadIS1Controller.h @@ -0,0 +1 @@ +#pragma once diff --git a/source/Thread/ThreadISIFController.h b/source/Thread/ThreadISIFController.h new file mode 100644 index 0000000..1a2c93c --- /dev/null +++ b/source/Thread/ThreadISIFController.h @@ -0,0 +1,12 @@ +#pragma once +#include "pch.h" + + + +class ZZ_iSIFControlThread :public QObject +{ + Q_OBJECT +public: + ZZ_iSIFControlThread(QObject* parent = nullptr); + ~ZZ_iSIFControlThread(); +}; \ No newline at end of file diff --git a/source/Thread/ThreadiSIFController.cpp b/source/Thread/ThreadiSIFController.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/ZZ_SDK/ZZ_Math_HDRONLY.h b/source/ZZ_SDK/ZZ_Math_HDRONLY.h new file mode 100644 index 0000000..d57693b --- /dev/null +++ b/source/ZZ_SDK/ZZ_Math_HDRONLY.h @@ -0,0 +1,84 @@ +#pragma once +namespace ZZ_MATH +{ + + template + void MinHeapify(T*arry, int size, int element) + { + int lchild = element * 2 + 1, rchild = lchild + 1; + while (rchild < size) + { + if (arry[element] <= arry[lchild] && arry[element] <= arry[rchild]) + { + return; + } + if (arry[lchild] <= arry[rchild]) + { + std::swap(arry[element], arry[lchild]); + element = lchild; + } + else + { + std::swap(arry[element], arry[rchild]); + element = rchild; + } + lchild = element * 2 + 1; + rchild = lchild + 1; + } + if (lchild < size&&arry[lchild] < arry[element]) + { + std::swap(arry[lchild], arry[element]); + } + return; + } + + template + void MaxHeapify(T*arry, int size, int element) + { + int lchild = element * 2 + 1, rchild = lchild + 1; + while (rchild < size) + { + if (arry[element] >= arry[lchild] && arry[element] >= arry[rchild]) + { + return; + } + if (arry[lchild] >= arry[rchild]) + { + std::swap(arry[element], arry[lchild]); + element = lchild; + } + else + { + std::swap(arry[element], arry[rchild]); + element = rchild; + } + lchild = element * 2 + 1; + rchild = lchild + 1; + } + if (lchildarry[element]) + { + std::swap(arry[lchild], arry[element]); + } + return; + } + + + template + void HeapSort(T*arry, int size) + { + int i; + for (i = size - 1; i >= 0; i--) + { + MinHeapify(arry, size, i); + } + while (size > 0) + { + std::swap(arry[size - 1], arry[0]); + + size--; + MinHeapify(arry, size, 0); + } + return; + } + +}