-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pinger.cpp
128 lines (96 loc) · 3.54 KB
/
Pinger.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
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#include "Pinger.h"
THREAD_PROC_RETURN_VALUE PingerThread(void* pParam)
{
UNREFERENCED_PARAMETER(pParam);
FILE* logpingerfile = NULL;
char logpingerfilename[MAX_BUF_LEN];
// Estimated d to the object (in m).
//double objDistance = 0;
// Estimated bearing to the object (in rad).
double objBearing = 0;
double pingerdir = 0, pingerdirerr = 0, pingerdist = 0, pingerdisterr = 0;
CHRONO chrono;
// Missing error checking...
IplImage* overlayimage = cvCreateImage(cvSize(videoimgwidth, videoimgheight), IPL_DEPTH_8U, 3);
#ifndef USE_OPENCV_HIGHGUI_CPP_API
cvSet(overlayimage, CV_RGB(0, 0, 0), NULL);
#else
cv::Mat overlayimagemat;
overlayimagemat = cv::cvarrToMat(overlayimage);
overlayimagemat = cv::Mat::zeros(overlayimagemat.size(), overlayimagemat.type());
#endif // !USE_OPENCV_HIGHGUI_CPP_API
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0f, 1.0f);
EnterCriticalSection(&strtimeCS);
sprintf(logpingerfilename, LOG_FOLDER"logpinger_%.64s.csv", strtimeex_fns());
LeaveCriticalSection(&strtimeCS);
logpingerfile = fopen(logpingerfilename, "w");
if (logpingerfile == NULL)
{
printf("Unable to create log file.\n");
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}
fprintf(logpingerfile,
"%% Time (in s);Angle to the pinger (in deg);Angle error (in deg);Distance to the pinger (in m);Distance error (in m);\n"
);
fflush(logpingerfile);
StartChrono(&chrono);
for (;;)
{
uSleep(1000*captureperiod);
if (bExit) break;
if (!bPingerTrackingControl) continue;
#ifndef USE_OPENCV_HIGHGUI_CPP_API
cvSet(overlayimage, CV_RGB(0, 0, 0), NULL);
#else
overlayimagemat = cv::Mat::zeros(overlayimagemat.size(), overlayimagemat.type());
#endif // !USE_OPENCV_HIGHGUI_CPP_API
EnterCriticalSection(&PingerCS);
if (bPingerTrackingControl)
{
#pragma region bUseFile_pinger
if (bUseFile_pinger)
{
EnterCriticalSection(&StateVariablesCS);
u = u_pinger;
// Get the results from pingerdetection.py (needs to be launched/killed from the mission file using system command...)...
FILE* filedetect = fopen(LOG_FOLDER"pingerdetection.txt", "r");
if (filedetect != NULL)
{
if (fscanf(filedetect, "%lf;%lf;%lf;%lf", &pingerdir, &pingerdirerr, &pingerdist, &pingerdisterr) != 1) printf("Invalid detection file.\n");
if (fclose(filedetect) != EXIT_SUCCESS) printf("fclose() failed.\n");
}
fprintf(logpingerfile, "%f;%f;%f;%f;%f;\n",
GetTimeElapsedChronoQuick(&chrono), pingerdir, pingerdirerr, pingerdist, pingerdisterr
);
fflush(logpingerfile);
objBearing = fmod_2PI_deg2rad(-pingerdir);
wpsi = Center(psihat)+objBearing-fmod_2PI_deg2rad(-preferreddir_pinger); // Try to always keep it to its side, e.g. preferreddir_pinger=345...
//bDistanceControl = FALSE;
//bBrakeControl = FALSE;
bHeadingControl = TRUE;
LeaveCriticalSection(&StateVariablesCS);
}
#pragma endregion
}
LeaveCriticalSection(&PingerCS);
EnterCriticalSection(&PingerOverlayImgCS);
CopyResizeScale(overlayimage, PingerOverlayImg, bCropOnResize);
LeaveCriticalSection(&PingerOverlayImgCS);
if (bExit) break;
}
StopChronoQuick(&chrono);
fclose(logpingerfile);
cvReleaseImage(&overlayimage);
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}