-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
160 lines (119 loc) · 3.68 KB
/
main.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
/*
* Copyright (C) 2009 Giacomo Spigler
* CopyPolicy: Released under the terms of the GNU GPL v3.0.
*/
#include "common.h"
#include "camera.h"
#include "server.h"
#include <cv.h>
#include <highgui.h>
#include <unistd.h>
#ifdef __arm__
#define RASPBERRY
#endif
using namespace robo;
// these should goto config.json/yaml
const char *UDS_PATH = "/tmp/robo.vision.s";
const char *VIDEO_0 = "/dev/video0";
const char *VIDEO_1 = "/dev/video1";
const char *VIDEO_0_IMG = "img1.png";
const char *VIDEO_1_IMG = "img2.png";
/*
int ww = 800;
int hh = 600;
*/
int ww = 640;
int hh = 480;
int max_attempts = 100;
int attemp_sleep_usec = 1000;
int main() {
int res = 0;
uint64_t iterations = 0;
Camera c1;
Camera c2;
Server srv;
res = srv.initialize(UDS_PATH);
if (res)
return res;
/* POC Code Below, pulls two images and saved them. Or if not
on raspberry, then displays them. */
c1.initialize(VIDEO_0, ww, hh, 15);
c2.initialize(VIDEO_1, ww, hh, 15);
#ifndef RASPBERRY
cvNamedWindow(VIDEO_0, CV_WINDOW_AUTOSIZE);
cvNamedWindow(VIDEO_1, CV_WINDOW_AUTOSIZE);
#endif
IplImage *l1 = cvCreateImage(cvSize(ww, hh), 8, 3);
IplImage *l2 = cvCreateImage(cvSize(ww, hh), 8, 3);
while (1) {
++iterations;
proto::Request request;
proto::Response response;
// srv operations can block forever
res = srv.get_request(request);
if (res)
break;
response.trx_id = request.trx_id;
response.cmd = request.cmd;
response.data = 0;
if (request.cmd == proto::CMD_PING) {
// ignore failure, show must go on
srv.send_response(response);
continue;
}
if (request.cmd == proto::CMD_EXIT) {
logger(LOG_INFO, "Exit cmd received");
break;
}
if (request.cmd != proto::CMD_GET_MAP) {
logger(LOG_ERROR, "Invalid cmd=%u trx_id=%u", request.cmd, request.trx_id);
continue;
}
logger(LOG_TRACE, "Loop");
// Below is super flawed. Time difference between two captures is important, but
// for now (in our case where things are not "moving", we are OK). Remember
// this is an indoor toy robot and we do not expect zipping objects or basketball
// players, runners, cats, dogs, bees, etc.
int attempts = max_attempts;
while (--attempts > 0) {
if (!c1.update(0))
break;
usleep(attemp_sleep_usec);
}
while (--attempts > 0) {
if (!c2.update(0))
break;
usleep(attemp_sleep_usec);
}
if (attempts <= 0) {
logger(LOG_ERROR, "Failed capturing images in %d attempts", max_attempts);
break;
}
c1.toIplImage((unsigned char *)l1->imageData, l1->width);
c2.toIplImage((unsigned char *)l2->imageData, l2->width);
#ifndef RASPBERRY
cvShowImage(VIDEO_0, l1);
cvShowImage(VIDEO_1, l2);
if((cvWaitKey(10) & 255) == 27)
break;
#endif
response.data = iterations; /* dummy, TODO pass actual data here when impl is ready */
// ignore res, show must go on...
srv.send_response(response);
}
logger(LOG_INFO, "Exiting");
#ifndef RASPBERRY
cvDestroyWindow(VIDEO_0);
cvDestroyWindow(VIDEO_1);
#else
/* DEMO CODE, remove this when impl is ready to send data via srv */
cvSaveImage(VIDEO_0_IMG, l1);
cvSaveImage(VIDEO_1_IMG, l2);
#endif
cvReleaseImage(&l1);
cvReleaseImage(&l2);
c1.shutdown();
c2.shutdown();
srv.shutdown();
return 0;
}