Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpPylonGrabberGigE.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description: Implementation of vpPylonGrabberGigE class.
31 *
32 * Authors:
33 * Wenfeng CAI
34 */
35
41
42#include "vpPylonGrabberGigE.h"
43
44#ifdef VISP_HAVE_PYLON
45
46#include <visp3/core/vpException.h>
47#include <visp3/core/vpTime.h>
48
54 vpPylonGrabberGigE::vpPylonGrabberGigE() : m_camera(), m_index(0), m_numCameras(0), m_connected(false)
55{
57}
58
63
68{
69 Pylon::CTlFactory &TlFactory = Pylon::CTlFactory::GetInstance();
70 Pylon::DeviceInfoList_t lstDevices;
71 Pylon::DeviceInfoList_t filter; // Filter for GigE cameras.
72 Pylon::CBaslerGigEDeviceInfo gige_devinfo;
73 filter.push_back(gige_devinfo);
74 TlFactory.EnumerateDevices(lstDevices, filter);
75
76 m_numCameras = lstDevices.size();
77 return m_numCameras;
78}
79
85std::ostream &vpPylonGrabberGigE::getCameraInfo(std::ostream &os)
86{
87 connect();
88
89 Pylon::CDeviceInfo deviceInfo = m_camera.GetDeviceInfo();
90 // Get the camera control object.
91 GenApi::INodeMap &control = m_camera.GetNodeMap();
92
93 GenApi::CIntegerPtr widthMax = control.GetNode("WidthMax");
94 GenApi::CIntegerPtr heightMax = control.GetNode("HeightMax");
95
96 os << "Camera information: " << std::endl;
97 os << " Serial number : " << deviceInfo.GetSerialNumber() << std::endl;
98 os << " Camera model : " << deviceInfo.GetModelName() << std::endl;
99 os << " Camera vendor : " << deviceInfo.GetVendorName() << std::endl;
100 os << " Resolution : " << widthMax->GetValue() << "x" << heightMax->GetValue() << std::endl;
101 os << " Firmware version : " << deviceInfo.GetDeviceVersion() << std::endl;
102
103 return os;
104}
105
113{
114 connect();
115
116 if (m_connected == true) {
117 return &m_camera;
118 }
119 else {
120 return nullptr;
121 }
122}
123
131{
132 connect();
133
134 float frame_rate = m_camera.AcquisitionFrameRateAbs.GetValue();
135 return frame_rate;
136}
137
145{
146 connect();
147
148 if (GenApi::IsReadable(m_camera.GainAbs))
149 return m_camera.GainAbs.GetValue();
150 else if (GenApi::IsReadable(m_camera.GainRaw))
151 return m_camera.GainRaw.GetValue();
152 else
153 throw vpException(vpException::notImplementedError, "Don't know how to get gain.");
154}
155
168{
169 connect();
170
171 if (GenApi::IsReadable(m_camera.BlackLevelAbs))
172 return m_camera.BlackLevelAbs.GetValue();
173 else if (GenApi::IsReadable(m_camera.BlackLevelRaw))
174 return m_camera.BlackLevelRaw.GetValue();
175 else
176 throw vpException(vpException::notImplementedError, "Don't know how to get blacklevel.");
177}
178
191{
192 connect();
193
194 if (GenApi::IsReadable(m_camera.ExposureTimeAbs))
195 return m_camera.ExposureTimeAbs.GetValue() * 0.001;
196 else if (GenApi::IsReadable(m_camera.ExposureTimeRaw))
197 return m_camera.ExposureTimeRaw.GetValue();
198 else
199 throw vpException(vpException::notImplementedError, "Don't know how to get exposure.");
200}
201
209{
210 connect();
211
212 float gamma = m_camera.Gamma.GetValue();
213 return gamma;
214}
215
222std::string vpPylonGrabberGigE::getCameraSerial(unsigned int index)
223{
225
226 if (index >= m_numCameras) {
227 throw(vpException(vpException::badValue, "The camera with index %u is not present. Only %d cameras connected.",
228 index, m_numCameras));
229 }
230
231 Pylon::CTlFactory &TlFactory = Pylon::CTlFactory::GetInstance();
232 Pylon::DeviceInfoList_t lstDevices; // List of connected cameras
233 Pylon::DeviceInfoList_t filter; // Filter for GigE cameras.
234 Pylon::CBaslerGigEDeviceInfo gige_devinfo;
235 filter.push_back(gige_devinfo);
236 TlFactory.EnumerateDevices(lstDevices, filter);
237
238 std::ostringstream os;
239 os << lstDevices[index].GetSerialNumber();
240 return os.str();
241}
242
253{
254 connect();
255
256 bool success = selectUserSet(user_set);
257
258 if (success) {
259 m_camera.UserSetLoad.Execute();
260 vpTime::wait(200); // How long you have to wait?
261 success = m_camera.UserSetLoad.IsDone();
262 }
263
264 return success;
265}
266
274{
275 connect();
276
277 Basler_GigECamera::UserSetDefaultSelectorEnums user_set = m_camera.UserSetDefaultSelector.GetValue();
278
279 switch (user_set) {
280 case Basler_GigECamera::UserSetDefaultSelector_Default:
281 return USERSET_DEFAULT;
282 break;
283 case Basler_GigECamera::UserSetDefaultSelector_UserSet1:
284 return USERSET_USERSET1;
285 break;
286 case Basler_GigECamera::UserSetDefaultSelector_UserSet2:
287 return USERSET_USERSET2;
288 break;
289 case Basler_GigECamera::UserSetDefaultSelector_UserSet3:
290 return USERSET_USERSET3;
291 break;
292 default:
293 return USERSET_UNKNOWN;
294 }
295}
296
314{
315 if (index >= m_numCameras) {
316 throw(vpException(vpException::badValue, "The camera with index %u is not present. Only %d cameras connected.",
317 index, m_numCameras));
318 }
319
320 m_index = index;
321}
322
329void vpPylonGrabberGigE::setCameraSerial(const std::string &serial)
330{
331 m_numCameras = getNumCameras();
332 for (unsigned int i = 0; i < m_numCameras; i++) {
333 if (getCameraSerial(i) == serial) {
334 m_index = i;
335 return;
336 }
337 }
338 throw(vpException(vpException::badValue, "The camera with serial id %s is not present.", serial.c_str()));
339}
340
348float vpPylonGrabberGigE::setFrameRate(float frame_rate)
349{
350 connect();
351
352 m_camera.AcquisitionFrameRateAbs.SetValue(frame_rate);
353
354 return m_camera.AcquisitionFrameRateAbs.GetValue();
355}
356
370float vpPylonGrabberGigE::setGain(bool gain_auto, float gain_value)
371{
372 connect();
373
374 if (gain_auto)
375 m_camera.GainAuto.SetValue(Basler_GigECamera::GainAuto_Continuous);
376 else
377 m_camera.GainAuto.SetValue(Basler_GigECamera::GainAuto_Off);
378
379 if (GenApi::IsWritable(m_camera.GainAbs)) {
380 m_camera.GainAbs.SetValue(gain_value);
381 return m_camera.GainAbs.GetValue();
382 }
383 else if (GenApi::IsWritable(m_camera.GainRaw)) {
384 m_camera.GainRaw.SetValue(gain_value);
385 return m_camera.GainRaw.GetValue();
386 }
387 else
388 throw vpException(vpException::notImplementedError, "Don't know how to set gain.");
389}
390
405float vpPylonGrabberGigE::setBlackLevel(float blacklevel_value)
406{
407 connect();
408
409 if (GenApi::IsWritable(m_camera.BlackLevelAbs)) {
410 m_camera.BlackLevelAbs.SetValue(blacklevel_value);
411 return m_camera.BlackLevelAbs.GetValue();
412 }
413 else if (GenApi::IsWritable(m_camera.BlackLevelRaw)) {
414 m_camera.BlackLevelRaw.SetValue(blacklevel_value);
415 return m_camera.BlackLevelRaw.GetValue();
416 }
417 else
418 throw vpException(vpException::notImplementedError, "Don't know how to set blacklevel.");
419}
420
438float vpPylonGrabberGigE::setExposure(bool exposure_on, bool exposure_auto, float exposure_value)
439{
440 connect();
441
442 if (exposure_on)
443 m_camera.ExposureMode.SetValue(Basler_GigECamera::ExposureMode_Timed);
444 else
445 m_camera.ExposureMode.SetValue(Basler_GigECamera::ExposureMode_Off);
446
447 if (exposure_auto)
448 m_camera.ExposureAuto.SetValue(Basler_GigECamera::ExposureAuto_Continuous);
449 else
450 m_camera.ExposureAuto.SetValue(Basler_GigECamera::ExposureAuto_Off);
451
452 if (GenApi::IsWritable(m_camera.ExposureTimeAbs)) {
453 m_camera.ExposureTimeAbs.SetValue(exposure_value * 1000);
454 return m_camera.ExposureTimeAbs.GetValue() * 0.001;
455 }
456 else if (GenApi::IsWritable(m_camera.ExposureTimeRaw)) {
457 m_camera.ExposureTimeRaw.SetValue(exposure_value);
458 return m_camera.ExposureTimeRaw.GetValue();
459 }
460 else
461 throw vpException(vpException::notImplementedError, "Don't know how to set exposure.");
462}
463
476float vpPylonGrabberGigE::setGamma(bool gamma_on, float gamma_value)
477{
478 connect();
479
480 if (GenApi::IsWritable(m_camera.GammaEnable))
481 m_camera.GammaEnable.SetValue(gamma_on);
482
483 if (GenApi::IsWritable(m_camera.Gamma)) {
484 m_camera.Gamma.SetValue(gamma_value);
485 return m_camera.Gamma.GetValue();
486 }
487 else
488 throw vpException(vpException::notImplementedError, "Don't know how to set gamma.");
489}
490
502bool vpPylonGrabberGigE::saveUserSet(UserSetName user_set, bool set_default)
503{
504 connect();
505
506 bool success = selectUserSet(user_set);
507
508 if (success) {
509 m_camera.UserSetSave.Execute();
510 vpTime::wait(200); // How long you have to wait?
511 success = m_camera.UserSetSave.IsDone();
512 }
513
514 if (success && set_default)
515 success = setUserSetDefault(user_set);
516
517 return success;
518}
519
530{
531 connect();
532
533 switch (user_set) {
534 case USERSET_DEFAULT:
535 m_camera.UserSetDefaultSelector.SetValue(Basler_GigECamera::UserSetDefaultSelector_Default);
536 return true;
537 break;
538 case USERSET_USERSET1:
539 m_camera.UserSetDefaultSelector.SetValue(Basler_GigECamera::UserSetDefaultSelector_UserSet1);
540 return true;
541 break;
542 case USERSET_USERSET2:
543 m_camera.UserSetDefaultSelector.SetValue(Basler_GigECamera::UserSetDefaultSelector_UserSet2);
544 return true;
545 break;
546 case USERSET_USERSET3:
547 m_camera.UserSetDefaultSelector.SetValue(Basler_GigECamera::UserSetDefaultSelector_UserSet3);
548 return true;
549 break;
550 default:
551 return false;
552 }
553}
554
561{
562 connect();
563
564 if (!m_camera.IsGrabbing()) {
565 m_camera.StartGrabbing(1);
566 }
567 if (m_connected && m_camera.IsGrabbing())
568 init = true;
569 else
570 init = false;
571}
572
579{
580 if (m_camera.IsGrabbing()) {
581 m_camera.StopGrabbing();
582 }
583 if (m_connected && m_camera.IsGrabbing())
584 init = true;
585 else
586 init = false;
587}
588
595{
596 if (m_connected == false) {
597 m_numCameras = getNumCameras();
598 if (m_numCameras == 0) {
599 throw(vpException(vpException::fatalError, "No camera found"));
600 }
601
602 if (!m_camera.IsPylonDeviceAttached()) {
603 Pylon::CTlFactory &TlFactory = Pylon::CTlFactory::GetInstance();
604 Pylon::DeviceInfoList_t lstDevices;
605 Pylon::DeviceInfoList_t filter; // Filter for GigE cameras.
606 Pylon::CBaslerGigEDeviceInfo gige_devinfo;
607 filter.push_back(gige_devinfo);
608 TlFactory.EnumerateDevices(lstDevices, filter);
609
610 m_camera.Attach(TlFactory.CreateDevice(lstDevices[m_index]));
611 }
612 // Connect to a camera
613 m_camera.Open();
614 m_connected = true;
615 }
616 if (m_connected && m_camera.IsGrabbing())
617 init = true;
618 else
619 init = false;
620}
621
628{
629 if (m_connected == true) {
630 m_camera.Close();
631 m_connected = false;
632 }
633 if (m_connected && m_camera.IsGrabbing())
634 init = true;
635 else
636 init = false;
637}
638
648{
649 stopCapture();
650 disconnect();
651}
652
659{
660 open();
661
662 Pylon::CGrabResultPtr grabResult;
663 // Retrieve an image
664 if (!m_camera.RetrieveResult(2000, grabResult)) {
665 throw(vpException(vpException::fatalError, "Cannot retrieve image from camera with serial %s",
666 getCameraSerial(m_index).c_str()));
667 }
668
669 if (grabResult->GrabSucceeded()) {
670 height = grabResult->GetHeight();
671 width = grabResult->GetWidth();
672 I.resize(height, width);
673
674 Pylon::CImageFormatConverter imageConvert;
675 imageConvert.OutputPixelFormat = Pylon::PixelType_Mono8;
676 imageConvert.OutputPaddingX = 0;
677 // Create a converted image
678 imageConvert.Convert(I.bitmap, sizeof(unsigned char) * width * height, (Pylon::IImage &)grabResult);
679 }
680}
681
688{
689 open();
690
691 Pylon::CGrabResultPtr grabResult;
692 // Retrieve an image
693 if (!m_camera.RetrieveResult(2000, grabResult)) {
694 throw(vpException(vpException::fatalError, "Cannot retrieve image from camera with serial %s",
695 getCameraSerial(m_index).c_str()));
696 }
697
698 if (grabResult->GrabSucceeded()) {
699 height = grabResult->GetHeight();
700 width = grabResult->GetWidth();
701 I.resize(height, width);
702
703 Pylon::CImageFormatConverter imageConvert;
704 imageConvert.OutputPixelFormat = Pylon::PixelType_BGRA8packed;
705 imageConvert.OutputPaddingX = 0;
706 // Create a converted image
707 Pylon::CPylonImage destImage;
708 imageConvert.Convert(destImage, (Pylon::IImage &)grabResult);
709 Pylon::SBGRA8Pixel *pixel = (Pylon::SBGRA8Pixel *)destImage.GetBuffer();
710 for (unsigned int i = 0; i < height; i++) {
711 for (unsigned int j = 0; j < width; j++) {
712 unsigned int p_index = i * width + j;
713 I[i][j].R = pixel[p_index].R;
714 I[i][j].G = pixel[p_index].G;
715 I[i][j].B = pixel[p_index].B;
716 I[i][j].A = pixel[p_index].A;
717 }
718 }
719 }
720}
721
731
737{
738 open();
739 acquire(I);
740}
741
754{
755 connect();
756 startCapture();
757}
758
768{
769 connect();
770
771 switch (user_set) {
772 case USERSET_DEFAULT:
773 m_camera.UserSetSelector.SetValue(Basler_GigECamera::UserSetSelector_Default);
774 return true;
775 break;
776 case USERSET_USERSET1:
777 m_camera.UserSetSelector.SetValue(Basler_GigECamera::UserSetSelector_UserSet1);
778 return true;
779 break;
780 case USERSET_USERSET2:
781 m_camera.UserSetSelector.SetValue(Basler_GigECamera::UserSetSelector_UserSet2);
782 return true;
783 break;
784 case USERSET_USERSET3:
785 m_camera.UserSetSelector.SetValue(Basler_GigECamera::UserSetSelector_UserSet3);
786 return true;
787 break;
788 default:
789 return false;
790 }
791}
792
802
812END_VISP_NAMESPACE
813#else
814// Work around to avoid warning:
815// libvisp_pylon.a(vpPylonGrabberGigE.cpp.o) has no symbols
816void dummy_vpPylonGrabberGigE() { }
817#endif // #ifdef VISP_HAVE_PYLON
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
@ notImplementedError
Not implemented.
Definition vpException.h:69
@ fatalError
Fatal error.
Definition vpException.h:72
unsigned int height
Number of rows in the image.
bool init
Set to true if the frame grabber has been initialized.
unsigned int width
Number of columns in the image.
Definition of the vpImage class member functions.
Definition vpImage.h:131
void close()
Stop active camera capturing images and disconnect the active camera.
void setCameraIndex(unsigned int index)
bool loadUserSet(UserSetName user_set)
Loads the selected configuration into the camera's volatile memory and makes it the active configurat...
float setBlackLevel(float blacklevel_value=0)
std::string getCameraSerial(unsigned int index)
bool setUserSetDefault(UserSetName user_set)
Sets the configuration set to be used as the default startup set.
Pylon::CInstantCamera * getCameraHandler()
void acquire(vpImage< unsigned char > &I)
unsigned int getNumCameras()
float setExposure(bool exposure_on, bool exposure_auto, float exposure_value=0)
void setCameraSerial(const std::string &serial)
UserSetName getUserSetDefault()
Gets the configuration set being used as the default startup set.
float setGamma(bool gamma_on, float gamma_value=1)
float setFrameRate(float frame_rate)
bool selectUserSet(UserSetName user_set)
Selects the configuration set to load, save, or configure.
vpPylonGrabber & operator>>(vpImage< unsigned char > &I)
std::ostream & getCameraInfo(std::ostream &os)
float setGain(bool gain_auto, float gain_value=0)
bool saveUserSet(UserSetName user_set, bool set_default=false)
Saves the current active configuration set into the selected user set.
@ USERSET_DEFAULT
The default user set.
@ USERSET_UNKNOWN
User set not supported.
@ USERSET_USERSET1
User set 1.
@ USERSET_USERSET3
User set 3.
@ USERSET_USERSET2
User set 2.
VISP_EXPORT int wait(double t0, double t)