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