forked from fripon/freeture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.cpp
681 lines (479 loc) · 15.1 KB
/
Device.cpp
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
/*
Device.cpp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2015 Yoan Audureau
* FRIPON-GEOPS-UPSUD-CNRS
*
* License: GNU General Public License
*
* FreeTure 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.
* FreeTure 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 FreeTure. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 20/07/2015
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file Device.cpp
* \author Yoan Audureau -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 02/09/2014
* \brief
*/
#include "Device.h"
boost::log::sources::severity_logger< LogSeverityLevel > Device::logger;
Device::Init Device::initializer;
Device::Device(cameraParam cp, framesParam fp, videoParam vp, int cid) {
mCam = NULL;
mCamID = 0;
mVerbose = true;
mNbDev = -1;
mVideoFramesInput = false;
mGenCamID = cid;
mFPS = cp.ACQ_FPS;
mNightExposure = cp.ACQ_NIGHT_EXPOSURE;
mNightGain = cp.ACQ_NIGHT_GAIN;
mDayExposure = cp.ACQ_DAY_EXPOSURE;
mDayGain = cp.ACQ_DAY_GAIN;
mMinExposureTime = -1;
mMaxExposureTime = -1;
mMinGain = -1;
mMaxGain = -1;
mShiftBits = cp.SHIFT_BITS;
mFormat = cp.ACQ_FORMAT;
mCustomSize = cp.ACQ_RES_CUSTOM_SIZE;
mSizeWidth = cp.ACQ_WIDTH;
mSizeHeight = cp.ACQ_HEIGHT;
mDeviceType = UNDEFINED_INPUT_TYPE;
mvp = vp;
mfp = fp;
//mNbFrame = 0;
}
Device::Device() {
mFormat = MONO8;
mNightExposure = 0;
mNightGain = 0;
mDayExposure = 0;
mDayGain = 0;
mMinExposureTime = -1;
mMaxExposureTime = -1;
mMinGain = -1;
mMaxGain = -1;
mFPS = 30;
mCamID = 0;
mGenCamID = 0;
mCustomSize = false;
mSizeWidth = 640;
mSizeHeight = 480;
mCam = NULL;
mVideoFramesInput = false;
mShiftBits = false;
mNbDev = -1;
mVerbose = true;
mDeviceType = UNDEFINED_INPUT_TYPE;
vector<string> finput, vinput;
mvp.INPUT_VIDEO_PATH = vinput;
mfp.INPUT_FRAMES_DIRECTORY_PATH = finput;
//mNbFrame = 0;
}
Device::~Device(){
if(mCam != NULL)
delete mCam;
}
bool Device::createCamera(int id, bool create) {
if(id >=0 && id < mDevices.size()) {
// Create Camera object with the correct sdk.
if(!createDevicesWith(mDevices.at(id).second.second)){
cout << "Fail to select correct sdk : "<< mDevices.at(id).second.second << endl;
return false;
}
mCamID = mDevices.at(id).first;
if(mCam != NULL) {
if(create) {
if(!mCam->createDevice(mCamID)){
BOOST_LOG_SEV(logger, fail) << "Fail to create device with ID : " << id;
mCam->grabCleanse();
return false;
}else{
//BOOST_LOG_SEV(logger, fail) << "Success to create device with ID : " << id;
}
}
return true;
}
}
BOOST_LOG_SEV(logger, fail) << "No device with ID " << id;
return false;
}
bool Device::createCamera() {
if(mGenCamID >=0 && mGenCamID < mDevices.size()) {
// Create Camera object with the correct sdk.
if(!createDevicesWith(mDevices.at(mGenCamID).second.second))
return false;
mCamID = mDevices.at(mGenCamID).first;
if(mCam != NULL) {
if(!mCam->createDevice(mCamID)){
BOOST_LOG_SEV(logger, fail) << "Fail to create device with ID : " << mGenCamID;
mCam->grabCleanse();
return false;
}
return true;
}
}
BOOST_LOG_SEV(logger, fail) << "No device with ID " << mGenCamID;
return false;
}
bool Device::recreateCamera() {
if(mGenCamID >=0 && mGenCamID < mDevices.size()) {
mCamID = mDevices.at(mGenCamID).first;
if(mCam != NULL) {
if(!mCam->createDevice(mCamID)){
BOOST_LOG_SEV(logger, fail) << "Fail to create device with ID : " << mGenCamID;
mCam->grabCleanse();
return false;
}
return true;
}
}
BOOST_LOG_SEV(logger, fail) << "No device with ID " << mGenCamID;
return false;
}
void Device::setVerbose(bool status) {
mVerbose = status;
}
CamSdkType Device::getDeviceSdk(int id){
if(id >=0 && id < mDevices.size()) {
return mDevices.at(id).second.second;
}
return UNKNOWN;
}
bool Device::createDevicesWith(CamSdkType sdk) {
switch(sdk) {
case VIDEOFILE :
{
mVideoFramesInput = true;
mCam = new CameraVideo(mvp.INPUT_VIDEO_PATH, mVerbose);
mCam->grabInitialization();
}
break;
case FRAMESDIR :
{
mVideoFramesInput = true;
// Create camera using pre-recorded fits2D in input.
mCam = new CameraFrames(mfp.INPUT_FRAMES_DIRECTORY_PATH, 1, mVerbose);
if(!mCam->grabInitialization())
throw "Fail to prepare acquisition on the first frames directory.";
}
break;
case V4L2 :
{
#ifdef LINUX
mCam = new CameraV4l2();
#endif
}
break;
case VIDEOINPUT :
{
#ifdef WINDOWS
mCam = new CameraWindows();
#endif
}
break;
case ARAVIS :
{
#ifdef LINUX
mCam = new CameraGigeAravis(mShiftBits);
#endif
}
break;
case PYLONGIGE :
{
#ifdef WINDOWS
mCam = new CameraGigePylon();
#endif
}
break;
case TIS :
{
#ifdef WINDOWS
mCam = new CameraGigeTis();
#endif
}
break;
default :
cout << "Unknown sdk." << endl;
}
return true;
}
InputDeviceType Device::getDeviceType(CamSdkType t) {
switch(t){
case VIDEOFILE :
return VIDEO;
break;
case FRAMESDIR :
return SINGLE_FITS_FRAME;
break;
case V4L2 :
case VIDEOINPUT :
case ARAVIS :
case PYLONGIGE :
case TIS :
return CAMERA;
break;
case UNKNOWN :
return UNDEFINED_INPUT_TYPE;
break;
}
return UNDEFINED_INPUT_TYPE;
}
void Device::listDevices(bool printInfos) {
int nbCam = 0;
mNbDev = 0;
pair<int, CamSdkType> elem; // general index to specify camera to use
pair<int,pair<int,CamSdkType>> subElem; // index in a specific sdk
vector<pair<int,string>> listCams;
#ifdef WINDOWS
// PYLONGIGE
mCam = new CameraGigePylon();
listCams = mCam->getCamerasList();
for(int i = 0; i < listCams.size(); i++) {
elem.first = mNbDev; elem.second = PYLONGIGE;
subElem.first = listCams.at(i).first; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] " << listCams.at(i).second << endl;
mNbDev++;
}
delete mCam;
// TIS
mCam = new CameraGigeTis();
listCams = mCam->getCamerasList();
for(int i = 0; i < listCams.size(); i++) {
elem.first = mNbDev; elem.second = TIS;
subElem.first = listCams.at(i).first; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] " << listCams.at(i).second << endl;
mNbDev++;
}
delete mCam;
// WINDOWS
mCam = new CameraWindows();
listCams = mCam->getCamerasList();
for(int i = 0; i < listCams.size(); i++) {
// Avoid to list basler
std::string::size_type pos1 = listCams.at(i).second.find("Basler");
std::string::size_type pos2 = listCams.at(i).second.find("BASLER");
if((pos1 != std::string::npos) || (pos2 != std::string::npos)) {
//std::cout << "found \"words\" at position " << pos1 << std::endl;
} else {
elem.first = mNbDev; elem.second = VIDEOINPUT;
subElem.first = listCams.at(i).first; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] " << listCams.at(i).second << endl;
mNbDev++;
}
}
delete mCam;
#else
// ARAVIS
createDevicesWith(ARAVIS);
listCams = mCam->getCamerasList();
for(int i = 0; i < listCams.size(); i++) {
elem.first = mNbDev; elem.second = ARAVIS;
subElem.first = listCams.at(i).first; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] " << listCams.at(i).second << endl;
mNbDev++;
}
delete mCam;
// V4L2
createDevicesWith(V4L2);
listCams = mCam->getCamerasList();
for(int i = 0; i < listCams.size(); i++) {
elem.first = mNbDev; elem.second = V4L2;
subElem.first = listCams.at(i).first; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] " << listCams.at(i).second << endl;
mNbDev++;
}
delete mCam;
#endif
// VIDEO
elem.first = mNbDev; elem.second = VIDEOFILE;
subElem.first = 0; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] VIDEO FILES" << endl;
mNbDev++;
// FRAMES
elem.first = mNbDev; elem.second = FRAMESDIR;
subElem.first = 0; subElem.second = elem;
mDevices.push_back(subElem);
if(printInfos) cout << "[" << mNbDev << "] FRAMES DIRECTORY" << endl;
mNbDev++;
mCam = NULL;
}
bool Device::getDeviceName() {
return mCam->getCameraName();
}
bool Device::setCameraPixelFormat() {
if(!mCam->setPixelFormat(mFormat)){
mCam->grabCleanse();
BOOST_LOG_SEV(logger,fail) << "Fail to set camera format.";
return false;
}
return true;
}
bool Device::getSupportedPixelFormats() {
mCam->getAvailablePixelFormats();
return true;
}
InputDeviceType Device::getDeviceType() {
return mCam->getDeviceType();
}
bool Device::getCameraExposureBounds(double &min, double &max) {
mCam->getExposureBounds(min, max);
return true;
}
void Device::getCameraExposureBounds() {
mCam->getExposureBounds(mMinExposureTime, mMaxExposureTime);
}
bool Device::getCameraGainBounds(int &min, int &max) {
mCam->getGainBounds(min, max);
return true;
}
void Device::getCameraGainBounds() {
mCam->getGainBounds(mMinGain, mMaxGain);
}
bool Device::setCameraNightExposureTime() {
if(!mCam->setExposureTime(mNightExposure)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set night exposure time to " << mNightExposure;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraDayExposureTime() {
if(!mCam->setExposureTime(mDayExposure)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set day exposure time to " << mDayExposure;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraNightGain() {
if(!mCam->setGain(mNightGain)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set night gain to " << mNightGain;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraDayGain() {
if(!mCam->setGain(mDayGain)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set day gain to " << mDayGain;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraExposureTime(double value) {
if(!mCam->setExposureTime(value)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set exposure time to " << value;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraGain(int value) {
if(!mCam->setGain(value)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set gain to " << value;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::setCameraFPS() {
if(!mCam->setFPS(mFPS)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set FPS to " << mFPS;
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::initializeCamera() {
if(!mCam->grabInitialization()){
BOOST_LOG_SEV(logger, fail) << "Fail to initialize camera.";
mCam->grabCleanse();
return false;
}
return true;
}
bool Device::startCamera() {
BOOST_LOG_SEV(logger, notification) << "Starting camera...";
if(!mCam->acqStart())
return false;
return true;
}
bool Device::stopCamera() {
BOOST_LOG_SEV(logger, notification) << "Stopping camera...";
mCam->acqStop();
mCam->grabCleanse();
return true;
}
bool Device::runContinuousCapture(Frame &img) {
if(mCam->grabImage(img)) {
//img.mFrameNumber = mNbFrame;
//mNbFrame++;
return true;
}
return false;
}
bool Device::runSingleCapture(Frame &img) {
if(mCam->grabSingleImage(img, mCamID))
return true;
return false;
}
bool Device::setCameraSize() {
if(!mCam->setSize(mSizeWidth, mSizeHeight, mCustomSize)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set camera size.";
return false;
}
return true;
}
bool Device::setCameraSize(int w, int h) {
if(!mCam->setSize(w, h, true)) {
BOOST_LOG_SEV(logger, fail) << "Fail to set camera size.";
return false;
}
return true;
}
bool Device::getCameraFPS(double &fps) {
if(!mCam->getFPS(fps)) {
//BOOST_LOG_SEV(logger, fail) << "Fail to get fps value from camera.";
return false;
}
return true;
}
bool Device::getCameraStatus() {
return mCam->getStopStatus();
}
bool Device::getCameraDataSetStatus() {
return mCam->getDataSetStatus();
}
bool Device::loadNextCameraDataSet(string &location) {
return mCam->loadNextDataSet(location);
}
bool Device::getExposureStatus() {
return mCam->mExposureAvailable;
}
bool Device::getGainStatus() {
return mCam->mGainAvailable;
}