-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAcqThread.cpp
1223 lines (821 loc) · 44 KB
/
AcqThread.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
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
/*
AcqThread.cpp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2016 Yoan Audureau, Chiara Marmo
* 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: 03/10/2016
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file AcqThread.cpp
* \author Yoan Audureau, Chiara Marmo -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 21/01/2015
* \brief Acquisition thread.
*/
#include "AcqThread.h"
boost::log::sources::severity_logger< LogSeverityLevel > AcqThread::logger;
AcqThread::Init AcqThread::initializer;
AcqThread::AcqThread( boost::circular_buffer<Frame> *fb,
// vector<Frame> *frame_sprite,
boost::mutex *fb_m,
boost::condition_variable *fb_c,
bool *sSignal,
boost::mutex *sSignal_m,
boost::condition_variable *sSignal_c,
bool *dSignal,
boost::mutex *dSignal_m,
boost::condition_variable *dSignal_c,
DetThread *detection,
StackThread *stack,
int cid,
dataParam dp,
stackParam sp,
stationParam stp,
detectionParam dtp,
cameraParam acq,
framesParam fp,
videoParam vp,
fitskeysParam fkp) {
frameBuffer = fb;
// frameSprite = frame_sprite;
frameBuffer_mutex = fb_m;
frameBuffer_condition = fb_c;
stackSignal = sSignal;
stackSignal_mutex = sSignal_m;
stackSignal_condition = sSignal_c;
detSignal = dSignal;
detSignal_mutex = dSignal_m;
detSignal_condition = dSignal_c;
pDetection = detection;
pStack = stack;
mThread = NULL;
//sprite = NULL;
mMustStop = false;
mDevice = NULL;
mThreadTerminated = false;
mNextAcqIndex = 0;
pExpCtrl = NULL;
mDeviceID = cid;
mdp = dp;
msp = sp;
mstp = stp;
mdtp = dtp;
mcp = acq;
mvp = vp;
mfp = fp;
//mut_sprite = new mutex();
spr_reader;
}
AcqThread::~AcqThread(void){
if(mDevice != NULL)
delete mDevice;
if(mThread != NULL)
delete mThread;
if(pExpCtrl != NULL)
delete pExpCtrl;
/*if(sprite != NULL)
delete sprite;*/
}
void AcqThread::stopThread(){
mMustStopMutex.lock();
mMustStop = true;
mMustStopMutex.unlock();
if(mThread != NULL)
while(mThread->timed_join(boost::posix_time::seconds(2)) == false)
mThread->interrupt();
}
bool AcqThread::startThread() {
// Create a device.
mDevice = new Device(mcp, mfp, mvp, mDeviceID);
// Search available devices.
mDevice->listDevices(false);
// CREATE CAMERA
if(!mDevice->createCamera())
return false;
// Prepare continuous acquisition.
if(!prepareAcquisitionOnDevice())
return false;
// Create acquisition thread.
mThread = new boost::thread(boost::ref(*this));
//sprite = new SpriteThread(std::ref(mut_sprite),frameSprite, mDevice, spr_reader, frameSprite->capacity());
return true;
}
bool AcqThread::getThreadStatus(){
return mThreadTerminated;
}
void AcqThread::operator()(){
bool stop = false;
BOOST_LOG_SCOPED_THREAD_TAG("LogName", "ACQ_THREAD");
BOOST_LOG_SEV(logger,notification) << "\n";
BOOST_LOG_SEV(logger,notification) << "==============================================";
BOOST_LOG_SEV(logger,notification) << "========== START ACQUISITION THREAD ==========";
BOOST_LOG_SEV(logger,notification) << "==============================================";
try {
// Search next acquisition according to the current time.
selectNextAcquisitionSchedule(TimeDate::splitIsoExtendedDate(to_iso_extended_string(boost::posix_time::microsec_clock::universal_time())));
// Exposure adjustment variables.
bool exposureControlStatus = false;
bool exposureControlActive = false;
bool cleanStatus = false;
// If exposure can be set on the input device.
if(mDevice->getExposureStatus()) {
pExpCtrl = new ExposureControl( mcp.EXPOSURE_CONTROL_FREQUENCY,
mcp.EXPOSURE_CONTROL_SAVE_IMAGE,
mcp.EXPOSURE_CONTROL_SAVE_INFOS,
mdp.DATA_PATH,
mstp.STATION_NAME);
}
TimeMode previousTimeMode = NONE;
/// Acquisition process.
do {
// Location of a video or frames if input type is FRAMES or VIDEO.
string location = "";
// Load videos file or frames directory if input type is FRAMES or VIDEO
if(!mDevice->loadNextCameraDataSet(location)) break;
if(pDetection != NULL) pDetection->setCurrentDataSet(location);
// Reference time to compute interval between regular captures.
string cDate = to_simple_string(boost::posix_time::microsec_clock::universal_time());
string refDate = cDate.substr(0, cDate.find("."));
chrono::duration<double> dur_elapsed;
chrono::time_point<chrono::high_resolution_clock> start;
do {
start = chrono::high_resolution_clock::now();
// Container for the grabbed image.
Frame newFrame;
// Time counter of grabbing a frame.
double tacq = (double)getTickCount();
// Grab a frame.
if(mDevice->runContinuousCapture(newFrame)) {
BOOST_LOG_SEV(logger, normal) << "============= FRAME " << newFrame.mFrameNumber << " ============= ";
cout << "============= FRAME " << newFrame.mFrameNumber << " ============= " << endl;
if(spr_reader.extractValueForKeyword("ACQ_SPRITE_ENABLED")=="true")
{
//***Here, we (Matthieu and Sebastien) add a function to only analyse when the sun is "sleeping"
string vec_time = to_simple_string(boost::posix_time::microsec_clock::universal_time());
int pos_space = vec_time.find(' ');
int time_hour = atoi(vec_time.substr(pos_space+1,3).c_str());
/*if(time_hour>=21 || time_hour<=2)
{
sprite->addFrame(newFrame);
}*/
//sprite->addFrame(newFrame);
}
// If camera type in input is FRAMES or VIDEO.
if(mDevice->mVideoFramesInput) {
// Push the new frame in the framebuffer.
boost::mutex::scoped_lock lock(*frameBuffer_mutex);
frameBuffer->push_back(newFrame);
lock.unlock();
// Notify detection thread.
if(pDetection != NULL) {
boost::mutex::scoped_lock lock2(*detSignal_mutex);
*detSignal = true;
detSignal_condition->notify_one();
lock2.unlock();
}
// Slow down the time in order to give more time to the detection process.
int twait = 100;
if(mvp.INPUT_TIME_INTERVAL == 0 && mfp.INPUT_TIME_INTERVAL > 0)
twait = mfp.INPUT_TIME_INTERVAL;
else if(mvp.INPUT_TIME_INTERVAL > 0 && mfp.INPUT_TIME_INTERVAL == 0)
twait = mvp.INPUT_TIME_INTERVAL;
#ifdef WINDOWS
Sleep(twait);
#else
#ifdef LINUX
usleep(twait * 1000);
#endif
#endif
}else {
// Get current time in seconds.
int currentTimeInSec = newFrame.mDate.hours * 3600 + newFrame.mDate.minutes * 60 + (int)newFrame.mDate.seconds;
// Detect day or night.
TimeMode currentTimeMode = NONE;
if((currentTimeInSec > mStopSunsetTime) || (currentTimeInSec < mStartSunriseTime)) {
currentTimeMode = NIGHT;
}else if((currentTimeInSec > mStartSunriseTime) && (currentTimeInSec < mStopSunsetTime)) {
currentTimeMode = DAY;
}
// If exposure control is not active, the new frame can be shared with others threads.
if(!exposureControlStatus) {
// Push the new frame in the framebuffer.
boost::mutex::scoped_lock lock(*frameBuffer_mutex);
frameBuffer->push_back(newFrame);
lock.unlock();
// Notify detection thread.
if(pDetection != NULL) {
if(previousTimeMode != currentTimeMode && mdtp.DET_MODE != DAYNIGHT) {
BOOST_LOG_SEV(logger, notification) << "TimeMode has changed ! ";
boost::mutex::scoped_lock lock(*detSignal_mutex);
*detSignal = false;
lock.unlock();
cout << "Send interruption signal to detection process " << endl;
pDetection->interruptThread();
}else if(mdtp.DET_MODE == currentTimeMode || mdtp.DET_MODE == DAYNIGHT) {
boost::mutex::scoped_lock lock2(*detSignal_mutex);
*detSignal = true;
detSignal_condition->notify_one();
lock2.unlock();
}
}
// Notify stack thread.
if(pStack != NULL) {
// TimeMode has changed.
if(previousTimeMode != currentTimeMode && msp.STACK_MODE != DAYNIGHT) {
BOOST_LOG_SEV(logger, notification) << "TimeMode has changed ! ";
boost::mutex::scoped_lock lock(*stackSignal_mutex);
*stackSignal = false;
lock.unlock();
// Force interruption.
cout << "Send interruption signal to stack " << endl;
pStack->interruptThread();
}else if(msp.STACK_MODE == currentTimeMode || msp.STACK_MODE == DAYNIGHT) {
boost::mutex::scoped_lock lock3(*stackSignal_mutex);
*stackSignal = true;
stackSignal_condition->notify_one();
lock3.unlock();
}
}
cleanStatus = false;
}else {
// Exposure control is active, the new frame can't be shared with others threads.
if(!cleanStatus) {
// If stack process exists.
if(pStack != NULL) {
boost::mutex::scoped_lock lock(*stackSignal_mutex);
*stackSignal = false;
lock.unlock();
// Force interruption.
cout << "Send interruption signal to stack " << endl;
pStack->interruptThread();
}
// If detection process exists
if(pDetection != NULL) {
boost::mutex::scoped_lock lock(*detSignal_mutex);
*detSignal = false;
lock.unlock();
cout << "Sending interruption signal to detection process... " << endl;
pDetection->interruptThread();
}
// Reset framebuffer.
cout << "Cleaning frameBuffer..." << endl;
boost::mutex::scoped_lock lock(*frameBuffer_mutex);
frameBuffer->clear();
lock.unlock();
cleanStatus = true;
}
}
previousTimeMode = currentTimeMode;
// Adjust exposure time.
if(pExpCtrl != NULL && exposureControlActive)
{
pyrDown(newFrame.mImg,newFrame.mImg, Size(newFrame.mImg.cols / 2, newFrame.mImg.rows / 2));
exposureControlStatus = pExpCtrl->controlExposureTime(mDevice, newFrame.mImg, newFrame.mDate, mdtp.MASK, mDevice->mMinExposureTime, mcp.ACQ_FPS);
}
// Get current date YYYYMMDD.
string currentFrameDate = TimeDate::getYYYYMMDD(newFrame.mDate);
// If the date has changed, sun ephemeris must be updated.
if(currentFrameDate != mCurrentDate) {
BOOST_LOG_SEV(logger, notification) << "Date has changed. Former Date is " << mCurrentDate << ". New Date is " << currentFrameDate << "." ;
computeSunTimes();
}
// Acquisition at regular time interval is enabled.
if(mcp.regcap.ACQ_REGULAR_ENABLED && !mDevice->mVideoFramesInput) {
cDate = to_simple_string(boost::posix_time::microsec_clock::universal_time());
string nowDate = cDate.substr(0, cDate.find("."));
boost::posix_time::ptime t1(boost::posix_time::time_from_string(refDate));
boost::posix_time::ptime t2(boost::posix_time::time_from_string(nowDate));
boost::posix_time::time_duration td = t2 - t1;
long secTime = td.total_seconds();
cout << "NEXT REGCAP : " << (int)(mcp.regcap.ACQ_REGULAR_CFG.interval - secTime) << "s" << endl;
// Check it's time to run a regular capture.
if(secTime >= mcp.regcap.ACQ_REGULAR_CFG.interval) {
// Current time is after the sunset stop and before the sunrise start = NIGHT
if((currentTimeMode == NIGHT) && (mcp.regcap.ACQ_REGULAR_MODE == NIGHT || mcp.regcap.ACQ_REGULAR_MODE == DAYNIGHT)) {
BOOST_LOG_SEV(logger, notification) << "Run regular acquisition.";
runImageCapture( mcp.regcap.ACQ_REGULAR_CFG.rep,
mcp.regcap.ACQ_REGULAR_CFG.exp,
mcp.regcap.ACQ_REGULAR_CFG.gain,
mcp.regcap.ACQ_REGULAR_CFG.fmt,
mcp.regcap.ACQ_REGULAR_OUTPUT,
mcp.regcap.ACQ_REGULAR_PRFX);
// Current time is between sunrise start and sunset stop = DAY
}else if(currentTimeMode == DAY && (mcp.regcap.ACQ_REGULAR_MODE == DAY || mcp.regcap.ACQ_REGULAR_MODE == DAYNIGHT)) {
BOOST_LOG_SEV(logger, notification) << "Run regular acquisition.";
saveImageCaptured(newFrame, 0, mcp.regcap.ACQ_REGULAR_OUTPUT, mcp.regcap.ACQ_REGULAR_PRFX);
}
// Reset reference time in case a long exposure has been done.
cDate = to_simple_string(boost::posix_time::microsec_clock::universal_time());
refDate = cDate.substr(0, cDate.find("."));
}
}
// Acquisiton at scheduled time is enabled.
if(mcp.schcap.ACQ_SCHEDULE.size() != 0 && mcp.schcap.ACQ_SCHEDULE_ENABLED && !mDevice->mVideoFramesInput) {
int next = (mNextAcq.hours * 3600 + mNextAcq.min * 60 + mNextAcq.sec) - (newFrame.mDate.hours * 3600 + newFrame.mDate.minutes * 60 + newFrame.mDate.seconds);
if(next < 0) {
next = (24 * 3600) - (newFrame.mDate.hours * 3600 + newFrame.mDate.minutes * 60 + newFrame.mDate.seconds) + (mNextAcq.hours * 3600 + mNextAcq.min * 60 + mNextAcq.sec);
cout << "next : " << next << endl;
}
vector<int>tsch = TimeDate::HdecimalToHMS(next/3600.0);
cout << "NEXT SCHCAP : " << tsch.at(0) << "h" << tsch.at(1) << "m" << tsch.at(2) << "s" << endl;
// It's time to run scheduled acquisition.
if( mNextAcq.hours == newFrame.mDate.hours &&
mNextAcq.min == newFrame.mDate.minutes &&
(int)newFrame.mDate.seconds == mNextAcq.sec) {
CamPixFmt format;
format = mNextAcq.fmt;
runImageCapture( mNextAcq.rep,
mNextAcq.exp,
mNextAcq.gain,
format,
mcp.schcap.ACQ_SCHEDULE_OUTPUT,
"");
// Update mNextAcq
selectNextAcquisitionSchedule(newFrame.mDate);
}else {
// The current time has elapsed.
if(newFrame.mDate.hours > mNextAcq.hours) {
selectNextAcquisitionSchedule(newFrame.mDate);
}else if(newFrame.mDate.hours == mNextAcq.hours) {
if(newFrame.mDate.minutes > mNextAcq.min) {
selectNextAcquisitionSchedule(newFrame.mDate);
}else if(newFrame.mDate.minutes == mNextAcq.min) {
if(newFrame.mDate.seconds > mNextAcq.sec) {
selectNextAcquisitionSchedule(newFrame.mDate);
}
}
}
}
}
// Check sunrise and sunset time.
if( (((currentTimeInSec > mStartSunriseTime && currentTimeInSec < mStopSunriseTime) ||
(currentTimeInSec > mStartSunsetTime && currentTimeInSec < mStopSunsetTime))) && !mDevice->mVideoFramesInput) {
exposureControlActive = true;
}else {
// Print time before sunrise.
if(currentTimeInSec < mStartSunriseTime || currentTimeInSec > mStopSunsetTime ) {
vector<int> nextSunrise;
if(currentTimeInSec < mStartSunriseTime)
nextSunrise = TimeDate::HdecimalToHMS((mStartSunriseTime - currentTimeInSec) / 3600.0);
if(currentTimeInSec > mStopSunsetTime)
nextSunrise = TimeDate::HdecimalToHMS(((24*3600 - currentTimeInSec) + mStartSunriseTime ) / 3600.0);
cout << "NEXT SUNRISE : " << nextSunrise.at(0) << "h" << nextSunrise.at(1) << "m" << nextSunrise.at(2) << "s" << endl;
}
// Print time before sunset.
if(currentTimeInSec > mStopSunriseTime && currentTimeInSec < mStartSunsetTime){
vector<int> nextSunset;
nextSunset = TimeDate::HdecimalToHMS((mStartSunsetTime - currentTimeInSec) / 3600.0);
cout << "NEXT SUNSET : " << nextSunset.at(0) << "h" << nextSunset.at(1) << "m" << nextSunset.at(2) << "s" << endl;
}
// Reset exposure time when sunrise or sunset is finished.
if(exposureControlActive) {
// In DAYTIME : Apply minimum available exposure time.
if((currentTimeInSec >= mStopSunriseTime && currentTimeInSec < mStartSunsetTime)){
BOOST_LOG_SEV(logger, notification) << "Apply day exposure time : " << mDevice->getDayExposureTime();
mDevice->setCameraDayExposureTime();
BOOST_LOG_SEV(logger, notification) << "Apply day exposure time : " << mDevice->getDayGain();
mDevice->setCameraDayGain();
// In NIGHTTIME : Apply maximum available exposure time.
}else if((currentTimeInSec >= mStopSunsetTime) || (currentTimeInSec < mStartSunriseTime)){
BOOST_LOG_SEV(logger, notification) << "Apply night exposure time." << mDevice->getNightExposureTime();
mDevice->setCameraNightExposureTime();
BOOST_LOG_SEV(logger, notification) << "Apply night exposure time." << mDevice->getNightGain();
mDevice->setCameraNightGain();
}
}
exposureControlActive = false;
exposureControlStatus = false;
}
}
}
tacq = (((double)getTickCount() - tacq)/getTickFrequency())*1000;
std::cout << " [ TIME ACQ ] : " << tacq << " ms ~cFPS(" << (1.0/(tacq/1000.0)) << ")" << endl;
BOOST_LOG_SEV(logger, normal) << " [ TIME ACQ ] : " << tacq << " ms";
mMustStopMutex.lock();
stop = mMustStop;
mMustStopMutex.unlock();
dur_elapsed = chrono::high_resolution_clock::now() - start;
//cerr<<"Ara "<<dur_elapsed.count()<<endl;
}while(stop == false && !mDevice->getCameraStatus());
// Reset detection process to prepare the analyse of a new data set.
if(pDetection != NULL) {
pDetection->getDetMethod()->resetDetection(true);
pDetection->getDetMethod()->resetMask();
pDetection->updateDetectionReport();
if(!pDetection->getRunStatus())
break;
}
// Clear framebuffer.
boost::mutex::scoped_lock lock(*frameBuffer_mutex);
frameBuffer->clear();
lock.unlock();
}while(mDevice->getCameraDataSetStatus() && stop == false);
}catch(const boost::thread_interrupted&){
BOOST_LOG_SEV(logger,notification) << "AcqThread ended.";
cout << "AcqThread ended." <<endl;
}catch(exception& e){
cout << "An exception occured : " << e.what() << endl;
BOOST_LOG_SEV(logger, critical) << "An exception occured : " << e.what();
}catch(const char * msg) {
cout << endl << msg << endl;
}
mDevice->stopCamera();
mThreadTerminated = true;
std::cout << "Acquisition Thread TERMINATED." << endl;
BOOST_LOG_SEV(logger,notification) << "Acquisition Thread TERMINATED";
}
void AcqThread::selectNextAcquisitionSchedule(TimeDate::Date date){
if(mcp.schcap.ACQ_SCHEDULE.size() != 0){
// Search next acquisition
for(int i = 0; i < mcp.schcap.ACQ_SCHEDULE.size(); i++){
if(date.hours < mcp.schcap.ACQ_SCHEDULE.at(i).hours){
mNextAcqIndex = i;
break;
}else if(date.hours == mcp.schcap.ACQ_SCHEDULE.at(i).hours){
if(date.minutes < mcp.schcap.ACQ_SCHEDULE.at(i).min){
mNextAcqIndex = i;
break;
}else if(date.minutes == mcp.schcap.ACQ_SCHEDULE.at(i).min){
if(date.seconds < mcp.schcap.ACQ_SCHEDULE.at(i).sec){
mNextAcqIndex = i;
break;
}
}
}
}
mNextAcq = mcp.schcap.ACQ_SCHEDULE.at(mNextAcqIndex);
}
}
bool AcqThread::buildAcquisitionDirectory(string YYYYMMDD){
namespace fs = boost::filesystem;
string root = mdp.DATA_PATH + mstp.STATION_NAME + "_" + YYYYMMDD +"/";
string subDir = "captures/";
string finalPath = root + subDir;
mOutputDataPath = finalPath;
BOOST_LOG_SEV(logger,notification) << "CompleteDataPath : " << mOutputDataPath;
path p(mdp.DATA_PATH);
path p1(root);
path p2(root + subDir);
// If DATA_PATH exists
if(fs::exists(p)){
// If DATA_PATH/STATI ON_YYYYMMDD/ exists
if(fs::exists(p1)){
// If DATA_PATH/STATION_YYYYMMDD/captures/ doesn't exists
if(!fs::exists(p2)){
// If fail to create DATA_PATH/STATION_YYYYMMDD/captures/
if(!fs::create_directory(p2)){
BOOST_LOG_SEV(logger,critical) << "Unable to create captures directory : " << p2.string();
return false;
// If success to create DATA_PATH/STATION_YYYYMMDD/captures/
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create captures directory : " << p2.string();
return true;
}
}
// If DATA_PATH/STATION_YYYYMMDD/ doesn't exists
}else{
// If fail to create DATA_PATH/STATION_YYYYMMDD/
if(!fs::create_directory(p1)){
BOOST_LOG_SEV(logger,fail) << "Unable to create STATION_YYYYMMDD directory : " << p1.string();
return false;
// If success to create DATA_PATH/STATION_YYYYMMDD/
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create STATION_YYYYMMDD directory : " << p1.string();
// If fail to create DATA_PATH/STATION_YYYYMMDD/stack/
if(!fs::create_directory(p2)){
BOOST_LOG_SEV(logger,critical) << "Unable to create captures directory : " << p2.string();
return false;
// If success to create DATA_PATH/STATION_YYYYMMDD/stack/
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create captures directory : " << p2.string();
return true;
}
}
}
// If DATA_PATH doesn't exists
}else{
// If fail to create DATA_PATH
if(!fs::create_directory(p)){
BOOST_LOG_SEV(logger,fail) << "Unable to create DATA_PATH directory : " << p.string();
return false;
// If success to create DATA_PATH
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create DATA_PATH directory : " << p.string();
// If fail to create DATA_PATH/STATION_YYYYMMDD/
if(!fs::create_directory(p1)){
BOOST_LOG_SEV(logger,fail) << "Unable to create STATION_YYYYMMDD directory : " << p1.string();
return false;
// If success to create DATA_PATH/STATION_YYYYMMDD/
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create STATION_YYYYMMDD directory : " << p1.string();
// If fail to create DATA_PATH/STATION_YYYYMMDD/captures/
if(!fs::create_directory(p2)){
BOOST_LOG_SEV(logger,critical) << "Unable to create captures directory : " << p2.string();
return false;
// If success to create DATA_PATH/STATION_YYYYMMDD/captures/
}else{
BOOST_LOG_SEV(logger,notification) << "Success to create captures directory : " << p2.string();
return true;
}
}
}
}
return true;
}
void AcqThread::runImageCapture(int imgNumber, int imgExposure, int imgGain, CamPixFmt imgFormat, ImgFormat imgOutput, string imgPrefix) {
// Stop camera
mDevice->stopCamera();
// Stop stack process.
if(pStack != NULL){
boost::mutex::scoped_lock lock(*stackSignal_mutex);
*stackSignal = false;
lock.unlock();
// Force interruption.
BOOST_LOG_SEV(logger, notification) << "Send reset signal to stack. ";
pStack->interruptThread();
}
// Stop detection process.
if(pDetection != NULL){
boost::mutex::scoped_lock lock(*detSignal_mutex);
*detSignal = false;
lock.unlock();
BOOST_LOG_SEV(logger, notification) << "Send reset signal to detection process. ";
pDetection->interruptThread();
}
// Reset framebuffer.
BOOST_LOG_SEV(logger, notification) << "Cleaning frameBuffer...";
boost::mutex::scoped_lock lock(*frameBuffer_mutex);
frameBuffer->clear();
lock.unlock();
for(int i = 0; i < imgNumber; i++) {
BOOST_LOG_SEV(logger, notification) << "Prepare capture n° " << i;
// Configuration for single capture.
Frame frame;
BOOST_LOG_SEV(logger, notification) << "Exposure time : " << imgExposure;
frame.mExposure = imgExposure;
BOOST_LOG_SEV(logger, notification) << "Gain : " << imgGain;
frame.mGain = imgGain;
EParser<CamPixFmt> format;
BOOST_LOG_SEV(logger, notification) << "Format : " << format.getStringEnum(imgFormat);
frame.mFormat = imgFormat;
if(mcp.ACQ_RES_CUSTOM_SIZE) {
frame.mHeight = mcp.ACQ_HEIGHT;
frame.mWidth = mcp.ACQ_WIDTH;
}
// Run single capture.
BOOST_LOG_SEV(logger, notification) << "Run single capture.";
if(mDevice->runSingleCapture(frame)) {
BOOST_LOG_SEV(logger, notification) << "Single capture succeed !";
cout << "Single capture succeed !" << endl;
saveImageCaptured(frame, i, imgOutput, imgPrefix);
}else{
BOOST_LOG_SEV(logger, fail) << "Single capture failed !";
}
}
#ifdef WINDOWS
Sleep(1000);
#else
#ifdef LINUX
sleep(1);
#endif
#endif
BOOST_LOG_SEV(logger, notification) << "Restarting camera in continuous mode...";
// RECREATE CAMERA
if(!mDevice->recreateCamera())
throw "Fail to restart camera.";
prepareAcquisitionOnDevice();
}
void AcqThread::saveImageCaptured(Frame &img, int imgNum, ImgFormat outputType, string imgPrefix) {
if(img.mImg.data) {
string YYYYMMDD = TimeDate::getYYYYMMDD(img.mDate);
if(buildAcquisitionDirectory(YYYYMMDD)) {
string fileName = imgPrefix + "_" + TimeDate::getYYYYMMDDThhmmss(img.mDate) + "_UT-" + Conversion::intToString(imgNum);
switch(outputType) {
case JPEG :
{
switch(img.mFormat) {
case MONO12 :
{
Mat temp;
img.mImg.copyTo(temp);
Mat newMat = ImgProcessing::correctGammaOnMono12(temp, 2.2);
Mat newMat2 = Conversion::convertTo8UC1(newMat);
SaveImg::saveJPEG(newMat2, mOutputDataPath + fileName);
}
break;
default :
{
Mat temp;
img.mImg.copyTo(temp);
Mat newMat = ImgProcessing::correctGammaOnMono8(temp, 2.2);
SaveImg::saveJPEG(newMat, mOutputDataPath + fileName);
}
}
}
break;
case FITS :
{
Fits2D newFits(mOutputDataPath);
newFits.loadKeys(mfkp, mstp);
newFits.kGAINDB = img.mGain;
newFits.kEXPOSURE = img.mExposure/1000000.0;
newFits.kONTIME = img.mExposure/1000000.0;
newFits.kELAPTIME = img.mExposure/1000000.0;
newFits.kDATEOBS = TimeDate::getIsoExtendedFormatDate(img.mDate);
double debObsInSeconds = img.mDate.hours*3600 + img.mDate.minutes*60 + img.mDate.seconds;
double julianDate = TimeDate::gregorianToJulian(img.mDate);
double julianCentury = TimeDate::julianCentury(julianDate);
newFits.kCRVAL1 = TimeDate::localSideralTime_2(julianCentury, img.mDate.hours, img.mDate.minutes, (int)img.mDate.seconds, mstp.SITELONG);
newFits.kCTYPE1 = "RA---ARC";
newFits.kCTYPE2 = "DEC--ARC";
newFits.kEQUINOX = 2000.0;
switch(img.mFormat) {
case MONO12 :
{
// Convert unsigned short type image in short type image.
Mat newMat = Mat(img.mImg.rows, img.mImg.cols, CV_16SC1, Scalar(0));
// Set bzero and bscale for print unsigned short value in soft visualization.
newFits.kBZERO = 32768;
newFits.kBSCALE = 1;
unsigned short *ptr = NULL;
short *ptr2 = NULL;
for(int i = 0; i < img.mImg.rows; i++){
ptr = img.mImg.ptr<unsigned short>(i);
ptr2 = newMat.ptr<short>(i);
for(int j = 0; j < img.mImg.cols; j++){
if(ptr[j] - 32768 > 32767){
ptr2[j] = 32767;
}else{
ptr2[j] = ptr[j] - 32768;
}
}
}
// Create FITS image with BITPIX = SHORT_IMG (16-bits signed integers), pixel with TSHORT (signed short)
if(newFits.writeFits(newMat, S16, fileName))
cout << ">> Fits saved in : " << mOutputDataPath << fileName << endl;
}
break;
default :
{
if(newFits.writeFits(img.mImg, UC8, fileName))
cout << ">> Fits saved in : " << mOutputDataPath << fileName << endl;
}
}
}
break;
}
}
}
}
bool AcqThread::computeSunTimes() {
int sunriseStartH = 0, sunriseStartM = 0, sunriseStopH = 0, sunriseStopM = 0,
sunsetStartH = 0, sunsetStartM = 0, sunsetStopH = 0, sunsetStopM = 0;