]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Add trees overlap check
[hubacji1/iamcar.git] / base / main.cc
1 /*
2 This file is part of I am car.
3
4 I am car is nree software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <algorithm>
19 #include <iostream>
20 #include <jsoncpp/json/json.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include "compile.h"
25 #include "obstacle.h"
26 #include "rrtplanner.h"
27 // OpenGL
28 #include <GL/gl.h>
29 #include <GL/glu.h>
30 #include <SDL2/SDL.h>
31
32 // debug
33 //#define JSONLOGEDGES
34 //#define JSONLOGSAMPLES
35
36 // choose
37 //#define USE_INTERRUPT
38 // or
39 //#define USE_TMAX
40 // or
41 //#define USE_LOADF
42 // or
43 #define USE_PTHREAD
44
45 #ifdef USE_INTERRUPT
46         #define USE_GL
47 #endif
48
49 bool run_planner = true;
50
51 SDL_Window* gw = NULL;
52 SDL_GLContext gc;
53
54 bool init();
55 bool initGL();
56
57 void hint(int)
58 {
59         run_planner = false;
60 }
61
62 #ifdef USE_PTHREAD
63 struct next_arg {
64         bool *gf;
65         T2 *p;
66 };
67
68 void *next_run(void *arg)
69 {
70         struct next_arg *na = (struct next_arg *) arg;
71         T2 *lp = (T2 *) na->p;
72         bool *gf = na->gf;
73         while (!*gf && lp->elapsed() < TMAX) {
74                 if (lp->next())
75                         *gf = true;
76                 lp->tend();
77         }
78         pthread_exit(NULL);
79         return NULL;
80 }
81 #endif
82
83 int main()
84 {
85 #ifdef USE_GL
86         init();
87 #endif
88
89         Json::Value jvi; // JSON input
90         Json::Value jvo; // JSON output
91         unsigned int i = 0;
92         unsigned int j = 0;
93         std::cin >> jvi;
94         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
95
96         PLANNER p(
97                         new RRTNode(
98                                 jvi["init"][0].asFloat(),
99                                 jvi["init"][1].asFloat(),
100                                 jvi["init"][2].asFloat()),
101                         new RRTNode(
102                                 jvi["goal"][0].asFloat(),
103                                 jvi["goal"][1].asFloat(),
104                                 jvi["goal"][2].asFloat()));
105         std::vector<CircleObstacle> co;
106         std::vector<SegmentObstacle> so;
107         for (auto o: jvi["obst"]) {
108                 if (o["circle"] != Json::nullValue) {
109                         co.push_back(CircleObstacle(
110                                                 o["circle"][0].asFloat(),
111                                                 o["circle"][1].asFloat(),
112                                                 o["circle"][2].asFloat()));
113                 }
114                 if (o["segment"] != Json::nullValue) {
115                         so.push_back(SegmentObstacle(
116                                 new RRTNode(
117                                         o["segment"][0][0].asFloat(),
118                                         o["segment"][0][1].asFloat(),
119                                         0),
120                                 new RRTNode(
121                                         o["segment"][1][0].asFloat(),
122                                         o["segment"][1][1].asFloat(),
123                                         0)));
124                 }
125         }
126         p.link_obstacles(&co, &so);
127         p.ocost(p.root());
128         p.ocost(p.goal());
129
130 #ifdef USE_LOADF
131         std::vector<RRTNode *> steered;
132         for (auto jn: jvi["traj"][0]) {
133                 steered.push_back(new RRTNode(
134                                         jn[0].asFloat(),
135                                         jn[1].asFloat(),
136                                         jn[2].asFloat(),
137                                         jn[3].asFloat(),
138                                         jn[4].asFloat()));
139         }
140         std::reverse(steered.begin(), steered.end());
141         RRTNode *pn = p.root();
142         for (auto n: steered) {
143                 if (IS_NEAR(pn, n))
144                         continue;
145                 pn->add_child(n, p.cost(pn, n));
146                 pn = n;
147                 p.glplot();
148         }
149         pn->add_child(p.goal(), p.cost(pn, p.goal()));
150         p.goal_found(true);
151         p.tlog(p.findt());
152         if (p.opt_path()) {
153                 p.tlog(p.findt());
154                 p.glplot();
155         }
156         p.glplot();
157         sleep(2);
158 #elif defined USE_INTERRUPT
159         signal(SIGINT, hint);
160         signal(SIGTERM, hint);
161         p.tstart();
162         while (run_planner) {
163                 p.next();
164                 p.tend();
165                 if (p.opt_path())
166                         p.tlog(p.findt());
167                 p.glplot();
168         }
169 #elif defined USE_TMAX
170         p.tstart();
171         p.tend();
172         while (!p.goal_found() && p.elapsed() < TMAX) {
173                 p.next();
174                 p.tend();
175                 if (p.opt_path())
176                         p.tlog(p.findt());
177         }
178 #elif defined USE_PTHREAD
179         bool gf = false;
180         float mc = 9999;
181         pthread_t rt; // root thread
182         pthread_t gt; // goal thread
183         pthread_t ct; // connect thread
184
185         struct next_arg ra;
186         ra.gf = &gf;
187         ra.p = &p.p_root_;
188
189         struct next_arg ga;
190         ga.gf = &gf;
191         ga.p = &p.p_goal_;
192
193         p.tstart();
194         p.p_root_.tstart();
195         p.p_goal_.tstart();
196         pthread_create(&rt, NULL, &next_run, (void *) &ra);
197         pthread_create(&gt, NULL, &next_run, (void *) &ga);
198         volatile int nofrn = 0;
199         volatile int nofgn = 0;
200         RRTNode *rn;
201         RRTNode *gn;
202         while (!gf && p.elapsed() < TMAX) {
203                 // overlap trees
204                 nofrn = p.p_root_.nodes().size() - 1;  // TODO workaround
205                 nofgn = p.p_goal_.nodes().size() - 1;
206                 for (int i = 0; i < nofrn; i++) {
207                         rn = p.p_root_.nodes()[i];
208                         if (rn->parent() == nullptr)
209                                 continue;
210                         for (int j = 0; j < nofgn; j++) {
211                                 gn = p.p_goal_.nodes()[j];
212                                 if (gn->parent() == nullptr)
213                                         continue;
214                                 if (rn->ccost() + gn->ccost() < mc &&
215                                                 IS_NEAR(rn, gn)) {
216                                         gf = true;
217                                 }
218                         }
219                 }
220                 // end of overlap trees
221                 p.tend();
222         }
223         pthread_join(rt, NULL);
224         pthread_join(gt, NULL);
225         if (gf)
226                 p.tlog(p.findt());
227         if (p.opt_path())
228                 p.tlog(p.findt());
229 #endif
230 #ifdef JSONLOGEDGES
231         p.logr(p.root());
232 #endif
233
234         // statistics to error output
235         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
236         std::cerr << "Goal found is " << p.goal_found() << std::endl;
237         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
238         std::cerr << "#samples is " << p.samples().size() << std::endl;
239         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
240         std::cerr << "trajectories costs:" << std::endl;
241         for (j = 0; j < p.clog().size(); j++)
242                 std::cerr << "- " << p.clog()[j] << std::endl;
243         std::cerr << "RRT #nodes:" << std::endl;
244         for (j = 0; j < p.nlog().size(); j++)
245                 std::cerr << "- " << p.nlog()[j] << std::endl;
246         std::cerr << "trajectories seconds:" << std::endl;
247         for (j = 0; j < p.slog().size(); j++)
248                 std::cerr << "- " << p.slog()[j] << std::endl;
249         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
250         std::cerr << std::endl;
251         for (auto edges: p.rlog())
252                 std::cerr << "- " << edges.size() << std::endl;
253
254         // JSON output
255         jvo["elap"] = p.elapsed();
256         // log cost
257         for (j = 0; j < p.clog().size(); j++)
258                 jvo["cost"][j] = p.clog()[j];
259         // log #nodes
260         for (j = 0; j < p.nlog().size(); j++)
261                 jvo["node"][j] = p.nlog()[j];
262         // log seconds
263         for (j = 0; j < p.slog().size(); j++)
264                 jvo["secs"][j] = p.slog()[j];
265         // log traj
266         i = 0;
267         j = 0;
268         for (auto traj: p.tlog()) {
269                 i = 0;
270                 for (auto n: traj) {
271                         jvo["traj"][j][i][0] = n->x();
272                         jvo["traj"][j][i][1] = n->y();
273                         jvo["traj"][j][i][2] = n->h();
274                         jvo["traj"][j][i][3] = n->t();
275                         jvo["traj"][j][i][4] = n->s();
276                         i++;
277                 }
278                 j++;
279         }
280 #ifdef JSONLOGEDGES
281         i = 0;
282         j = 0;
283         for (auto edges: p.rlog()) {
284                 j = 0;
285                 for (auto e: edges) {
286                         jvo["edge"][i][j][0][0] = e->init()->x();
287                         jvo["edge"][i][j][0][1] = e->init()->y();
288                         jvo["edge"][i][j][0][2] = e->init()->h();
289                         jvo["edge"][i][j][1][0] = e->goal()->x();
290                         jvo["edge"][i][j][1][1] = e->goal()->y();
291                         jvo["edge"][i][j][1][2] = e->goal()->h();
292                         j++;
293                 }
294                 i++;
295         }
296 #endif
297 #ifdef JSONLOGSAMPLES
298         i = 0;
299         j = 0;
300         for (auto s: p.samples()) {
301                 jvo["samp"][j][0] = s->x();
302                 jvo["samp"][j][1] = s->y();
303                 jvo["samp"][j][2] = s->h();
304                 j++;
305         }
306 #endif
307         // print output
308         std::cout << jvo << std::endl;
309
310 #ifdef USE_GL
311         SDL_DestroyWindow(gw);
312         SDL_Quit();
313 #endif
314
315         // free mem
316         for (auto o: so) {
317                 delete o.init();
318                 delete o.goal();
319         }
320         return 0;
321 }
322
323 bool init()
324 {
325         if (SDL_Init(SDL_INIT_VIDEO) < 0) {
326                 std::cerr << "SDL could not initialize! SDL_Error: ";
327                 std::cerr << SDL_GetError();
328                 std::cerr << std::endl;
329                 return false;
330         }
331         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
332         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
333         gw = SDL_CreateWindow(
334                         "I am car",
335                         SDL_WINDOWPOS_UNDEFINED,
336                         SDL_WINDOWPOS_UNDEFINED,
337                         SCREEN_WIDTH,
338                         SCREEN_HEIGHT,
339                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
340         if (gw == NULL) {
341                 std::cerr << "Window could not be created! SDL_Error: ";
342                 std::cerr << SDL_GetError();
343                 std::cerr << std::endl;
344                 return false;
345         }
346         gc = SDL_GL_CreateContext(gw);
347         if (gc == NULL) {
348                 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
349                 std::cerr << SDL_GetError();
350                 std::cerr << std::endl;
351                 return false;
352         }
353         if (SDL_GL_SetSwapInterval(1) < 0) {
354                 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
355                 std::cerr << SDL_GetError();
356                 std::cerr << std::endl;
357                 return false;
358         }
359         if (!initGL()) {
360                 std::cerr << "Unable to initialize OpenGL!";
361                 std::cerr << std::endl;
362                 return false;
363         }
364         return true;
365 }
366
367 bool initGL()
368 {
369         GLenum error = GL_NO_ERROR;
370         glMatrixMode(GL_PROJECTION);
371         glLoadIdentity();
372         error = glGetError();
373         if (error != GL_NO_ERROR) {
374                 std::cerr << "Error initializing OpenGL! ";
375                 std::cerr << gluErrorString(error);
376                 std::cerr << std::endl;
377                 return false;
378         }
379         glMatrixMode(GL_MODELVIEW);
380         glLoadIdentity();
381         error = glGetError();
382         if (error != GL_NO_ERROR) {
383                 std::cerr << "Error initializing OpenGL! ";
384                 std::cerr << gluErrorString(error);
385                 std::cerr << std::endl;
386                 return false;
387         }
388         glClearColor(1, 1, 1, 1);
389         error = glGetError();
390         if (error != GL_NO_ERROR) {
391                 std::cerr << "Error initializing OpenGL! ";
392                 std::cerr << gluErrorString(error);
393                 std::cerr << std::endl;
394                 return false;
395         }
396         return true;
397 }