395 lines
8.2 KiB
C++
395 lines
8.2 KiB
C++
#include "pch.h"
|
|
#include "ATPControl_General.h"
|
|
#include <math.h>
|
|
#include "ZZ_Math.h"
|
|
|
|
CATPControl_General::CATPControl_General()
|
|
{
|
|
m_pfWaveLength = NULL;
|
|
m_iPixels = 2048;
|
|
}
|
|
|
|
CATPControl_General::~CATPControl_General()
|
|
{
|
|
// if (m_pfWaveLength!=NULL)
|
|
// {
|
|
// delete m_pfWaveLength;
|
|
// }
|
|
}
|
|
|
|
int CATPControl_General::ATPInitialize(USHORT usIndex)
|
|
{
|
|
findSpectraMeters(m_stuDeviceInfo_ATP);
|
|
bool bRes = switchSpectraMeters(m_stuDeviceInfo_ATP.descriptor[usIndex].serial);
|
|
if (bRes)
|
|
{
|
|
ErrorFlag efRes = initialize();
|
|
if (efRes ==INIT_SUCCESS)
|
|
{
|
|
m_iExposureTimeInMS = getIntegrationTimeMinimum();
|
|
m_pfWaveLength = getWavelength();
|
|
int iPixels = getPixelCount();
|
|
|
|
#ifndef _DEBUG
|
|
setCCDTecTemperature(10);
|
|
#endif
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
return 2;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int CATPControl_General::ATPClose()
|
|
{
|
|
bool bRes = closeSpectraMeter();
|
|
if (bRes)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int CATPControl_General::SetPixels(int iPixels)
|
|
{
|
|
m_iPixels = iPixels;
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::GetDeviceListInfo(CComboBox *pcboDeviceList, CString &cstrSN)
|
|
{
|
|
u8 cPN[11];
|
|
|
|
findSpectraMeters(m_stuDeviceInfo_ATP);
|
|
for (int i = 0; i < m_stuDeviceInfo_ATP.length; i++)
|
|
{
|
|
cstrSN = CA2W(m_stuDeviceInfo_ATP.descriptor[i].serial);
|
|
bool bRes = switchSpectraMeters(m_stuDeviceInfo_ATP.descriptor[i].serial);
|
|
if (bRes)
|
|
{
|
|
getProductPN(cPN);
|
|
CString cstrTemp(cPN);
|
|
cstrTemp = cstrTemp.Left(8);
|
|
if (cstrTemp==L"ATP5020P")
|
|
{
|
|
cstrTemp = "IS2";
|
|
}
|
|
pcboDeviceList->AddString(cstrTemp);
|
|
}
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
if (m_stuDeviceInfo_ATP.length >= 1)
|
|
{
|
|
pcboDeviceList->SetCurSel(0);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::GetWaveLength(float *pfWaveLength)
|
|
{
|
|
//initialize();
|
|
memcpy(pfWaveLength,m_pfWaveLength, m_iPixels);
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::GetAttribute()
|
|
{
|
|
int m_iAttribute=getAttribute();
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::GetDeviceTemperature(float &fTemperature)
|
|
{
|
|
unsigned char ucTemp[10];
|
|
getCCDTecTemperature(ucTemp);
|
|
//USES_CONVERSION;
|
|
//CString cstrTemp;
|
|
//cstrTemp = A2T((char*)ucTemp);
|
|
fTemperature = (float)atof((char*)ucTemp);
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::WavelengthToPixelIndex(float fStartWavelength, float fEndWavelength, UINT &uiStartPixel, UINT &uiEndPixel)
|
|
{
|
|
for (int i=0; i<m_iPixels; i++)
|
|
{
|
|
if (fStartWavelength <= m_pfWaveLength[i])
|
|
{
|
|
uiStartPixel = i;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < m_iPixels; i++)
|
|
{
|
|
if (fEndWavelength <= m_pfWaveLength[i])
|
|
{
|
|
uiEndPixel = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::SetDownwellingSunlightIndexRange(UINT uiStartPixel, UINT uiEndPixel)
|
|
{
|
|
m_uiStartPixel = uiStartPixel;
|
|
m_uiEndPixel = uiEndPixel;
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::GetDownwellingSunlightIndex(UINT uiIndex)
|
|
{
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::SingleShot(ATPDataFrame &dfData)
|
|
{
|
|
getSpectrum(m_iExposureTimeInMS);
|
|
bool bFlagIsCaptureFinished = false;
|
|
while (!bFlagIsCaptureFinished)
|
|
{
|
|
bFlagIsCaptureFinished = getSpectrumDataReadyFlag();
|
|
Sleep(m_iExposureTimeInMS);
|
|
}
|
|
Spectrumsp specDataTemp = ReadSpectrum();
|
|
//////////////////////////////////////////////////////////////////////////
|
|
GetDeviceTemperature(dfData.fTemperature);
|
|
dfData.usExposureTime = (USHORT)m_iExposureTimeInMS;
|
|
memcpy(dfData.iData, specDataTemp.array, m_iPixels *sizeof(int));
|
|
dfData.dTimes = 0;
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::SingleShotDark(ATPDataFrame &dfData)
|
|
{
|
|
getDarkSpectrum(m_iExposureTimeInMS);
|
|
bool bFlagIsCaptureFinished = false;
|
|
while (!bFlagIsCaptureFinished)
|
|
{
|
|
bFlagIsCaptureFinished = getSpectrumDataReadyFlag();
|
|
if (m_iExposureTimeInMS<2000)
|
|
{
|
|
Sleep(m_iExposureTimeInMS);
|
|
}
|
|
else
|
|
{
|
|
Sleep(2000);
|
|
}
|
|
}
|
|
Spectrumsp specDataTemp = ReadSpectrum();
|
|
//////////////////////////////////////////////////////////////////////////
|
|
GetDeviceTemperature(dfData.fTemperature);
|
|
dfData.usExposureTime = (USHORT)m_iExposureTimeInMS;
|
|
memcpy(dfData.iData, specDataTemp.array, m_iPixels * sizeof(int));
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::SingleShotDeducted(ATPDataFrame &dfData)
|
|
{
|
|
ATPDataFrame dfDark;
|
|
//Spectrumsp spec1,specDark;
|
|
//SetExposureTime(1000);
|
|
SingleShot(dfData);
|
|
if (m_iExposureTimeInMS<=5000)
|
|
{
|
|
SingleShotDark(dfDark);
|
|
SubATPDataFrame(dfData, dfDark);
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::SetExposureTime(int iExposureTimeInMS)
|
|
{
|
|
m_iExposureTimeInMS = iExposureTimeInMS;
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::Test()
|
|
{
|
|
//Spectrumsp specData1;
|
|
//SingleShot(specData1);
|
|
//SingleShotDark(specData1);
|
|
//SingleShot(specData1);
|
|
// int s = getUsedPixelStart();
|
|
// int e = getUsedPixelEnd();
|
|
// int a1 = getIntegrationTimeMaximum();
|
|
// int a2 = getIntegrationTimeMinimum();
|
|
// float *b = new float[10000];
|
|
// float *a = new float[4096];
|
|
// a = getWavelength();
|
|
// b = getWavelength();
|
|
//delete[] a;
|
|
// getSpectrum(100);
|
|
// Spectrumsp spec,spec1;
|
|
// int iFlag = 0;
|
|
// while (!iFlag)
|
|
// {
|
|
// iFlag = getSpectrumDataReadyFlag();
|
|
// Sleep(100);
|
|
// }
|
|
// spec = ReadSpectrum();
|
|
// getSpectrum(100);
|
|
// iFlag = getSpectrumDataReadyFlag();
|
|
// spec1= ReadSpectrum();
|
|
//v gfcxdelete spec.array;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::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;
|
|
|
|
SetExposureTime(2000);
|
|
|
|
while (!bFlagIsAutoExposureOK && !bFlagIsAutoExposureFailed)
|
|
{
|
|
ATPDataFrame dfTemp;
|
|
|
|
if (iRepeatCount++ > 30)
|
|
{
|
|
bFlagIsAutoExposureFailed = true;
|
|
bFlagIsOverTrying = true;
|
|
break;
|
|
}
|
|
|
|
fExposureTime = (float)m_iExposureTimeInMS;
|
|
fTempExposureTime = fExposureTime;
|
|
|
|
SingleShot(dfTemp);
|
|
HeapSort(dfTemp.iData, m_iPixels);
|
|
|
|
double dSum = 0;
|
|
int iCount = m_iPixels /100;
|
|
for (int i = 0; i < iCount; i++)
|
|
{
|
|
dSum += dfTemp.iData[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;
|
|
SetExposureTime(13000);
|
|
bFlagIsOverMaxExposureTime = true;
|
|
break;
|
|
}
|
|
|
|
SetExposureTime((int)fExposureTime);
|
|
}
|
|
fPredictedExposureTime = fExposureTime;
|
|
return 0;
|
|
}
|
|
|
|
int CATPControl_General::CheckRange(float fFactor, ATPDataFrame &dfData)
|
|
{
|
|
int *piData = new int[m_iPixels];
|
|
memcpy(piData, dfData.iData, m_iPixels);
|
|
return 0;
|
|
}
|
|
|
|
void CATPControl_General::SubATPDataFrame(ATPDataFrame& dfLeft,ATPDataFrame const dfRight)
|
|
{
|
|
for (int i=0;i< m_iPixels;i++)
|
|
{
|
|
dfLeft.iData[i] = dfLeft.iData[i] - dfRight.iData[i];
|
|
}
|
|
}
|
|
|
|
void CATPControl_General::SubATPDataFrame(ATPDataFrame const dfLeft, ATPDataFrame const dfRight, ATPDataFrame& dfResult)
|
|
{
|
|
for (int i = 0; i < m_iPixels; i++)
|
|
{
|
|
dfResult.iData[i] = dfLeft.iData[i] - dfRight.iData[i];
|
|
}
|
|
}
|