[dualcamgrabber] working with software triggering

This commit is contained in:
Jan Grewe 2025-03-12 15:26:04 +01:00
parent 4f2d45df6b
commit 0fa532d59a
3 changed files with 82 additions and 38 deletions

View File

@ -18,9 +18,12 @@ void DualcamGrabber::run() {
} }
Pylon::CInstantCameraArray &cameras = wrapper->getCameraArray(); Pylon::CInstantCameraArray &cameras = wrapper->getCameraArray();
wrapper->frameRate(static_cast<uint>(framerate), -1); wrapper->frameRate(static_cast<uint>(100), -1);
wrapper->exposureTime(exposure); wrapper->exposureTime(exposure);
wrapper->gain(gain); wrapper->gain(gain);
wrapper->enableSoftwareTrigger(0);
wrapper->enableSoftwareTrigger(1);
cameras.StartGrabbing(); cameras.StartGrabbing();
Pylon::CGrabResultPtr frame0, frame1; Pylon::CGrabResultPtr frame0, frame1;
@ -28,40 +31,76 @@ void DualcamGrabber::run() {
Pylon::CPylonImage rightImage; Pylon::CPylonImage rightImage;
Pylon::CPylonImage stitchedImage; Pylon::CPylonImage stitchedImage;
std::string errorMessage = ""; std::string errorMessage = "";
bool failure = false;
auto before = high_resolution_clock::now();
auto after = high_resolution_clock::now();
auto after1 = high_resolution_clock::now();
auto stitch = high_resolution_clock::now();
auto stitchncopy = high_resolution_clock::now();
auto grab_duration = duration_cast<microseconds>(after - before);
auto grab1_duration = duration_cast<microseconds>(after1 - before);
auto stitch_duration = duration_cast<microseconds>(stitch - after);
auto stitchncopy_duration = duration_cast<microseconds>(stitchncopy - after);
auto done = high_resolution_clock::now();
auto total_duration = duration_cast<microseconds>(done - before);
int expected_usecs = (int)(1./framerate * 1000000);
while (cameras.IsGrabbing() && !stop_request && !failure) {
if (counter > 0) {
long delay = total_duration.count() - expected_usecs;
if (delay > 0) {
emit delayed(delay, counter-1);
} else {
// std::cerr << "expected ifi " << expected_usecs << " real ifi: " << total_duration.count() << " waiting for " << (-1*delay) << std::endl;
usleep(-delay);
}
}
before = high_resolution_clock::now();
while (cameras.IsGrabbing() && !stop_request) { if (cameras[0].WaitForFrameTriggerReady(1000, Pylon::TimeoutHandling_ThrowException) &
auto before = high_resolution_clock::now(); cameras[1].WaitForFrameTriggerReady(1000, Pylon::TimeoutHandling_ThrowException)) {
auto after1 = high_resolution_clock::now(); // std::cerr << "executing software triggers" << std::endl;
cameras[0].ExecuteSoftwareTrigger();
cameras[1].ExecuteSoftwareTrigger();
}
try {
cameras[0].RetrieveResult( 5000, frame0, Pylon::TimeoutHandling_ThrowException );
after1 = high_resolution_clock::now();
cameras[1].RetrieveResult( 5000, frame1, Pylon::TimeoutHandling_ThrowException );
leftImage.AttachGrabResultBuffer( frame0 );
rightImage.AttachGrabResultBuffer( frame1 );
} catch( ... ) {
qDebug() << "Grabbing frame failed! " << e.what();
failure = true
}
after = high_resolution_clock::now();
if (!failure && leftImage.IsValid() && rightImage.IsValid()) {
try { try {
cameras[0].RetrieveResult( 5000, frame0, Pylon::TimeoutHandling_ThrowException ); StitchImage::StitchToRight(leftImage, rightImage, &stitchedImage, errorMessage);
after1 = high_resolution_clock::now(); stitch = high_resolution_clock::now();
cameras[1].RetrieveResult( 5000, frame1, Pylon::TimeoutHandling_ThrowException ); MyImage *img = new MyImage(stitchedImage.GetWidth(), stitchedImage.GetHeight());
leftImage.AttachGrabResultBuffer( frame0 ); img->setFrame(stitchedImage);
rightImage.AttachGrabResultBuffer( frame1 ); buffer->push(img);
} catch(const std::exception& e) { } catch(const std::exception& e) {
qDebug() << "Grabbing frame failed! " << e.what(); std::cerr << e.what() << '\n';
} }
auto after = high_resolution_clock::now(); }
if (leftImage.IsValid() && rightImage.IsValid()) {
try { stitchncopy = high_resolution_clock::now();
StitchImage::StitchToRight(leftImage, rightImage, &stitchedImage, errorMessage); grab_duration = duration_cast<microseconds>(after - before);
MyImage *img = new MyImage(stitchedImage.GetWidth(), stitchedImage.GetHeight()); grab1_duration = duration_cast<microseconds>(after1 - before);
img->setFrame(stitchedImage); stitch_duration = duration_cast<microseconds>(stitch - after);
buffer->push(img); stitchncopy_duration = duration_cast<microseconds>(stitchncopy - after);
} catch(const std::exception& e) { // std::cerr << "framecount: " << counter << " grab1_duration (us): " << grab1_duration.count() << " grab_all (us): " << grab_duration.count() << std::endl;
std::cerr << e.what() << '\n'; // std::cerr << "\tpure stich (us): " << stitch_duration.count() << " stich'n'copy (us): " << stitchncopy_duration.count() << std::endl;
} done = high_resolution_clock::now();
} total_duration = duration_cast<microseconds>(done - before);
auto stitch = high_resolution_clock::now(); // double rate = (1./((double)total_duration.count()/1000000));
auto grab_duration = duration_cast<microseconds>(after - before); // std::cerr << "total_duration (us): " << total_duration.count() << " rate: " << rate << std::endl;
auto grab1_duration = duration_cast<microseconds>(after1 - before); counter += 1;
auto stitch_duration = duration_cast<microseconds>(stitch - after);
std::cerr << "framecount: " << counter << " grab_duration (us): " << grab_duration.count() << "\t" << " stitching (us): " << stitch_duration.count() << " grab1_duration (us): " << grab1_duration.count() << std::endl;
counter += 1;
auto done = high_resolution_clock::now();
auto total_duration = duration_cast<microseconds>(done - before);
double rate = (1./((double)total_duration.count()/1000000));
std::cerr << "total_duration (us): " << total_duration.count() << " rate: " << rate << std::endl;
} }
cameras.StopGrabbing(); cameras.StopGrabbing();
} }

