-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBaslerCamera.cpp
More file actions
executable file
·2019 lines (1849 loc) · 61.3 KB
/
BaslerCamera.cpp
File metadata and controls
executable file
·2019 lines (1849 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2022
// European Synchrotron Radiation Facility
// CS40220 38043 Grenoble Cedex 9
// FRANCE
//
// Contact: lima@esrf.fr
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
#ifdef WIN32
#include <winsock2.h>
#endif
#include <sstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include "BaslerCamera.h"
#include "BaslerVideoCtrlObj.h"
using namespace lima;
using namespace lima::Basler;
using namespace std;
#if !defined(WIN32)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define min(A,B) std::min(A,B)
#define max(A,B) std::max(A,B)
#endif
const static std::string IP_PREFIX = "ip://";
const static std::string SN_PREFIX = "sn://";
const static std::string UNAME_PREFIX = "uname://";
//---------------------------
//- utility function
//---------------------------
static inline const char* _get_ip_addresse(const char *name_ip)
{
if(inet_addr(name_ip) != INADDR_NONE)
return name_ip;
else
{
struct hostent *host = gethostbyname(name_ip);
if(!host)
{
char buffer[256];
snprintf(buffer,sizeof(buffer),"Can not found ip for host %s ",name_ip);
throw LIMA_HW_EXC(Error,buffer);
}
return inet_ntoa(*((struct in_addr*)host->h_addr));
}
}
//---------------------------
//- EventHandler
//---------------------------
class Camera::_EventHandler : public CBaslerUniversalCameraEventHandler,
public CBaslerUniversalImageEventHandler
{
DEB_CLASS_NAMESPC(DebModCamera, "Camera", "_EventHandler");
public:
_EventHandler(Camera &aCam) :
m_cam(aCam), m_buffer_mgr(m_cam.m_buffer_ctrl_obj.getBuffer())
{
};
virtual void OnImageGrabbed(CBaslerUniversalInstantCamera &camera,
const CBaslerUniversalGrabResultPtr &grabResult);
unsigned short m_block_id;
private:
void _check_missing_frame(const CBaslerUniversalGrabResultPtr &ptrGrabResult);
Camera& m_cam;
StdBufferCbMgr& m_buffer_mgr;
};
//---------------------------
//- Ctor
//---------------------------
Camera::Camera(const std::string& camera_id,int packet_size,int receive_priority)
: m_nb_frames(1),
m_status(Ready),
m_image_number(0),
m_exp_time(1.),
m_latency_time(0.),
m_socketBufferSize(0),
m_is_usb(false),
m_blank_image_for_missed(false),
Camera_(NULL),
m_event_handler(NULL),
m_receive_priority(receive_priority),
m_video_flag_mode(false),
m_video(NULL)
{
DEB_CONSTRUCTOR();
m_camera_id = camera_id;
try
{
// Create the transport layer object needed to enumerate or
// create a camera object of type Camera_t::DeviceClass()
DEB_TRACE() << "Create a camera object of type Camera_t::DeviceClass()";
CTlFactory& TlFactory = CTlFactory::GetInstance();
CDeviceInfo di;
// by default use ip:// scheme if none is given
if (m_camera_id.find("://") == std::string::npos)
{
m_camera_id = "ip://" + m_camera_id;
}
if(!m_camera_id.compare(0, IP_PREFIX.size(), IP_PREFIX))
{
// m_camera_id is not really necessarily an IP, it may also be a DNS name
Pylon::String_t pylon_camera_ip(_get_ip_addresse(m_camera_id.substr(IP_PREFIX.size()).c_str()));
//- Find the Pylon device thanks to its IP Address
di.SetIpAddress( pylon_camera_ip);
DEB_TRACE() << "Create the Pylon device attached to ip address: "
<< DEB_VAR1(m_camera_id);
}
else if (!m_camera_id.compare(0, SN_PREFIX.size(), SN_PREFIX))
{
Pylon::String_t serial_number(m_camera_id.substr(SN_PREFIX.size()).c_str());
//- Find the Pylon device thanks to its serial number
di.SetSerialNumber(serial_number);
DEB_TRACE() << "Create the Pylon device attached to serial number: "
<< DEB_VAR1(m_camera_id);
}
else if(!m_camera_id.compare(0, UNAME_PREFIX.size(), UNAME_PREFIX))
{
Pylon::String_t user_name(m_camera_id.substr(UNAME_PREFIX.size()).c_str());
//- Find the Pylon device thanks to its user name
di.SetUserDefinedName(user_name);
DEB_TRACE() << "Create the Pylon device attached to user name: "
<< DEB_VAR1(m_camera_id);
}
else
{
THROW_CTL_ERROR(InvalidValue) << "Unrecognized camera id: " << camera_id;
}
IPylonDevice* device = CTlFactory::GetInstance().CreateFirstDevice(di);
if (!device)
{
THROW_HW_ERROR(Error) << "Unable to find camera with selected IP!";
}
//- Create the Basler Camera object
DEB_TRACE() << "Create the Camera object corresponding to the created Pylon device";
Camera_ = new Camera_t(device);
if(!Camera_)
{
THROW_HW_ERROR(Error) << "Unable to get the camera from transport_layer!";
}
//- Get detector model and type
m_detector_type = Camera_->GetDeviceInfo().GetVendorName();
m_detector_model = Camera_->GetDeviceInfo().GetModelName();
m_is_usb = Camera_->GetDeviceInfo().IsUsbDriverTypeAvailable();
//- Infos:
DEB_TRACE() << DEB_VAR2(m_detector_type,m_detector_model);
DEB_TRACE() << "SerialNumber = " << Camera_->GetDeviceInfo().GetSerialNumber();
DEB_TRACE() << "UserDefinedName = " << Camera_->GetDeviceInfo().GetUserDefinedName();
DEB_TRACE() << "DeviceVersion = " << Camera_->GetDeviceInfo().GetDeviceVersion();
DEB_TRACE() << "DeviceFactory = " << Camera_->GetDeviceInfo().GetDeviceFactory();
DEB_TRACE() << "FriendlyName = " << Camera_->GetDeviceInfo().GetFriendlyName();
DEB_TRACE() << "FullName = " << Camera_->GetDeviceInfo().GetFullName();
DEB_TRACE() << "DeviceClass = " << Camera_->GetDeviceInfo().GetDeviceClass();
// Register Event handler
m_event_handler = new _EventHandler(*this);
Camera_->RegisterImageEventHandler(m_event_handler,
RegistrationMode_ReplaceAll,
Cleanup_None);
// Camera event processing must be enabled first. The default is off.
Camera_->GrabCameraEvents = true;
// Open the camera
DEB_TRACE() << "Open camera";
Camera_->Open();
if(!Camera_->EventSelector.IsWritable())
THROW_HW_ERROR(Error) << "The device doesn't support events.";
if(packet_size > 0 && !m_is_usb) {
Camera_->GevSCPSPacketSize.SetValue(packet_size);
}
// Set the image format and AOI
DEB_TRACE() << "Set the image format and AOI";
// basler model string last character codes for color (c) or monochrome (m)
std::list<string> formatList;
if (m_detector_model.find("gc") != std::string::npos ||
m_detector_model.find("uc") != std::string::npos
)
{
// The list Order here has sense, if supported, the first format in the list will be applied
// as default one, and in case of color camera the default will defined the max buffer
// size for the memory allocation. Since YUV422Packed is 1.5 byte per pixel it is available
// before the Bayer 8bit (1 byte per pixel).
formatList.push_back(string("BayerRG16"));
formatList.push_back(string("BayerBG16"));
formatList.push_back(string("BayerRG12"));
formatList.push_back(string("BayerBG12"));
formatList.push_back(string("YUV422Packed"));
formatList.push_back(string("BayerRG8"));
formatList.push_back(string("BayerBG8"));
m_color_flag = true;
}
else
{
formatList.push_back(string("Mono16"));
formatList.push_back(string("Mono12"));
formatList.push_back(string("Mono10"));
formatList.push_back(string("Mono8"));
m_color_flag = false;
}
bool formatSetFlag = false;
for(list<string>::iterator it = formatList.begin(); it != formatList.end(); it++)
{
GenApi::IEnumEntry *anEntry = Camera_->PixelFormat.GetEntryByName((*it).c_str());
if(anEntry && GenApi::IsAvailable(anEntry))
{
formatSetFlag = true;
Camera_->PixelFormat.SetIntValue(anEntry->GetValue());
DEB_TRACE() << "Set pixel format to " << *it;
break;
}
}
if(!formatSetFlag)
THROW_HW_ERROR(Error) << "Unable to set PixelFormat for the camera!";
DEB_TRACE() << "Set the ROI to full frame";
if(isRoiAvailable())
{
Roi aFullFrame(0,0,Camera_->WidthMax(),Camera_->HeightMax());
setRoi(aFullFrame);
}
// Set Binning to 1, only if the camera has this functionality
if (isBinningAvailable())
{
DEB_TRACE() << "Set BinningH & BinningV to 1";
Camera_->BinningVertical.SetValue(1);
Camera_->BinningHorizontal.SetValue(1);
}
DEB_TRACE() << "Get the Detector Max Size";
m_detector_size = Size(Camera_->WidthMax(), Camera_->HeightMax());
// Set the camera to continuous frame mode
DEB_TRACE() << "Set the camera to continuous frame mode";
Camera_->AcquisitionMode.SetValue(AcquisitionMode_Continuous);
if ( IsAvailable(Camera_->ExposureAuto ))
{
DEB_TRACE() << "Set ExposureAuto to Off";
Camera_->ExposureAuto.SetValue(ExposureAuto_Off);
}
if (IsAvailable(Camera_->TestImageSelector ))
{
DEB_TRACE() << "Set TestImage to Off";
Camera_->TestImageSelector.SetValue(TestImageSelector_Off);
}
// Start with internal trigger
// Force cache variable (camera register) to get trigger really initialized at first call
m_trigger_mode = ExtTrigSingle;
setTrigMode(IntTrig);
// Same thing for exposure time, camera stays with previous setting
double min_exp, max_exp;
getExposureTimeRange(min_exp, max_exp);
//fast basler models do not support 1.0 second exposure but lower value
double exp_time = min(1.0, max_exp);
setExpTime(exp_time);
// Get the image buffer size
DEB_TRACE() << "Get the image buffer size";
ImageSize_ = (size_t)(Camera_->PayloadSize.GetValue());
}
catch (Pylon::GenericException &e)
{
DeviceInfoList_t list;
CTlFactory::GetInstance().EnumerateDevices(list);
if(!list.empty())
DEB_ALWAYS() << "Device founds:";
else
DEB_ALWAYS() << "No Camera found!";
for(auto dev: list)
{
DEB_ALWAYS() << "------------------------------------";
DEB_ALWAYS() << "SerialNumber = " << dev.GetSerialNumber();
DEB_ALWAYS() << "UserDefinedName = " << dev.GetUserDefinedName();
DEB_ALWAYS() << "DeviceVersion = " << dev.GetDeviceVersion();
DEB_ALWAYS() << "DeviceFactory = " << dev.GetDeviceFactory();
DEB_ALWAYS() << "FriendlyName = " << dev.GetFriendlyName();
DEB_ALWAYS() << "FullName = " << dev.GetFullName();
DEB_ALWAYS() << "DeviceClass = " << dev.GetDeviceClass();
DEB_ALWAYS() << "\n";
}
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
// if color camera video capability will be available
m_video_flag_mode = m_color_flag;
}
//---------------------------
//- Dtor
//---------------------------
Camera::~Camera()
{
DEB_DESTRUCTOR();
try
{
Camera_->DeregisterImageEventHandler(m_event_handler);
// Stop Acq thread
delete m_event_handler;
m_event_handler = NULL;
// Close camera
DEB_TRACE() << "Close camera";
delete Camera_;
Camera_ = NULL;
}
catch (Pylon::GenericException &e)
{
// Error handling
DEB_ERROR() << e.GetDescription();
}
}
void Camera::prepareAcq()
{
DEB_MEMBER_FUNCT();
m_image_number=0;
// new flag to better manage multiple acqStart() with trigger mode IntTrigMult
// startAcq can be recalled before the threadFunction has processed the new image and
// incremented the counter m_image_number
m_acq_started = false;
m_event_handler->m_block_id = 0; // reset block id counter
}
//---------------------------
//- Camera::start()
//---------------------------
void Camera::startAcq()
{
DEB_MEMBER_FUNCT();
try
{
_startAcq();
// start acquisition at first image
// code moved from prepareAcq(), otherwise with color camera
// CtVideo::_prepareAcq() which calls stopAcq() will kill the acquisition
if(m_trigger_mode == IntTrigMult)
this->Camera_->TriggerSoftware.Execute();
}
catch (GenICam::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
}
void Camera::_startAcq()
{
DEB_MEMBER_FUNCT();
if(!m_acq_started)
{
if(m_video)
m_video->getBuffer().setStartTimestamp(Timestamp::now());
else
m_buffer_ctrl_obj.getBuffer().setStartTimestamp(Timestamp::now());
if (m_nb_frames)
Camera_->StartGrabbing(m_nb_frames,GrabStrategy_OneByOne,GrabLoop_ProvidedByInstantCamera);
else
Camera_->StartGrabbing(GrabStrategy_OneByOne,GrabLoop_ProvidedByInstantCamera);
m_acq_started = true;
}
try
{
if(m_trigger_mode != IntTrig)
Camera_->WaitForFrameTriggerReady(1000, TimeoutHandling_ThrowException);
}
catch(GenICam::GenericException &e)
{
THROW_HW_ERROR(Error) << "Wait ready for trigger failed: "
<< e.GetDescription();
}
}
//---------------------------
//- Camera::stopAcq()
//---------------------------
void Camera::stopAcq()
{
_stopAcq(false);
}
//---------------------------
//- Camera::_stopAcq()
//---------------------------
void Camera::_stopAcq(bool internalFlag)
{
DEB_MEMBER_FUNCT();
try
{
// Stop acquisition
DEB_TRACE() << "Stop acquisition";
Camera_->StopGrabbing();
_setStatus(Camera::Ready,false);
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
}
void Camera::_forceVideoMode(bool force)
{
DEB_MEMBER_FUNCT();
m_video_flag_mode = force;
}
//---------------------------
//- Camera::_EventHandler::OnImageGrabbed()
//---------------------------
void Camera::_EventHandler::OnImageGrabbed(CBaslerUniversalInstantCamera &camera,
const CBaslerUniversalGrabResultPtr &ptrGrabResult)
{
DEB_MEMBER_FUNCT();
try
{
if(m_cam.m_video_flag_mode)
{
VideoMode mode;
m_cam.m_video->getVideoMode(mode);
if(ptrGrabResult->GrabSucceeded())
{
m_cam.m_video->callNewImage((char*)ptrGrabResult->GetBuffer(),
ptrGrabResult->GetWidth(),
ptrGrabResult->GetHeight(),
mode);
++m_cam.m_image_number;
}
}
else
{
if (ptrGrabResult->GrabSucceeded())
{
// Access the image data.
uint8_t* pImageBuffer = (uint8_t*) ptrGrabResult->GetBuffer();
_check_missing_frame(ptrGrabResult);
HwFrameInfoType frame_info;
frame_info.acq_frame_nb = m_cam.m_image_number;
void *framePt = m_buffer_mgr.getFrameBufferPtr(m_cam.m_image_number);
const FrameDim& fDim = m_buffer_mgr.getFrameDim();
void* srcPt = ((char*)pImageBuffer);
DEB_TRACE() << "memcpy:" << DEB_VAR2(srcPt,framePt);
memcpy(framePt,srcPt,fDim.getMemSize());
if(!m_buffer_mgr.newFrameReady(frame_info))
m_cam._stopAcq(true);
++m_cam.m_image_number;
}
}
}
catch (Pylon::GenericException &e)
{
// Error handling
DEB_ERROR() << "GeniCam Error! "<< e.GetDescription();
m_cam._setStatus(Camera::Fault, true);
}
}
void Camera::_EventHandler::_check_missing_frame(const CBaslerUniversalGrabResultPtr &ptrGrabResult)
{
DEB_MEMBER_FUNCT();
if(!m_cam.m_is_usb) // GigE Camera
{
auto block_id = ptrGrabResult->GetBlockID();
if(block_id) // 0 -> not available for this camera
{
++m_block_id;
if(!m_block_id) ++m_block_id; // overflow to 0
if(block_id != m_block_id) // missed a frame
{
DEB_WARNING() << "Missed frame expected : "
<< m_block_id
<< " get : "
<< block_id;
if(m_cam.m_blank_image_for_missed)
{
unsigned short missed_frames = block_id - m_block_id;
if(m_block_id > block_id) --missed_frames; // overflow
//missing frames are blank
for(int i = 0;i < missed_frames;++i)
{
void *framePt = m_buffer_mgr.getFrameBufferPtr(m_cam.m_image_number);
const FrameDim& fDim = m_buffer_mgr.getFrameDim();
memset(framePt,0,fDim.getMemSize());
HwFrameInfoType frame_info;
frame_info.acq_frame_nb = m_cam.m_image_number;
DEB_WARNING() << "Frame " << m_cam.m_image_number << " is blank";
if(!m_buffer_mgr.newFrameReady(frame_info))
{
m_cam._stopAcq(true);
break;
}
++m_cam.m_image_number;
}
}
m_block_id = block_id;
}
}
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getDetectorImageSize(Size& size)
{
DEB_MEMBER_FUNCT();
// get the max image size of the detector (the chip)
size = m_detector_size;
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getImageType(ImageType& type)
{
DEB_MEMBER_FUNCT();
PixelFormatEnums ps;
try
{
ps = Camera_->PixelFormat.GetValue();
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
switch( ps )
{
case PixelFormat_Mono8:
case PixelFormat_BayerRG8:
case PixelFormat_BayerBG8:
case PixelFormat_RGB8Packed:
case PixelFormat_BGR8Packed:
case PixelFormat_RGBA8Packed:
case PixelFormat_BGRA8Packed:
case PixelFormat_YUV411Packed:
case PixelFormat_YUV422Packed:
case PixelFormat_YUV444Packed:
type= Bpp8;
break;
case PixelFormat_Mono10:
case PixelFormat_BayerRG10:
case PixelFormat_BayerBG10:
type= Bpp10;
break;
case PixelFormat_Mono12:
case PixelFormat_BayerRG12:
case PixelFormat_BayerBG12:
type= Bpp12;
break;
case PixelFormat_Mono16: //- this is in fact 12 bpp inside a 16bpp image
case PixelFormat_BayerRG16:
case PixelFormat_BayerBG16:
type= Bpp16;
break;
default:
THROW_HW_ERROR(Error) << "Unsupported Pixel Format : " << ps;
break;
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::setImageType(ImageType type)
{
DEB_MEMBER_FUNCT();
try
{
switch( type )
{
case Bpp8:
this->Camera_->PixelFormat.SetValue(PixelFormat_Mono8);
break;
case Bpp10:
this->Camera_->PixelFormat.SetValue(PixelFormat_Mono10);
break;
case Bpp12:
this->Camera_->PixelFormat.SetValue(PixelFormat_Mono12);
break;
case Bpp16:
this->Camera_->PixelFormat.SetValue(PixelFormat_Mono16);
break;
default:
THROW_HW_ERROR(NotSupported) << "Cannot change the pixel format of the camera !";
break;
}
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getDetectorType(string& type)
{
DEB_MEMBER_FUNCT();
type = m_detector_type;
return;
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getDetectorModel(string& type)
{
DEB_MEMBER_FUNCT();
type = m_detector_model;
return;
}
//-----------------------------------------------------
//
//-----------------------------------------------------
HwBufferCtrlObj* Camera::getBufferCtrlObj()
{
return &m_buffer_ctrl_obj;
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::setTrigMode(TrigMode mode)
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(mode);
if(mode == m_trigger_mode)
return; // Nothing to do
try
{
GenApi::IEnumEntry *enumEntryFrameStart = Camera_->TriggerSelector.GetEntryByName("FrameStart");
if(enumEntryFrameStart && GenApi::IsAvailable(enumEntryFrameStart))
this->Camera_->TriggerSelector.SetValue( TriggerSelector_FrameStart );
else
this->Camera_->TriggerSelector.SetValue( TriggerSelector_AcquisitionStart );
if ( mode == IntTrig )
{
//- INTERNAL
this->Camera_->TriggerMode.SetValue( TriggerMode_Off );
this->Camera_->ExposureMode.SetValue(ExposureMode_Timed);
// setExposure() can disable FrameRate if latency_time is ~0,
// do not reenable FrameRate here if not required
// and when cold start the camera can have the FrameRate enabled
// from previous acquisition, so disable it if latency is 0
if (m_latency_time >= 1e-6)
this->Camera_->AcquisitionFrameRateEnable.SetValue(true);
else
this->Camera_->AcquisitionFrameRateEnable.SetValue(false);
}
else if ( mode == IntTrigMult )
{
this->Camera_->TriggerMode.SetValue(TriggerMode_On);
this->Camera_->TriggerSource.SetValue(TriggerSource_Software);
this->Camera_->AcquisitionFrameRateEnable.SetValue( false );
this->Camera_->ExposureMode.SetValue(ExposureMode_Timed);
}
else if ( mode == ExtGate )
{
//- EXTERNAL - TRIGGER WIDTH
this->Camera_->TriggerMode.SetValue( TriggerMode_On );
this->Camera_->TriggerSource.SetValue(TriggerSource_Line1);
this->Camera_->AcquisitionFrameRateEnable.SetValue( false );
this->Camera_->ExposureMode.SetValue( ExposureMode_TriggerWidth );
}
else //ExtTrigMult
{
this->Camera_->TriggerMode.SetValue( TriggerMode_On );
this->Camera_->TriggerSource.SetValue(TriggerSource_Line1);
this->Camera_->AcquisitionFrameRateEnable.SetValue( false );
this->Camera_->ExposureMode.SetValue( ExposureMode_Timed );
}
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
m_trigger_mode = mode;
}
void Camera::getTrigMode(TrigMode& mode)
{
DEB_MEMBER_FUNCT();
AutoMutex aLock(m_cond.mutex());
mode = m_trigger_mode;
DEB_RETURN() << DEB_VAR1(m_trigger_mode);
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::_readTrigMode()
{
DEB_MEMBER_FUNCT();
int frameStart = TriggerMode_Off, acqStart, expMode;
try
{
acqStart = this->Camera_->TriggerMode.GetValue();
GenApi::IEnumEntry *enumEntryFrameStart = Camera_->TriggerSelector.GetEntryByName("FrameStart");
if(enumEntryFrameStart && GenApi::IsAvailable(enumEntryFrameStart))
{
this->Camera_->TriggerSelector.SetValue( TriggerSelector_FrameStart );
frameStart = this->Camera_->TriggerMode.GetValue();
}
expMode = this->Camera_->ExposureMode.GetValue();
if(acqStart == TriggerMode_On)
{
int source = this->Camera_->TriggerSource.GetValue();
if(source == TriggerSource_Software)
m_trigger_mode = IntTrigMult;
else if(expMode == ExposureMode_TriggerWidth)
m_trigger_mode = ExtGate;
else
m_trigger_mode = ExtTrigMult;
}
else
m_trigger_mode = IntTrig;
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
DEB_RETURN() << DEB_VAR4(m_trigger_mode,acqStart, frameStart, expMode);
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::setTrigActivation(TrigActivation activation)
{
DEB_MEMBER_FUNCT();
try
{
TriggerActivationEnums act =
static_cast<TriggerActivationEnums>(activation);
// If the parameter TriggerActivation is available for this camera
if (GenApi::IsAvailable(Camera_->TriggerActivation))
Camera_->TriggerActivation.SetValue(act);
}
catch (Pylon::GenericException &e)
{
DEB_WARNING() << e.GetDescription();
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getTrigActivation(TrigActivation& activation) const
{
DEB_MEMBER_FUNCT();
try
{
TriggerActivationEnums act;
// If the parameter AcquisitionFrameCount is available for this camera
if (GenApi::IsAvailable(Camera_->TriggerActivation))
act = Camera_->TriggerActivation.GetValue();
activation = static_cast<TrigActivation>(act);
}
catch (Pylon::GenericException &e)
{
DEB_WARNING() << e.GetDescription();
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::setExpTime(double exp_time)
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(exp_time);
TrigMode mode;
getTrigMode(mode);
try
{
if(mode != ExtGate) { // the expTime can not be set in ExtGate!
// ExposureTimeBaseAbs is only available for GigE Ace camera
if (IsAvailable(Camera_->ExposureTimeBaseAbs))
{
//If scout or pilot, exposure time has to be adjusted using
// the exposure time base + the exposure time raw.
//see ImageGrabber for more details !!!
Camera_->ExposureTimeBaseAbs.SetValue(100.0); //- to be sure we can set the Raw setting on the full range (1 .. 4095)
double raw = ::ceil(exp_time / 50);
Camera_->ExposureTimeRaw.SetValue(static_cast<int> (raw));
raw = static_cast<double> (Camera_->ExposureTimeRaw.GetValue());
Camera_->ExposureTimeBaseAbs.SetValue(1E6 * (exp_time / raw));
DEB_TRACE() << "raw = " << raw;
DEB_TRACE() << "ExposureTimeBaseAbs = " << (1E6 * (exp_time / raw));
}
else
{
if (IsAvailable(Camera_->ExposureTime) || m_is_usb)
Camera_->ExposureTime.SetValue(1E6 * exp_time);
else
Camera_->ExposureTimeAbs.SetValue(1E6 * exp_time);
}
}
m_exp_time = exp_time;
// set the frame rate using expo time + latency
if (m_latency_time < 1e-6) // Max camera speed
{
Camera_->AcquisitionFrameRateEnable.SetValue(false);
}
else
{
double rate = 1/ (m_latency_time + m_exp_time);
Camera_->AcquisitionFrameRateEnable.SetValue(true);
DEB_TRACE() << DEB_VAR1(rate);
if (IsAvailable(Camera_->AcquisitionFrameRate) || m_is_usb)
{
double minrate = Camera_->AcquisitionFrameRate.GetMin();
double maxrate = Camera_->AcquisitionFrameRate.GetMax();
if (rate < minrate) rate = minrate;
if (rate > maxrate) rate = maxrate;
Camera_->AcquisitionFrameRate.SetValue(rate);
DEB_TRACE() << DEB_VAR1(Camera_->AcquisitionFrameRate.GetValue());
}
else
{
double minrate = Camera_->AcquisitionFrameRateAbs.GetMin();
double maxrate = Camera_->AcquisitionFrameRateAbs.GetMax();
if (rate < minrate) rate = minrate;
if (rate > maxrate) rate = maxrate;
Camera_->AcquisitionFrameRateAbs.SetValue(rate);
DEB_TRACE() << DEB_VAR1(Camera_->AcquisitionFrameRateAbs.GetValue());
}
}
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getExpTime(double& exp_time)
{
DEB_MEMBER_FUNCT();
try
{
double value = 1.0E-6 * static_cast<double>(Camera_->ExposureTime.GetValue());
exp_time = value;
}
catch (Pylon::GenericException &e)
{
// Error handling
THROW_HW_ERROR(Error) << e.GetDescription();
}
DEB_RETURN() << DEB_VAR1(exp_time);
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::setLatTime(double lat_time)
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(lat_time);
m_latency_time = lat_time;
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getLatTime(double& lat_time)
{
DEB_MEMBER_FUNCT();
lat_time = m_latency_time;
DEB_RETURN() << DEB_VAR1(lat_time);
}
//-----------------------------------------------------
//
//-----------------------------------------------------
void Camera::getExposureTimeRange(double& min_expo, double& max_expo) const
{
DEB_MEMBER_FUNCT();
try
{
// Pilot and and Scout do not have TimeAbs capability
// ExposureTimeBaseAbs is available fot GigE cams only
if (IsAvailable(Camera_->ExposureTimeBaseAbs) && !m_is_usb)
{
// memorize initial value of exposure time
DEB_TRACE() << "memorize initial value of exposure time";
int initial_raw = Camera_->ExposureTimeRaw.GetValue();
DEB_TRACE() << "initial_raw = " << initial_raw;
double initial_base = Camera_->ExposureTimeBaseAbs.GetValue();
DEB_TRACE() << "initial_base = " << initial_base;
DEB_TRACE() << "compute Min/Max allowed values of exposure time";
// fix raw/base in order to get the Max of Exposure
Camera_->ExposureTimeBaseAbs.SetValue(Camera_->ExposureTimeBaseAbs.GetMax());
max_expo = 1E-06 * Camera_->ExposureTimeBaseAbs.GetValue() * Camera_->ExposureTimeRaw.GetMax();
DEB_TRACE() << "max_expo = " << max_expo << " (s)";
// fix raw/base in order to get the Min of Exposure
Camera_->ExposureTimeBaseAbs.SetValue(Camera_->ExposureTimeBaseAbs.GetMin());
min_expo = 1E-06 * Camera_->ExposureTimeBaseAbs.GetValue() * Camera_->ExposureTimeRaw.GetMin();
DEB_TRACE() << "min_expo = " << min_expo << " (s)";
// reload initial value of exposure time
Camera_->ExposureTimeBaseAbs.SetValue(initial_base);
Camera_->ExposureTimeRaw.SetValue(initial_raw);
DEB_TRACE() << "initial value of exposure time was reloaded";
}
else
{
if (IsAvailable(Camera_->ExposureTime) || m_is_usb) {
min_expo = Camera_->ExposureTime.GetMin()*1e-6;
max_expo = Camera_->ExposureTime.GetMax()*1e-6;