#include "StdAfx.h"
|
#include "CameraController.h"
|
#include "CHReviewSetting/SystemInfo.h"
|
//#include "akIPCNetAOISystem.h"//cmark
|
#include "akAOISharedSystem.h"//20191017
|
#include "ReviewInterface.h"
|
|
typedef CCameraControl* (*CAMERACONTROL)(int);
|
|
CCameraController::CCameraController(void)
|
{
|
}
|
|
CCameraController::~CCameraController(void)
|
{
|
DisconnectCamera();
|
}
|
|
int CCameraController::ConnectCamera(const VectorCameraControlInfo& vecCameraControlInfo)
|
{
|
m_vecCameraContorlInfo = vecCameraControlInfo;
|
int nConnectCount = 0;
|
int nMaxBufferCount = 0;
|
|
//enum CameraLibrary_Type { CameraLibrary_Simulation=0, CameraLibrary_Sapera, CameraLibrary_FlyCapture, CameraLibrary_JAI, CameraLibrary_Count };
|
int nCamIndex = 0;
|
for (VectorCameraControlInfoIt it=m_vecCameraContorlInfo.begin(); it!=m_vecCameraContorlInfo.end(); it++)
|
{
|
CCameraControl *pCameraControl = NULL;
|
switch(it->GetLibraryType())
|
{
|
case CameraLibrary_Simulation:
|
{
|
HINSTANCE m_hInstGrabber=AfxLoadLibrary(_T("CHCameraControlSimulationd_x64_vc15.dll"));
|
if(m_hInstGrabber == NULL)
|
{
|
AfxMessageBox(_T("카메라 운용 모드 진입 실패(CameraControlSimulation.dll Read Fail)"));
|
break ;
|
}
|
CAMERACONTROL funcGrabInst;
|
funcGrabInst=(CAMERACONTROL)GetProcAddress(m_hInstGrabber,"CameraControlNew");
|
pCameraControl = funcGrabInst(nCamIndex++);
|
}
|
break;
|
|
case CameraLibrary_Sapera:
|
{
|
HINSTANCE m_hInstGrabber=AfxLoadLibrary(_T("CHCameraControlSaperad_x64_vc15.dll"));
|
if(m_hInstGrabber == NULL)
|
{
|
AfxMessageBox(_T("카메라 운용 모드 진입 실패(CameraControlSapera.dll Read Fail)"));
|
break ;
|
}
|
CAMERACONTROL funcGrabInst;
|
funcGrabInst=(CAMERACONTROL)GetProcAddress(m_hInstGrabber,"CameraControlNew");
|
pCameraControl = funcGrabInst(nCamIndex++);
|
}
|
|
break;
|
|
case CameraLibrary_FlyCapture:
|
{
|
HINSTANCE m_hInstGrabber=AfxLoadLibrary(_T("CHCameraControlFlyCaptured_x64_vc15.dll"));
|
if(m_hInstGrabber == NULL)
|
{
|
AfxMessageBox(_T("카메라 운용 모드 진입 실패(CameraControlFlyCapture.dll Read Fail)"));
|
break ;
|
}
|
CAMERACONTROL funcGrabInst;
|
funcGrabInst=(CAMERACONTROL)GetProcAddress(m_hInstGrabber,"CameraControlNew");
|
pCameraControl = funcGrabInst(nCamIndex++);
|
}
|
break;
|
|
case CameraLibrary_JAI:
|
{
|
HINSTANCE m_hInstGrabber=AfxLoadLibrary(_T("CHCameraControlJaid_x64_vc15.dll"));
|
if(m_hInstGrabber == NULL)
|
{
|
AfxMessageBox(_T("카메라 운용 모드 진입 실패(CameraControlJai.dll Read Fail)"));
|
break ;
|
}
|
CAMERACONTROL funcGrabInst;
|
funcGrabInst=(CAMERACONTROL)GetProcAddress(m_hInstGrabber,"CameraControlNew");
|
pCameraControl = funcGrabInst(nCamIndex++);
|
}
|
break;
|
case CAMERA_LIBRARY_PYLON:
|
{
|
HINSTANCE m_hInstGrabber = AfxLoadLibrary(_T("CHCameraControlPylon_x64_vc15.dll"));
|
if (m_hInstGrabber == NULL)
|
{
|
AfxMessageBox(_T("카메라 운용 모드 진입 실패(CHCameraControlPylon.dll Read Fail)"));
|
break;
|
}
|
CAMERACONTROL funcGrabInst;
|
funcGrabInst = (CAMERACONTROL)GetProcAddress(m_hInstGrabber, "CameraControlNew");
|
pCameraControl = funcGrabInst(nCamIndex++);
|
}
|
|
break;
|
|
}
|
|
|
if (pCameraControl==NULL) continue;
|
|
if (!pCameraControl->Connect(*it))
|
{
|
//#3361_ LYW CF AOI Review 전설비 Align Cam 비정상 연결 시 팝업 기능 추가 START
|
if (it->GetLibraryType() == CAMERA_LIBRARY_PYLON)
|
{
|
CString strMessage;
|
strMessage.Format(_T("Align카메라 연결 실패 idx = %d"), nCamIndex);
|
if (IDOK == AfxMessageBox(strMessage, MB_OK | MB_ICONQUESTION))
|
{
|
g_pLog->DisplayMessage(_T("Align Camera Connet Fail! Exit Review System"));
|
//업데이트시
|
//exit(1);
|
return FALSE;
|
}
|
|
}
|
else if(it->GetLibraryType() == CameraLibrary_Sapera)
|
{
|
CString strMessage;
|
strMessage.Format(_T("Review카메라 연결 실패 idx = %d"), nCamIndex);
|
if (IDOK == AfxMessageBox(strMessage, MB_OK | MB_ICONQUESTION))
|
{
|
g_pLog->DisplayMessage(_T("Review Camera Connet Fail! Exit Review System"));
|
//업데이트시
|
//exit(1);
|
return FALSE;
|
}
|
}
|
|
////#3361_ LYW CF AOI Review 전설비 Align Cam 비정상 연결 시 팝업 기능 추가 END
|
delete pCameraControl;
|
continue;
|
}
|
|
// set flip type
|
pCameraControl->SetFlipMode(it->GetFlipType());
|
|
pCameraControl->SetCC2P(static_cast<ICameraControl2Parent*>(this));
|
|
int nBufferWidth = pCameraControl->GetWidth();
|
int nBufferHeight = pCameraControl->GetHeight();
|
int nBufferChannels = pCameraControl->GetChannels();
|
int nBufferCount = it->GetImageBufferCount();
|
|
// max buffer count
|
nMaxBufferCount = max(nBufferCount, nMaxBufferCount);
|
|
// camera image buffer
|
int nImageSize = nBufferWidth * nBufferHeight * nBufferChannels;
|
|
CCameraImageData* pCameraImageData = new CCameraImageData();
|
if (pCameraImageData)
|
{
|
for (int i=0; i<nBufferCount; i++)
|
{
|
CCHImageData *pImageData = new CCHImageData(nBufferWidth, nBufferHeight, 8, nBufferChannels);
|
|
if (pImageData)
|
{
|
pImageData->ClearImage(128);
|
pCameraImageData->AddImageData(pImageData);
|
}
|
}
|
|
m_vecCameraImage.push_back(pCameraImageData);
|
m_vecCameraImageIndex.push_back(-1);
|
m_vecCameraImageSize.push_back(nImageSize);
|
}
|
|
m_vecCameraControl.push_back(pCameraControl);
|
|
nConnectCount++;
|
}
|
|
// camera image count
|
if (nMaxBufferCount>0)
|
{
|
m_vecCameraImageCount.resize(nMaxBufferCount);
|
}
|
|
return nConnectCount;
|
}
|
|
void CCameraController::DisconnectCamera()
|
{
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
CCameraControl *pCameraControl = static_cast<CCameraControl*>(*it);
|
pCameraControl->Freeze();
|
pCameraControl->Abort();
|
pCameraControl->Disconnect();
|
delete pCameraControl;
|
pCameraControl = NULL;
|
}
|
m_vecCameraControl.clear();
|
|
for (VectorCameraImageDataIt it=m_vecCameraImage.begin(); it!=m_vecCameraImage.end(); it++)
|
{
|
CCameraImageData *pCamereImageData = static_cast<CCameraImageData*>(*it);
|
pCamereImageData->Reset();
|
delete pCamereImageData;
|
pCamereImageData = NULL;
|
}
|
m_vecCameraImage.clear();
|
|
m_vecCameraContorlInfo.clear();
|
m_vecCameraImageIndex.clear();
|
m_vecCameraImageSize.clear();
|
|
m_vecCameraImageCount.clear();
|
}
|
|
void CCameraController::Camera_ClearIndex(int nIndex)
|
{
|
for (VectorIntegerIt it=m_vecCameraImageCount.begin(); it!=m_vecCameraImageCount.end(); it++)
|
{
|
(*it) = 0;
|
}
|
|
for (VectorIntegerIt it=m_vecCameraImageIndex.begin(); it!=m_vecCameraImageIndex.end(); it++)
|
{
|
(*it) = nIndex;
|
}
|
}
|
|
int CCameraController::Camera_Control(int nControlType)
|
{
|
int nResult;
|
switch(nControlType)
|
{
|
case CameraControlStop:
|
Camera_Stop();
|
return 1;
|
|
case CameraControlLive:
|
nResult = Camera_Live();
|
return nResult;
|
|
case CameraControlStart:
|
nResult = Camera_Start();
|
return nResult;
|
}
|
|
return 0;
|
}
|
|
void CCameraController::Camera_Stop()
|
{
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
(*it)->Freeze();
|
g_pLog->DisplayMessage(_T("[CameraController]Camera_Abort!"));
|
}
|
}
|
|
int CCameraController::Camera_Live()
|
{
|
int nValue = 1;
|
|
Camera_ClearIndex();
|
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
|
//#3561 210908 LYW ADD Start
|
int nSub;
|
try
|
{
|
(*it)->SetTriggerMode(ModeInternal);
|
nSub = (*it)->Grab();
|
}
|
catch (...)
|
{
|
nSub = FALSE;
|
}
|
//#3561 210908 LYW ADD End
|
|
if (nSub==1)
|
g_pLog->DisplayMessage(_T("[CameraController]Camera_Grab Success![ModeInternal]"));
|
else
|
g_pLog->DisplayMessage(_T("[CameraController]Camera_Grab FAIL![ModeInternal]"));
|
|
nValue = nValue && nSub;
|
}
|
|
return nValue;
|
}
|
|
int CCameraController::Camera_Start()
|
{
|
int nValue = 1;
|
|
Camera_ClearIndex();
|
|
CProcessTimer processTimer;
|
processTimer.Start();
|
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
(*it)->SetTriggerMode(ModeExternal);
|
|
int nSub = (*it)->Grab();
|
|
if (nSub==1)
|
{
|
g_pLog->DisplayMessage(_T("[CameraController]Camera_Grab Success![ModeExternal]"));
|
}
|
else
|
{
|
g_pLog->DisplayMessage(_T("[CameraController]Camera_Grab FAIL![ModeExternal]"));
|
}
|
|
nValue = nValue && nSub;
|
}
|
processTimer.End();
|
|
g_pLog->DisplayMessage(_T("Camera Start - All Trigger Set : %f ms"), processTimer.GetDurationMilliSecond());
|
return nValue;
|
}
|
|
int CCameraController::Camera_Start(int nCount)
|
{
|
int nValue = 1;
|
|
Camera_ClearIndex();
|
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
(*it)->SetTriggerMode(ModeExternal);
|
|
int nSub = (*it)->Snap(nCount);
|
|
if (nSub==1)
|
{
|
g_pLog->DisplayMessage(_T("CCameraController Grab Success![ModeExternal]"));
|
}
|
else
|
{
|
g_pLog->DisplayMessage(_T("CCameraController Grab FAIL![ModeExternal]"));
|
}
|
|
nValue = nValue && nSub;
|
}
|
|
return nValue;
|
}
|
|
BOOL CCameraController::SetCameraSoftwareTrigger(int nCameraIdx)
|
{
|
|
//공유메모리 ======================================
|
_ProtocolReview* pProtocolDat ;
|
//akIPCNetAOISystem* akiPCIntance ;
|
CakAOISharedSystem* pAOIShared;
|
|
BOOL CKakiPCIntance= FALSE;
|
if(CReviewInterface::GetInstance() != NULL)
|
{
|
pAOIShared = CReviewInterface::GetInstance();
|
pProtocolDat = pAOIShared->getReviewProtocol();
|
|
if(pProtocolDat != NULL)
|
{
|
CKakiPCIntance = TRUE;
|
};
|
}
|
//=================================================
|
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
if ((*it)->GetConnected() == FALSE) return FALSE; // check connect
|
|
int nSub = (*it)->SetTriggerProcess();
|
|
if (nSub==1)
|
{
|
g_pLog->DisplayMessage(_T("[CameraController] SetCameraSoftwareTrigger Grab Success!"));
|
|
if(CKakiPCIntance == TRUE)
|
{
|
|
pProtocolDat->SetReviewedDefectNum(0,pProtocolDat->GetReviewedDefectNum(0)+1); //공유메모리
|
//g_pLog->DisplayMessage(_T("리뷰 할 갯수: %d 리뷰한 객수 : %d"),pProtocolDat->GetPlanDefectCount(0),pProtocolDat->GetReviewedDefectNum(0)); //cmark
|
|
}
|
|
}
|
else
|
{
|
g_pLog->DisplayMessage(_T("[CameraController] SetCameraSoftwareTrigger Grab Fail!"));
|
return FALSE;
|
}
|
}
|
return TRUE;
|
}
|
|
CCHImageData* CCameraController::GetCameraImage(int nCameraIdx, int nFrameIdx)
|
{
|
if (nCameraIdx<0 || nCameraIdx>=(int)m_vecCameraImage.size()) return NULL;
|
|
if (nFrameIdx<0 || nFrameIdx>=(m_vecCameraImage[nCameraIdx])->GetImageCount()) return NULL;
|
|
return (m_vecCameraImage[nCameraIdx])->GetImageData(nFrameIdx);
|
}
|
|
BOOL CCameraController::GetConnected(int nCameraIdx)
|
{
|
if (nCameraIdx<0 || nCameraIdx>=(int)m_vecCameraImage.size()) return FALSE;
|
|
return m_vecCameraControl[nCameraIdx]->GetConnected();
|
}
|
|
BOOL CCameraController::GetConnected()
|
{
|
if (m_vecCameraControl.size()<1) return FALSE;
|
|
BOOL bValue = TRUE;
|
|
for (VectorCameraControlIt it=m_vecCameraControl.begin(); it!=m_vecCameraControl.end(); it++)
|
{
|
bValue = bValue && (*it)->GetConnected();
|
}
|
|
return bValue;
|
}
|
|
BOOL CCameraController::GetLiveImage(int nCameraIdx, CCHImageData* pImageData)
|
{
|
if (nCameraIdx<0 || nCameraIdx>=(int)m_vecCameraControl.size())
|
{
|
return FALSE;
|
}
|
|
if (m_vecCameraControl[nCameraIdx]->GetImageData(pImageData)==FALSE)
|
{
|
return FALSE;
|
}
|
|
return TRUE;
|
}
|
|
BOOL CCameraController::ICC2P_FrameCaptured(int nCameraIndex, int nFrameIndex, int nFrameCount)
|
{
|
if (nCameraIndex<0 || nCameraIndex>=(int)m_vecCameraControl.size())
|
{
|
return FALSE;
|
}
|
|
BYTE *pCameraBuffer = (BYTE*)m_vecCameraControl[nCameraIndex]->GetImageAddress(nFrameIndex);
|
if (pCameraBuffer==NULL)
|
{
|
return FALSE;
|
}
|
|
// get camera image size
|
int nWidth = m_vecCameraControl[nCameraIndex]->GetWidth();
|
int nHeight = m_vecCameraControl[nCameraIndex]->GetHeight();
|
int nChannels = m_vecCameraControl[nCameraIndex]->GetChannels();
|
|
// add image index
|
int nImageIndex = m_vecCameraImageIndex[nCameraIndex] + 1;
|
if (nImageIndex>=(m_vecCameraImage[nCameraIndex])->GetImageCount())
|
{
|
nImageIndex = 0;
|
}
|
|
if (nCameraIndex == 1)
|
{
|
nCameraIndex = nCameraIndex;
|
}
|
|
// check buffer image size
|
if ((m_vecCameraImage[nCameraIndex])->GetImageData(nImageIndex)->GetWidth()!=nWidth || (m_vecCameraImage[nCameraIndex])->GetImageData(nImageIndex)->GetHeight()!=nHeight)
|
{
|
(m_vecCameraImage[nCameraIndex])->GetImageData(nImageIndex)->CreateImage(nWidth, nHeight, 8, nChannels);
|
}
|
|
BYTE *pImageBuffer = (BYTE*)((m_vecCameraImage[nCameraIndex])->GetImageData(nImageIndex))->GetImageBuffer();
|
if (pImageBuffer==NULL)
|
{
|
return FALSE;
|
}
|
|
memcpy(pImageBuffer, pCameraBuffer, sizeof(BYTE)*m_vecCameraImageSize[nCameraIndex]);
|
|
m_vecCameraImageIndex[nCameraIndex] = nImageIndex;
|
|
return TRUE;
|
}
|
|
|
BOOL CCameraController::ICC2P_SnapCompleted(int nCameraIndex, int nFrameCount)
|
{
|
|
return TRUE;
|
}
|