You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cigarette/上海二车间58#ZB25小盒-单相机-GPU-延后两个/Cigarette/CaptureThreadHIK.cpp

492 lines
15 KiB
C++

#include "CaptureThreadHIK.h"
#include <QtWidgets/QApplication>
#include "PLCDevice.h"
#include "common.h"
#include <windows.h>
extern bool g_debug_mode; //相机调试模式,调试模式必须暂停状态才能打开
extern SingleCamInfoStruct SingleCamInfo[NumberOfSupportedCameras];
extern PLCDevice * m_PLCDevice;
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
void __stdcall LossCallBack1(unsigned int pData, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
SingleCamInfo[CaptureThreadHIKptr->Local_Num].OffLine = true;
CaptureThreadHIKptr->terminate();
}
void __stdcall FallingGpioEvent1(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
int unit_count = CaptureThreadHIKptr->p_unit_queue->count();
if (unit_count > 0)
{
cv::Mat long_image;
for (int i = 0; i < unit_count; i++)
{
cv::Mat image;
CaptureThreadHIKptr->p_unit_queue->take(image);
if (0 == i)
{
long_image = cv::Mat::zeros(image.rows * unit_count, image.cols, image.type());
}
cv::Rect r(0, i * image.rows, image.cols, image.rows);
cv::Mat roi = long_image(r);
image.copyTo(roi);
}
CaptureThreadHIKptr->p_image_queue->put(std::make_pair(unit_count, long_image));
CaptureThreadHIKptr->p_shooted_queue->put(true);
}
CaptureThreadHIKptr->p_unit_queue->clear();
}
void __stdcall RisingGpioEvent1(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
#ifdef IMM_FEED_BACK//当前周期
if (CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
}
//当前周期
#elif defined ONE_TIME_SHIFT//延后一个周期
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
//延后一个周期
//延后两个周期
#else
if(CaptureThreadHIKptr->p_result_wait_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_result_wait_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
CaptureThreadHIKptr->p_result_wait_queue->put(true);
}
//延后两个周期
#endif
}
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
void __stdcall LossCallBack2(unsigned int pData, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
SingleCamInfo[CaptureThreadHIKptr->Local_Num].OffLine = true;
CaptureThreadHIKptr->terminate();
}
void __stdcall FallingGpioEvent2(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
int unit_count = CaptureThreadHIKptr->p_unit_queue->count();
if (unit_count > 0)
{
cv::Mat long_image;
for (int i = 0; i < unit_count; i++)
{
cv::Mat image;
CaptureThreadHIKptr->p_unit_queue->take(image);
if (0 == i)
{
long_image = cv::Mat::zeros(image.rows * unit_count, image.cols, image.type());
}
cv::Rect r(0, i * image.rows, image.cols, image.rows);
cv::Mat roi = long_image(r);
image.copyTo(roi);
}
CaptureThreadHIKptr->p_image_queue->put(std::make_pair(unit_count, long_image));
CaptureThreadHIKptr->p_shooted_queue->put(true);
}
CaptureThreadHIKptr->p_unit_queue->clear();
}
void __stdcall RisingGpioEvent2(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
#ifdef IMM_FEED_BACK
if (CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
}
#elif defined ONE_TIME_SHIFT
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
#else
if(CaptureThreadHIKptr->p_result_wait_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_result_wait_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
CaptureThreadHIKptr->p_result_wait_queue->put(true);
}
#endif
}
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
void __stdcall LossCallBack3(unsigned int pData, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
SingleCamInfo[CaptureThreadHIKptr->Local_Num].OffLine = true;
CaptureThreadHIKptr->terminate();
}
void __stdcall FallingGpioEvent3(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
int unit_count = CaptureThreadHIKptr->p_unit_queue->count();
if (unit_count > 0)
{
cv::Mat long_image;
for (int i = 0; i < unit_count; i++)
{
cv::Mat image;
CaptureThreadHIKptr->p_unit_queue->take(image);
if (0 == i)
{
long_image = cv::Mat::zeros(image.rows * unit_count, image.cols, image.type());
}
cv::Rect r(0, i * image.rows, image.cols, image.rows);
cv::Mat roi = long_image(r);
image.copyTo(roi);
}
CaptureThreadHIKptr->p_image_queue->put(std::make_pair(unit_count, long_image));
CaptureThreadHIKptr->p_shooted_queue->put(true);
}
CaptureThreadHIKptr->p_unit_queue->clear();
}
void __stdcall RisingGpioEvent3(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
#ifdef IMM_FEED_BACK
if (CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
}
#elif defined ONE_TIME_SHIFT
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
#else
if(CaptureThreadHIKptr->p_result_wait_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_result_wait_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
CaptureThreadHIKptr->p_result_wait_queue->put(true);
}
#endif
}
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
void __stdcall LossCallBack4(unsigned int pData, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
SingleCamInfo[CaptureThreadHIKptr->Local_Num].OffLine = true;
CaptureThreadHIKptr->terminate();
}
void __stdcall FallingGpioEvent4(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
int unit_count = CaptureThreadHIKptr->p_unit_queue->count();
if (unit_count > 0)
{
cv::Mat long_image;
for (int i = 0; i < unit_count; i++)
{
cv::Mat image;
CaptureThreadHIKptr->p_unit_queue->take(image);
if (0 == i)
{
long_image = cv::Mat::zeros(image.rows * unit_count, image.cols, image.type());
}
cv::Rect r(0, i * image.rows, image.cols, image.rows);
cv::Mat roi = long_image(r);
image.copyTo(roi);
}
CaptureThreadHIKptr->p_image_queue->put(std::make_pair(unit_count, long_image));
CaptureThreadHIKptr->p_shooted_queue->put(true);
}
CaptureThreadHIKptr->p_unit_queue->clear();
}
void __stdcall RisingGpioEvent4(MV_EVENT_OUT_INFO* pEventInfo, void* pUser)
{
CaptureThreadHIK* CaptureThreadHIKptr = (CaptureThreadHIK*)pUser;
#ifdef IMM_FEED_BACK
if (CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
}
#elif defined ONE_TIME_SHIFT
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
#else
if(CaptureThreadHIKptr->p_result_wait_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_result_wait_queue->take(temp);
if (CaptureThreadHIKptr->p_result_queue->count() > 0)
{
bool result;
CaptureThreadHIKptr->p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CaptureThreadHIKptr->CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CaptureThreadHIKptr->CamHandle, "LineTriggerSoftware");
}
}
}
if(CaptureThreadHIKptr->p_shooted_queue->count() > 0)
{
bool temp;
CaptureThreadHIKptr->p_shooted_queue->take(temp);
CaptureThreadHIKptr->p_result_wait_queue->put(true);
}
#endif
}
#endif
void(*LossCallBack[NumberOfSupportedCameras])(unsigned int pData, void* pUser) = {
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
LossCallBack1
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
,LossCallBack2
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
,LossCallBack3
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
,LossCallBack4
#endif
};
void(*FallingGpioEvent[NumberOfSupportedCameras])(MV_EVENT_OUT_INFO* pEventInfo, void* pUser) = {
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
FallingGpioEvent1
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
,FallingGpioEvent2
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
,FallingGpioEvent3
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
,FallingGpioEvent4
#endif
};
void(*RisingGpioEvent[NumberOfSupportedCameras])(MV_EVENT_OUT_INFO* pEventInfo, void* pUser) = {
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
RisingGpioEvent1
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
,RisingGpioEvent2
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
,RisingGpioEvent3
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
,RisingGpioEvent4
#endif
};
void CaptureThreadHIK::fpsTimeout(void)
{
uint64_t delta = m_cntGrabbedImages - m_cntLastGrabbedImages;
m_cntLastGrabbedImages = m_cntGrabbedImages;
QString data = QString("%1").arg(delta);
emit updateStatistics(data.left(4), Local_Num);
}
//-----------------------------------------------------------------------------
CaptureThreadHIK::CaptureThreadHIK(void *camhandle, bool boTerminated,int Num) :
CamHandle( camhandle ), boTerminated_( boTerminated ),Local_Num(Num)
//-----------------------------------------------------------------------------
{
p_unit_queue = new ASyncQueue<cv::Mat>(Unit_Queue_Size);
g_pImage_buf = (unsigned char*)malloc(3000 * 3000 * 3);
}
CaptureThreadHIK::~CaptureThreadHIK()
{
delete p_unit_queue;
free(g_pImage_buf);
}
//-----------------------------------------------------------------------------
void CaptureThreadHIK::process( void )
//-----------------------------------------------------------------------------
{
int nRet = MV_OK, nnRet = MV_OK;
nRet = MV_CC_SetEnumValueByString(CamHandle, "EventSelector", "Line0FallingEdge");
if (nRet) { std::cout << "can not set EventSelector" << std::endl; nnRet = nRet; }
nRet = MV_CC_SetEnumValueByString(CamHandle, "EventNotification", "On");
if (nRet) { std::cout << "can not set EventNotification" << std::endl; nnRet = nRet; }
nRet = MV_CC_SetEnumValueByString(CamHandle, "EventSelector", "Line0RisingEdge");
if (nRet) { std::cout << "can not set EventSelector" << std::endl; nnRet = nRet; }
nRet = MV_CC_SetEnumValueByString(CamHandle, "EventNotification", "On");
if (nRet) { std::cout << "can not set EventNotification" << std::endl; nnRet = nRet; }
nRet = MV_CC_RegisterExceptionCallBack(CamHandle, LossCallBack[Local_Num], this);
if (nRet) { std::cout << "can not register loss callback" << std::endl; nnRet = nRet; }
nRet = MV_CC_RegisterEventCallBackEx(CamHandle, "Line0FallingEdge", FallingGpioEvent[Local_Num], this);
if (nRet) { std::cout << "can not register GPIO callback" << std::endl; nnRet = nRet; }
nRet = MV_CC_RegisterEventCallBackEx(CamHandle, "Line0RisingEdge", RisingGpioEvent[Local_Num], this);
if (nRet) { std::cout << "can not register GPIO callback" << std::endl; nnRet = nRet; }
m_Timer = new QTimer();
connect(m_Timer, SIGNAL(timeout()), this, SLOT(fpsTimeout()));
m_Timer->start(1000);
unsigned int nDataSize;
MVCC_INTVALUE_EX stIntValue = { 0 };
nRet = MV_CC_GetIntValueEx(CamHandle, "PayloadSize", &stIntValue);
if (nRet) { std::cout << "Get PayloadSize error" << std::endl; }
nDataSize = stIntValue.nCurValue;
MV_CC_StartGrabbing(CamHandle);
Ready = true;
while(!boTerminated_)
{
nRet = MV_CC_GetOneFrameTimeout(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 200);
if (MV_OK == nRet)
{
m_cntGrabbedImages++;
cv::Mat openCVImage(stFrameInfo.nHeight, stFrameInfo.nWidth, CV_8UC1, g_pImage_buf);
cv::Mat image_clone = openCVImage.clone();
if (!g_debug_mode)
{
p_unit_queue->put(image_clone); //放入临时队列
}
else
{
p_debug_queue->put(image_clone); //放入调试队列
}
}
#ifdef IMM_FEED_BACK
if (p_result_queue->count() > 0)
{
bool result;
p_result_queue->take(result);
if (!result)
{
MV_CC_SetEnumValue(CamHandle, "LineSelector", 1);
MV_CC_SetCommandValue(CamHandle, "LineTriggerSoftware");
}
}
#endif
QCoreApplication::processEvents();//Make sure the timer is triggered
}
MV_CC_StopGrabbing(CamHandle);
MV_CC_CloseDevice(CamHandle);
delete m_Timer;
}
//-----------------------------------------------------------------------------