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