]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Optimize path only once when path 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 DBG_LOADF
99         std::vector<RRTNode *> steered;
100         for (auto jn: jvi["traj"][0]) {
101                 steered.push_back(new RRTNode(
102                                         jn[0].asFloat(),
103                                         jn[1].asFloat(),
104                                         jn[2].asFloat(),
105                                         jn[3].asFloat(),
106                                         jn[4].asFloat()));
107         }
108         std::reverse(steered.begin(), steered.end());
109         RRTNode *pn = p.root();
110         for (auto n: steered) {
111                 pn->add_child(n, p.cost(pn, n));
112                 pn = n;
113         }
114         pn->add_child(p.goal(), p.cost(pn, p.goal()));
115         p.tlog(p.findt());
116         if (p.opt_path())
117                 p.tlog(p.findt());
118 #else
119 #ifdef USE_INTERRUPT
120         signal(SIGINT, hint);
121         signal(SIGTERM, hint);
122         p.tstart();
123         while (run_planner) {
124                 p.next();
125                 p.tend();
126                 if (p.opt_path())
127                         p.tlog(p.findt());
128 #ifdef USE_GL
129                 p.glplot();
130 #endif
131         }
132         if (!p.goal_found()) {
133 #if NNVERSION > 1
134                 nn = p.nn(p.iy_, p.goal(), p.cost);
135 #else
136                 nn = p.nn(p.nodes(), p.goal(), p.cost);
137 #endif
138                 tr = p.findt(nn);
139                 p.tlog(tr);
140                 if (p.opt_path())
141                         p.tlog(p.findt(nn));
142         }
143 #if JSONLOGEDGES > 0
144         p.logr(p.root());
145 #endif
146 #elif defined USE_TMAX // USE TMAX
147         p.tstart();
148         p.tend();
149         while (!p.goal_found() && p.elapsed() < TMAX) {
150                 p.next();
151                 p.tend();
152                 if (p.opt_path())
153                         p.tlog(p.findt());
154         }
155 #if JSONLOGEDGES > 0
156         p.logr(p.root());
157 #endif
158 #else // NOT USE_INTERRUPT
159         i = 0;
160         while (!p.goal_found() && i < 20) {
161                 p.tstart();
162                 p.tend();
163                 while (p.elapsed() < TMAX) {
164                         p.next();
165                         p.tend();
166                 }
167                 if (!p.goal_found()) {
168 #if NNVERSION > 1
169                         nn = p.nn(p.iy_, p.goal(), p.cost);
170 #else
171                         nn = p.nn(p.nodes(), p.goal(), p.cost);
172 #endif
173                         tr = p.findt(nn);
174                 } else {
175                         nn = p.goal();
176                         tr = p.findt();
177                 }
178                 p.tlog(tr);
179 #if JSONLOGEDGES > 0
180                 p.logr(p.root());
181 #endif
182                 if (p.opt_path())
183                         p.tlog(p.findt(nn));
184                 if (!p.goal_found()) {
185                         nn = p.tlog().back().back();
186                         for (j = p.tlog().back().size() - 1; j > 0; j--) {
187                                 float s1 = p.tlog().back()[j]->s();
188                                 float s2 = p.tlog().back()[j - 1]->s();
189                                 if (sgn(s1) != sgn(s2)) {
190                                         nn = p.tlog().back()[j - 1];
191                                         break;
192                                 }
193                         }
194                         p.tlog(p.findt(nn));
195                         p.rebase(nn);
196                 }
197                 i++;
198         }
199         std::cerr << "#iterations is " << i << std::endl;
200 #endif // USE_INTERRUPT
201 #endif // DBG_LOADF
202
203         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
204         std::cerr << "Goal found is " << p.goal_found() << std::endl;
205         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
206         std::cerr << "#samples is " << p.samples().size() << std::endl;
207         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
208         std::cerr << "trajectories costs:" << std::endl;
209         for (j = 0; j < p.clog().size(); j++)
210                 std::cerr << "- " << p.clog()[j] << std::endl;
211         std::cerr << "RRT #nodes:" << std::endl;
212         for (j = 0; j < p.nlog().size(); j++)
213                 std::cerr << "- " << p.nlog()[j] << std::endl;
214         std::cerr << "trajectories seconds:" << std::endl;
215         for (j = 0; j < p.slog().size(); j++)
216                 std::cerr << "- " << p.slog()[j] << std::endl;
217         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
218         std::cerr << std::endl;
219         for (auto edges: p.rlog())
220                 std::cerr << "- " << edges.size() << std::endl;
221
222         // JSON output
223         jvo["elap"] = p.elapsed();
224         // log cost
225         for (j = 0; j < p.clog().size(); j++)
226                 jvo["cost"][j] = p.clog()[j];
227         // log #nodes
228         for (j = 0; j < p.nlog().size(); j++)
229                 jvo["node"][j] = p.nlog()[j];
230         // log seconds
231         for (j = 0; j < p.slog().size(); j++)
232                 jvo["secs"][j] = p.slog()[j];
233         // log traj
234         i = 0;
235         j = 0;
236         for (auto traj: p.tlog()) {
237                 i = 0;
238                 for (auto n: traj) {
239                         jvo["traj"][j][i][0] = n->x();
240                         jvo["traj"][j][i][1] = n->y();
241                         jvo["traj"][j][i][2] = n->h();
242                         jvo["traj"][j][i][3] = n->t();
243                         jvo["traj"][j][i][4] = n->s();
244                         i++;
245                 }
246                 j++;
247         }
248 #if JSONLOGEDGES > 0
249         // log edges
250         i = 0;
251         j = 0;
252         for (auto edges: p.rlog()) {
253                 j = 0;
254                 for (auto e: edges) {
255                         jvo["edge"][i][j][0][0] = e->init()->x();
256                         jvo["edge"][i][j][0][1] = e->init()->y();
257                         jvo["edge"][i][j][0][2] = e->init()->h();
258                         jvo["edge"][i][j][1][0] = e->goal()->x();
259                         jvo["edge"][i][j][1][1] = e->goal()->y();
260                         jvo["edge"][i][j][1][2] = e->goal()->h();
261                         j++;
262                 }
263                 i++;
264         }
265 #endif
266 #if JSONLOGSAMPLES > 0
267         // log samples
268         i = 0;
269         j = 0;
270         for (auto s: p.samples()) {
271                 jvo["samp"][j][0] = s->x();
272                 jvo["samp"][j][1] = s->y();
273                 jvo["samp"][j][2] = s->h();
274                 j++;
275         }
276 #endif
277         // print output
278         std::cout << jvo << std::endl;
279
280 #ifdef USE_GL
281         SDL_DestroyWindow(gw);
282         SDL_Quit();
283 #endif
284
285         // free mem
286         for (auto o: so) {
287                 delete o.init();
288                 delete o.goal();
289         }
290         return 0;
291 }
292
293 bool init()
294 {
295         if (SDL_Init(SDL_INIT_VIDEO) < 0) {
296                 std::cerr << "SDL could not initialize! SDL_Error: ";
297                 std::cerr << SDL_GetError();
298                 std::cerr << std::endl;
299                 return false;
300         }
301         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
302         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
303         gw = SDL_CreateWindow(
304                         "I am car",
305                         SDL_WINDOWPOS_UNDEFINED,
306                         SDL_WINDOWPOS_UNDEFINED,
307                         SCREEN_WIDTH,
308                         SCREEN_HEIGHT,
309                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
310         if (gw == NULL) {
311                 std::cerr << "Window could not be created! SDL_Error: ";
312                 std::cerr << SDL_GetError();
313                 std::cerr << std::endl;
314                 return false;
315         }
316         gc = SDL_GL_CreateContext(gw);
317         if (gc == NULL) {
318                 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
319                 std::cerr << SDL_GetError();
320                 std::cerr << std::endl;
321                 return false;
322         }
323         if (SDL_GL_SetSwapInterval(1) < 0) {
324                 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
325                 std::cerr << SDL_GetError();
326                 std::cerr << std::endl;
327                 return false;
328         }
329         if (!initGL()) {
330                 std::cerr << "Unable to initialize OpenGL!";
331                 std::cerr << std::endl;
332                 return false;
333         }
334         return true;
335 }
336
337 bool initGL()
338 {
339         GLenum error = GL_NO_ERROR;
340         glMatrixMode(GL_PROJECTION);
341         glLoadIdentity();
342         error = glGetError();
343         if (error != GL_NO_ERROR) {
344                 std::cerr << "Error initializing OpenGL! ";
345                 std::cerr << gluErrorString(error);
346                 std::cerr << std::endl;
347                 return false;
348         }
349         glMatrixMode(GL_MODELVIEW);
350         glLoadIdentity();
351         error = glGetError();
352         if (error != GL_NO_ERROR) {
353                 std::cerr << "Error initializing OpenGL! ";
354                 std::cerr << gluErrorString(error);
355                 std::cerr << std::endl;
356                 return false;
357         }
358         glClearColor(1, 1, 1, 1);
359         error = glGetError();
360         if (error != GL_NO_ERROR) {
361                 std::cerr << "Error initializing OpenGL! ";
362                 std::cerr << gluErrorString(error);
363                 std::cerr << std::endl;
364                 return false;
365         }
366         return true;
367 }