@@ -38,15 +38,17 @@ DWORD WINAPI streamVideo(LPVOID lpParameter)
38
38
{
39
39
int & server = *((int *)lpParameter);
40
40
41
- VideoCapture cap (1 );
41
+ // VideoCapture cap(1);
42
42
43
43
while (true ) {
44
44
Mat src;
45
45
Mat dst;
46
46
Size size (160 , 120 );
47
47
48
- cap >> src;
49
- resize (src, dst, size);
48
+ // cap >> src;
49
+ // resize(src, dst, size);
50
+
51
+ resize (colorImg, dst, size);
50
52
51
53
int dstSize = dst.total () * dst.elemSize ();
52
54
@@ -63,24 +65,93 @@ DWORD WINAPI streamVideo(LPVOID lpParameter)
63
65
}
64
66
65
67
int check_wall (Mat depthImg) {
66
- int x_wall = 200 ;
68
+ /* int x_wall = 250 ;
67
69
int y_wall = 2;
68
70
int count = 0;
69
71
for (int i = 320 - x_wall / 2; i < 320 + x_wall / 2; i++) {
70
- for (int j = 300 - y_wall / 2 ; j < 300 + y_wall / 2 ; j++) {
71
- int depth = depthImg.at <USHORT>(j, i);
72
-
73
- if (depth == 0 )
74
- count++;
75
- else if (depth < 800 && depth > 600 )
76
- return 1 ;
77
- else if (depth <= 600 )
78
- return 2 ;
72
+ for (int j = 240 - y_wall / 2; j < 240 + y_wall / 2; j++) {
73
+ int depth = depthImg.at<USHORT>(j, i);
74
+
75
+ if (depth == 0)
76
+ count++;
77
+ else if (depth < 800 && depth > 600)
78
+ return 1;
79
+ else if (depth <= 600)
80
+ return 2;
81
+ }
82
+ }
83
+ if (count >= 170)
84
+ return 3;
85
+ return 0;
86
+ */
87
+ int countleft = 0 ;
88
+ int typeleft = 0 ;
89
+ int xleft = 0 ;
90
+ int flagleft = 0 ;
91
+ for (int i = 0 ; i < 320 ; i++) {
92
+ int depth = depthImg.at <USHORT>(240 , i);
93
+
94
+ int x = (int )(depth * (i - 320 ) / 5240 );
95
+ if (depth == 0 )
96
+ countleft++;
97
+ else if (depth < 800 && depth > 600 ) {
98
+ typeleft = 1 ;
99
+ xleft = x;
100
+ }
101
+ else if (depth <= 600 ) {
102
+ typeleft = 2 ;
103
+ xleft = x;
104
+ }
105
+ else {
106
+ if (x >= -20 ) {
107
+ flagleft = 1 ;
108
+ }
109
+ }
110
+ }
111
+
112
+
113
+ int countright = 0 ;
114
+ int typeright = 0 ;
115
+ int xright = 0 ;
116
+ int flagright = 0 ;
117
+ for (int i = 639 ; i >= 320 ; i--) {
118
+ int depth = depthImg.at <USHORT>(240 , i);
119
+
120
+ int x = (int )(depth * (i - 320 ) / 5240 );
121
+ if (depth == 0 )
122
+ countright++;
123
+ else if (depth < 800 && depth > 600 ) {
124
+ typeright = 1 ;
125
+ xright = x;
126
+ }
127
+ else if (depth <= 600 ) {
128
+ typeright = 2 ;
129
+ xright = x;
79
130
}
131
+ else {
132
+ if (x <= 20 ) {
133
+ flagright = 1 ;
134
+ }
135
+ }
136
+ }
137
+
138
+ if (typeleft == 1 || typeleft == 2 ) {
139
+ if (xleft >= -20 )
140
+ return typeleft;
141
+ }
142
+
143
+ else if (typeright == 1 || typeright == 2 ) {
144
+ if (xright <= 20 )
145
+ return typeright;
146
+ }
147
+ else if (flagleft == 1 && flagright == 1 )
148
+ return 0 ;
149
+ else {
150
+ if (countleft > 250 || countright > 250 )
151
+ return 2 ;
80
152
}
81
- if (count == 400 )
82
- return 3 ;
83
153
return 0 ;
154
+
84
155
}
85
156
86
157
int main ()
@@ -146,16 +217,17 @@ int main()
146
217
cout << " => Error on accepting..." << endl;
147
218
continue ;
148
219
}
220
+
221
+ DWORD timeout = 100 ;
222
+ setsockopt (server, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof (timeout));
223
+
149
224
}
150
225
151
226
// ////////////////////////////////////////////
152
227
// video streaming
153
228
// ////////////////////////////////////////////
154
229
155
230
if (mode == ' s' || mode == ' c' ) {
156
- DWORD myThreadID;
157
- HANDLE myHandle = CreateThread (0 , 0 , streamVideo, &server, 0 , &myThreadID);
158
-
159
231
strcpy (buffer, " => Server connected...\n " );
160
232
send (server, buffer, bufsize, 0 );
161
233
}
@@ -167,11 +239,9 @@ int main()
167
239
if (mode == ' c' ) {
168
240
169
241
while (true ) {
170
- cout << " receive: " ;
171
242
if ((nReadBytes = recv (server, buffer, bufsize, 0 )) == SOCKET_ERROR)
172
243
break ;
173
244
174
- cout << buffer << endl;
175
245
}
176
246
if (nReadBytes == SOCKET_ERROR) {
177
247
closesocket (server);
@@ -185,6 +255,7 @@ int main()
185
255
RobotConnector robot;
186
256
KinectConnector kin = KinectConnector ();
187
257
258
+
188
259
if (!robot.Connect (Create_Comport) || !kin.Connect ()) {
189
260
cout << " Error : Can't connect to robot or kinect" << endl;
190
261
@@ -195,39 +266,52 @@ int main()
195
266
continue ;
196
267
}
197
268
269
+ kin.GrabData (depthImg, colorImg, indexImg, pointImg);
270
+
271
+ if (mode == ' s' || mode == ' c' ) {
272
+ DWORD myThreadID;
273
+ HANDLE myHandle = CreateThread (0 , 0 , streamVideo, &server, 0 , &myThreadID);
274
+ }
275
+
198
276
robot.DriveDirect (0 , 0 );
199
277
cvNamedWindow (" Robot" );
200
-
278
+
279
+ int counter = 0 ;
201
280
while (true )
202
281
{
203
282
204
283
double vx, vz;
205
- vx = vz = 0.0 ;
284
+ if (counter == 0 )
285
+ vx = vz = 0.0 ;
206
286
207
287
if (mode == ' s' ) {
208
- cout << " receive: " ;
209
- if ((nReadBytes = recv (server, buffer, bufsize, 0 )) == SOCKET_ERROR)
210
- break ;
211
-
212
- vx = ((buffer[1 ] - ' 0' )) / 10.0 ;
213
- if (buffer[0 ] == ' -' )
214
- vx *= -1 ;
288
+ if ((nReadBytes = recv (server, buffer, bufsize, 0 )) == SOCKET_ERROR) {
289
+ if (WSAGetLastError () != WSAETIMEDOUT)
290
+ break ;
291
+ else if (counter > 0 )
292
+ counter--;
293
+ }
294
+ else {
295
+ counter = 3 ;
215
296
216
- vz = ((buffer[4 ] - ' 0' )) / 10.0 ;
217
- if (buffer[3 ] == ' -' )
218
- vz *= -1 ;
297
+ vx = ((buffer[1 ] - ' 0' )) / 10.0 ;
298
+ if (buffer[0 ] == ' -' )
299
+ vx *= -1 ;
219
300
220
- cout << " vx = " << vx << " , vz = " << vz << endl;
301
+ vz = ((buffer[4 ] - ' 0' )) / 10.0 ;
302
+ if (buffer[3 ] == ' -' )
303
+ vz *= -1 ;
304
+ }
221
305
}
222
306
else {
223
307
char c = 0 ;
224
308
c = cvWaitKey (30 );
225
309
switch (c) {
226
- case ' w' : vx = +1 ; break ;
227
- case ' s' : vx = -1 ; break ;
228
- case ' a' : vz = +1 ; break ;
229
- case ' d' : vz = -1 ; break ;
230
- default : vx = 0 ; break ;
310
+ case ' w' : vx = +1 ; break ;
311
+ case ' s' : vx = -1 ; break ;
312
+ case ' a' : vz = +1 ; break ;
313
+ case ' d' : vz = -1 ; break ;
314
+ default : vx = 0 ; break ;
231
315
}
232
316
}
233
317
@@ -253,6 +337,8 @@ int main()
253
337
int velR = (int )(vr*Create_MaxVel);
254
338
255
339
robot.DriveDirect (velL, velR);
340
+
341
+ cvWaitKey (40 );
256
342
}
257
343
258
344
if (mode == ' s' || mode == ' c' ) {
0 commit comments