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