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