-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathcapture_RPi.cpp
902 lines (788 loc) · 27.6 KB
/
capture_RPi.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
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <tr1/memory>
#include <stdlib.h>
#include <signal.h>
#include <fstream>
#include <stdarg.h>
#include <iomanip>
#include <cstring>
#include <sstream>
#include "include/allsky_common.h"
// CG holds all configuration variables.
// There are only a few cases where it's not passed to a function.
// When it's passed, functions call it "cg", so use upper case for global version.
config CG;
#include "include/mode_mean.h"
#define CAMERA_TYPE "RPi"
#define IS_RPi
#include "ASI_functions.cpp"
using namespace std;
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// These are global so they can be used by other routines.
// Variables for command-line settings are first.
timeval exposureStartDateTime; // date/time an image started
std::vector<int> compressionParameters;
bool bMain = true;
bool bDisplay = false;
std::string dayOrNight;
int numTotalErrors = 0; // Total number of errors, fyi
int numConsecutiveErrors = 0; // Number of consecutive errors
int maxErrors = 4; // Max number of errors in a row before we exit
bool gotSignal = false; // Did we get signal?
int iNumOfCtrl = NOT_SET; // Number of camera control capabilities
pthread_t threadDisplay = 0; // Not used by Rpi;
int numExposures = 0; // how many valid pictures have we taken so far?
int currentBpp = NOT_SET; // bytes per pixel: 8, 16, or 24
int currentBitDepth = NOT_SET; // 8 or 16
raspistillSetting myRaspistillSetting;
modeMeanSetting myModeMeanSetting;
std::string errorOutput;
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// Build capture command to capture the image from the camera.
// If an argument is IS_DEFAULT, the user didn't set it so don't pass to the program and
// the default will be used.
int RPicapture(config cg, cv::Mat *image)
{
// Define command line.
string command = cg.cmdToUse;
// Ensure no prior process is still running.
string kill = "pkill --signal SIGKILL '" + command + "' 2> /dev/null";
static bool showed_kill_command = false;
if (! showed_kill_command) {
// only show once - it never changes
showed_kill_command = true;
Log(4, " > Kill command: %s\n", kill.c_str());
}
system(kill.c_str());
stringstream ss, ss2;
ss << cg.fullFilename;
command += " --thumb none --output '" + ss.str() + "'"; // don't include a thumbnail in the file
if (cg.isLibcamera)
{
// libcamera tuning file
if (cg.currentTuningFile != NULL && *cg.currentTuningFile != '\0') {
ss.str("");
ss << cg.currentTuningFile;
command += " --tuning-file '" + ss.str() + "'";
}
if (strcmp(cg.imageExt, "png") == 0)
command += " --encoding png";
}
else
{
command += " --burst -st";
}
// --timeout (in MS) determines how long the video will run before it takes a picture.
// Value of 0 runs forever.
if (cg.preview)
{
stringstream wh;
wh << cg.width << "," << cg.height;
command += " --timeout 5000";
command += " --preview '0,0," + wh.str() + "'"; // x,y,width,height
}
else
{
ss.str("");
// Daytime auto-exposure pictures don't need a very long --timeout since the exposures are
// normally short so the camera can home in on the correct exposure quickly.
if (cg.currentAutoExposure)
{
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF)
{
// We do our own auto-exposure so no need to wait at all.
// TODO: --immediate 0 works fine on Bookworm.
// If it also works on Bullseye then use it when we no longer support Buster.
// Tried --immediate, but on Buster (don't know about Bullseye), it hung exposures.
ss << 1;
}
else if (dayOrNight == "DAY")
ss << 2 * MS_IN_SEC;
else // NIGHT
{
// could use longer but it'll take forever for pictures to appear
ss << 10 * MS_IN_SEC;
}
}
else
{
// Manual exposure shots don't need any time to home in since we're specifying the time.
ss << 1;
}
if (ss.str() != "") command += " --timeout " + ss.str();
command += " --nopreview";
}
// https://www.raspberrypi.com/documentation/accessories/camera.html#raspistill
// Mode Size Aspect Ratio Frame rates FOV Binning/Scaling
// 0 automatic selection
// 1 2028x1080 169:90 0.1-50fps Partial 2x2 binned
// 2 2028x1520 4:3 0.1-50fps Full 2x2 binned <<< bin==2
// 3 4056x3040 4:3 0.005-10fps Full None <<< bin==1
// 4 1332x990 74:55 50.1-120fps Partial 2x2 binned <<< else
ss.str("");
ss2.str("");
if (cg.isLibcamera)
{
// Ideally for bin 2 we'd use information from below,
// but that's pretty hard to do.
// 'SRGGB10_CSI2P' : 1332x990
// 'SRGGB12_CSI2P' : 2028x1080 2028x1520 4056x3040
// bin 2x2 bin 1x1
// cg.width and cg.height are already reduced for binning as needed.
if (cg.currentBin == 1 || cg.currentBin == 2)
{
ss << cg.width;
ss2 << cg.height;
command += " --width " + ss.str() + " --height " + ss2.str();
}
}
else
{
if (cg.currentBin == 1)
command += " --mode 3";
else if (cg.currentBin == 2)
{
command += " --mode 2";
//x ss << cg.width / 2;
//x ss2 << cg.height / 2;
//x command += " --mode 2 --width " + ss.str() + " --height " + ss2.str();
}
}
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF)
{
cg.currentExposure_us = myRaspistillSetting.shutter_us;
}
// Check if automatic determined exposure time is selected
if (cg.currentAutoExposure)
{
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF) {
ss.str("");
ss << cg.currentExposure_us;
if (! cg.isLibcamera)
command += " --exposure off";
command += " --shutter " + ss.str();
} else {
// libcamera doesn't use "exposure off/auto". For auto-exposure set shutter to 0.
if (cg.isLibcamera)
command += " --shutter 0";
else
command += " --exposure auto";
}
}
else if (cg.currentExposure_us) // manual exposure
{
if (! cg.isLibcamera)
command += " --exposure off";
ss.str("");
ss << cg.currentExposure_us;
command += " --shutter " + ss.str();
}
// Check if auto gain is selected
if (cg.currentAutoGain)
{
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF)
{
ss.str("");
ss << myRaspistillSetting.analoggain;
command += " --analoggain " + ss.str();
}
else if (cg.isLibcamera)
{
command += " --analoggain 0"; // 0 makes it autogain
}
else
{
command += " --analoggain 1"; // 1 makes it autogain
}
}
else // Is manual gain
{
ss.str("");
ss << cg.currentGain;
command += " --analoggain " + ss.str();
}
/* TODO: what exif fields should we use?
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF) {
stringstream strExposureTime;
stringstream strReinforcement;
strExposureTime << myRaspistillSetting.shutter_us;
strReinforcement << myRaspistillSetting.analoggain;
command += " --exif IFD0.Artist=li_" + strExposureTime.str() + "_" + strReinforcement.str();
}
*/
// libcamera: if the red and blue numbers are given it turns off AWB.
// Check if R and B component are given
if (cg.currentAutoAWB) {
command += " --awb auto";
}
else {
if (! cg.isLibcamera)
command += " --awb off"; // raspistill requires explicitly turning off
// If we don't specify when they are the default then auto mode is enabled.
ss.str("");
ss << cg.currentWBR << "," << cg.currentWBB;
command += " --awbgains " + ss.str();
}
if (cg.rotation != cg.defaultRotation) {
ss.str("");
ss << cg.rotation;
command += " --rotation " + ss.str();
}
if (cg.flip != cg.defaultFlip) {
if (cg.flip == 1 || cg.flip == 3)
command += " --hflip"; // horizontal flip
if (cg.flip == 2 || cg.flip == 3)
command += " --vflip"; // vertical flip
}
if (cg.saturation != cg.defaultSaturation) {
ss.str("");
ss << cg.saturation;
command += " --saturation "+ ss.str();
}
if (cg.contrast != cg.defaultContrast) {
ss.str("");
ss << cg.contrast;
command += " --contrast "+ ss.str();
}
if (cg.sharpness != cg.defaultSharpness) {
ss.str("");
ss << cg.sharpness;
command += " --sharpness "+ ss.str();
}
if (cg.quality != cg.defaultQuality) {
ss.str("");
ss << cg.quality;
command += " --quality " + ss.str();
}
if (*cg.extraArgs)
{
// add the extra arguments as is; do not parse them
ss.str("");
ss << cg.extraArgs;
command += " " + ss.str();
}
// Log the command we're going to run without the
// LIBCAMERA_LOG...
// string and without any redirect of stdout or stderr.
// Those strings confuse some users.
// Tried putting this in putenv() but it didn't seem to work.
char const *s1 = "LIBCAMERA_LOG_LEVELS=ERROR,FATAL ";
string s2 = "";
if (cg.isLibcamera)
{
// If there have been 2 consecutive errors, chances are this one will fail too,
// so capture the error message.
if (cg.debugLevel >= 3 || numConsecutiveErrors >= 2)
s2 = " > " + errorOutput + " 2>&1";
else
s2 = " 2> /dev/null"; // gets rid of a bunch of libcamera verbose messages
}
// Create the command we'll actually run.
// It needs to include the current size plus future strings s1 and s2.
int size = command.length() + strlen(s1) + s2.length() + 10; // +10 "just in case"
char cmd[size];
strncpy(cmd, command.c_str(), size-1);
Log(2, " > Running: %s\n", cmd);
if (cg.isLibcamera)
{
command = s1 + command + s2;
strncpy(cmd, command.c_str(), size-1);
}
// Execute the command.
int ret = system(cmd);
if (WIFEXITED(ret) && WEXITSTATUS(ret) == 0)
{
*image = cv::imread(cg.fullFilename, cv::IMREAD_UNCHANGED);
if (! image->data) {
Log(-1, "*** %s: WARNING: Error re-reading file '%s'; skipping further processing.\n",
cg.ME, basename(cg.fullFilename));
}
ret = 0; // Makes it easier for caller to determine if there was an error.
}
// else, error message is printed by caller.
return(ret);
}
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
CG.ME = basename(argv[0]);
CG.allskyHome = getenv("ALLSKY_HOME");
if (CG.allskyHome == NULL)
{
Log(0, "*** %s: ERROR: ALLSKY_HOME not set!\n", CG.ME);
exit(EXIT_ERROR_STOP);
}
if (! getCommandLineArguments(&CG, argc, argv, false))
{
// getCommandLineArguments outputs an error message.
exit(EXIT_ERROR_STOP);
}
char bufTime[128] = { 0 };
char bufTemp[1024] = { 0 };
char const *bayer[] = { "RG", "BG", "GR", "GB" };
bool justTransitioned = false;
ASI_ERROR_CODE asiRetCode; // used for return code from ASI functions.
int retCode;
cv::Mat pRgb; // the image
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
setlinebuf(stdout); // Line buffer output so entries appear in the log immediately.
CG.ct = ctRPi;
processConnectedCameras(); // exits on error
ASI_CAMERA_INFO ASICameraInfo;
// This gives a segmentation fault if cameraNumber isn't connected.
asiRetCode = ASIGetCameraProperty(&ASICameraInfo, CG.cameraNumber);
if (asiRetCode != ASI_SUCCESS)
{
Log(0, "*** %s: ERROR: ASIGetCamerProperty() returned: %s\n", CG.ME, getRetCode(asiRetCode));
exit(EXIT_ERROR_STOP);
}
asiRetCode = ASIGetNumOfControls(CG.cameraNumber, &iNumOfCtrl);
if (asiRetCode != ASI_SUCCESS)
{
Log(0, "*** %s: ERROR: ASIGetNumOfControls() returned: %s\n", CG.ME, getRetCode(asiRetCode));
exit(EXIT_ERROR_STOP);
}
// Set defaults that depend on the camera type.
if (! setDefaults(&CG, ASICameraInfo))
closeUp(EXIT_ERROR_STOP);
if (CG.configFile[0] != '\0' && ! getConfigFileArguments(&CG))
{
// getConfigFileArguments() outputs error messages
exit(EXIT_ERROR_STOP);
}
if (! CG.saveCC && ! CG.help)
{
displayHeader(CG);
}
doLocale(&CG);
if (CG.help)
{
displayHelp(CG);
closeUp(EXIT_OK);
}
// Do argument error checking if we're not going to exit soon.
if (! CG.saveCC && ! validateSettings(&CG, ASICameraInfo))
closeUp(EXIT_ERROR_STOP);
if (! checkForValidExtension(&CG)) {
// checkForValidExtension() displayed the error message.
closeUp(EXIT_ERROR_STOP);
}
errorOutput = CG.saveDir;
errorOutput += "/capture_RPi_debug.txt";
int iMaxWidth, iMaxHeight;
double pixelSize;
iMaxWidth = ASICameraInfo.MaxWidth;
iMaxHeight = ASICameraInfo.MaxHeight;
pixelSize = ASICameraInfo.PixelSize;
if (CG.width == 0 || CG.height == 0)
{
CG.width = iMaxWidth;
CG.height = iMaxHeight;
}
else
{
validateLong(&CG.width, 0, iMaxWidth, "Width", false);
validateLong(&CG.height, 0, iMaxHeight, "Height", false);
}
long originalWidth = CG.width;
long originalHeight = CG.height;
// Limit these to a reasonable value based on the size of the sensor.
validateLong(&CG.overlay.iTextLineHeight, 0, (long)(iMaxHeight / 2), "Line Height", true);
validateLong(&CG.overlay.iTextX, 0, (long)iMaxWidth - 10, "Text X", true);
validateLong(&CG.overlay.iTextY, 0, (long)iMaxHeight - 10, "Text Y", true);
validateFloat(&CG.overlay.fontsize, 0.1, iMaxHeight / 2, "Font Size", true);
validateLong(&CG.overlay.linewidth, 0, (long)(iMaxWidth / 2), "Font Weight", true);
if (CG.saveCC)
{
saveCameraInfo(ASICameraInfo, CG.CC_saveFile, iMaxWidth, iMaxHeight, pixelSize, bayer[ASICameraInfo.BayerPattern]);
closeUp(EXIT_OK);
}
outputCameraInfo(ASICameraInfo, CG, iMaxWidth, iMaxHeight, pixelSize, bayer[ASICameraInfo.BayerPattern]);
// checkExposureValues() must come after outputCameraInfo().
(void) checkExposureValues(&CG);
// Handle "auto" imageType.
if (CG.imageType == AUTO_IMAGE_TYPE)
{
// user will have to manually set for 8- or 16-bit mono mode
CG.imageType = IMG_RGB24;
}
if (CG.imageType == IMG_RAW16)
{
CG.sType = "RAW16";
currentBpp = 2;
currentBitDepth = 16;
}
else if (CG.imageType == IMG_RGB24)
{
CG.sType = "RGB24";
currentBpp = 3;
currentBitDepth = 8;
}
else if (CG.imageType == IMG_RAW8)
{
CG.sType = "RAW8";
currentBpp = 1;
currentBitDepth = 8;
}
else
{
Log(0, "*** %s: ERROR: Unknown Image Type: %d\n", CG.ME, CG.imageType);
exit(EXIT_ERROR_STOP);
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
displaySettings(CG);
// Initialization
int originalITextX = CG.overlay.iTextX;
int originalITextY = CG.overlay.iTextY;
int originalFontsize = CG.overlay.fontsize;
int originalLinewidth = CG.overlay.linewidth;
// Have we displayed "not taking picture during day/night" messages, if applicable?
bool displayedNoDaytimeMsg = false;
bool displayedNoNighttimeMsg = false;
// Start taking pictures
while (bMain)
{
// Find out if it is currently DAY or NIGHT
dayOrNight = calculateDayOrNight(CG.latitude, CG.longitude, CG.angle);
std::string lastDayOrNight = dayOrNight;
if (CG.takeDarkFrames)
{
// We're doing dark frames so turn off autoexposure and autogain, and use
// nightime gain, delay, and exposure to mimic a nightime shot.
CG.currentSkipFrames = 0;
CG.currentAutoExposure = false;
CG.nightAutoExposure = false;
CG.currentExposure_us = CG.nightMaxAutoExposure_us;
CG.currentMaxAutoExposure_us = CG.nightMaxAutoExposure_us;
if (CG.isColorCamera)
{
CG.currentAutoAWB = false;
CG.currentWBR = CG.nightWBR;
CG.currentWBB = CG.nightWBB;
}
CG.currentDelay_ms = CG.nightDelay_ms;
CG.currentBin = CG.nightBin;
CG.currentGain = CG.nightGain;
// not needed since we're not using auto gain, but set to be consistent
CG.currentMaxAutoGain = CG.nightMaxAutoGain;
CG.currentAutoGain = false;
CG.myModeMeanSetting.currentMean = NOT_SET;
CG.myModeMeanSetting.currentMean_threshold = NOT_SET;
if (CG.isCooledCamera)
{
CG.currentEnableCooler = CG.nightEnableCooler;
CG.currentTargetTemp = CG.nightTargetTemp;
}
CG.currentTuningFile = CG.nightTuningFile;
Log(1, "Taking dark frames...\n");
if (CG.notificationImages) {
(void) displayNotificationImage("--expires 0 DarkFrames &");
}
}
else if (dayOrNight == "DAY")
{
if (justTransitioned == true)
{
// Just transitioned from night to day, so execute end of night script
Log(1, "Processing end of night data\n");
snprintf(bufTemp, sizeof(bufTemp)-1, "%s/scripts/endOfNight.sh &", CG.allskyHome);
// Not too useful to check return code for commands run in the background.
system(bufTemp);
justTransitioned = false;
displayedNoDaytimeMsg = false;
}
if (! CG.daytimeCapture)
{
// true == for daytime
displayedNoDaytimeMsg = day_night_timeSleep(displayedNoDaytimeMsg, CG, true);
// No need to do any of the code below so go back to the main loop.
continue;
}
Log(1, "==========\n=== Starting daytime capture ===\n==========\n");
if (numExposures == 0 && CG.dayAutoExposure)
CG.currentSkipFrames = CG.daySkipFrames;
// We only skip initial frames if we are starting in daytime and using auto-exposure.
CG.currentAutoExposure = CG.dayAutoExposure;
CG.currentExposure_us = CG.dayExposure_us;
CG.currentMaxAutoExposure_us = CG.dayMaxAutoExposure_us;
if (CG.isColorCamera)
{
CG.currentAutoAWB = CG.dayAutoAWB;
CG.currentWBR = CG.dayWBR;
CG.currentWBB = CG.dayWBB;
}
CG.currentDelay_ms = CG.dayDelay_ms;
CG.currentBin = CG.dayBin;
CG.currentGain = CG.dayGain;
CG.currentMaxAutoGain = CG.dayMaxAutoGain;
CG.currentAutoGain = CG.dayAutoGain;
CG.myModeMeanSetting.currentMean = CG.myModeMeanSetting.dayMean;
CG.myModeMeanSetting.currentMean_threshold = CG.myModeMeanSetting.dayMean_threshold;
if (CG.isCooledCamera)
{
CG.currentEnableCooler = CG.dayEnableCooler;
CG.currentTargetTemp = CG.dayTargetTemp;
}
CG.currentTuningFile = CG.dayTuningFile;
}
else // NIGHT
{
if (justTransitioned == true)
{
// Just transitioned from day to night, so execute end of day script
Log(1, "Processing end of day data\n");
snprintf(bufTemp, sizeof(bufTemp)-1, "%s/scripts/endOfDay.sh &", CG.allskyHome);
// Not too useful to check return code for commands run in the background.
system(bufTemp);
justTransitioned = false;
}
if (! CG.nighttimeCapture)
{
// false == for nighttime
displayedNoNighttimeMsg = day_night_timeSleep(displayedNoNighttimeMsg, CG, false);
// No need to do any of the code below so go back to the main loop.
continue;
}
Log(1, "==========\n=== Starting nighttime capture ===\n==========\n");
// We only skip initial frames if we are starting in nighttime and using auto-exposure.
if (numExposures == 0 && CG.nightAutoExposure)
CG.currentSkipFrames = CG.nightSkipFrames;
CG.currentAutoExposure = CG.nightAutoExposure;
CG.currentExposure_us = CG.nightExposure_us;
CG.currentMaxAutoExposure_us = CG.nightMaxAutoExposure_us;
if (CG.isColorCamera)
{
CG.currentAutoAWB = CG.nightAutoAWB;
CG.currentWBR = CG.nightWBR;
CG.currentWBB = CG.nightWBB;
}
CG.currentDelay_ms = CG.nightDelay_ms;
CG.currentBin = CG.nightBin;
CG.currentGain = CG.nightGain;
CG.currentMaxAutoGain = CG.nightMaxAutoGain;
CG.currentAutoGain = CG.nightAutoGain;
CG.myModeMeanSetting.currentMean = CG.myModeMeanSetting.nightMean;
CG.myModeMeanSetting.currentMean_threshold = CG.myModeMeanSetting.nightMean_threshold;
if (CG.isCooledCamera)
{
CG.currentEnableCooler = CG.nightEnableCooler;
CG.currentTargetTemp = CG.nightTargetTemp;
}
CG.currentTuningFile = CG.nightTuningFile;
}
// ========== Done with dark frame / day / night settings
if (CG.myModeMeanSetting.currentMean > 0.0)
{
// Using Allsky auto-exposure/gain algorithm
CG.myModeMeanSetting.modeMean = true;
if (! aegInit(CG, myRaspistillSetting, myModeMeanSetting))
{
closeUp(EXIT_ERROR_STOP);
}
}
else
{
CG.myModeMeanSetting.modeMean = false;
myModeMeanSetting.meanAuto = MEAN_AUTO_OFF;
}
// TODO: after merging myModeMeanSetting into CG.myModeMeanSetting, delete this line.
myModeMeanSetting.modeMean = CG.myModeMeanSetting.modeMean;
// Want initial exposures to have the exposure time and gain the user specified.
if (numExposures == 0)
{
myRaspistillSetting.shutter_us = CG.currentExposure_us;
myRaspistillSetting.analoggain = CG.currentGain;
}
if (numExposures == 0 || CG.dayBin != CG.nightBin)
{
// Adjusting variables for chosen binning.
// Only need to do at the beginning and if bin changes.
CG.height = originalHeight / CG.currentBin;
CG.width = originalWidth / CG.currentBin;
CG.overlay.iTextX = originalITextX / CG.currentBin;
CG.overlay.iTextY = originalITextY / CG.currentBin;
CG.overlay.fontsize = originalFontsize / CG.currentBin;
CG.overlay.linewidth = originalLinewidth / CG.currentBin;
if (numExposures > 0)
{
// If not the first time, free the prior pRgb.
pRgb.release();
}
if (CG.imageType == IMG_RAW16)
{
pRgb.create(cv::Size(CG.width, CG.height), CV_16UC1);
}
else if (CG.imageType == IMG_RGB24)
{
pRgb.create(cv::Size(CG.width, CG.height), CV_8UC3);
}
else // RAW8 and Y8
{
pRgb.create(cv::Size(CG.width, CG.height), CV_8UC1);
}
}
// Here and below, indent sub-messages with " > " so it's clear they go with the un-indented line.
// This simply makes it easier to see things in the log file.
// Wait for switch day time -> night time or night time -> day time
while (bMain && lastDayOrNight == dayOrNight)
{
// date/time is added to many log entries to make it easier to associate them
// with an image (which has the date/time in the filename).
exposureStartDateTime = getTimeval();
char exposureStart[128];
snprintf(exposureStart, sizeof(exposureStart), "%s", formatTime(exposureStartDateTime, "%F %T"));
Log(2, "-----\n");
Log(1, "STARTING EXPOSURE at: %s @ %s\n", exposureStart, length_in_units(myRaspistillSetting.shutter_us, true));
// Get start time for overlay. Make sure it has the same time as exposureStart.
if (CG.overlay.showTime)
{
sprintf(bufTime, "%s", formatTime(exposureStartDateTime, CG.timeFormat));
}
// For dark frames we already know the finalFilename.
if (! CG.takeDarkFrames)
{
// Create the name of the file that goes in the images/<date> directory.
snprintf(CG.finalFileName, sizeof(CG.finalFileName), "%s-%s.%s",
CG.fileNameOnly, formatTime(exposureStartDateTime, "%Y%m%d%H%M%S"), CG.imageExt);
snprintf(CG.fullFilename, sizeof(CG.fullFilename), "%s/%s", CG.saveDir, CG.finalFileName);
}
// Capture and save image
retCode = RPicapture(CG, &pRgb);
if (retCode == 0)
{
numExposures++;
numConsecutiveErrors = 0;
// We currently have no way to get the actual white balance values,
// so use what the user requested.
CG.lastWBR = CG.currentWBR;
CG.lastWBB = CG.currentWBB;
CG.lastFocusMetric = CG.determineFocus ? (int)round(get_focus_metric(pRgb)) : -1;
// If takeDarkFrames is off, add overlay text to the image
if (! CG.takeDarkFrames)
{
CG.lastExposure_us = myRaspistillSetting.shutter_us;
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF)
{
CG.lastGain = myRaspistillSetting.analoggain;
}
else
{
CG.lastGain = CG.currentGain; // ZWO gain=0.1 dB , RPi gain=factor
}
CG.lastMean = aegCalcMean(pRgb, true);
if (myModeMeanSetting.meanAuto != MEAN_AUTO_OFF)
{
// set myRaspistillSetting.shutter_us and myRaspistillSetting.analoggain
aegGetNextExposureSettings(&CG, myRaspistillSetting, myModeMeanSetting);
if (CG.lastMean == -1)
{
Log(-1, "*** %s: ERROR: aegCalcMean() returned mean of -1.\n", CG.ME);
Log(2, " > Sleeping from failed exposure: %.1f seconds\n", (float)CG.currentDelay_ms / MS_IN_SEC);
usleep(CG.currentDelay_ms * US_IN_MS);
continue;
}
}
else {
myRaspistillSetting.shutter_us = CG.currentExposure_us;
myRaspistillSetting.analoggain = CG.currentGain;
}
if (CG.currentSkipFrames == 0 &&
CG.overlay.overlayMethod == OVERLAY_METHOD_LEGACY &&
doOverlay(pRgb, CG, bufTime, 0) > 0)
{
// if we added anything to overlay, write the file out
bool result = cv::imwrite(CG.fullFilename, pRgb, compressionParameters);
if (! result) fprintf(stderr, "*** ERROR: Unable to write to '%s'\n", CG.fullFilename);
}
}
// We skip the initial frames to give auto-exposure time to
// lock in on a good exposure. If it does that quickly, stop skipping images.
if (CG.goodLastExposure && CG.currentSkipFrames > 0)
{
Log(2, " >>>> Turning off Skip Frames\n");
CG.currentSkipFrames = 0;
}
if (CG.currentSkipFrames > 0)
{
CG.currentSkipFrames--;
Log(2, " >>>> Skipping this frame. %d left to skip\n", CG.currentSkipFrames);
// Do not save this frame or sleep after it.
// We just started taking images so no need to check if DAY or NIGHT changed
if (remove(CG.fullFilename) != 0)
Log(0, "*** %s: ERROR: Unable to remove '%s': %s\n",
CG.ME, CG.fullFilename, strerror(errno));
continue;
}
else
{
char cmd[1100+strlen(CG.allskyHome)];
Log(1, " > Saving %s image '%s'\n", CG.takeDarkFrames ? "dark" : dayOrNight.c_str(), CG.finalFileName);
snprintf(cmd, sizeof(cmd), "%s/scripts/saveImage.sh %s '%s'", CG.allskyHome, dayOrNight.c_str(), CG.fullFilename);
add_variables_to_command(CG, cmd, exposureStartDateTime);
strcat(cmd, " &");
// Not too useful to check return code for commands run in the background.
system(cmd);
}
std::string s;
if (CG.currentAutoExposure)
s = "auto";
else
s = "manual";
delayBetweenImages(CG, myRaspistillSetting.shutter_us, s);
}
else
{
// Unable to take picture.
// The child command is "/bin/sh" will will basically never get a signal
// even if the camera program does, so check for a signal in WEXITSTATUS() not retCode.
if (WIFSIGNALED(WEXITSTATUS(retCode)))
{
Log(1, " >>> %s: WARNING: %s received signal %d, retCode=0x%x\n",
CG.ME, CG.cmdToUse, WTERMSIG(WEXITSTATUS(retCode)), retCode);
}
else if (WIFEXITED(retCode))
{
// "Normal" return but command failed.
Log(1, " >>> %s: WARNING: Unable to take picture, return code=0x%0x, WEXITSTATUS=0x%0x\n",
CG.ME, retCode, WEXITSTATUS(retCode));
}
else
{
// Not sure what this would be...
Log(1, " >>> %s: WARNING: Unable to take picture, command did not return normally, return code=0x%0x WEXITSTATUS=0x%0x\n",
CG.ME, retCode, WEXITSTATUS(retCode));
}
numTotalErrors++;
numConsecutiveErrors++;
if (numConsecutiveErrors >= maxErrors)
{
Log(0, "*** %s: ERROR: maximum number of consecutive errors of %d reached; capture program stopped. Total errors=%'d.\n", CG.ME, maxErrors, numTotalErrors);
Log(0, "Make sure cable between camera and Pi is all the way in.\n");
Log(0, "Look in '%s' for details.\n", errorOutput.c_str());
closeUp(EXIT_ERROR_STOP);
}
// Don't wait the full amount of time on error in case the error was a one off.
long timeToSleep = (float)CG.currentDelay_ms * .25;
Log(2, " > Sleeping from failed exposure: %.1f seconds\n", (float)timeToSleep / MS_IN_SEC);
usleep(timeToSleep * US_IN_MS);
}
// Check for day or night based on location and angle
dayOrNight = calculateDayOrNight(CG.latitude, CG.longitude, CG.angle);
}
if (lastDayOrNight != dayOrNight)
justTransitioned = true;
}
closeUp(EXIT_OK);
}