View File

@ -53,6 +53,7 @@ public slots:
signals: signals:
void terminated(); void terminated();
void delayed(int, int);
}; };
#endif // DUALCAMGRABBER_H #endif // DUALCAMGRABBER_H

View File

@ -49,6 +49,7 @@ double DualcamWrapper::maxFrameRate(int camindex) {
bool DualcamWrapper::frameRate(uint new_framerate, int camindex) { bool DualcamWrapper::frameRate(uint new_framerate, int camindex) {
qDebug() << "Setting FrameRate to " << new_framerate << " for camera " << camindex;
if (valid) { if (valid) {
if (camindex == -1) { if (camindex == -1) {
frameRate(new_framerate, 0); frameRate(new_framerate, 0);
@ -72,6 +73,7 @@ bool DualcamWrapper::frameRate(uint new_framerate, int camindex) {
double DualcamWrapper::frameRate(int camindex) { double DualcamWrapper::frameRate(int camindex) {
qDebug() << "Reading FrameRate from camera " << camindex;
assert(camindex >= 0 && camindex < 2); assert(camindex >= 0 && camindex < 2);
double rate = -1.; double rate = -1.;
if (valid) { if (valid) {
@ -85,6 +87,7 @@ double DualcamWrapper::frameRate(int camindex) {
double DualcamWrapper::exposureTime(int camindex) { double DualcamWrapper::exposureTime(int camindex) {
qDebug() << "Reading ExposureTime from camera " << camindex;
assert(camindex > 0 && camindex < 2); assert(camindex > 0 && camindex < 2);
double time = -1.; double time = -1.;
if (valid) { if (valid) {
@ -98,6 +101,7 @@ double DualcamWrapper::exposureTime(int camindex) {
bool DualcamWrapper::exposureTime(double exposure_time, int camindex) { bool DualcamWrapper::exposureTime(double exposure_time, int camindex) {
qDebug() << "Setting exposure time to " << exposure_time << " for camera " << camindex;
if (valid) { if (valid) {
if (camindex == -1) { if (camindex == -1) {
exposureTime(exposure_time, 0); exposureTime(exposure_time, 0);
@ -110,6 +114,7 @@ bool DualcamWrapper::exposureTime(double exposure_time, int camindex) {
exp_time.SetValue( exposure_time ); exp_time.SetValue( exposure_time );
GenApi::CEnumerationPtr(nodemap.GetNode( "ExposureTimeMode" ))->FromString("Timed");; GenApi::CEnumerationPtr(nodemap.GetNode( "ExposureTimeMode" ))->FromString("Timed");;
} catch (...) { } catch (...) {
qWarning() << "Could not set exposure for cam " << camindex; qWarning() << "Could not set exposure for cam " << camindex;
} }
} }
@ -142,6 +147,7 @@ uint32_t DualcamWrapper::sensorHeight(int camindex) {
} }
double DualcamWrapper::gain(int camindex) { double DualcamWrapper::gain(int camindex) {
qDebug() << "Reading Gain from camera " << camindex;
assert(camindex >= 0 && camindex < 2); assert(camindex >= 0 && camindex < 2);
double gain = -1.; double gain = -1.;
if (valid) { if (valid) {
@ -298,23 +304,21 @@ GenApi::INodeMap& DualcamWrapper::getNodemap(int camindex){
} }
void DualcamWrapper::enableSoftwareTrigger(int camindex){ void DualcamWrapper::enableSoftwareTrigger(int camindex){
qDebug() << "Enabling software trigger for camera " << camindex;
GenApi::INodeMap &nodemap = getNodemap( camindex ); GenApi::INodeMap &nodemap = getNodemap( camindex );
// Enable triggered image acquisition for the Frame Start trigger
Pylon::CEnumParameter(nodemap, "TriggerMode").SetValue("On"); Pylon::CEnumParameter(nodemap, "TriggerMode").SetValue("On");
// Set the trigger source to Line 1 Pylon::CEnumParameter(nodemap, "TriggerSource").SetValue("Software");
Pylon::CEnumParameter(nodemap, "TriggerSource").SetValue("Line1"); // Pylon::CEnumParameter(nodemap, "TriggerActivation").SetValue("LevelHigh");
// Set the trigger activation mode to level high
Pylon::CEnumParameter(nodemap, "TriggerActivation").SetValue("LevelHigh");
} }
void DualcamWrapper::disableSoftwareTrigger(int camindex){ void DualcamWrapper::disableSoftwareTrigger(int camindex){
qDebug() << "Disabling software trigger for camera " << camindex;
GenApi::INodeMap& nodemap = getNodemap(camindex); GenApi::INodeMap& nodemap = getNodemap(camindex);
// Disable triggered image acquisition for the Frame Start trigger
Pylon::CEnumParameter(nodemap, "TriggerMode").SetValue("Off"); Pylon::CEnumParameter(nodemap, "TriggerMode").SetValue("Off");
} }
bool DualcamWrapper::softwareTriggerEnabeled(int camindex){ bool DualcamWrapper::softwareTriggerEnabeled(int camindex){
qDebug() << "Checking software trigger for camera " << camindex;
GenApi::INodeMap& nodemap = getNodemap(camindex); GenApi::INodeMap& nodemap = getNodemap(camindex);
// Get the current state.
return Pylon::CEnumParameter(nodemap, "TriggerMode").GetValue() == "On"; return Pylon::CEnumParameter(nodemap, "TriggerMode").GetValue() == "On";
} }