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