[grabber] is continuously grabbing

This commit is contained in:
Jan Grewe 2020-03-12 18:19:57 +01:00
parent c41d4ed035
commit 3062db01cc
5 changed files with 42 additions and 33 deletions

View File

@ -1,13 +1,22 @@
#include "grabber.h" #include "grabber.h"
#include <iostream> #include <iostream>
#include <pylon/PylonIncludes.h>
void Grabber::run() { void Grabber::run() {
int count = 0; int count = 0;
if (camera->isOpen()) {
Pylon::CGrabResultPtr frame;
Pylon::CInstantCamera *cam = camera->getCamera();
cam->StartGrabbing();
while (!stop_request) { while (!stop_request) {
std::cerr << "running: " << count << std::endl; camera->frameRate(75);
MyImage img;
cam->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException);
img.setFrame(frame);
buffer->push(img);
count += 1; count += 1;
msleep(500);
} }
cam->StopGrabbing();
std::cerr << "terminated: " << count << std::endl; std::cerr << "terminated: " << count << std::endl;
//emit terminated(); }
} }

View File

@ -3,18 +3,23 @@
#include <QObject> #include <QObject>
#include <QThread> #include <QThread>
#include "pylonwrapper.h"
#include "imagebuffer.h"
class Grabber : public QThread class Grabber : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
Grabber(QObject *parent = nullptr) : QThread(parent) {} Grabber(PylonWrapper *camera, ImageBuffer*buffer, QObject *parent = nullptr) :
QThread(parent), camera(camera), buffer(buffer) {}
void run() override; void run() override;
void stop(); void stop();
private: private:
bool stop_request = false; bool stop_request = false;
PylonWrapper *camera;
ImageBuffer *buffer;
public slots: public slots:
void requestStop() { void requestStop() {

View File

@ -46,6 +46,7 @@ void ImageBuffer::push(const MyImage &img) {
if (load < static_cast<int64_t>(buffer.capacity())) if (load < static_cast<int64_t>(buffer.capacity()))
load += 1; load += 1;
mutex.unlock(); mutex.unlock();
std::cerr << "load: " << bufferLoad() << "\t pressure: " << bufferPreassure() << std::endl;
} }
bool ImageBuffer::bufferNotEmpty() { bool ImageBuffer::bufferNotEmpty() {

View File

@ -20,7 +20,6 @@
#include <QToolBar> #include <QToolBar>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include "grabber.h"
#if defined(QT_PRINTSUPPORT_LIB) #if defined(QT_PRINTSUPPORT_LIB)
# include <QtPrintSupport/qtprintsupportglobal.h> # include <QtPrintSupport/qtprintsupportglobal.h>
@ -42,6 +41,8 @@ PylonRecorder::PylonRecorder(QWidget *parent)
scrollArea->setVisible(false); scrollArea->setVisible(false);
setCentralWidget(scrollArea); setCentralWidget(scrollArea);
pylon = new PylonWrapper(); pylon = new PylonWrapper();
buffer = new ImageBuffer();
grabber = new Grabber(pylon, buffer);
createActions(); createActions();
updateActions(); updateActions();
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5); resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
@ -50,6 +51,8 @@ PylonRecorder::PylonRecorder(QWidget *parent)
PylonRecorder::~PylonRecorder(){ PylonRecorder::~PylonRecorder(){
delete pylon; delete pylon;
delete buffer;
delete grabber;
} }
bool PylonRecorder::loadFile(const QString &fileName) { bool PylonRecorder::loadFile(const QString &fileName) {
@ -290,6 +293,7 @@ void PylonRecorder::createActions() {
grab_still_action->setShortcut(tr("Ctrl+ ")); grab_still_action->setShortcut(tr("Ctrl+ "));
grab_continuous_action = camera_menu->addAction(tr("&grab continuous"), this, &PylonRecorder::startRecording); grab_continuous_action = camera_menu->addAction(tr("&grab continuous"), this, &PylonRecorder::startRecording);
grab_continuous_action->setShortcut(tr("Ctrl+Enter")); grab_continuous_action->setShortcut(tr("Ctrl+Enter"));
grab_stop_action = camera_menu->addAction(tr("&stop grabbing"), this, &PylonRecorder::stopRecording);
QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&About"), this, &PylonRecorder::about); helpMenu->addAction(tr("&About"), this, &PylonRecorder::about);
@ -301,6 +305,8 @@ void PylonRecorder::createActions() {
toolbar->addAction(disconnect_camera_action); toolbar->addAction(disconnect_camera_action);
toolbar->addSeparator(); toolbar->addSeparator();
toolbar->addAction(grab_still_action); toolbar->addAction(grab_still_action);
toolbar->addAction(grab_continuous_action);
toolbar->addAction(grab_stop_action);
} }
void PylonRecorder::updateActions() { void PylonRecorder::updateActions() {
@ -313,6 +319,7 @@ void PylonRecorder::updateActions() {
connect_camera_action->setEnabled(!pylon->isOpen()); connect_camera_action->setEnabled(!pylon->isOpen());
grab_still_action->setEnabled(pylon->isOpen()); grab_still_action->setEnabled(pylon->isOpen());
grab_continuous_action->setEnabled(pylon->isOpen()); grab_continuous_action->setEnabled(pylon->isOpen());
} }
void PylonRecorder::scaleImage(double factor) { void PylonRecorder::scaleImage(double factor) {
@ -357,31 +364,13 @@ void PylonRecorder::disconnectCamera() {
void PylonRecorder::startRecording() { void PylonRecorder::startRecording() {
int framecount = 500; int framecount = 500;
Grabber grabber; grabber->start();
grabber.start();
Pylon::CGrabResultPtr frame;
if (pylon->isOpen()) {
pylon->frameRate(75);
Pylon::CInstantCamera *cam = pylon->getCamera();
cam->StartGrabbing();
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < framecount; ++i) {
MyImage img;
cam->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException);
img.setFrame(frame);
buffer.push(img);
std::cerr << i << ": Buffer stats: capacity: " << buffer.capacity() << "\tload: " << buffer.bufferLoad() << "\t pressure: " << buffer.bufferPreassure() << "frameid: " << frame->GetID() << std::endl;
}
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
cam->StopGrabbing();
std::cerr << "elapsed time: " << elapsed.count() << " s\n";
}
grabber.requestStop();
grabber.wait(10000);
} }
void PylonRecorder::stopRecording() {} void PylonRecorder::stopRecording() {
grabber->requestStop();
grabber->wait(10000);
}
void PylonRecorder::grabStillFromPylon() { void PylonRecorder::grabStillFromPylon() {
if (pylon->isOpen()) { if (pylon->isOpen()) {

View File

@ -4,6 +4,8 @@
#include <QMainWindow> #include <QMainWindow>
#include "pylonwrapper.h" #include "pylonwrapper.h"
#include "imagebuffer.h" #include "imagebuffer.h"
#include "grabber.h"
#include <QImage> #include <QImage>
#if defined(QT_PRINTSUPPORT_LIB) #if defined(QT_PRINTSUPPORT_LIB)
# include <QtPrintSupport/qtprintsupportglobal.h> # include <QtPrintSupport/qtprintsupportglobal.h>
@ -66,7 +68,9 @@ private:
QScrollArea *scrollArea; QScrollArea *scrollArea;
double scaleFactor = 1; double scaleFactor = 1;
PylonWrapper *pylon; PylonWrapper *pylon;
ImageBuffer buffer; ImageBuffer *buffer;
Grabber *grabber;
bool grabbing;
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
QPrinter printer; QPrinter printer;
@ -81,6 +85,7 @@ private:
QAction *fitToWindowAct; QAction *fitToWindowAct;
QAction *grab_still_action; QAction *grab_still_action;
QAction *grab_continuous_action; QAction *grab_continuous_action;
QAction *grab_stop_action;
QAction *connect_camera_action; QAction *connect_camera_action;
QAction *disconnect_camera_action; QAction *disconnect_camera_action;
}; };