-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
509 lines (418 loc) · 13.5 KB
/
main.c
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
#include <signal.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <termios.h>
#include <sys/time.h>
#include <errno.h>
#include <syslog.h>
#include "util.h"
#include "display-kms.h"
#include "v4l2.h"
#include "vpe-common.h"
#include "drawing.h"
#include "input_cmd.h"
#include "exam_cv.h"
#include "car_lib.h"
#include <unistd.h>
#define CAPTURE_IMG_W 1280
#define CAPTURE_IMG_H 720
#define CAPTURE_IMG_SIZE (CAPTURE_IMG_W*CAPTURE_IMG_H*2) // YUYU : 16bpp
#define CAPTURE_IMG_FORMAT "uyvy"
#define VPE_OUTPUT_W 320
#define VPE_OUTPUT_H 180
// display output & dump format: NV12, w:320, h:180
//#define VPE_OUTPUT_IMG_SIZE (VPE_OUTPUT_W*VPE_OUTPUT_H*3/2) // NV12 : 12bpp
//#define VPE_OUTPUT_FORMAT "nv12"
// display output & dump format: yuyv, w:320, h:180
//#define VPE_OUTPUT_IMG_SIZE (VPE_OUTPUT_W*VPE_OUTPUT_H*2)
//#define VPE_OUTPUT_FORMAT "yuyv"
#define VPE_OUTPUT_IMG_SIZE (VPE_OUTPUT_W*VPE_OUTPUT_H*3)
#define VPE_OUTPUT_FORMAT "bgr24"
#define OVERLAY_DISP_FORCC FOURCC('A','R','2','4')
#define OVERLAY_DISP_W 480
#define OVERLAY_DISP_H 272
#define TIME_TEXT_X 385 //320
#define TIME_TEXT_Y 260 //240
#define TIME_TEXT_COLOR 0xffffffff //while
#define DUMP_MSGQ_KEY 1020
#define DUMP_MSGQ_MSG_TYPE 0x02
typedef enum {
DUMP_NONE,
DUMP_CMD,
DUMP_READY,
DUMP_WRITE_TO_FILE,
DUMP_DONE
}DumpState;
typedef struct _DumpMsg{
long type;
int state_msg;
}DumpMsg;
struct thr_data {
struct display *disp;
struct v4l2 *v4l2;
struct vpe *vpe;
struct buffer **input_bufs;
DumpState dump_state;
unsigned char dump_img_data[VPE_OUTPUT_IMG_SIZE];
int msgq_id;
bool bfull_screen;
bool bstream_start;
pthread_t threads[3];
};
typedef struct Mode{
int run;
int view;
int cameraAngle;
int minPixelCount;
int maxVariance;
}Mode;
typedef struct Points{
int row;
int col;
}Points;
typedef struct Hsv {
int H_max;
int H_min;
int S_max;
int S_min;
int V_max;
int V_min;
}Hsv;
typedef struct Homography {
Points upperPoint;
Points lowerPoint;
}Homography;
typedef struct LineDetection {
Homography homography;
int binarization;
}LineDetection;
typedef struct TrafficLight {
Hsv red;
Hsv green;
Hsv orange;
Hsv arrow;
int sizeMax;
int sizeMin;
}TrafficLight;
typedef struct StopSign {
Hsv red;
int sizeMax;
int sizeMin;
}StopSign;
typedef struct Params {
LineDetection lineDetection;
TrafficLight trafficLight;
StopSign stopsign;
Mode mode;
int streaming;
}Params;
Params params;
/**
* @brief Alloc vpe input buffer and a new buffer object
* @param data: pointer to parameter of thr_data
* @retval none
*/
static int allocate_input_buffers(struct thr_data *data)
{
int i;
struct vpe *vpe = data->vpe;
data->input_bufs = calloc(NUMBUF, sizeof(*data->input_bufs));
for(i = 0; i < NUMBUF; i++) {
data->input_bufs[i] = alloc_buffer(vpe->disp, vpe->src.fourcc, vpe->src.width, vpe->src.height, false);
}
if (!data->input_bufs)
ERROR("allocating shared buffer failed\n");
for (i = 0; i < NUMBUF; i++) {
/** Get DMABUF fd for corresponding buffer object */
vpe->input_buf_dmafd[i] = omap_bo_dmabuf(data->input_bufs[i]->bo[0]);
data->input_bufs[i]->fd[0] = vpe->input_buf_dmafd[i];
}
return 0;
}
/**
* @brief Free vpe input buffer and destroy a buffer object
* @param buffer: pointer to parameter of buffer object
n : count of buffer object
bmultiplanar : multipanar value of buffer object
* @retval none
*/
static void free_input_buffers(struct buffer **buffer, uint32_t n, bool bmultiplanar)
{
uint32_t i;
for (i = 0; i < n; i++) {
if (buffer[i]) {
close(buffer[i]->fd[0]);
omap_bo_del(buffer[i]->bo[0]);
if(bmultiplanar){
close(buffer[i]->fd[1]);
omap_bo_del(buffer[i]->bo[1]);
}
}
}
free(buffer);
}
/**
* @brief Draw operating time to overlay buffer.
* @param disp: pointer to parameter of struct display
time : operate time (ms)
* @retval none
*/
static void draw_operatingtime(struct display *disp, uint32_t time)
{
FrameBuffer tmpFrame;
unsigned char* pbuf[4];
char strtime[128];
memset(strtime, 0, sizeof(strtime));
sprintf(strtime, "%03d(ms)", time);
if(get_framebuf(disp->overlay_p_bo, pbuf) == 0) {
tmpFrame.buf = pbuf[0];
tmpFrame.format = draw_get_pixel_foramt(disp->overlay_p_bo->fourcc);//FORMAT_RGB888; //alloc_overlay_plane() -- FOURCC('R','G','2','4');
tmpFrame.stride = disp->overlay_p_bo->pitches[0];//tmpFrame.width*3;
drawString(&tmpFrame, strtime, TIME_TEXT_X, TIME_TEXT_Y, 0, TIME_TEXT_COLOR);
}
}
/**
* @brief Handle houht transform with opencv api
* @param disp: pointer to parameter of struct display
cambuf: vpe output buffer that converted capture image
* @retval none
*/
static void hough_transform(struct display *disp, struct buffer *cambuf)
{
unsigned char srcbuf[VPE_OUTPUT_W*VPE_OUTPUT_H*3];
uint32_t optime;
struct timeval st, et;
unsigned char* cam_pbuf[4];
if(get_framebuf(cambuf, cam_pbuf) == 0) {
memcpy(srcbuf, cam_pbuf[0], VPE_OUTPUT_W*VPE_OUTPUT_H*3);
gettimeofday(&st, NULL);
OpenCV_hough_transform(srcbuf, VPE_OUTPUT_W, VPE_OUTPUT_H, cam_pbuf[0], VPE_OUTPUT_W, VPE_OUTPUT_H);
gettimeofday(&et, NULL);
optime = ((et.tv_sec - st.tv_sec)*1000)+ ((int)et.tv_usec/1000 - (int)st.tv_usec/1000);
draw_operatingtime(disp, optime);
}
}
/**
* @brief Camera capture, capture image covert by VPE and display after sobel edge
* @param arg: pointer to parameter of thr_data
* @retval none
*/
void * capture_thread(void *arg)
{
struct thr_data *data = (struct thr_data *)arg;
struct v4l2 *v4l2 = data->v4l2;
struct vpe *vpe = data->vpe;
struct buffer *capt;
bool isFirst = true;
int index;
int i;
v4l2_reqbufs(v4l2, NUMBUF);
// init vpe input
vpe_input_init(vpe);
// allocate vpe input buffer
allocate_input_buffers(data);
if(vpe->dst.coplanar)
vpe->disp->multiplanar = true;
else
vpe->disp->multiplanar = false;
printf("disp multiplanar:%d \n", vpe->disp->multiplanar);
// init /allocate vpe output
vpe_output_init(vpe);
vpe_output_fullscreen(vpe, data->bfull_screen);
for (i = 0; i < NUMBUF; i++)
v4l2_qbuf(v4l2,vpe->input_buf_dmafd[i], i);
for (i = 0; i < NUMBUF; i++)
vpe_output_qbuf(vpe, i);
v4l2_streamon(v4l2);
vpe_stream_on(vpe->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
vpe->field = V4L2_FIELD_ANY;
while(1) {
index = v4l2_dqbuf(v4l2, &vpe->field);
vpe_input_qbuf(vpe, index);
if (isFirst) {
vpe_stream_on(vpe->fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
isFirst = false;
MSG("streaming started...");
data->bstream_start = true;
}
index = vpe_output_dqbuf(vpe);
capt = vpe->disp_bufs[index];
hough_transform(vpe->disp, capt);
if (disp_post_vid_buffer(vpe->disp, capt, 0, 0, vpe->dst.width, vpe->dst.height)) {
ERROR("Post buffer failed");
return NULL;
}
update_overlay_disp(vpe->disp);
if(data->dump_state == DUMP_READY) {
DumpMsg dumpmsg;
unsigned char* pbuf[4];
if(get_framebuf(capt, pbuf) == 0) {
switch(capt->fourcc) {
case FOURCC('Y','U','Y','V'):
case FOURCC('B','G','R','3'):
memcpy(data->dump_img_data, pbuf[0], VPE_OUTPUT_IMG_SIZE);
break;
case FOURCC('N','V','1','2'):
memcpy(data->dump_img_data, pbuf[0], VPE_OUTPUT_W*VPE_OUTPUT_H); // y data
memcpy(data->dump_img_data+VPE_OUTPUT_W*VPE_OUTPUT_H, pbuf[1], VPE_OUTPUT_W*VPE_OUTPUT_H/2); // uv data
break;
default :
MSG("DUMP.. not yet support format : %.4s\n", (char*)&capt->fourcc);
break;
}
} else {
MSG("dump capture buf fail !");
}
dumpmsg.type = DUMP_MSGQ_MSG_TYPE;
dumpmsg.state_msg = DUMP_WRITE_TO_FILE;
data->dump_state = DUMP_WRITE_TO_FILE;
if (-1 == msgsnd(data->msgq_id, &dumpmsg, sizeof(DumpMsg)-sizeof(long), 0)) {
MSG("state:%d, msg send fail\n", dumpmsg.state_msg);
}
}
vpe_output_qbuf(vpe, index);
index = vpe_input_dqbuf(vpe);
v4l2_qbuf(v4l2, vpe->input_buf_dmafd[index], index);
}
MSG("Ok!");
return NULL;
}
/**
* @brief Hough transform the captured image dump and save to file
* @param arg: pointer to parameter of thr_data
* @retval none
*/
void * capture_dump_thread(void *arg)
{
//초음파센서, 라인스캔센서, 속도, 방향 제어관련 코드
while(1){
thread2();
}
}
/**
* @brief handling an input command
* @param arg: pointer to parameter of thr_data
* @retval none
*/
void * input_thread(void *arg)
{
//키보드 컨트롤
struct Params * p = (Params *)arg;
keyboardController(p);
}
static struct thr_data* pexam_data = NULL;
/**
* @brief handling an SIGINT(CTRL+C) signal
* @param sig: signal type
* @retval none
*/
void signal_handler(int sig)
{
if(sig == SIGINT) {
pthread_cancel(pexam_data->threads[0]);
pthread_cancel(pexam_data->threads[1]);
pthread_cancel(pexam_data->threads[2]);
msgctl(pexam_data->msgq_id, IPC_RMID, 0);
v4l2_streamoff(pexam_data->v4l2);
vpe_stream_off(pexam_data->vpe->fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
vpe_stream_off(pexam_data->vpe->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
disp_free_buffers(pexam_data->vpe->disp, NUMBUF);
free_input_buffers(pexam_data->input_bufs, NUMBUF, false);
free_overlay_plane(pexam_data->vpe->disp);
disp_close(pexam_data->vpe->disp);
vpe_close(pexam_data->vpe);
v4l2_close(pexam_data->v4l2);
printf("-- 6_camera_opencv_disp example End --\n");
}
}
int main(int argc, char **argv)
{
struct v4l2 *v4l2;
struct vpe *vpe;
struct thr_data tdata;
int disp_argc = 3;
char* disp_argv[] = {"dummy", "-s", "4:480x272", "\0"}; // ���� ���� ���� Ȯ�� �� ó��..
int ret = 0;
printf("-- 6_camera_opencv_disp example Start --\n");
tdata.dump_state = DUMP_NONE;
memset(tdata.dump_img_data, 0, sizeof(tdata.dump_img_data));
// open vpe
vpe = vpe_open();
if(!vpe) {
return 1;
}
// vpe input (v4l cameradata)
vpe->src.width = CAPTURE_IMG_W;
vpe->src.height = CAPTURE_IMG_H;
describeFormat(CAPTURE_IMG_FORMAT, &vpe->src);
// vpe output (disp data)
vpe->dst.width = VPE_OUTPUT_W;
vpe->dst.height = VPE_OUTPUT_H;
describeFormat (VPE_OUTPUT_FORMAT, &vpe->dst);
vpe->disp = disp_open(disp_argc, disp_argv);
if (!vpe->disp) {
ERROR("disp open error!");
vpe_close(vpe);
return 1;
}
set_z_order(vpe->disp, vpe->disp->overlay_p.id);
set_global_alpha(vpe->disp, vpe->disp->overlay_p.id);
set_pre_multiplied_alpha(vpe->disp, vpe->disp->overlay_p.id);
alloc_overlay_plane(vpe->disp, OVERLAY_DISP_FORCC, 0, 0, OVERLAY_DISP_W, OVERLAY_DISP_H);
//vpe->deint = 0;
vpe->translen = 1;
MSG ("Input(Camera) = %d x %d (%.4s)\nOutput(LCD) = %d x %d (%.4s)",
vpe->src.width, vpe->src.height, (char*)&vpe->src.fourcc,
vpe->dst.width, vpe->dst.height, (char*)&vpe->dst.fourcc);
if ( vpe->src.height < 0 || vpe->src.width < 0 || vpe->src.fourcc < 0 || \
vpe->dst.height < 0 || vpe->dst.width < 0 || vpe->dst.fourcc < 0) {
ERROR("Invalid parameters\n");
}
v4l2 = v4l2_open(vpe->src.fourcc, vpe->src.width, vpe->src.height);
if (!v4l2) {
ERROR("v4l2 open error!");
disp_close(vpe->disp);
vpe_close(vpe);
return 1;
}
tdata.disp = vpe->disp;
tdata.v4l2 = v4l2;
tdata.vpe = vpe;
tdata.bfull_screen = true;
tdata.bstream_start = false;
if(-1 == (tdata.msgq_id = msgget((key_t)DUMP_MSGQ_KEY, IPC_CREAT | 0666))) {
fprintf(stderr, "%s msg create fail!!!\n", __func__);
return -1;
}
pexam_data = &tdata;
// car init
CarControlInit();
SpeedControlOnOff_Write(1);
//DesireSpeed_Write(100);
PositionControlOnOff_Write(UNCONTROL);
printf("Hello world");
ret = pthread_create(&tdata.threads[0], NULL, capture_thread, &tdata);
if(ret) {
MSG("Failed creating capture thread");
}
pthread_detach(tdata.threads[0]);
ret = pthread_create(&tdata.threads[1], NULL, capture_dump_thread, &tdata);
if(ret) {
MSG("Failed creating capture dump thread");
}
pthread_detach(tdata.threads[1]);
ret = pthread_create(&tdata.threads[2], NULL, input_thread, &tdata);
if(ret) {
MSG("Failed creating input thread");
}
pthread_detach(tdata.threads[2]);
/* register signal handler for <CTRL>+C in order to clean up */
if(signal(SIGINT, signal_handler) == SIG_ERR) {
MSG("could not register signal handler");
closelog();
exit(EXIT_FAILURE);
}
pause();
return ret;
}