-
Notifications
You must be signed in to change notification settings - Fork 0
/
LunAero.cpp
1296 lines (1206 loc) · 39 KB
/
LunAero.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
/*
* C_LunAero/LunAero.cpp - Primary file for robotic moon tracking scope
* Copyright (C) <2020> <Wesley T. Honeycutt>
*
* This program 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 program 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/>.
*/
/*
* C_LunAero moontracking program
*
*
* Requirements and Dependencies:
* - Raspberry Pi v4
* - wiringPi v2.52+
* While waiting for official release, use:
* cd /tmp
* wget https://project-downloads.drogon.net/wiringpi-latest.deb
* sudo dpkg -i wiringpi-latest.deb
* - bcm_host.h
* (default with Raspberry Pi)
* - GTK3.0
* To compile, you need the dev files "libgtk-3-dev"
* The libraries to run should be already on the Raspberry Pi
* - pthread
* (default with Raspberry Pi)
*
*
* Compilation instructions
* Open terminal and run the commands:
* cd /path/to/C_LunAero
* make
*
* Run instructions
* You must have a drive installed at /media/pi/MOON1
* Then run
* ./LunAero_Moontracker
*/
#include "LunAero.hpp"
/**
* This function (called from gtk_LunAero.cpp) handles checking the frame during live operation and
* processes the LOST_COUNTER
*
*/
void cb_framecheck() {
printf("getting current frame\n");
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "Time in Milliseconds ="
<< std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()
<< std::endl;
LOGGING.close();
}
current_frame();
//~ frame_centroid();
if (*val_ptr.LOST_COUNTERaddr == LOST_THRESH) {
sem_wait(&LOCK);
*val_ptr.ABORTaddr = 1;
sem_post(&LOCK);
}
}
/**
* This function is called on exit to clean up the environment. Primarily used to ensure that the
* raspivid system call is killed properly.
*
*
*/
void cleanup () {
// Placeholder in case we need to clean anything up on exit.
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "killing run" << std::endl;
LOGGING.close();
}
kill_raspivid();
usleep(1000000);
}
/**
* This function kills raspivid. It does this in an ugly way by finding the process ID of the raspivid
* instance and killing that PID.
*
*
*/
void kill_raspivid () {
sem_wait(&LOCK);
*val_ptr.STOP_DIRaddr = 3;
sem_post(&LOCK);
// HACK - This is a filthy hack to get kill the raspivid instance.
// Since fork interactions are hard, we will use this for now.
char line[1024];
FILE *cmd = popen("pidof raspivid", "r");
fgets(line, 1024, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
if (kill(pid, SIGINT) != 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: Unable to kill raspivid" << std::endl;
LOGGING.close();
}
}
}
/**
* This funciton creates an ID file to be stored with the recorded video which includes information
* about the unit the run worked on. The processor id is stored using the cpuinfo unique to the
* Raspberry Pi.
*
* @return status
*/
int create_id_file() {
std::ofstream idfile;
char line[1024];
FILE *cmd = popen("cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2", "r");
fgets(line, 1024, cmd);
pclose(cmd);
std::string linestr(line);
linestr.erase(std::remove(linestr.begin(), linestr.end(), '\n'), linestr.end());
IDPATH = FILEPATH + "/" + linestr + ".txt";
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "LUID: " << linestr << std::endl
<< "idpath: " << IDPATH << std::endl;
LOGGING.close();
}
std::string gmt = current_time(1);
idfile.open(IDPATH);
idfile
<< "LUID: "
<< linestr
<< std::endl
<< "UTC : "
<< gmt
<< std::endl;
idfile.close();
return 0;
}
/**
* This is a helper funciton which updates the abort code to end the run across all forks.
*
*
*/
void abort_code() {
sem_wait(&LOCK);
*val_ptr.ABORTaddr = 1;
sem_post(&LOCK);
}
/**
* This function fetches the current time and formats it as a string.
*
* @param gmt plus or minus tmz gmt
* @return str the current time formatted as a string
*/
std::string current_time(int gmt) {
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time(&rawtime);
if (gmt > 0) {
timeinfo = gmtime(&rawtime);
} else {
timeinfo = localtime(&rawtime);
}
strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", timeinfo);
std::string str(buffer);
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< str << std::endl;
LOGGING.close();
}
return str;
}
/**
* This function captures the frame from the preview window (a complicated process involving capturing
* the VC/DISPMANX screenshot, not the X window screensho)t, crops it to ignore the GTK, and determines
* how well the moon is centered in the cropped frame. Priority is given to checking whether the moon
* is touching the side of the cropped image. If the edge is not being touched, threshold limited
* brightness is used to find the center of mass of the bright spot and comparing it to the target
* location.
*
*
*/
void current_frame() {
// Don't bother if we have already told the program to abort
if (*val_ptr.ABORTaddr == 1) {
return;
}
// Run a system check to see if raspivid is running, if not, print a warning
FILE* fpidof = popen("pidof raspivid", "r");
if (fpidof) {
int p=0;
if (!(fscanf(fpidof, "%d", &p)>0 && p>0)) {
int local_cnt = *val_ptr.LOST_COUNTERaddr;
local_cnt = local_cnt + 1;
sem_wait(&LOCK);
*val_ptr.LOST_COUNTERaddr = local_cnt;
sem_post(&LOCK);
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "WARNING: lost moon counter increased to"
<< *val_ptr.LOST_COUNTERaddr
<< " cycles due to failure to find raspivid"
<< std::endl;
LOGGING.close();
}
pclose(fpidof);
return;
}
pclose(fpidof);
} else {
int local_cnt = *val_ptr.LOST_COUNTERaddr;
local_cnt = local_cnt + 1;
sem_wait(&LOCK);
*val_ptr.LOST_COUNTERaddr = local_cnt;
sem_post(&LOCK);
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "WARNING: lost moon counter increased to "
<< *val_ptr.LOST_COUNTERaddr
<< " cycles due to failure to find raspivid"
<< std::endl;
LOGGING.close();
}
return;
}
DISPMANX_DISPLAY_HANDLE_T display = 0;
DISPMANX_MODEINFO_T info;
DISPMANX_RESOURCE_HANDLE_T resource = 0;
VC_IMAGE_TYPE_T type = VC_IMAGE_RGB888;
DISPMANX_TRANSFORM_T transform = static_cast <DISPMANX_TRANSFORM_T> (0);
VC_RECT_T rect;
void *image;
uint32_t vc_image_ptr;
uint32_t screen = 0;
bcm_host_init();
// Get display info for the screen we are using.
display = vc_dispmanx_display_open( screen );
if (vc_dispmanx_display_get_info(display, &info) != 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: failed to get display info" << std::endl;
LOGGING.close();
}
*val_ptr.ABORTaddr = 1;
}
// This holds an image
image = calloc( 1, info.width * 3 * info.height );
if (!image) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: failed image assertion" << std::endl;
LOGGING.close();
}
*val_ptr.ABORTaddr = 1;
}
// Create space based on the screen info
resource = vc_dispmanx_resource_create( type, info.width, info.height, &vc_image_ptr);
if (!resource) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: failed to create VC Dispmanx Resource" << std::endl;
LOGGING.close();
}
*val_ptr.ABORTaddr = 1;
}
// Take a snapshot of the screen (stored in resource)
vc_dispmanx_snapshot(display, resource, transform);
// Read the rectangular data from resource into the image calloc
vc_dispmanx_rect_set(&rect, 0, 0, info.width, info.height);
vc_dispmanx_resource_read_data(resource, &rect, image, info.width*3);
// TODO - Make this an mmap stored image.
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< info.width << " x " << info.height << std::endl;
LOGGING.close();
}
std::string imgstr(static_cast<char*>(image), info.width*3*info.height);
int local_height = RVD_HEIGHT - 6;
int local_width = RVD_WIDTH - 4;
int local_xcorn = RVD_XCORN + 2;
int local_ycorn = RVD_YCORN + 3;
int matrix[local_height][local_width];
int wcnt = 0;
int hcnt = 0;
int wcnt_prime = 0;
int hcnt_prime = 0;
for (unsigned int i=0; i<imgstr.size(); i=i+3) {
if ((wcnt > (local_xcorn)) && (wcnt < (local_xcorn + local_width + 1))) {
if ((hcnt > (local_ycorn - 1)) && (hcnt < (local_ycorn + local_height))) {
int out;
out = 0.30*(int)imgstr[i] + 0.59*(int)imgstr[i+1] + 0.11*(int)imgstr[i+2];
if (out > 25) { // 10% threshold
out = 0;
} else {
out = 1;
}
matrix[hcnt_prime][wcnt_prime] = out;
wcnt_prime += 1;
if (wcnt_prime == local_width) {
wcnt_prime = 0;
hcnt_prime += 1;
}
}
}
wcnt += 1;
if (wcnt == info.width) {
wcnt = 0;
hcnt += 1;
}
}
// Optionally, save the image to a file on the disk so we can check that it makes sense
if (SAVE_DEBUG_IMAGE) {
std::string userenv = std::getenv("USER");
std::string filestr = "/media/" + userenv + "/" + DRIVE_NAME + "/out.pbm";
FILE *fp = fopen(filestr.c_str(), "wb");
//~ fprintf(fp, "P1\n%d %d\n1\n", frmwidth, frmheight);
fprintf(fp, "P1\n%d %d\n1\n", local_width, local_height);
for (int i=0; i<local_height; i++) {
for(int j=0; j<local_width; j++) {
if (j == local_width-1) {
fprintf(fp, "\n");
}
fprintf(fp, "%d", matrix[i][j]);
}
}
fclose(fp);
}
// Cleanup the VC resources
if (vc_dispmanx_resource_delete(resource) != 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: failed to delete vc resource" << std::endl;
LOGGING.close();
}
*val_ptr.ABORTaddr = 1;
}
if (vc_dispmanx_display_close(display) != 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "ERROR: failed to close vc display" << std::endl;
LOGGING.close();
}
*val_ptr.ABORTaddr = 1;
}
free(image);
//Check for bright or dark spots?
// Note the weird inversion when we use .pbm format
// Bright spots == 0
// dark spots == 1
int checkval = 0;
// Number of points to be "on edge" is 10% of edge
int w_thresh = WORK_WIDTH/EDGE_DIVISOR_W;
int h_thresh = WORK_HEIGHT/EDGE_DIVISOR_H;
// Store sum of each edge
int top_edge = 0;
int bottom_edge = 0;
int left_edge = 0;
int right_edge = 0;
for (int j=0; j<local_width; j++) {
if (matrix[0][j] == checkval) {
top_edge++;
}
if (matrix[local_height-1][j] == checkval) {
bottom_edge++;
}
}
for (int k=0; k<local_height; k++) {
if (matrix[k][0] == checkval) {
left_edge++;
}
if (matrix[k][local_width-1] == checkval) {
right_edge++;
}
}
// Calculate the centroid using sum.
int sumx = 0;
int sumy = 0;
int mcnt = 0;
for (int k=0; k<local_height; k++) {
for (int j=0; j<local_width; j++) {
if (matrix[k][j] == checkval) {
sumx = sumx + j;
sumy = sumy + k;
mcnt++;
}
}
}
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "sumx " << sumx << " sumy " << sumy << std::endl;
LOGGING.close();
}
// If nothing is found, return an increment to the moon loss counter
if ((sumx == 0) && (sumy == 0)) {
int local_cnt = *val_ptr.LOST_COUNTERaddr;
local_cnt = local_cnt + 1;
sem_wait(&LOCK);
*val_ptr.LOST_COUNTERaddr = local_cnt;
sem_post(&LOCK);
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "lost moon for " << *val_ptr.LOST_COUNTERaddr << " cycles" << std::endl;
LOGGING.close();
}
} else {
// something was found, reset moon loss counter
sem_wait(&LOCK);
*val_ptr.LOST_COUNTERaddr = 0;
sem_post(&LOCK);
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "Moon found centered at (" << (sumx/mcnt) << ", " << (sumy/mcnt) << ")\n" << std::endl
<< "top:bottom::left:right " << top_edge << ":" << bottom_edge << "::" << left_edge
<< ":" << right_edge <<std::endl;
LOGGING.close();
}
// Report edges only
if ((top_edge >= w_thresh) && (bottom_edge < w_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "+top edge" << std::endl;
LOGGING.close();
}
}
if ((bottom_edge >= w_thresh) && (top_edge < w_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "+bottom edge" << std::endl;
LOGGING.close();
}
}
if ((left_edge >= h_thresh) && (right_edge < h_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "+left edge" << std::endl;
LOGGING.close();
}
}
if ((left_edge <= h_thresh) && (right_edge > h_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "+right edge" << std::endl;
LOGGING.close();
}
}
// Check if bright spot is near the edge of the frame
// If so, move away from that edge
// If not or near both edges, use centroid
if ((top_edge >= w_thresh) && (bottom_edge < w_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "detected light on top edge" << std::endl;
LOGGING.close();
}
mot_up_command();
} else if ((bottom_edge >= w_thresh) && (top_edge < w_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "detected light on bottom edge" << std::endl;
LOGGING.close();
}
mot_down_command();
} else {
if (abs((sumy/mcnt)-(local_height/2)) > ((local_height/2)*0.2)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "M_y = " << (sumy/mcnt)-(local_height/2) << std::endl;
LOGGING.close();
}
if (((sumy/mcnt)-(local_height/2)) > 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "centroid moving to down" << std::endl;
LOGGING.close();
}
mot_down_command();
} else {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "centroid moving to up" << std::endl;
LOGGING.close();
}
mot_up_command();
}
} else {
sem_wait(&LOCK);
*val_ptr.STOP_DIRaddr = 2;
sem_post(&LOCK);
}
}
if ((left_edge >= h_thresh) && (right_edge < h_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "detected light on left edge" << std::endl;
LOGGING.close();
}
mot_left_command();
} else if ((left_edge <= h_thresh) && (right_edge > h_thresh)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "detected light on right edge" << std::endl;
LOGGING.close();
}
mot_right_command();
} else {
if (abs((sumx/mcnt)-(local_width/2)) > ((local_width/2)*0.4)) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "M_x = " << (sumx/mcnt)-(local_width/2) << std::endl;
LOGGING.close();
}
if (((sumx/mcnt)-(local_width/2)) > 0) {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "centroid moving to right" << std::endl;
LOGGING.close();
}
mot_right_command();
} else {
if (DEBUG_COUT) {
LOGGING.open(LOGOUT, std::ios_base::app);
LOGGING
<< "centroid moving to left" << std::endl;
LOGGING.close();
}
mot_left_command();
}
} else {
// In the case we already need to stop one direction, stop both
if (*val_ptr.STOP_DIRaddr == 2) {
sem_wait(&LOCK);
*val_ptr.STOP_DIRaddr = 3;
sem_post(&LOCK);
} else {
sem_wait(&LOCK);
*val_ptr.STOP_DIRaddr = 1;
sem_post(&LOCK);
}
}
}
}
return;
}
/**
* This function takes the two input strings and uses them to issue a notificaiton alert to the
* Raspian desktop. This is a variation of the linux command ``notify-send``, and it requires
* libnotify-dev installed on the system.
*
* @param input1 The string which should be at the top of the notification
* @param input2 The string's bottomtext, more descriptive.
* @return status
*/
int notify_handler(std::string input1, std::string input2) {
notify_init("LunAero");
NotifyNotification* n = notify_notification_new (input1.c_str(), input2.c_str(), 0);
notify_notification_set_timeout(n, EMG_DUR); // 10 seconds
if (!notify_notification_show(n, 0)) {
std::cerr << "Libnotify failed. I hope you have terminal open!" << std::endl;
return -1;
}
return 0;
}
/**
* This function checks the save location properties to determine if the location is acceptable for
* saving video. This only has limited capacity to determine the fitness of the drive. The quality
* of the save location is determined based on 1) drive is findable 2) valid /proc/mounts file 3)
* remaining space left on the drive. Remaining space is calculated based on 1000000*t_s where t_s
* is the seconds a video will be recorded based on the settings.cfg line RECORD_DURATION. If the
* available space is less than this, the program stops with an error. If it is less than 10 times this
* value, a warning issued, but the program continues. Reported free space and available space on the
* drive is recorded in the log.
*
* @return status
*/
int startup_disk_check() {
std::string userenv = std::getenv("USER");
std::string local_path = "/media/" + userenv + "/" + DRIVE_NAME;
DEFAULT_FILEPATH = local_path + "/";
std::ifstream mountsfile("/proc/mounts", std::ifstream::in);
if (!mountsfile.good()) {
notify_handler("LunAero Error", "Input stream to /proc/mounts is not valid");
return 1;
}
int found_mount = 0;
std::string line;
while (std::getline(mountsfile, line)) {
if (line.find(local_path) != std::string::npos) {
found_mount = 1;
break;
}
}
mountsfile.close();
if (found_mount) {
namespace fs = std::filesystem;
fs::space_info tmp = fs::space(local_path);
if (tmp.available < (1000000 * std::chrono::duration<double>(RECORD_DURATION).count())) {
notify_handler("LunAero Error", "The space on this drive is too low with "
+ std::to_string(tmp.available)
+ " bytes remaining");
return 2;
} else if (tmp.available < (10 * 1000000 * std::chrono::duration<double>(RECORD_DURATION).count())) {
notify_handler("LunAero Warning", "This drive only has "
+ std::to_string(tmp.available)
+ " bytes of space remaining.");
}
DISK_OUTPUT.push_back("Free space: " + std::to_string(tmp.free));
DISK_OUTPUT.push_back("Available space: " + std::to_string(tmp.available));
return 0;
} else {
std::cout << "1" << std::endl;
notify_handler("LunAero Error", "Could not find a drive mounted at " + DEFAULT_FILEPATH);
return 3;
}
}
/**
* This function handles the strings and values parsed from the settings.cfg file and assigns them
* to the global values.
*
* @param name String obtained while parsing the settings.cfg file
* @param value The value associated with name from settings.cfg file
* @return status
*/
int parse_checklist(std::string name, std::string value) {
// Boolean cases
if (name == "DEBUG_COUT"
|| name == "SAVE_DEBUG_IMAGE"
) {
// Define booleans
bool result;
if (value == "true" || value == "True" || value == "TRUE") {
result = true;
} else if (value == "false" || value == "False" || value == "FALSE") {
result = false;
} else {
std::cerr << "Invalid boolean value in settings.cfg item: " << name << std::endl;
return 1;
}
if (name == "DEBUG_COUT") {
DEBUG_COUT = result;
} else if (name == "SAVE_DEBUG_IMAGE") {
SAVE_DEBUG_IMAGE = result;
}
}
// Int cases
else if (
name == "FONT_MOD"
|| name == "EDGE_DIVISOR_W"
|| name == "EDGE_DIVISOR_H"
|| name == "FRAMECHECK_FREQ"
|| name == "MMAL_ERROR_THRESH"
|| name == "RPI_FPS"
|| name == "RPI_BR"
|| name == "SHUT_JUMP"
|| name == "SHUT_JUMP_BIG"
|| name == "FREQ"
|| name == "MIN_DUTY"
|| name == "MAX_DUTY"
|| name == "BRAKE_DUTY"
|| name == "APINP"
|| name == "APIN1"
|| name == "APIN2"
|| name == "BPIN1"
|| name == "BPIN2"
|| name == "BPINP"
|| name == "EMG_DUR"
|| name == "LOST_THRESH"
|| name == "RAW_BRIGHT_THRESH"
) {
int result = std::stoi(value);
if (name == "FONT_MOD") {
FONT_MOD = result;
} else if (name == "EDGE_DIVISOR_W") {
EDGE_DIVISOR_W = result;
} else if (name == "EDGE_DIVISOR_H") {
EDGE_DIVISOR_H = result;
} else if (name == "FRAMECHECK_FREQ") {
FRAMECHECK_FREQ = result;
} else if (name == "MMAL_ERROR_THRESH") {
MMAL_ERROR_THRESH = result;
} else if (name == "RPI_FPS") {
RPI_FPS = result;
} else if (name == "RPI_BR") {
RPI_BR = result;
} else if (name == "SHUT_JUMP") {
SHUT_JUMP = result;
} else if (name == "SHUT_JUMP_BIG") {
SHUT_JUMP_BIG = result;
} else if (name == "FREQ") {
FREQ = result;
} else if (name == "MIN_DUTY") {
MIN_DUTY = result;
} else if (name == "MAX_DUTY") {
MAX_DUTY = result;
} else if (name == "BRAKE_DUTY") {
BRAKE_DUTY = result;
} else if (name == "APINP") {
APINP = result;
} else if (name == "APIN1") {
APIN1 = result;
} else if (name == "APIN2") {
APIN2 = result;
} else if (name == "BPIN1") {
BPIN1 = result;
} else if (name == "BPIN2") {
BPIN2 = result;
} else if (name == "BPINP") {
BPINP = result;
} else if (name == "EMG_DUR") {
EMG_DUR = result;
} else if (name == "LOST_THRESH") {
LOST_THRESH = result;
} else if (name == "RAW_BRIGHT_THRESH") {
RAW_BRIGHT_THRESH = result;
}
}
// Double cases
else if (
name == "RECORD_DURATION"
|| name == "LOOSE_WHEEL_DURATION"
) {
double result = std::stod(value);
if (name == "RECORD_DURATION") {
RECORD_DURATION = (std::chrono::duration<double>) result;
} else if (name == "LOOSE_WHEEL_DURATION") {
LOOSE_WHEEL_DURATION = (std::chrono::duration<double>) result;
}
}
// Float cases
else if (
name == "BRIGHT_THRESH"
) {
float result = std::stof(value);
if (name == "BRIGHT_THRESH") {
BRIGHT_THRESH = result;
}
}
// String cases
else if (
name == "KV_QUIT"
|| name == "KV_RUN"
|| name == "KV_LEFT"
|| name == "KV_RIGHT"
|| name == "KV_UP"
|| name == "KV_DOWN"
|| name == "KV_STOP"
|| name == "KV_REFRESH"
|| name == "KV_S_UP_UP"
|| name == "KV_S_DOWN_DOWN"
|| name == "KV_S_UP"
|| name == "KV_S_DOWN"
|| name == "KV_ISO"
|| name == "RPI_EX"
|| name == "DRIVE_NAME"
) {
if (name == "KV_QUIT") {
KV_QUIT = value;
} else if (name == "KV_RUN") {
KV_RUN = value;
} else if (name == "KV_LEFT") {
KV_LEFT = value;
} else if (name == "KV_RIGHT") {
KV_RIGHT = value;
} else if (name == "KV_UP") {
KV_UP = value;
} else if (name == "KV_DOWN") {
KV_DOWN = value;
} else if (name == "KV_STOP") {
KV_STOP = value;
} else if (name == "KV_REFRESH") {
KV_REFRESH = value;
} else if (name == "KV_S_UP_UP") {
KV_S_UP_UP = value;
} else if (name == "KV_S_DOWN_DOWN") {
KV_S_DOWN_DOWN = value;
} else if (name == "KV_S_UP") {
KV_S_UP = value;
} else if (name == "KV_S_DOWN") {
KV_S_DOWN = value;
} else if (name == "KV_ISO") {
KV_ISO = value;
} else if (name == "RPI_EX") {
RPI_EX = value;
} else if (name == "DRIVE_NAME") {
DRIVE_NAME = value;
}
} else {
std::cerr << "Did not recognize entry " << name << " in config file, skipping" << std::endl;
}
return 0;
}
/**
* This function is called to create a default settings config file. Creates the file in the current
* working directory as ./settings.cfg. If the file at that path after writing is smaller than 2kb,
* this function returns an error status.
*
* @return status
*/
int create_default_config() {
std::string config_loc = "./settings.cfg";
std::ofstream config_file;
config_file.open(config_loc);
config_file
<< "############# settings #############" << std::endl
<< "# #" << std::endl
<< "# part of LunAero_C #" << std::endl
<< "# Wesley T. Honeycutt, 2020 #" << std::endl
<< "# #" << std::endl
<< "# Comment lines with octothorpe #" << std::endl
<< "# Code lines must be \\n terminated #" << std::endl
<< "# #" << std::endl
<< "####################################" << std::endl << std::endl
<< "### General settings" << std::endl << std::endl
<< "# Save log with debugging output (prints everything verbose)" << std::endl
<< "DEBUG_COUT = true" << std::endl << std::endl
<< "# Duration to record video before starting a new one (in seconds)" << std::endl
<< "RECORD_DURATION = 1800" << std::endl
<< ""
<< "# The name given to your external storage drive for videos" << std::endl
<< "DRIVE_NAME = MOON1" << std::endl
<< ""
<< "# Should LunAero save a screenshot from the raspivid output every cycle?" << std::endl
<< "# It will be saved to to /your/path/out.ppm" << std::endl
<< "SAVE_DEBUG_IMAGE = false" << std::endl << std::endl << std::endl << std::endl << std::endl
<< "### Keybindings" << std::endl
<< "# If you want to use non-alphabetic keys, check to see what the key value is called before editing." << std::endl << std::endl
<< "# Keybinding for quit command" << std::endl
<< "KV_QUIT = q" << std::endl << std::endl
<< "# Keybinding for beginning the recording/entering automatic mode" << std::endl
<< "KV_RUN = Return" << std::endl << std::endl
<< "# Keybinding for left manual motor movement." << std::endl
<< "KV_LEFT = Left" << std::endl << std::endl
<< "# Keybinding for right manual motor movement." << std::endl
<< "KV_RIGHT = Right" << std::endl << std::endl
<< "# Keybinding for up manual motor movement." << std::endl
<< "KV_UP = Up" << std::endl << std::endl
<< "# Keybinding for down manual motor movement." << std::endl
<< "KV_DOWN = Down" << std::endl << std::endl
<< "# Keybinding for motor stop command." << std::endl
<< "KV_STOP = space" << std::endl << std::endl
<< "# Keybinding for raspivid refresh command." << std::endl
<< "KV_REFRESH = z" << std::endl << std::endl
<< "# Keybinding for greatly increasing the shutter speed." << std::endl
<< "KV_S_UP_UP = g" << std::endl << std::endl
<< "# Keybinding for greatly decreasing the shutter speed." << std::endl
<< "KV_S_DOWN_DOWN = b" << std::endl << std::endl
<< "# Keybinding for increasing the shutter speed." << std::endl
<< "KV_S_UP = h" << std::endl << std::endl
<< "# Keybinding for decreasing the shutter speed." << std::endl
<< "KV_S_DOWN = n" << std::endl << std::endl
<< "# Keybinding for cycling the ISO value." << std::endl
<< "KV_ISO = i" << std::endl << std::endl << std::endl << std::endl << std::endl
<< "### GUI settings" << std::endl << std::endl
<< "# Modifier value which effects the font size automatically determined for the GTK window" << std::endl
<< "FONT_MOD = 20" << std::endl << std::endl
<< "# Number of milliseconds an emergency message should remain on the desktop before disappearing in the" << std::endl
<< "# event of certain crash conditions." << std::endl
<< "EMG_DUR = 10" << std::endl << std::endl << std::endl << std::endl
<< "### Image processing settings" << std::endl << std::endl
<< "# Divisor for the number pixels on the top and bottom edges to warrant a move. Bigger is more sensitive." << std::endl
<< "EDGE_DIVISOR_W = 20" << std::endl << std::endl
<< "# Divisor for the number pixels on the left and right edges to warrant a move. Bigger is more sensitive." << std::endl
<< "EDGE_DIVISOR_H = 20" << std::endl << std::endl
<< "# Frequency which the automatic edge detection should occur. This is a value roughly in milliseconds," << std::endl
<< "# dependent on the cycle time of the processor." << std::endl
<< "# WARNING Editing this value changes a bunch of behaviors. You can touch it, but be careful."
<< std::endl
<< "FRAMECHECK_FREQ = 50" << std::endl << std::endl << std::endl << std::endl
<< "### Raspivid and Camera settings" << std::endl << std::endl
<< "# Number of MMAL errors encountered in a row before LunAero should crash with an error because something" << std::endl
<< "# has gone wrong with the hardware." << std::endl
<< "MMAL_ERROR_THRESH = 100" << std::endl << std::endl
<< "# Recording framerate of the raspivid command" << std::endl
<< "RPI_FPS = 30" << std::endl << std::endl
<< "# Recording bitrate for raspivid command" << std::endl
<< "RPI_BR = 8000000" << std::endl << std::endl
<< "# Recording exposure mode for raspivid command. Use a string from this list:" << std::endl
<< "# auto: use automatic exposure mode" << std::endl
<< "# night: select setting for night shooting" << std::endl
<< "# nightpreview:" << std::endl
<< "# backlight: select setting for backlit subject" << std::endl
<< "# spotlight:" << std::endl
<< "# sports: select setting for sports (fast shutter etc.)" << std::endl
<< "# snow: select setting optimised for snowy scenery" << std::endl
<< "# beach: select setting optimised for beach" << std::endl
<< "# verylong: select setting for long exposures" << std::endl
<< "# fixedfps: constrain fps to a fixed value" << std::endl
<< "# antishake: antishake mode" << std::endl
<< "# fireworks: select setting optimised for fireworks" << std::endl
<< "RPI_EX = auto" << std::endl << std::endl
<< "# Value to adjust the shutter speed when using up or down buttons." << std::endl
<< "SHUT_JUMP = 100" << std::endl << std::endl
<< "# Value to adjust the shutter speed when using the up-up or down-down buttons." << std::endl
<< "# Should be greater than SHUT_JUMP" << std::endl
<< "SHUT_JUMP_BIG = 1000" << std::endl << std::endl
<< "# Threshold value for number of cycles the moon is \"lost\" for" << std::endl
<< "LOST_THRESH = 30" << std::endl << std::endl << std::endl << std::endl
<< "### Motor and Speed settings" << std::endl << std::endl
<< "# Number of seconds the left-right motor should force high speed movement to compensate for loose" << std::endl
<< "# laser cut gears." << std::endl
<< "LOOSE_WHEEL_DURATION = 2" << std::endl << std::endl
<< "# PWM operation frequency in Hz" << std::endl
<< "FREQ = 10000" << std::endl << std::endl