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/上海四车间3#-倾斜胶点-四相机-CPU-无延后/Cigarette/CaptureThreadBasler.cpp

182 lines
7.3 KiB
C++

#include "CaptureThreadBasler.h"
#include <QtWidgets/QApplication>
#include "PLCDevice.h"
#include "common.h"
#include <qdebug.h>
extern PLCDevice * m_PLCDevice;
//-----------------------------------------------------------------------------
CaptureThreadBasler::CaptureThreadBasler(Pylon::CBaslerUniversalInstantCamera* pCurrDev, bool boTerminated,int Num,int shoot) :
pDev_( pCurrDev ), boTerminated_( boTerminated ),Local_Num(Num)
//-----------------------------------------------------------------------------
{
p_unit_queue = new ASyncQueue<cv::Mat>(Unit_Queue_Size);
Shoot_Num = shoot;
}
CaptureThreadBasler::~CaptureThreadBasler()
{
delete p_unit_queue;
}
//-----------------------------------------------------------------------------
void CaptureThreadBasler::process(void)
//-----------------------------------------------------------------------------
{
bool last_high = false;
bool last_result = false;
try
{
CfgEventPtr = new CSampleConfigurationEventHandler;
CfgEventPtr->channel_ = Local_Num;
pDev_->RegisterConfiguration(CfgEventPtr, Pylon::RegistrationMode_Append, Pylon::Cleanup_Delete);
ImageEventPtr = new CSampleImageEventHandler;
ImageEventPtr->p_image_queue_ = p_image_queue;
ImageEventPtr->p_result_queue_ = p_result_queue;
ImageEventPtr->p_unit_queue_ = p_unit_queue;
ImageEventPtr->p_shooted_queue_ = p_shooted_queue;
ImageEventPtr->p_debug_queue_ = p_debug_queue;
ImageEventPtr->m_cntGrabbedImages_ = &m_cntGrabbedImages;
ImageEventPtr->Shoot_Num_ = Shoot_Num;
ImageEventPtr->pDev__ = pDev_;
ImageEventPtr->ioTimer = m_IOTimer;
pDev_->RegisterImageEventHandler(ImageEventPtr, Pylon::RegistrationMode_Append, Pylon::Cleanup_None);
#ifndef USB_BASLER_NEW_FW
if (pDev_->TriggerSelector.TrySetValue(Basler_UniversalCameraParams::TriggerSelector_FrameBurstStart))
{
pDev_->TriggerMode.SetValue(Basler_UniversalCameraParams::TriggerMode_On);
pDev_->TriggerSource.SetValue(Basler_UniversalCameraParams::TriggerSource_Line1);
pDev_->TriggerActivation.SetValue(Basler_UniversalCameraParams::TriggerActivation_RisingEdge);
pDev_->AcquisitionBurstFrameCount.SetValue(Shoot_Num);
if (pDev_->EventSelector.TrySetValue(Basler_UniversalCameraParams::EventSelector_FrameBurstStart))
{
if (!pDev_->EventNotification.TrySetValue(Basler_UniversalCameraParams::EventNotification_On))
{
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_GenICamEvent);
}
BurstEventPtr = new CSampleCameraEventHandler;
m_IOTimer = new QTimer();
connect(m_IOTimer, SIGNAL(timeout()), this, SLOT(ioTimeout()));
m_IOTimer->setSingleShot(true);
BurstEventPtr->p_unit_queue_ = p_unit_queue;
BurstEventPtr->p_result_wait_queue_ = p_result_wait_queue;
BurstEventPtr->p_result_queue_ = p_result_queue;
BurstEventPtr->p_shooted_queue_ = p_shooted_queue;
BurstEventPtr->p_image_queue_ = p_image_queue;
BurstEventPtr->pDev__ = pDev_;
BurstEventPtr->ioTimer = m_IOTimer;
pDev_->RegisterCameraEventHandler(BurstEventPtr, "EventFrameBurstStartData", eMyFrameBurstStartEvent, Pylon::RegistrationMode_Append, Pylon::Cleanup_None);
}
if(pDev_->EventSelector.TrySetValue("Line1RisingEdge"))
{
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_Off);
}
if(pDev_->EventSelector.TrySetValue("Line1FallingEdge"))
{
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_Off);
}
}
#else
/*Set here*/
BurstEventPtr = new CSampleCameraEventHandler;
BurstEventPtr->p_unit_queue_ = p_unit_queue;
BurstEventPtr->p_result_wait_queue_ = p_result_wait_queue;
BurstEventPtr->p_result_queue_ = p_result_queue;
BurstEventPtr->p_shooted_queue_ = p_shooted_queue;
BurstEventPtr->p_image_queue_ = p_image_queue;
BurstEventPtr->pDev__ = pDev_;
BurstEventPtr->ioTimer = m_IOTimer;
if (pDev_->TriggerSelector.TrySetValue(Basler_UniversalCameraParams::TriggerSelector_FrameBurstStart))
{
DEBUG(" TriggerSelector_FrameBurstStart\n");
pDev_->TriggerMode.SetValue(Basler_UniversalCameraParams::TriggerMode_Off);
if(pDev_->EventSelector.TrySetValue(Basler_UniversalCameraParams::EventSelector_FrameBurstStart))
{
DEBUG(" EventSelector_FrameBurstStart\n");
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_Off);
}
}
pDev_->RegisterCameraEventHandler(BurstEventPtr, "EventLine1RisingEdgeData", Line1RisingEdge, Pylon::RegistrationMode_Append, Pylon::Cleanup_None);
pDev_->EventSelector.SetValue("Line1RisingEdge");
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_On);
pDev_->RegisterCameraEventHandler(BurstEventPtr, "EventLine1FallingEdgeData", Line1FallingEdge, Pylon::RegistrationMode_Append, Pylon::Cleanup_None);
pDev_->EventSelector.SetValue("Line1FallingEdge");
pDev_->EventNotification.SetValue(Basler_UniversalCameraParams::EventNotification_On);
#endif
const unsigned int timeout_ms = 200;
// This smart pointer will receive the grab result data.
Pylon::CGrabResultPtr ptrGrabResult;
pDev_->StartGrabbing();
m_Timer = new QTimer();
connect(m_Timer, SIGNAL(timeout()), this, SLOT(fpsTimeout()));
m_Timer->start(1000);
Ready = true;
while (!boTerminated_)
{
// Retrieve grab results and notify the camera event and image event handlers.
pDev_->RetrieveResult(timeout_ms, ptrGrabResult, Pylon::TimeoutHandling_Return);
#ifdef IMM_FEED_BACK
if (p_result_queue->count() > 0)
{
bool result;
p_result_queue->take(result);
if (!result){
pDev_->UserOutputSelector.SetValue(Basler_UniversalCameraParams::UserOutputSelector_UserOutput3);
pDev_->UserOutputValue.SetValue(false);
pDev_->UserOutputValue.SetValue(true);
}
else{
pDev_->UserOutputSelector.SetValue(Basler_UniversalCameraParams::UserOutputSelector_UserOutput1);
pDev_->UserOutputValue.SetValue(false);
pDev_->UserOutputValue.SetValue(true);
}
}
#endif
QCoreApplication::processEvents();//Make sure the timer is triggered
}
#ifndef USB_BASLER_NEW_FW
// Disable sending Event Overrun events.
if (pDev_->EventSelector.TrySetValue(Basler_UniversalCameraParams::EventSelector_FrameBurstStart))
{
pDev_->EventNotification.TrySetValue(Basler_UniversalCameraParams::EventNotification_Off);
}
if (m_IOTimer) delete m_IOTimer;
#endif
if (m_Timer) delete m_Timer;
pDev_->StopGrabbing();
}
catch(const Pylon::GenericException& e)
{
emit error( QString::fromStdString( e.GetDescription()) );
std::cout << e.GetSourceLine()<<e.GetDescription() << std::endl;
pDev_->StopGrabbing();
if(CfgEventPtr)pDev_->DeregisterConfiguration(CfgEventPtr);
if (ImageEventPtr)pDev_->DeregisterImageEventHandler(ImageEventPtr);
if (BurstEventPtr)pDev_->DeregisterCameraEventHandler(BurstEventPtr, "EventFrameBurstStartData");
}
}
void CaptureThreadBasler::fpsTimeout()
{
uint64_t delta = m_cntGrabbedImages - m_cntLastGrabbedImages;
m_cntLastGrabbedImages = m_cntGrabbedImages;
QString data = QString("%1").arg(delta);
emit updateStatistics(data.left(4), Local_Num);
}
void CaptureThreadBasler::ioTimeout()
{
pDev_->UserOutputSelector.SetValue(Basler_UniversalCameraParams::UserOutputSelector_UserOutput1);
pDev_->UserOutputValue.SetValue(false);
pDev_->UserOutputSelector.SetValue(Basler_UniversalCameraParams::UserOutputSelector_UserOutput3);
pDev_->UserOutputValue.SetValue(false);
}