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