]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Return path to first cusps if goal not found
[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 <signal.h>
22 #include "compile.h"
23 #include "obstacle.h"
24 #include "rrtplanner.h"
25 // OpenGL
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include <SDL2/SDL.h>
29
30 #define USE_GL
31 #define USE_INTERRUPT
32 #define USE_TMAX
33 #define USE_TMAX_1ST_CUSPS
34
35 bool run_planner = true;
36
37 SDL_Window* gw = NULL;
38 SDL_GLContext gc;
39
40 bool init();
41 bool initGL();
42
43 void hint(int)
44 {
45         run_planner = false;
46 }
47
48 int main()
49 {
50 #ifdef USE_GL
51         init();
52 #endif
53
54         Json::Value jvi; // JSON input
55         Json::Value jvo; // JSON output
56         unsigned int i = 0;
57         unsigned int j = 0;
58         std::cin >> jvi;
59         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
60
61         PLANNER p(
62                         new RRTNode(
63                                 jvi["init"][0].asFloat(),
64                                 jvi["init"][1].asFloat(),
65                                 jvi["init"][2].asFloat()),
66                         new RRTNode(
67                                 jvi["goal"][0].asFloat(),
68                                 jvi["goal"][1].asFloat(),
69                                 jvi["goal"][2].asFloat()));
70         std::vector<CircleObstacle> co;
71         std::vector<SegmentObstacle> so;
72         for (auto o: jvi["obst"]) {
73                 if (o["circle"] != Json::nullValue) {
74                         co.push_back(CircleObstacle(
75                                                 o["circle"][0].asFloat(),
76                                                 o["circle"][1].asFloat(),
77                                                 o["circle"][2].asFloat()));
78                 }
79                 if (o["segment"] != Json::nullValue) {
80                         so.push_back(SegmentObstacle(
81                                 new RRTNode(
82                                         o["segment"][0][0].asFloat(),
83                                         o["segment"][0][1].asFloat(),
84                                         0),
85                                 new RRTNode(
86                                         o["segment"][1][0].asFloat(),
87                                         o["segment"][1][1].asFloat(),
88                                         0)));
89                 }
90         }
91         p.link_obstacles(&co, &so);
92         p.ocost(p.root());
93         p.ocost(p.goal());
94
95         RRTNode *nn;
96         std::vector<RRTNode *> tr;
97
98 #ifdef USE_INTERRUPT
99         signal(SIGINT, hint);
100         signal(SIGTERM, hint);
101         p.tstart();
102         while (run_planner) {
103                 p.next();
104                 p.tend();
105                 if (p.opt_path())
106                         p.tlog(p.findt());
107 #ifdef USE_GL
108                 p.glplot();
109 #endif
110         }
111         if (!p.goal_found()) {
112 #if NNVERSION > 1
113                 nn = p.nn(p.iy_, p.goal(), p.cost);
114 #else
115                 nn = p.nn(p.nodes(), p.goal(), p.cost);
116 #endif
117                 tr = p.findt(nn);
118                 p.tlog(tr);
119                 while (p.opt_path())
120                         p.tlog(p.findt(nn));
121         }
122 #if JSONLOGEDGES > 0
123         p.logr(p.root());
124 #endif
125 #elif defined USE_TMAX // USE TMAX
126         p.tstart();
127         p.tend();
128         while (p.elapsed() < TMAX) {
129                 p.next();
130                 p.tend();
131                 while (p.opt_path())
132                         p.tlog(p.findt());
133         }
134         if (!p.goal_found()) {
135 #if NNVERSION > 1
136                 nn = p.nn(p.iy_, p.goal(), p.cost);
137 #else
138                 nn = p.nn(p.nodes(), p.goal(), p.cost);
139 #endif
140                 tr = p.findt(nn);
141 #ifdef USE_TMAX_1ST_CUSPS
142                 while (p.opt_path()) {}
143                 tr = p.findt(nn);
144                 std::reverse(tr.begin(), tr.end());
145                 for (auto n: tr) {
146                         if (n->s() != 0 &&
147                                         n->parent() &&
148                                         n->parent()->s() != 0 &&
149                                         sgn(n->s()) != sgn(n->parent()->s())) {
150                                 nn = n;
151                                 break;
152                         }
153                 }
154                 tr = p.findt(nn);
155 #endif
156         } else {
157                 nn = p.goal();
158                 tr = p.findt();
159         }
160         p.tlog(tr);
161 #if JSONLOGEDGES > 0
162         p.logr(p.root());
163 #endif
164         while (p.opt_path())
165                 p.tlog(p.findt(nn));
166 #else // NOT USE_INTERRUPT
167         i = 0;
168         while (!p.goal_found() && i < 20) {
169                 p.tstart();
170                 p.tend();
171                 while (p.elapsed() < TMAX) {
172                         p.next();
173                         p.tend();
174                 }
175                 if (!p.goal_found()) {
176 #if NNVERSION > 1
177                         nn = p.nn(p.iy_, p.goal(), p.cost);
178 #else
179                         nn = p.nn(p.nodes(), p.goal(), p.cost);
180 #endif
181                         tr = p.findt(nn);
182                 } else {
183                         nn = p.goal();
184                         tr = p.findt();
185                 }
186                 p.tlog(tr);
187 #if JSONLOGEDGES > 0
188                 p.logr(p.root());
189 #endif
190                 while (p.opt_path())
191                         p.tlog(p.findt(nn));
192                 if (!p.goal_found()) {
193                         nn = p.tlog().back().back();
194                         for (j = p.tlog().back().size() - 1; j > 0; j--) {
195                                 float s1 = p.tlog().back()[j]->s();
196                                 float s2 = p.tlog().back()[j - 1]->s();
197                                 if (sgn(s1) != sgn(s2)) {
198                                         nn = p.tlog().back()[j - 1];
199                                         break;
200                                 }
201                         }
202                         p.tlog(p.findt(nn));
203                         p.rebase(nn);
204                 }
205                 i++;
206         }
207         std::cerr << "#iterations is " << i << std::endl;
208 #endif // USE_INTERRUPT
209
210         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
211         std::cerr << "Goal found is " << p.goal_found() << std::endl;
212         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
213         std::cerr << "#samples is " << p.samples().size() << std::endl;
214         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
215         std::cerr << "trajectories costs:" << std::endl;
216         for (j = 0; j < p.clog().size(); j++)
217                 std::cerr << "- " << p.clog()[j] << std::endl;
218         std::cerr << "RRT #nodes:" << std::endl;
219         for (j = 0; j < p.nlog().size(); j++)
220                 std::cerr << "- " << p.nlog()[j] << std::endl;
221         std::cerr << "trajectories seconds:" << std::endl;
222         for (j = 0; j < p.slog().size(); j++)
223                 std::cerr << "- " << p.slog()[j] << std::endl;
224         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
225         std::cerr << std::endl;
226         for (auto edges: p.rlog())
227                 std::cerr << "- " << edges.size() << std::endl;
228
229         // JSON output
230         jvo["elap"] = p.elapsed();
231         // log cost
232         for (j = 0; j < p.clog().size(); j++)
233                 jvo["cost"][j] = p.clog()[j];
234         // log #nodes
235         for (j = 0; j < p.nlog().size(); j++)
236                 jvo["node"][j] = p.nlog()[j];
237         // log seconds
238         for (j = 0; j < p.slog().size(); j++)
239                 jvo["secs"][j] = p.slog()[j];
240         // log traj
241         i = 0;
242         j = 0;
243         for (auto traj: p.tlog()) {
244                 i = 0;
245                 for (auto n: traj) {
246                         jvo["traj"][j][i][0] = n->x();
247                         jvo["traj"][j][i][1] = n->y();
248                         jvo["traj"][j][i][2] = n->h();
249                         jvo["traj"][j][i][3] = n->t();
250                         jvo["traj"][j][i][4] = n->s();
251                         i++;
252                 }
253                 j++;
254         }
255 #if JSONLOGEDGES > 0
256         // log edges
257         i = 0;
258         j = 0;
259         for (auto edges: p.rlog()) {
260                 j = 0;
261                 for (auto e: edges) {
262                         jvo["edge"][i][j][0][0] = e->init()->x();
263                         jvo["edge"][i][j][0][1] = e->init()->y();
264                         jvo["edge"][i][j][1][0] = e->goal()->x();
265                         jvo["edge"][i][j][1][1] = e->goal()->y();
266                         j++;
267                 }
268                 i++;
269         }
270 #endif
271 #if JSONLOGSAMPLES > 0
272         // log samples
273         i = 0;
274         j = 0;
275         for (auto s: p.samples()) {
276                 jvo["samp"][j][0] = s->x();
277                 jvo["samp"][j][1] = s->y();
278                 jvo["samp"][j][2] = s->h();
279                 j++;
280         }
281 #endif
282         // print output
283         std::cout << jvo << std::endl;
284
285 #ifdef USE_GL
286         SDL_DestroyWindow(gw);
287         SDL_Quit();
288 #endif
289
290         // free mem
291         for (auto o: so) {
292                 delete o.init();
293                 delete o.goal();
294         }
295         return 0;
296 }
297
298 bool init()
299 {
300         if (SDL_Init(SDL_INIT_VIDEO) < 0) {
301                 std::cerr << "SDL could not initialize! SDL_Error: ";
302                 std::cerr << SDL_GetError();
303                 std::cerr << std::endl;
304                 return false;
305         }
306         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
307         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
308         gw = SDL_CreateWindow(
309                         "I am car",
310                         SDL_WINDOWPOS_UNDEFINED,
311                         SDL_WINDOWPOS_UNDEFINED,
312                         SCREEN_WIDTH,
313                         SCREEN_HEIGHT,
314                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
315         if (gw == NULL) {
316                 std::cerr << "Window could not be created! SDL_Error: ";
317                 std::cerr << SDL_GetError();
318                 std::cerr << std::endl;
319                 return false;
320         }
321         gc = SDL_GL_CreateContext(gw);
322         if (gc == NULL) {
323                 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
324                 std::cerr << SDL_GetError();
325                 std::cerr << std::endl;
326                 return false;
327         }
328         if (SDL_GL_SetSwapInterval(1) < 0) {
329                 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
330                 std::cerr << SDL_GetError();
331                 std::cerr << std::endl;
332                 return false;
333         }
334         if (!initGL()) {
335                 std::cerr << "Unable to initialize OpenGL!";
336                 std::cerr << std::endl;
337                 return false;
338         }
339         return true;
340 }
341
342 bool initGL()
343 {
344         GLenum error = GL_NO_ERROR;
345         glMatrixMode(GL_PROJECTION);
346         glLoadIdentity();
347         error = glGetError();
348         if (error != GL_NO_ERROR) {
349                 std::cerr << "Error initializing OpenGL! ";
350                 std::cerr << gluErrorString(error);
351                 std::cerr << std::endl;
352                 return false;
353         }
354         glMatrixMode(GL_MODELVIEW);
355         glLoadIdentity();
356         error = glGetError();
357         if (error != GL_NO_ERROR) {
358                 std::cerr << "Error initializing OpenGL! ";
359                 std::cerr << gluErrorString(error);
360                 std::cerr << std::endl;
361                 return false;
362         }
363         glClearColor(1, 1, 1, 1);
364         error = glGetError();
365         if (error != GL_NO_ERROR) {
366                 std::cerr << "Error initializing OpenGL! ";
367                 std::cerr << gluErrorString(error);
368                 std::cerr << std::endl;
369                 return false;
370         }
371         return true;
372 }