Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpPylonGrabberUsb.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 vpPylonGrabberUsb class.
31 *
32 * Authors:
33 * Wenfeng CAI
34 */
35
41
42#include "vpPylonGrabberUsb.h"
43
44#ifdef VISP_HAVE_PYLON
45
46#include <visp3/core/vpException.h>
47#include <visp3/core/vpTime.h>
48
54 vpPylonGrabberUsb::vpPylonGrabberUsb() : 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 USB cameras.
72 Pylon::CBaslerUsbDeviceInfo usb_devinfo;
73 filter.push_back(usb_devinfo);
74 TlFactory.EnumerateDevices(lstDevices, filter);
75
76 m_numCameras = lstDevices.size();
77 return m_numCameras;
78}
79
85std::ostream &vpPylonGrabberUsb::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.AcquisitionFrameRate.GetValue();
135 return frame_rate;
136}
137
145{
146 connect();
147
148 if (GenApi::IsReadable(m_camera.Gain))
149 return m_camera.Gain.GetValue();
150 else
151 throw vpException(vpException::notImplementedError, "Don't know how to get gain.");
152}
153
166{
167 connect();
168
169 if (GenApi::IsReadable(m_camera.BlackLevel))
170 return m_camera.BlackLevel.GetValue();
171 else
172 throw vpException(vpException::notImplementedError, "Don't know how to get blacklevel.");
173}
174
187{
188 connect();
189
190 if (GenApi::IsReadable(m_camera.ExposureTime))
191 return m_camera.ExposureTime.GetValue() * 0.001;
192 else
193 throw vpException(vpException::notImplementedError, "Don't know how to get exposure.");
194}
195
203{
204 connect();
205
206 float gamma = m_camera.Gamma.GetValue();
207 return gamma;
208}
209
216std::string vpPylonGrabberUsb::getCameraSerial(unsigned int index)
217{
219
220 if (index >= m_numCameras) {
221 throw(vpException(vpException::badValue, "The camera with index %u is not present. Only %d cameras connected.",
222 index, m_numCameras));
223 }
224
225 Pylon::CTlFactory &TlFactory = Pylon::CTlFactory::GetInstance();
226 Pylon::DeviceInfoList_t lstDevices; // List of connected cameras
227 Pylon::DeviceInfoList_t filter; // Filter for USB cameras.
228 Pylon::CBaslerUsbDeviceInfo usb_devinfo;
229 filter.push_back(usb_devinfo);
230 TlFactory.EnumerateDevices(lstDevices, filter);
231
232 std::ostringstream os;
233 os << lstDevices[index].GetSerialNumber();
234 return os.str();
235}
236
247{
248 connect();
249
250 bool success = selectUserSet(user_set);
251
252 if (success) {
253 m_camera.UserSetLoad.Execute();
254 vpTime::wait(200); // How long you have to wait?
255 success = m_camera.UserSetLoad.IsDone();
256 }
257
258 return success;
259}
260
268{
269 connect();
270
271 Basler_UsbCameraParams::UserSetDefaultEnums user_set = m_camera.UserSetDefault.GetValue();
272
273 switch (user_set) {
274 case Basler_UsbCameraParams::UserSetDefault_Default:
275 return USERSET_DEFAULT;
276 break;
277 case Basler_UsbCameraParams::UserSetDefault_UserSet1:
278 return USERSET_USERSET1;
279 break;
280 case Basler_UsbCameraParams::UserSetDefault_UserSet2:
281 return USERSET_USERSET2;
282 break;
283 case Basler_UsbCameraParams::UserSetDefault_UserSet3:
284 return USERSET_USERSET3;
285 break;
286 default:
287 return USERSET_UNKNOWN;
288 }
289}
290
307void vpPylonGrabberUsb::setCameraIndex(unsigned int index)
308{
309 if (index >= m_numCameras) {
310 throw(vpException(vpException::badValue, "The camera with index %u is not present. Only %d cameras connected.",
311 index, m_numCameras));
312 }
313
314 m_index = index;
315}
316
323void vpPylonGrabberUsb::setCameraSerial(const std::string &serial)
324{
325 m_numCameras = getNumCameras();
326 for (unsigned int i = 0; i < m_numCameras; i++) {
327 if (getCameraSerial(i) == serial) {
328 m_index = i;
329 return;
330 }
331 }
332 throw(vpException(vpException::badValue, "The camera with serial id %s is not present.", serial.c_str()));
333}
334
342float vpPylonGrabberUsb::setFrameRate(float frame_rate)
343{
344 connect();
345
346 m_camera.AcquisitionFrameRate.SetValue(frame_rate);
347
348 return m_camera.AcquisitionFrameRate.GetValue();
349}
350
364float vpPylonGrabberUsb::setGain(bool gain_auto, float gain_value)
365{
366 connect();
367
368 if (gain_auto)
369 m_camera.GainAuto.SetValue(Basler_UsbCameraParams::GainAuto_Continuous);
370 else
371 m_camera.GainAuto.SetValue(Basler_UsbCameraParams::GainAuto_Off);
372
373 if (GenApi::IsWritable(m_camera.Gain)) {
374 m_camera.Gain.SetValue(gain_value);
375 return m_camera.Gain.GetValue();
376 }
377 else
378 throw vpException(vpException::notImplementedError, "Don't know how to set gain.");
379}
380
395float vpPylonGrabberUsb::setBlackLevel(float blacklevel_value)
396{
397 connect();
398
399 if (GenApi::IsWritable(m_camera.BlackLevel)) {
400 m_camera.BlackLevel.SetValue(blacklevel_value);
401 return m_camera.BlackLevel.GetValue();
402 }
403 else
404 throw vpException(vpException::notImplementedError, "Don't know how to set blacklevel.");
405}
406
424float vpPylonGrabberUsb::setExposure(bool exposure_on, bool exposure_auto, float exposure_value)
425{
426 connect();
427
428 if (exposure_on)
429 m_camera.ExposureMode.SetValue(Basler_UsbCameraParams::ExposureMode_Timed);
430 else
431 m_camera.ExposureMode.SetValue(Basler_UsbCameraParams::ExposureMode_TriggerWidth);
432
433 if (exposure_auto)
434 m_camera.ExposureAuto.SetValue(Basler_UsbCameraParams::ExposureAuto_Continuous);
435 else
436 m_camera.ExposureAuto.SetValue(Basler_UsbCameraParams::ExposureAuto_Off);
437
438 if (GenApi::IsWritable(m_camera.ExposureTime)) {
439 m_camera.ExposureTime.SetValue(exposure_value * 1000);
440 return m_camera.ExposureTime.GetValue() * 0.001;
441 }
442 else
443 throw vpException(vpException::notImplementedError, "Don't know how to set exposure.");
444}
445
458float vpPylonGrabberUsb::setGamma(bool gamma_on, float gamma_value)
459{
460 connect();
461
462 if (GenApi::IsWritable(m_camera.Gamma)) {
463 if (gamma_on)
464 m_camera.Gamma.SetValue(gamma_value);
465 else
466 m_camera.Gamma.SetValue(1);
467 return m_camera.Gamma.GetValue();
468 }
469 else
470 throw vpException(vpException::notImplementedError, "Don't know how to set gamma.");
471}
472
484bool vpPylonGrabberUsb::saveUserSet(UserSetName user_set, bool set_default)
485{
486 connect();
487
488 bool success = selectUserSet(user_set);
489
490 if (success) {
491 m_camera.UserSetSave.Execute();
492 vpTime::wait(200); // How long you have to wait?
493 success = m_camera.UserSetSave.IsDone();
494 }
495
496 if (success && set_default)
497 success = setUserSetDefault(user_set);
498
499 return success;
500}
501
512{
513 connect();
514
515 switch (user_set) {
516 case USERSET_DEFAULT:
517 m_camera.UserSetDefault.SetValue(Basler_UsbCameraParams::UserSetDefault_Default);
518 return true;
519 break;
520 case USERSET_USERSET1:
521 m_camera.UserSetDefault.SetValue(Basler_UsbCameraParams::UserSetDefault_UserSet1);
522 return true;
523 break;
524 case USERSET_USERSET2:
525 m_camera.UserSetDefault.SetValue(Basler_UsbCameraParams::UserSetDefault_UserSet2);
526 return true;
527 break;
528 case USERSET_USERSET3:
529 m_camera.UserSetDefault.SetValue(Basler_UsbCameraParams::UserSetDefault_UserSet3);
530 return true;
531 break;
532 default:
533 return false;
534 }
535}
536
543{
544 connect();
545
546 if (!m_camera.IsGrabbing()) {
547 m_camera.StartGrabbing(1);
548 }
549 if (m_connected && m_camera.IsGrabbing())
550 init = true;
551 else
552 init = false;
553}
554
561{
562 if (m_camera.IsGrabbing()) {
563 m_camera.StopGrabbing();
564 }
565 if (m_connected && m_camera.IsGrabbing())
566 init = true;
567 else
568 init = false;
569}
570
577{
578 if (m_connected == false) {
579 m_numCameras = getNumCameras();
580 if (m_numCameras == 0) {
581 throw(vpException(vpException::fatalError, "No camera found"));
582 }
583
584 if (!m_camera.IsPylonDeviceAttached()) {
585 Pylon::CTlFactory &TlFactory = Pylon::CTlFactory::GetInstance();
586 Pylon::DeviceInfoList_t lstDevices;
587 Pylon::DeviceInfoList_t filter; // Filter for USB cameras.
588 Pylon::CBaslerUsbDeviceInfo usb_devinfo;
589 filter.push_back(usb_devinfo);
590 TlFactory.EnumerateDevices(lstDevices, filter);
591
592 m_camera.Attach(TlFactory.CreateDevice(lstDevices[m_index]));
593 }
594 // Connect to a camera
595 m_camera.Open();
596 m_connected = true;
597 }
598 if (m_connected && m_camera.IsGrabbing())
599 init = true;
600 else
601 init = false;
602}
603
610{
611 if (m_connected == true) {
612 m_camera.Close();
613 m_connected = false;
614 }
615 if (m_connected && m_camera.IsGrabbing())
616 init = true;
617 else
618 init = false;
619}
620
630{
631 stopCapture();
632 disconnect();
633}
634
641{
642 open();
643
644 Pylon::CGrabResultPtr grabResult;
645 // Retrieve an image
646 if (!m_camera.RetrieveResult(2000, grabResult)) {
647 throw(vpException(vpException::fatalError, "Cannot retrieve image from camera with serial %s",
648 getCameraSerial(m_index).c_str()));
649 }
650
651 if (grabResult->GrabSucceeded()) {
652 height = grabResult->GetHeight();
653 width = grabResult->GetWidth();
654 I.resize(height, width);
655
656 Pylon::CImageFormatConverter imageConvert;
657 imageConvert.OutputPixelFormat = Pylon::PixelType_Mono8;
658 imageConvert.OutputPaddingX = 0;
659 // Create a converted image
660 imageConvert.Convert(I.bitmap, sizeof(unsigned char) * width * height, (Pylon::IImage &)grabResult);
661 }
662}
663
670{
671 open();
672
673 Pylon::CGrabResultPtr grabResult;
674 // Retrieve an image
675 if (!m_camera.RetrieveResult(2000, grabResult)) {
676 throw(vpException(vpException::fatalError, "Cannot retrieve image from camera with serial %s",
677 getCameraSerial(m_index).c_str()));
678 }
679
680 if (grabResult->GrabSucceeded()) {
681 height = grabResult->GetHeight();
682 width = grabResult->GetWidth();
683 I.resize(height, width);
684
685 Pylon::CImageFormatConverter imageConvert;
686 imageConvert.OutputPixelFormat = Pylon::PixelType_BGRA8packed;
687 imageConvert.OutputPaddingX = 0;
688 // Create a converted image
689 Pylon::CPylonImage destImage;
690 imageConvert.Convert(destImage, (Pylon::IImage &)grabResult);
691 Pylon::SBGRA8Pixel *pixel = (Pylon::SBGRA8Pixel *)destImage.GetBuffer();
692 for (unsigned int i = 0; i < height; i++) {
693 for (unsigned int j = 0; j < width; j++) {
694 unsigned int p_index = i * width + j;
695 I[i][j].R = pixel[p_index].R;
696 I[i][j].G = pixel[p_index].G;
697 I[i][j].B = pixel[p_index].B;
698 I[i][j].A = pixel[p_index].A;
699 }
700 }
701 }
702}
703
713
719{
720 open();
721 acquire(I);
722}
723
736{
737 connect();
738 startCapture();
739}
740
750{
751 connect();
752
753 switch (user_set) {
754 case USERSET_DEFAULT:
755 m_camera.UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_Default);
756 return true;
757 break;
758 case USERSET_USERSET1:
759 m_camera.UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet1);
760 return true;
761 break;
762 case USERSET_USERSET2:
763 m_camera.UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet2);
764 return true;
765 break;
766 case USERSET_USERSET3:
767 m_camera.UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet3);
768 return true;
769 break;
770 default:
771 return false;
772 }
773}
774
784
794END_VISP_NAMESPACE
795#else
796// Work around to avoid warning:
797// libvisp_pylon.a(vpPylonGrabberUsb.cpp.o) has no symbols
798void dummy_vpPylonGrabberUsb() { }
799#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
bool loadUserSet(UserSetName user_set)
Loads the selected configuration into the camera's volatile memory and makes it the active configurat...
vpPylonGrabber & operator>>(vpImage< unsigned char > &I)
unsigned int getNumCameras()
float setBlackLevel(float blacklevel_value=0)
Pylon::CInstantCamera * getCameraHandler()
std::ostream & getCameraInfo(std::ostream &os)
float setFrameRate(float frame_rate)
float setGamma(bool gamma_on, float gamma_value=1)
bool setUserSetDefault(UserSetName user_set)
Sets the configuration set to be used as the default startup set.
UserSetName getUserSetDefault()
Gets the configuration set being used as the default startup set.
void close()
Stop active camera capturing images and disconnect the active camera.
bool selectUserSet(UserSetName user_set)
Selects the configuration set to load, save, or configure.
void setCameraIndex(unsigned int index)
bool saveUserSet(UserSetName user_set, bool set_default=false)
Saves the current active configuration set into the selected user set.
std::string getCameraSerial(unsigned int index)
void acquire(vpImage< unsigned char > &I)
float setExposure(bool exposure_on, bool exposure_auto, float exposure_value=0)
void setCameraSerial(const std::string &serial)
float setGain(bool gain_auto, float gain_value=0)
@ 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)