-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClientMat.cpp.cpp
More file actions
330 lines (249 loc) · 9.5 KB
/
Copy pathSocketClientMat.cpp.cpp
File metadata and controls
330 lines (249 loc) · 9.5 KB
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
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#define MAX_DATA_LENGTH 255
#define AMB_STATE_0 0
#define AMB_STATE_1 1
#define AMB_STATE_2 2
#define AMB_STATE_3 3
#define BLACK_STATE_0 4
#define BLACK_STATE_1 5
#include "WinsockMatTransmissionClient.h"
#include "Information.h"
#include "SerialPort.h"
#include "LaneDetect.h"
#include <iostream>
using namespace cv;
clock_t start, stop;
char* portName = "\\\\.\\COM6";
SerialPort* arduino;
char sendString[MAX_DATA_LENGTH];
int main()
{
unsigned char buffer[2], buf_char; int buf_int, _a, _b;
buf_int = 1667;
int state = AMB_STATE_1;
int socket_num;
int bytesSent;
information I;
arduino = new SerialPort(portName);
if (arduino->isConnected()){
cout << "ok arduino connection has done succesfully~" << endl << endl;
}
else{
cout << "oh no arduino connection has failed" << endl << endl;
}
WinsockMatTransmissionClient socketMat;
if (socketMat.socketConnect("165.246.235.33", 23957) < 0)
{
return 0;
}
socket_num = (int)socketMat.get_SOCKET();
cv::VideoCapture capture(0);
cv::Mat image;
for (;;)
{
I.set_time();
if (state == AMB_STATE_0){
//1) 아무것도 없는 default 상태 모든게 정지해있음
//여기선 서버로부터 신호를 대기중임
// 신호를 수신하면 2) 로 넘어감
I.set_sender_id('A');
cout << "*************AMB_STATE_0*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
recv(socket_num, (char*)&I, sizeof(I), 0);
if (I.get_packet_id() == 'A'){
state = AMB_STATE_1;
}
}
else if (state == AMB_STATE_1){
//2) 사고 지역을 향해 주행하는 상태
// 여기선 계속하여 번호판 인식(방해차량)을 하고 인식을 했을때 방해차량 번호판과 같다면
// 비키라는 신호를 서버에게 보내고 차량 정지 후 서버로부터 출발 신호를 대기함
// 출발 신호를 수신하면 3)으로 넘어감
I.set_sender_id('A');
cout << "*************AMB_STATE_1*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
Mat frame;
VideoCapture cap(0); // open the video file for reading
float temp_angle = 0;
if (!cap.isOpened()) // if not success, exit program
cout << "Cannot open the video file" << endl;
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
//원본영상의 frame을 받는다.
cout << "Input video's Frame per seconds : " << fps << endl;
cap.read(frame);
LaneDetect detect(frame);
//차선인식 설정 끝
while (1){
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
cvtColor(frame, frame, CV_BGR2GRAY);
start = clock();
detect.nextFrame(frame);
// img = detect.currFrame;
temp_angle = detect.getAngle();
//img = detect.temp2;
stop = clock();
cout << "fps : " << 1.0 / (((double)(stop - start)) / CLOCKS_PER_SEC) << endl;
buf_int = (int)temp_angle * (20000 / 360);
if (buf_int >= 3333) buf_int = 3333;
else if (buf_int <= 0) buf_int = 1;
cout << "angle_pulse : " << buf_int << endl;
_a = buf_int % 256; buffer[0] = (char)_a;
_b = (buf_int / 256); buffer[1] = (char)_b;
stop = clock();
if (arduino->isConnected()){
bool hasWritten = arduino->writeSerialPort(&buffer[1], MAX_DATA_LENGTH);
if (hasWritten) std::cout << "Data Written Successfully" << std::endl;
else std::cout << "Data was not written" << std::endl;
}
cout << "fps : " << 1.0 / (((double)(stop - start)) / CLOCKS_PER_SEC) << endl;
if (waitKey(10) == 27) //wait for 'esc' key press for 10 ms. If 'esc' key is pressed, break loop
{
cout << "video paused!, press q to quit, any other key to continue" << endl;
if (waitKey(0) == 'q')
{
cout << "terminated by user" << endl;
break;
}
}
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//여기에 방해차량 번호판을 인식하는 코드가 필요함
//주행코드도 필요함
//
//
//if(방해차량 번호판 옳다면) {
// I.set_packet_id('D');
// send(socket_num, (char*)&I, sizeof(I), 0)
// 그리고 차량 정지
//recv(socket_num, (char*)&I, sizeof(I), 0);
//if (I.get_packet_id() == 'C'){
// state = AMB_STATE_2;
//}
//}
}
else if (state == AMB_STATE_2){
//3) 사고 지역을 향해 주행하는 상태(앞에 방해차량은 제거됨)
// 여기선 계속하여 번호판 인식(사고차량)을 하고 인식을 했을때 사고차량 번호판과 같다면
// 4) 로 넘어감
I.set_sender_id('A');
cout << "*************AMB_STATE_2*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
Mat frame;
VideoCapture cap(0); // open the video file for reading
float temp_angle = 0;
if (!cap.isOpened()) // if not success, exit program
cout << "Cannot open the video file" << endl;
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
//원본영상의 frame을 받는다.
cout << "Input video's Frame per seconds : " << fps << endl;
cap.read(frame);
LaneDetect detect(frame);
//차선인식 설정 끝
while (1){
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
cvtColor(frame, frame, CV_BGR2GRAY);
start = clock();
detect.nextFrame(frame);
// img = detect.currFrame;
temp_angle = detect.getAngle();
//img = detect.temp2;
stop = clock();
cout << "fps : " << 1.0 / (((double)(stop - start)) / CLOCKS_PER_SEC) << endl;
buf_int = (int)temp_angle * (20000 / 360);
if (buf_int >= 3333) buf_int = 3333;
else if (buf_int <= 0) buf_int = 1;
cout << "angle_pulse : " << buf_int << endl;
_a = buf_int % 256; buffer[0] = (char)_a;
_b = (buf_int / 256); buffer[1] = (char)_b;
stop = clock();
cout << "fps : " << 1.0 / (((double)(stop - start)) / CLOCKS_PER_SEC) << endl;
if (waitKey(10) == 27) //wait for 'esc' key press for 10 ms. If 'esc' key is pressed, break loop
{
cout << "video paused!, press q to quit, any other key to continue" << endl;
if (waitKey(0) == 'q')
{
cout << "terminated by user" << endl;
break;
}
}
}
//여기에 사고차량 번호판을 인식하는 코드가 필요함
//주행 코드도 필요함
//if(사고차량 번호판 옳다면) {
// I.set_packet_id('F');
// send(socket_num, (char*)&I, sizeof(I), 0)
// state == AMB_STATE_3
//}
}
else if (state == AMB_STATE_3){
//4) 사고차량 번호판을 확인한 상태
// 여기선 서버로 영상신호를 보냄
// 계속하여 영상 신호를 보내다가 일정 거리가 되면 정지하고 도착신호를 서버에 전송하고 종료
I.set_sender_id('A');
cout << "*************AMB_STATE_3*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
if (!capture.isOpened())
return 0;
capture >> image;
if (image.empty())
return 0;
socketMat.transmit(image);
imshow("original_video", image);
cvWaitKey(27);
send(socket_num, (char*)&I, sizeof(I), 0);
//if 사고차량과 일정거리가 되면
//I.set_packet_id('G');
//send(socket_num, (char*)&I, sizeof(I), 0);
}
else if (state == BLACK_STATE_0){
//1) 아무것도 없는 default 상태 서버와 Amb 는 작동중임
// 여기선 server 로 부터 비키라는 신호를 수신함
// 신호를 수신하면 2) state 로 넘어감
I.set_sender_id('B');
cout << "*************BLACK_STATE_0*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
recv(socket_num, (char*)&I, sizeof(I), 0);
if (I.get_packet_id() == 'B'){
//여기에 주차 주행 코드를 넣어야 함
}
//이곳에 주차 완료조건을 달성하면
//state = BLACK_STATE_1;
}
else if (state == BLACK_STATE_1){
//2) 여기선 갓길로 주차를 하는 주행 구현해야함
// 주차가 완료 되면 서버에게 신호를 보내고 종료
I.set_sender_id('B');
cout << "*************BLACK_STATE_1*************" << endl;
cout << "sender_id : " << I.get_sender_id() << endl;
cout << "time : " << I.get_time() << endl;
cout << "packet_id : " << I.get_packet_id() << endl << endl;
I.set_packet_id('H');
send(socket_num, (char*)&I, sizeof(I), 0);
}
}
socketMat.socketDisconnect();
return 0;
}