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