-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathObstacle.cpp
383 lines (297 loc) · 11.8 KB
/
Obstacle.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
// 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 "Obstacle.h"
using namespace cv;
using namespace std;
// From Julien DAMERS.
vector< vector<double> > lidarDistanceAndAngle (Mat imgIn,
double rangeMax,
double verticalFOV,
double horizontalFOV,
double cameraHeight,
int maxHorizontalChecking,
int maxVerticalChecking,
int debug_ground)
{
Mat channels[3];
split(imgIn,channels);
Mat processed = channels[0];
if (maxHorizontalChecking > processed.cols/2)
{
maxHorizontalChecking = processed.cols/2;
}
vector<double> lidDist(maxHorizontalChecking*2,rangeMax);
vector<double> lidHorizontalAngle(maxHorizontalChecking*2,rangeMax);
vector<double> lidVerticalAngle;
vector< vector<double> > lidInfo;
// Geometry length in cm, angles in rad
double h = cameraHeight;
double theta = verticalFOV*M_PI/180;
double alpha = (M_PI/2) - (theta/2);
double hypMin = h / cos(alpha);
double xMin = hypMin * sin(alpha);
double hypMax = rangeMax;
double beta = acos(h/hypMax);
double xMax = sqrt((hypMax*hypMax)-(h*h));
double gamma = beta - alpha;
int verticalPixPerDeg = (int)(processed.cols/horizontalFOV);
int horizontalPixPerDeg = (int)(processed.rows/verticalFOV);
double depthScale = rangeMax/ 255;
// Intialize necessary variables
double newAlphaStraight = 0;
double supposedDistStraight = 0;
int supposedPixValue = 0;
int actualPixValueLeft = 0;
int actualPixValueRight = 0;
double straightDistanceFromRov = 0;
double newAlphaRotated = 0;
double rotatedDistanceFromRov = 0;
double supposedDistRotated = 0;
double minThetaDistLeft = 10000;
double minThetaDistRight = 10000;
// Debug variables
Mat imgLid2d;
Mat imgObstacles;
double minThetaLeft = 0;
double minThetaRight = 0;
if(debug_ground)
{
// Initializing Debug Variables
imgLid2d = processed.clone();
imgObstacles = processed.clone();
lidVerticalAngle.resize(maxHorizontalChecking*2,rangeMax);
minThetaLeft = 0;
minThetaRight = 0;
// Print geometry calculations
cout << "theta deg: " << theta*180/M_PI << " alpha deg: " << alpha*180/M_PI << " beta deg: " << beta*180/M_PI << " gamma deg: " << gamma*180/M_PI << endl;
cout << "theta rad: " << theta << " alpha rad: " << alpha << " beta rad: " << beta << " gamma rad: " << gamma << endl;
cout << "hypMin: " << hypMin << "cm, hypMax: " << hypMax << "cm, xMin: "<< xMin << "cm, xMax: " << xMax << "cm"<< endl;
cout << "depthScale: " << depthScale << " vertical pix per deg: " << verticalPixPerDeg << " horizontal pix per deg: ";
cout << horizontalPixPerDeg << endl;
}
for (int j = 0; j < maxHorizontalChecking; j++)
{
minThetaDistLeft = 10000;
minThetaDistRight = 10000;
newAlphaRotated = j * ((1*M_PI)/(horizontalPixPerDeg*180));
for (int i = 0; i < maxVerticalChecking; i++)
{
newAlphaStraight = alpha + i * ((1*M_PI)/(verticalPixPerDeg*180));
supposedDistStraight = h / cos(newAlphaStraight);
straightDistanceFromRov = supposedDistStraight * sin(newAlphaStraight);
rotatedDistanceFromRov = straightDistanceFromRov / cos(newAlphaRotated);
supposedDistRotated = rotatedDistanceFromRov / sin(newAlphaStraight);
supposedPixValue = (int)(255 + 2 - supposedDistRotated / depthScale);
actualPixValueLeft = (int)processed.at<uchar>((processed.rows-1)-i,processed.cols/2 -1 - j);
actualPixValueRight = (int)processed.at<uchar>((processed.rows-1)-i,processed.cols/2 + j);
// Checking if the pixel detected is the ground left side
if (abs(actualPixValueLeft - supposedPixValue) > 8)
{
if(debug_ground)
{
imgObstacles.at<uchar>((processed.rows-1)-i,processed.cols/2 -1 - j) = 255;
}
// If an obstacle, looking for the closest one
if ((255 -actualPixValueLeft)*depthScale < minThetaDistLeft)
{
minThetaDistLeft = (255 - actualPixValueLeft)*depthScale;
// Debug
if(debug_ground)
{
minThetaLeft = -(M_PI/2 - newAlphaStraight);
imgLid2d.at<uchar>((processed.rows-1)-i,processed.cols/2 -1 - j) = 255;
}
}
}
// Checking if the pixel detected is the ground left side
if (abs(actualPixValueRight- supposedPixValue) > 8)
{
if(debug_ground)
{
imgObstacles.at<uchar>((processed.rows-1)-i,processed.cols/2 + j) = 255;
}
//If an obstacle, looking for the closest one
if ((255 - actualPixValueRight)*depthScale < minThetaDistRight)
{
minThetaDistRight = (255 - actualPixValueRight)*depthScale;
if (debug_ground)
{
minThetaRight = - ((M_PI/2) - newAlphaStraight);
imgLid2d.at<uchar>((processed.rows-1)-i,processed.cols/2 + j) = 255;
}
}
}
}
lidHorizontalAngle[maxHorizontalChecking -1 -j] = newAlphaRotated;
lidHorizontalAngle[maxHorizontalChecking +j] = - newAlphaRotated;
lidDist[maxHorizontalChecking -1 -j] = minThetaDistLeft;
lidDist[maxHorizontalChecking + j] = minThetaDistRight;
if(debug_ground)
{
lidVerticalAngle[maxHorizontalChecking -1 -j] = minThetaLeft;
lidVerticalAngle[maxHorizontalChecking + j] = minThetaRight;
}
}
lidInfo.push_back(lidHorizontalAngle);
lidInfo.push_back(lidDist);
if(debug_ground)
{
lidInfo.push_back(lidVerticalAngle);
imwrite(PIC_FOLDER"result_lid2D.png", imgLid2d);
imwrite(PIC_FOLDER"result_obstacles.png", imgObstacles);
}
return(lidInfo);
}
void set_kinect_d_vectors_Video(int deviceid)
{
int i = 0; //, j = 0, index = 0;
struct timeval tv;
vector< vector<double> > lidarInfo;
IplImage* image = NULL;
// Missing error checking...
// Get an image from the webcam or video.
EnterCriticalSection(&imgsCS[deviceid]);
image = cvCreateImage(cvSize(imgs[deviceid]->width, imgs[deviceid]->height), imgs[deviceid]->depth, imgs[deviceid]->nChannels);
cvCopy(imgs[deviceid], image, 0);
LeaveCriticalSection(&imgsCS[deviceid]);
EnterCriticalSection(&StateVariablesCS);
//double rangeMax = 500; // In cm.
//double verticalFOV = 53.8;
//double horizontalFOV = 70.6;
//double cameraHeight = 31;
double rangeMax = maxkinectrange*100.0; // In cm.
double verticalFOV = VerticalBeamVideo[deviceid];
double horizontalFOV = HorizontalBeamVideo[deviceid];
double cameraHeight = zVideo[deviceid]*100; // In cm.
int maxHorizontalChecking = image->width/2-nbpixhborder;
int maxVerticalChecking = image->height/2-nbpixvborder;
LeaveCriticalSection(&StateVariablesCS);
lidarInfo = lidarDistanceAndAngle(cv::cvarrToMat(image), rangeMax, verticalFOV, horizontalFOV, cameraHeight, maxHorizontalChecking, maxVerticalChecking, debug_ground);
cvReleaseImage(&image);
if (gettimeofday(&tv, NULL) != EXIT_SUCCESS) { tv.tv_sec = 0; tv.tv_usec = 0; }
EnterCriticalSection(&StateVariablesCS);
for (i = 0; i < (int)lidarInfo[0].size(); i++)
{
alpha_mes_video[deviceid] = lidarInfo[0][i];
d_mes_video[deviceid] = lidarInfo[1][i]/100.0;
// For compatibility with a Seanet...
d_all_mes_video[deviceid].clear();
d_all_mes_video[deviceid].push_back(d_mes_video[deviceid]);
alpha_mes_video_vector[deviceid].push_back(alpha_mes_video[deviceid]);
d_mes_video_vector[deviceid].push_back(d_mes_video[deviceid]);
d_all_mes_video_vector[deviceid].push_back(d_all_mes_video[deviceid]);
t_video_history_vector[deviceid].push_back(tv.tv_sec+0.000001*tv.tv_usec);
xhat_video_history_vector[deviceid].push_back(xhat);
yhat_video_history_vector[deviceid].push_back(yhat);
psihat_video_history_vector[deviceid].push_back(psihat);
vrxhat_video_history_vector[deviceid].push_back(vrxhat);
if ((int)alpha_mes_video_vector[deviceid].size() > (int)lidarInfo[0].size())
{
alpha_mes_video_vector[deviceid].pop_front();
d_mes_video_vector[deviceid].pop_front();
d_all_mes_video_vector[deviceid].pop_front();
t_video_history_vector[deviceid].pop_front();
xhat_video_history_vector[deviceid].pop_front();
yhat_video_history_vector[deviceid].pop_front();
psihat_video_history_vector[deviceid].pop_front();
vrxhat_video_history_vector[deviceid].pop_front();
}
}
LeaveCriticalSection(&StateVariablesCS);
}
#ifdef DEVEL_WAITAREA
THREAD_PROC_RETURN_VALUE WaitAreaThread(void* pParam)
{
int id = (int)(intptr_t)pParam;
//FILE* logwaitareafile = NULL;
//char logwaitareafilename[MAX_BUF_LEN];
int i = 0;
CHRONO chrono;
//EnterCriticalSection(&strtimeCS);
//sprintf(logwaitareafilename, LOG_FOLDER"logwaitarea%d_%.64s.csv", id, strtimeex_fns());
//LeaveCriticalSection(&strtimeCS);
//logwaitareafile = fopen(logwaitareafilename, "w");
//if (logwaitareafile == NULL)
//{
// printf("Unable to create log file.\n");
// if (!bExit) bExit = TRUE; // Unexpected program exit...
// return 0;
//}
//
//fprintf(logwaitareafile, "%% Time (in s); Trigger (1 : on, 0 : off);\n");
//fflush(logwaitareafile);
StartChrono(&chrono);
for (;;)
{
uSleep(1000*(period_waitarea[id] > 0? period_waitarea[id]: 100));
if (bExit) break;
if (!bWaitArea[id]) continue;
EnterCriticalSection(&WaitAreaCS[id]);
res = checkinarea(polygons);
if (((res) && (bIn_waitarea))||((!res) && (!bIn_waitarea)))
{
bWaitAreaDetected[id] = TRUE;
#pragma region Actions
//fprintf(logwaitareafile, "%f;%d;\n", GetTimeElapsedChronoQuick(&chrono), bWaitAreaDetected[id]);
//fflush(logwaitareafile);
if (procid_waitarea[id] != -1)
{
// disablewaitarea to avoid multiple execute...
bWaitArea[id] = FALSE;
for (i = 0; i < nbretries_waitarea[id]; i++)
{
if (remove(WaitAreaFileName[id]) == 0) break;
uSleep(1000*retrydelay_waitarea[id]);
}
if (bEcho) printf("execute %d\n", procid_waitarea[id]);
ExecuteProcedure(procid_waitarea[id]);
bWaiting = FALSE; // To interrupt and force execution of the next commands...
}
#pragma endregion
}
LeaveCriticalSection(&WaitAreaCS[id]);
if (bExit) break;
}
StopChronoQuick(&chrono);
//fclose(logwaitareafile);
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}
#endif // DEVEL_WAITAREA
THREAD_PROC_RETURN_VALUE ObstacleThread(void* pParam)
{
UNREFERENCED_PARAMETER(pParam);
CHRONO chrono;
StartChrono(&chrono);
for (;;)
{
mSleep(50);
if (bKinectTo2DLIDAR)
{
set_kinect_d_vectors_Video(kinect_depth_videoid);
}
else
{
EnterCriticalSection(&StateVariablesCS);
alpha_mes_video_vector[kinect_depth_videoid].clear();
d_mes_video_vector[kinect_depth_videoid].clear();
d_all_mes_video_vector[kinect_depth_videoid].clear();
t_video_history_vector[kinect_depth_videoid].clear();
xhat_video_history_vector[kinect_depth_videoid].clear();
yhat_video_history_vector[kinect_depth_videoid].clear();
psihat_video_history_vector[kinect_depth_videoid].clear();
vrxhat_video_history_vector[kinect_depth_videoid].clear();
LeaveCriticalSection(&StateVariablesCS);
}
if (bExit) break;
}
StopChronoQuick(&chrono);
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}