-
Notifications
You must be signed in to change notification settings - Fork 0
/
stabilization_with_sensors.cpp
executable file
·1379 lines (1002 loc) · 43.1 KB
/
stabilization_with_sensors.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
//===================================================================
//= ** Drone Projected Image Stabilization =
//= Programmer : Eunbin Choi =
//= Date : 2019-11-10 =
//= Updates : MODEL CHANGING =
//===================================================================
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h> // File Control Definitions
#include <termios.h> // POSIX Terminal Control Definitions
#include <unistd.h> // UNIX Standard Definitions
#include <errno.h> // ERROR Number Definitions
#include <time.h>
#include <limits>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <atomic>
#include <cstdlib>
#include <pthread.h>
#include <queue>
#include <chrono>
#include <mutex>
#include <vector>
#include <cstdlib>
#include <X11/Xlib.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#define PI 3.14159265
#define READ_BUF_SIZE_ALTITUDE 10
#define READ_BUF_SIZE_DISTANCE 10
#define READ_BUF_SIZE_AHRS 48
#define UART_PORT_AHRS "/dev/ttyTHS1"
#define UART_PORT_DISTANCE "/dev/ttyDIS1"
#define UART_PORT_ALTITUDE "/dev/ttyALT1"
#define PRECISION 100
#define DISPLAY_LOG 1
#if DISPLAY_LOG == 0
#define printf(...) ;
#endif
// resolution
#define DISPLAY_RES_WIDTH 640
#define DISPLAY_RES_HEIGHT 360
#define MEDIA_RES_WIDTH 213
#define MEDIA_RES_HEIGHT 120
#define INFO_RES_WIDTH 640
#define INFO_RES_HEIGHT 120
#define FRAME_ROI_TOP 120
#define FRAME_ROI_BOTTOM 240
#define FRAME_ROI_LEFT 213
#define FRAME_ROI_RIGHT 427
//#define DISPLAY_RES_WIDTH 960
//#define DISPLAY_RES_HEIGHT 540
//#define MEDIA_RES_WIDTH 319
//#define MEDIA_RES_HEIGHT 180
//#define INFO_RES_WIDTH 960
//#define INFO_RES_HEIGHT 180
//#define FRAME_ROI_TOP 180
//#define FRAME_ROI_BOTTOM 360
//#define FRAME_ROI_LEFT 319
//#define FRAME_ROI_RIGHT 640
#define FRAME_ROI_OUTSIDE_TOP 0
#define FRAME_ROI_OUTSIDE_LEFT 0
#define proj_angle 24.5
#define proj_angle_rad 0.4
#define proj_angle_rad_half 0.2
#define angle_threshold 3
using namespace cv;
using namespace std;
typedef enum {
SENSORS_IDX_ROLL = 0,
SENSORS_IDX_PITCH = 1,
SENSORS_IDX_YAW = 2,
SENSORS_IDX_DISTANCE = 3,
SENSORS_IDX_ALTITUDE = 4
} SENSORS_IDX;
typedef enum {
PLAYMODE_IMAGE = 0,
PLAYMODE_VIDEO = 1
} PLAYMODE;
typedef struct {
char filename[1024];
int mode;
} MEDIAINFO;
// Global Variable
char dirbuff[1024];
char path_buff[1024];
string path;
char foldername_str[1024];
char filename_buff[1024];
char filename[9][1024] = {"SENSORS_VALUE","MATRIX_VALUE", "ROTATION_SENSORS_GAIN_TIME", \
"DISTANCE_SENSORS_GAIN_TIME", "ALTITUDE_SENSORS_GAIN_TIME", "PROJECTION_TIME", "MATRIX_CALCULATION_TIME", \
"FRAME_PER_SEC", "TOTAL_TIME(FPS)"};
clock_t start_rotation_sensors_time; clock_t end_rotation_sensors_time;
clock_t start_distance_sensors_time; clock_t end_distance_sensors_time;
clock_t start_altitude_sensors_time; clock_t end_altitude_sensors_time;
clock_t start_projection_time; clock_t end_projection_time;
clock_t start_matrix_calculation_time; clock_t end_matrix_calculation_time;
clock_t start_frame_per_sec; clock_t end_frame_per_sec;
clock_t start_total_time; clock_t end_total_time;
int file_res = 0;
MEDIAINFO media_list[] = { {"flower_img_only_HD.png", PLAYMODE_IMAGE}
, {"checker_img_only_HD.png", PLAYMODE_IMAGE} \
, {"measure1_img_only_HD.png", PLAYMODE_IMAGE} \
, {"measure2_img_only_HD.png", PLAYMODE_IMAGE} \
, {"videoplayback.mp4", PLAYMODE_VIDEO}
, {"blackimage.png", PLAYMODE_IMAGE}};
time_t current_time = time(NULL);
char buffertmp[1024];
char file_buffer[1024];
struct tm* struct_time = localtime(¤t_time);
bool g_bRunThreadAHRS = true;
bool g_bRunThreadAltitude = true;
bool g_bRunThreadDistance = true;
//bool g_bRunThreaddistance = true;
// Shared Variables
float sensors[5] = {0.0, 0.0, 0.0, 1.0, 0.0}; // Roll, Pitch, Yaw, Distance, Altitude, distance (distance);
float CMtoPIXEL;
// Function Init.
void delay (clock_t n);
void setting_serialport(int fd, speed_t baudrate, int wait_ch);
static int lookup(const char* arg);
char* file_create(char* filename);
void* threadforAHRS(void*);
void* threadforDistance(void*);
void* threadforAltitude(void*);
void transform_ROI( const Mat& src, Mat& dst, const Mat& M0, Size dsize, int flags, int borderType, const Scalar& borderValue, Point origin );
void transform_ori( const Mat& src, Mat& dst, const Mat& M0, Size dsize, int flags, int borderType, const Scalar& borderValue, Point origin );
//void* threadfordistance(void*);
bool is_roll_initialized = false;
bool is_pitch_initialized = false;
bool is_yaw_initialized = false;
bool is_distance_initialized = false;
bool is_altitude_initialized = false;
float initial_roll;
float initial_pitch;
float initial_yaw;
float initial_distance;
float initial_altitude;
float prev_real_roll;
float prev_real_pitch;
float prev_real_yaw;
float prev_real_altitude;
float prev_real_distance;
//float prev_real_distance;
int cnt_for_distance = 0;
int cnt_for_altitude = 0;
//int cnt_for_distance = 0;
bool stabilization_rotation_on = true;
bool stabilization_distance_on = true;
bool stabilization_altitude_on = true;
//bool stabilization_distance_on = true;
Mat pers_proj_roll;
Mat pers_proj_pitch;
Mat pers_proj_yaw;
Mat pers_proj_roll_pitch_yaw_distance_altitude;
Mat H_matrix;
int frame_cnt = 0;
int main( int argc, char** argv ) {
int thresult, status, i;
vector<void *(*)(void *)> thread_list;
vector<pthread_t> tident(10);
thread_list.push_back(threadforAHRS);
thread_list.push_back(threadforAltitude);
thread_list.push_back(threadforDistance);
for (i = 0; i < thread_list.size(); i++) {
if (pthread_create (&tident[i], NULL, thread_list[i], (void*)NULL) < 0){
perror ("error:");
exit(0);
}
}
Mat frame(DISPLAY_RES_HEIGHT, DISPLAY_RES_WIDTH, CV_8UC3);
//Mat frame_ROI(FRAME_ROI_LEFT, FRAME_ROI_TOP, CV_8UC3);
//initial media
Mat image_input = imread(media_list[0].filename, cv::IMREAD_COLOR);
int media_mode = media_list[0].mode;
Mat black_image = imread(media_list[5].filename, cv::IMREAD_COLOR);
VideoCapture video_cap;
Mat video_input;
// resized media for putting into the frame
Mat resized_media;
//Mat resized_blackimage;
// frame after warping
Mat frame_warped;
float Roll = 0, Pitch = 0, Yaw = 0;//, dist_x = 0, dist_y = 0, dist_z = 0;
//int Roll_sign = 1, Pitch_sign = 1, Yaw_sign = 1;
Size frame_res(DISPLAY_RES_WIDTH, DISPLAY_RES_HEIGHT); // image size(spatial resolution)
Size media_res(MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT); // image size(spatial resolution)
//Size black_res(INFO_RES_WIDTH, INFO_RES_HEIGHT); // image size(spatial resolution)
//Size frame_res(1280, 720); // image size(spatial resolution)
Point3f pt(0, DISPLAY_RES_WIDTH >> 1, DISPLAY_RES_HEIGHT >> 1); //translation offset
//Point3f pt(0, 640, 360); //translation offset
resize(image_input, resized_media, media_res);
//resize(black_image, resized_blackimage, black_res);
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
//resized_blackimage.copyTo(frame(Rect(FRAME_ROI_OUTSIDE_LEFT, FRAME_ROI_OUTSIDE_TOP, INFO_RES_WIDTH,INFO_RES_HEIGHT)));
namedWindow ("FRAME", cv::WINDOW_NORMAL);
cv::setWindowProperty("FRAME", cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
float Roll_r, Pitch_r, Yaw_r;
float f = 1, altitude, distance;
float trans_by_distance;
// Text overlay on the frame
Point textPos (FRAME_ROI_LEFT + 10, FRAME_ROI_TOP + 30);
// Point sensorPos (FRAME_ROI_OUTSIDE_LEFT + 50, FRAME_ROI_OUTSIDE_TOP + 100);
Point sensorPos (FRAME_ROI_OUTSIDE_LEFT + 30, FRAME_ROI_OUTSIDE_TOP + 100);
Point matrixPos1 (FRAME_ROI_OUTSIDE_LEFT + 50 , FRAME_ROI_OUTSIDE_TOP + 260);
Point matrixPos2 (FRAME_ROI_OUTSIDE_LEFT + 50 , FRAME_ROI_OUTSIDE_TOP + 290);
Point matrixPos3 (FRAME_ROI_OUTSIDE_LEFT + 50 , FRAME_ROI_OUTSIDE_TOP + 320);
int textFace = FONT_HERSHEY_SIMPLEX;
double textScale = 1;
double textThickness = 2;
char text_buffer[16] = "";
char sensor_buffer[50] ="";
char* buffer0 = file_create (filename[0]); // -1: abnormal, 0: normal, 1: already exist
fstream outfile_sensors(buffer0,std::fstream::app | std::fstream::out);
char* buffer1 = file_create (filename[1]);
fstream outfile_matrix (buffer1, std::fstream::out | std::fstream::app) ;
char* buffer6 = file_create (filename[6]);
fstream outfile_matrix_calculation_gain_time (buffer6, std::fstream::out | std::fstream::app);
char* buffer7 = file_create (filename[5]);
fstream outfile_projection_gain_time(buffer7, std::fstream::out | std::fstream::app);
//char* buffer8 = file_create (filename[7]);
//fstream outfile_frame_per_sec(buffer8, std::fstream::out | std::fstream::app);
char* buffer9 = file_create (filename[8]);
fstream outfile_total_time(buffer9, std::fstream::out | std::fstream::app);
while(true) {
start_total_time = clock();
if (media_mode == PLAYMODE_VIDEO) {
// read one video frame
video_cap >> video_input;
Rect blankRoi (0, 0, DISPLAY_RES_WIDTH, DISPLAY_RES_HEIGHT);
frame (blankRoi).setTo(Scalar(0));
// resize video frame to display
resize(video_input, resized_media, media_res);
// copy video to frame ROI
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
sprintf(text_buffer, "%d", frame_cnt);
putText(frame, text_buffer, textPos, textFace, textScale, Scalar(0, 0, 0), textThickness);
// sprintf(sensor_buffer, "R:%4.2f, P:%4.2f, Y:%4.2f", sensors[0], sensors[1], sensors[2]);
// sprintf(sensor_buffer, "R:%4.2f, P:%4.2f, Y:%4.2f, dist: %4.2f", sensors[0], sensors[1], sensors[2], sensors[3]);
sprintf(sensor_buffer, "R:%4.2f, P:%4.2f, Y:%4.2f, dist: %4.2f, alti: %4.2f", sensors[SENSORS_IDX_ROLL], sensors[SENSORS_IDX_PITCH], sensors[SENSORS_IDX_YAW], sensors[SENSORS_IDX_DISTANCE], sensors[SENSORS_IDX_ALTITUDE]);
putText(frame, sensor_buffer, sensorPos, textFace, textScale * 0.7, Scalar(255, 255,255), textThickness );
sprintf(sensor_buffer, "H: %4.2f, %4.2f, %4.2f", H_matrix.at<float>(0, 0), H_matrix.at<float>(0, 1), H_matrix.at<float>(0, 2));
putText(frame, sensor_buffer, matrixPos1, textFace, textScale * 0.7, Scalar(255, 255,255), textThickness );
sprintf(sensor_buffer, " %4.2f, %4.2f, %4.2f", H_matrix.at<float>(1, 0), H_matrix.at<float>(1, 1), H_matrix.at<float>(1, 2));
putText(frame, sensor_buffer, matrixPos2, textFace, textScale * 0.7, Scalar(255, 255,255), textThickness );
sprintf(sensor_buffer, " %4.2f, %4.2f, %4.2f", H_matrix.at<float>(2, 0), H_matrix.at<float>(2, 1), H_matrix.at<float>(2, 2));
putText(frame, sensor_buffer, matrixPos3, textFace, textScale * 0.7, Scalar(255, 255,255), textThickness );
//start_frame_per_sec = clock();
frame_cnt++;
}
start_matrix_calculation_time = clock();
for (int h = 0 ; h < 5 ; h++){
sensors[h] = ceil(sensors[h] * 10)/10;
}
if (stabilization_rotation_on){
Roll = sensors[SENSORS_IDX_ROLL];
Pitch = sensors[SENSORS_IDX_PITCH];;
Yaw = sensors[SENSORS_IDX_YAW];
}
else {
Roll = initial_roll;;
Pitch = initial_pitch;;
Yaw = initial_yaw;;
}
Roll_r = Roll * PI / 180.0;
Pitch_r = Pitch * PI / 180.0 * 0.0001;
Yaw_r = Yaw * PI / 180.0* 0.0001;
if (stabilization_distance_on) {
distance = sensors[SENSORS_IDX_DISTANCE];
//printf(" \n @@ ditance : changed f :%f\n", sensors[3]);
}
else {
distance = initial_distance;
}
if (stabilization_altitude_on){
altitude = sensors[SENSORS_IDX_ALTITUDE];
//altitude = altitude * CMtoPIXEL;
//printf(" \n ## altitude : changed f :%f\n", altitude);
}
else {
altitude = initial_altitude;
}
CMtoPIXEL = 360.0 / (distance * tan(proj_angle_rad));
trans_by_distance = (tan(proj_angle_rad)/2.0) * (distance - initial_distance /*- distance*/) * CMtoPIXEL;
altitude = altitude * CMtoPIXEL;
//printf("**MAIN sensors: %f %f %f %f\n", Roll_r, Pitch_r, Yaw_r, f);
/*printf("**MAIN SENSORS: roll: %f pitch: %f yaw: %f dist: %f alti: %f\n", sensors[SENSORS_IDX_ROLL]
, sensors[SENSORS_IDX_PITCH]
, sensors[SENSORS_IDX_YAW]
, sensors[SENSORS_IDX_DISTANCE]
, sensors[SENSORS_IDX_ALTITUDE]
);
*/
Point3f trOffset(0, DISPLAY_RES_WIDTH/2, DISPLAY_RES_HEIGHT/2);
float data_translate[9] = {1, 0, trOffset.y, 0, 1, trOffset.z, 0, 0, 1};
float data_translate_minus[9] = {1, 0, -trOffset.y, 0, 1, -trOffset.z, 0, 0, 1};
Mat H_translate = Mat(3, 3, CV_32F, data_translate);
Mat H_translate_minus = Mat(3, 3, CV_32F, data_translate_minus);
Point3f distance_point (0, 0, trans_by_distance);
Point3f altitude_point (0, 0, altitude);
float data_translate_distance[9]\
= {(float)initial_distance/distance,0,0,0,(float)initial_distance/distance, distance_point.z, 0,0,1};
Mat H_translate_distance = Mat(3, 3, CV_32F, data_translate_distance);
float data_translate_altitude[9] = {1,0,0, 0,1,altitude_point.z, 0,0,1};
Mat H_translate_altitude = Mat(3, 3, CV_32F, data_translate_altitude);
//start2 = clock();
float distance_used = distance * CMtoPIXEL;
float roll_trans_tx = distance_used*tan(proj_angle_rad_half)*sin(Roll_r);
float roll_trans_ty = distance_used*tan(proj_angle_rad_half)*(1-cos(Roll_r));
float Pitch_r_mul = Pitch_r * 10000;
float Yaw_r_mul = Yaw_r * 10000;
//f = 1;
// compensate matrix (not defined distortion) // stabilization
float data_roll[9] =\
{f*cos(Roll_r), f*sin(Roll_r), -roll_trans_tx ,\
-f*sin(Roll_r), f*cos(Roll_r), -roll_trans_ty,\
0, 0, 1};
float pitch_trans_ori = (distance_used) / (cos(proj_angle_rad_half));
float pitch_trans_tilt = (distance_used) / (cos((proj_angle_rad_half)-Pitch_r_mul));
float pitch_trans;
if (Pitch_r > 0) {pitch_trans = sqrtl(powl(pitch_trans_tilt*sin(Pitch_r_mul),2) + powl(pitch_trans_ori-pitch_trans_tilt*cos(Pitch_r_mul),2));}
else {pitch_trans = -(sqrtl(powl(pitch_trans_tilt*sin(Pitch_r_mul),2) + powl(pitch_trans_ori-pitch_trans_tilt*cos(Pitch_r_mul),2)));}
float data_pitch[9] = \
{f,0/*-pt.y*sin(Pitch_r)*/, 0, \
0, f*cos(Pitch_r)/*-pt.z*sin(Pitch_r)*/,pitch_trans,/* CMtoPIXEL*(((distance) / (cos(proj_angle_rad/2.0) + Pitch_r)) - ((distance) / cos(proj_angle_rad/2.0)))*/\
0, -sin(Pitch_r), 1};
float data_yaw[9] =\
{f*cos(Yaw_r)/*+pt.y*sin(Yaw_r)*/, 0, -(distance) * CMtoPIXEL * tan(Yaw_r_mul),\
0,/*+pt.z*sin(Yaw_r)*/ f, 0, \
+sin(Yaw_r), 0, 1};
pers_proj_roll = Mat(3, 3, CV_32F, data_roll);
pers_proj_pitch = Mat(3, 3, CV_32F, data_pitch);
pers_proj_yaw = Mat(3, 3, CV_32F, data_yaw);
// INTEGER CONVERSION
H_matrix = H_translate * H_translate_distance * H_translate_altitude *\
pers_proj_roll* pers_proj_pitch * pers_proj_yaw * H_translate_minus;
end_matrix_calculation_time = clock();
start_projection_time = clock();
if (stabilization_rotation_on && stabilization_distance_on && stabilization_altitude_on) {
/*for (int i = 0; i < DISPLAY_RES_HEIGHT; i++){
for (int j = 0; j < DISPLAY_RES_WIDTH; j++){
//for (int k = 0; k < 2; k ++){
frame_warped.at<float>(i,j,0) = (H_matrix.at<float>(0,0)*frame.at<float>(i,j,0)+H_matrix.at<float>(0,1)*frame.at<float>(i,j,1)+H_matrix.at<float>(0,2))/(H_matrix.at<float>(2,0)*frame.at<float>(i,j,0)+H_matrix.at<float>(2,1)*frame.at<float>(i,j,1)+H_matrix.at<float>(2,2));
frame_warped.at<float>(i,j,1) = (H_matrix.at<float>(1,0)*frame.at<float>(i,j,0)+H_matrix.at<float>(1,1)*frame.at<float>(i,j,1)+H_matrix.at<float>(1,2))/(H_matrix.at<float>(2,0)*frame.at<float>(i,j,0)+H_matrix.at<float>(2,1)*frame.at<float>(i,j,1)+H_matrix.at<float>(2,2));
//}
}
}*/
transform_ROI(frame, frame_warped, H_matrix, frame_res, INTER_LINEAR, BORDER_CONSTANT, Scalar(), Point(0,0));
imshow("FRAME", frame_warped);
} else {
imshow("FRAME", frame);
}
end_projection_time = clock();
char ch = waitKey(15);
if (ch == 'q') { // exit program
return -1;
} else if (ch == 'i') { // re-initialize
is_roll_initialized = false;
is_pitch_initialized = false;
is_yaw_initialized = false;
is_distance_initialized = false;
is_altitude_initialized = false;
} else if (ch == 'r') { // toggle rotational stabilization on/off
stabilization_rotation_on = !stabilization_rotation_on;
} else if (ch == '1') { // change image
image_input = imread(media_list[0].filename, cv::IMREAD_COLOR);
media_mode = media_list[0].mode;
resize(image_input, resized_media, media_res);
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
} else if (ch == '2') { // change image
image_input = imread(media_list[1].filename, cv::IMREAD_COLOR);
media_mode = media_list[1].mode;
resize(image_input, resized_media, media_res);
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
} else if (ch == '3') { // change image
image_input = imread(media_list[2].filename, cv::IMREAD_COLOR);
media_mode = media_list[2].mode;
resize(image_input, resized_media, media_res);
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
} else if (ch == '4') { // change image
image_input = imread(media_list[3].filename, cv::IMREAD_COLOR);
media_mode = media_list[3].mode;
resize(image_input, resized_media, media_res);
resized_media.copyTo(frame(Rect(FRAME_ROI_LEFT, FRAME_ROI_TOP, MEDIA_RES_WIDTH, MEDIA_RES_HEIGHT)));
} else if (ch == '5') { // change video
video_cap = VideoCapture(media_list[4].filename);
if (!video_cap.isOpened()) {
//printf("Video open error!!!\n");
exit(0);
}
media_mode = media_list[4].mode;
frame_cnt = 0;
}
else if (ch == 'd') { // toggle scaling stabilization on/off
stabilization_distance_on = !stabilization_distance_on;
}
else if (ch == 'a'){ // toggle fluctutation stabilization on/off
stabilization_altitude_on = !stabilization_altitude_on;
}
if (outfile_sensors.is_open ()) {
// cout<<"\n\n outfile for sensors Opens Successfully"<<endl;
outfile_sensors << frame_cnt << "," << Roll << "," << Pitch << "," << Yaw << "," << distance << "," << altitude << endl;
}
if (outfile_matrix.is_open ()) {
// cout<<"\n\n outfile for matrix Opens Successfully"<<endl;
outfile_matrix << frame_cnt << endl;
outfile_matrix << H_matrix.at<float>(0, 0) << "," << H_matrix.at<float>(0, 1) << "," << H_matrix.at<float>(0, 2)<< endl;
outfile_matrix << H_matrix.at<float>(1, 0) << "," << H_matrix.at<float>(1, 1) << "," << H_matrix.at<float>(1, 2)<< endl;
outfile_matrix << H_matrix.at<float>(2, 0) << "," << H_matrix.at<float>(2, 1) << "," << H_matrix.at<float>(2, 2)<< endl << endl;
}
if (outfile_matrix_calculation_gain_time.is_open ()) {
//cout<<"\n\n outfile for matrix calculatin gain time Opens Successfully"<<endl;
outfile_matrix_calculation_gain_time << frame_cnt << ","<< ((float)(end_matrix_calculation_time-start_matrix_calculation_time) / CLOCKS_PER_SEC) << endl;
}
if (outfile_projection_gain_time.is_open ()) {
//cout<<"\n\n outfile for projection gain time Opens Successfully"<<endl;
outfile_projection_gain_time << frame_cnt << "," << ((float)(end_projection_time-start_projection_time) / CLOCKS_PER_SEC) << endl;
}
// if (outfile_frame_per_sec.is_open ()){
// cout<<"\n\n outfile for total time Opens Successfully"<<endl;
// outfile_frame_per_sec << frame_cnt << "," << ((float)(end_frame_per_sec-start_frame_per_sec) / CLOCKS_PER_SEC) << endl;
// }
end_total_time = clock();
if (outfile_total_time.is_open ()){
//cout<<"\n\n outfile for total time Opens Successfully"<<endl;
outfile_total_time << frame_cnt << "," << ((float)(end_total_time-start_total_time) / CLOCKS_PER_SEC) << endl;
}
}
g_bRunThreadAHRS = false;
g_bRunThreadAltitude = false;
g_bRunThreadDistance = false;
for (i = 0 ; i < tident.size(); i++){
pthread_join (tident[i], (void **) &status);
}
outfile_sensors.close ();
outfile_matrix.close();
outfile_matrix_calculation_gain_time.close();
outfile_projection_gain_time.close();
}
void delay(clock_t n)
{
clock_t de = clock();
}
static int lookup(const char *arg)
{
if (arg == NULL) return false;
DIR *dirp;
bool bExists = false;
dirp = opendir (arg);
if (dirp != NULL){
bExists = true;
}
return bExists;
}
char* file_create(char* filename){
int k;
memset (dirbuff, 0, 1024);
memset (file_buffer, 0, 1024);
memset (buffertmp, 0, 1024);
getcwd(dirbuff, 1024);
sprintf(file_buffer,"%s/%04d-%02d-%02d_%02d:%02d:%02d",dirbuff, struct_time->tm_year+1900, struct_time->tm_mon+1, struct_time->tm_mday, struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);
bool findres = lookup (file_buffer);
if (findres == false){
//printf("folder does not exist, so %s will be created !!!\n", file_buffer);
if(mkdir (file_buffer, 0777) == -1 && errno != EEXIST) {
// printf("error while trying to create %s\n", file_buffer);
//k = -1; // abnormal exist with error
}
else {
//k = 0;
sprintf(buffertmp, "%s/%s.csv", file_buffer,filename);
} // normal exist with non-error
}
else {
//printf("folder does already exists !!!\n");
// k = 1; // file already exist
sprintf(buffertmp, "%s/%s.csv", file_buffer,filename);
}
//delay(10);
return buffertmp;
}
void setting_serialport(int fd, speed_t baudrate, int wait_ch)
{
struct termios SerialPortSettings = {0};
memset(&SerialPortSettings, 0, sizeof(SerialPortSettings));
// 8N1 Mode
SerialPortSettings.c_iflag = 0;
SerialPortSettings.c_oflag = 0;
SerialPortSettings.c_cflag = CS8 | CREAD | CLOCAL;
SerialPortSettings.c_lflag = 0;
// Setting Time outs
SerialPortSettings.c_cc[VMIN] = wait_ch; // Read at least 64 characters
SerialPortSettings.c_cc[VTIME] = 0; // Wait indefinetly TIME * 0.1
// Setting the Baud rate
cfsetispeed(&SerialPortSettings, baudrate);
cfsetospeed(&SerialPortSettings, baudrate);
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) {
//printf("\n ERROR ! in Setting attributes\n");
} else {
//printf("\n BaudRate = %d \n StopBits = 1 \n Parity = none\n", baudrate);
}
}
void *threadforAHRS(void* argumentPointer){ // AHRS sensor
int fdAHRS; // File Descriptor
//printf("\n +-----------------------------------------+");
//printf("\n | Serial Port Read (AHRS) |");
//printf("\n +-----------------------------------------+\n");
// Opening the Serial Port
fdAHRS = open(UART_PORT_AHRS, O_RDWR | O_NOCTTY);
if(fdAHRS == -1) { // Error Checking
//printf("Error! in Opening %s\n", UART_PORT_AHRS);
return NULL;
}
else {
//printf("%s Opened Successfully ", UART_PORT_AHRS);
}
setting_serialport(fdAHRS, B115200, 48);
char *tmp;
char delimiter[] = "*,'=?";
float split_data[6]; // Roll angle, Pitch angle, Yaw angle, X, Y, Z
int bytes_read = 0; // Number of bytes read by the read() system call
int i = 0;
//int opt = 0; // 1: roll, 2: pitch, 3: yaw
float Roll = 0, Pitch = 0, Yaw = 0, dist_x = 0, dist_y = 0, dist_z = 0;
int Roll_sign = 1, Pitch_sign = 1, Yaw_sign = 1;
float Roll_rad = 0, Pitch_rad = 0, Yaw_rad = 0;
tcflush(fdAHRS, TCIOFLUSH);
char read_buffer_ahrs[READ_BUF_SIZE_AHRS]= "";
char* buffer2 = file_create (filename[2]);
fstream outfile_rotation_sensors_gain_time(buffer2, std::fstream::out | std::fstream::app);
while(g_bRunThreadAHRS) {
start_rotation_sensors_time = clock();
memset(read_buffer_ahrs,0, READ_BUF_SIZE_AHRS);
tcflush(fdAHRS, TCIFLUSH);
bytes_read = read(fdAHRS, read_buffer_ahrs,READ_BUF_SIZE_AHRS);
if(bytes_read <= 0) continue;
i = 0;
// data is split per (,) unit
tmp = strtok(read_buffer_ahrs, delimiter);
if(tmp) {
split_data[i] = atof(tmp);
//printf("%f\n", split_data[i]);
i++;
}
// while(tmp != NULL){
while(i < 6) {
tmp = strtok(NULL, delimiter);
if(tmp) {
split_data[i] = atof(tmp);
//printf("%f\n", split_data[i]);
i++;
}
}
Roll = split_data[0]; Pitch = split_data[1]; Yaw = split_data[2];
if (is_roll_initialized == false) {
initial_roll = Roll; is_roll_initialized = true;
}
Roll = Roll-initial_roll;
// Pitch error compensation
if (is_pitch_initialized == false) {
initial_pitch = Pitch; is_pitch_initialized = true;
}
Pitch = Pitch-initial_pitch;
// Yaw error compensation
if (is_yaw_initialized == false) {
initial_yaw = Yaw; is_yaw_initialized = true;
}
Yaw = Yaw-initial_yaw;
if (prev_real_roll != Roll){
prev_real_roll = sensors[SENSORS_IDX_ROLL];
sensors[SENSORS_IDX_ROLL] = Roll;
}
if (prev_real_pitch != Pitch){
prev_real_pitch = sensors[SENSORS_IDX_PITCH];
sensors[SENSORS_IDX_PITCH] = Pitch;
}
if (prev_real_yaw != Yaw){
prev_real_yaw = sensors[SENSORS_IDX_YAW];
sensors[SENSORS_IDX_YAW] = Yaw;
}
end_rotation_sensors_time = clock();
if (outfile_rotation_sensors_gain_time.is_open ()) {
// cout<<"\n\n outfile for rotation sensor time Opens Successfully"<<endl;
outfile_rotation_sensors_gain_time << frame_cnt << "," << ((float)(end_rotation_sensors_time-start_rotation_sensors_time) / CLOCKS_PER_SEC) << endl;
}
}
outfile_rotation_sensors_gain_time.close();
tcflush(fdAHRS, TCIOFLUSH);
close(fdAHRS);
}
void* threadforDistance(void *argumentPointer){
const unsigned char HEADER = 0x59;
float distance_dist_value, distance_strength_value;
unsigned char a, b, c ,d , e, f, g, h;
unsigned char checksum;
char read_buffer_distance[READ_BUF_SIZE_DISTANCE] = "";
//CirCularQueue cq;
// int front = -1, rear = -1, n = 5;
// printf("\n +-----------------------------------------+");
// printf("\n | Serial Port Read (Distance, LIDAR) |");
// printf("\n +-----------------------------------------+\n");
int fdDistance = open(UART_PORT_DISTANCE, O_RDWR | O_NOCTTY);
// fdDistance = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);
if(fdDistance == -1) { // Error Checking
//printf("Error! in Opening %s\n", UART_PORT_DISTANCE);
return NULL;
} else {
//printf("%s Opened Successfully\n", UART_PORT_DISTANCE);
}
setting_serialport(fdDistance, B115200, 9);
tcflush(fdDistance, TCIOFLUSH);
bool flag = false;
cnt_for_distance = 0;
//bool checker = false;
int bytes_read = 0;
char* buffer3 = file_create (filename[3]);
fstream outfile_distance_sensors_gain_time (buffer3, std::fstream::out | std::fstream::app);
while (g_bRunThreadDistance) {
start_distance_sensors_time = clock();
//printf("distance loop in\n");
memset(read_buffer_distance, 0, READ_BUF_SIZE_DISTANCE);
tcflush(fdDistance, TCIFLUSH);
//flag = true;
//printf("distance before read0\n");
bytes_read = read(fdDistance, read_buffer_distance, READ_BUF_SIZE_DISTANCE);
///printf("distance after read0\n");
if(bytes_read <= 0) { /*printf("Distance: bytes_read <= 0\n");*/ continue; } // no data
if ((read_buffer_distance[0] != HEADER) ||
(read_buffer_distance[1] != HEADER)) {
// printf("Distance: HEADER is not correct\n");
continue;
} //header is incorrect
c = read_buffer_distance[2];
d = read_buffer_distance[3];
e = read_buffer_distance[4];
f = read_buffer_distance[5];
g = read_buffer_distance[6];
h = read_buffer_distance[7];
// read checksum byte
checksum = HEADER + HEADER + c + d + e + f + g + h;
unsigned char tmp = checksum & 0xFF;
if (read_buffer_distance[8] != tmp) { // checksum is incorrect
// printf("Distance: Checksum is not correct buf: %x calc: %x\n", read_buffer_distance[8], tmp);
continue;
}
distance_dist_value = ((int)c + (int)d * 256);
if (prev_real_distance == distance_dist_value ) {
continue;
}
// distance error compensation
if (is_distance_initialized == false) {
if ( distance_dist_value > 1200 || distance_dist_value < 30) continue;
initial_distance = distance_dist_value; is_distance_initialized = true;
}
if (is_distance_initialized == true){
if (sensors[1] > angle_threshold || sensors[2] > angle_threshold) {
distance_dist_value = prev_real_distance;
}
}
distance_strength_value = ((int)e + (int)f * 256);
cnt_for_distance++;
prev_real_distance = distance_dist_value;
sensors[SENSORS_IDX_DISTANCE] = distance_dist_value;
end_distance_sensors_time = clock();
if (outfile_distance_sensors_gain_time.is_open ()) {
//cout<<"\n\n outfile for distance sensor time Opens Successfully"<<endl;
outfile_distance_sensors_gain_time << frame_cnt << "," << ((float)(end_distance_sensors_time-start_distance_sensors_time) / CLOCKS_PER_SEC) << endl;
}
}
outfile_distance_sensors_gain_time.close();
tcflush(fdDistance, TCIOFLUSH);
close(fdDistance);
}
void* threadforAltitude(void *argumentPointer){
const unsigned char HEADER = 0x59;
float altitude_value, altitude_strength;
unsigned char a, b, c ,d , e, f, g, h;
unsigned char checksum;
char read_buffer_altitude[READ_BUF_SIZE_ALTITUDE] = "";
//CirCularQueue cq;
// int front = -1, rear = -1, n = 5;
// printf("\n +-----------------------------------------+");
// printf("\n | Serial Port Read (Altitude, LIDAR) |");
// printf("\n +-----------------------------------------+\n");
int fdAltitude = open(UART_PORT_ALTITUDE, O_RDWR | O_NOCTTY);
// fdAltitude = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);
if(fdAltitude == -1) { // Error Checking
// printf("Error! in Opening %s\n", UART_PORT_ALTITUDE);
return NULL;
} else {
// printf("%s Opened Successfully\n", UART_PORT_ALTITUDE);
}
setting_serialport(fdAltitude, B115200, 9);
tcflush(fdAltitude, TCIOFLUSH);
bool flag = false;
cnt_for_altitude = 0;
//bool checker = false;
int bytes_read = 0;