2 This file is part of I am car.
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.
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.
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/>.
21 #include <jsoncpp/json/json.h>
27 #include "rrtplanner.h"
28 #include "slotplanner.h"
35 //#define JSONLOGEDGES
36 //#define JSONLOGSAMPLES
39 //#define USE_INTERRUPT
48 //#define USE_SLOTPLANNER
51 //#define USE_SLOTPLANNER
57 std::chrono::high_resolution_clock::time_point TSTART_;
58 std::chrono::high_resolution_clock::time_point TEND_;
61 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
63 std::chrono::duration<float> DT_;
64 TEND_ = std::chrono::high_resolution_clock::now();
65 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
68 TELAPSED += DT_.count();
69 ELAPSED = DT_.count();
71 void TPRINT(const char *what) {
72 std::chrono::duration<float> DT_;
73 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
76 std::cerr << what << ": " << DT_.count() << std::endl;
79 bool run_planner = true;
81 SDL_Window* gw = NULL;
98 void *next_run(void *arg)
100 struct next_arg *na = (struct next_arg *) arg;
101 T2 *lp = (T2 *) na->p;
103 while (!*gf && lp->elapsed() < TMAX) {
119 Json::Value jvi; // JSON input
120 Json::Value jvo; // JSON output
124 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
128 jvi["init"][0].asFloat(),
129 jvi["init"][1].asFloat(),
130 jvi["init"][2].asFloat()),
132 jvi["goal"][0].asFloat(),
133 jvi["goal"][1].asFloat(),
134 jvi["goal"][2].asFloat()));
135 std::vector<CircleObstacle> co;
136 std::vector<SegmentObstacle> so;
137 for (auto o: jvi["obst"]) {
138 if (o["circle"] != Json::nullValue) {
139 co.push_back(CircleObstacle(
140 o["circle"][0].asFloat(),
141 o["circle"][1].asFloat(),
142 o["circle"][2].asFloat()));
144 if (o["segment"] != Json::nullValue) {
145 so.push_back(SegmentObstacle(
147 o["segment"][0][0].asFloat(),
148 o["segment"][0][1].asFloat(),
151 o["segment"][1][0].asFloat(),
152 o["segment"][1][1].asFloat(),
154 p.frame().add_bnode(so.back().init());
157 p.link_obstacles(&co, &so);
161 ParallelSlot ps = ParallelSlot();
162 #ifdef USE_SLOTPLANNER
164 for (auto xy: jvi["slot"]["polygon"]) {
165 ps.slot().add_bnode(new RRTNode(
170 if (ps.slot().bnodes().size() > 0)
173 jvo["ppse"] = ELAPSED;
174 TPRINT("ParallelSlot");
176 if (ps.cusp().size() > 0) {
177 p.goal(ps.cusp().front().front());
178 p.slot_cusp(ps.cusp().front()); // use first found solution
179 jvo["midd"][0] = p.goal()->x();
180 jvo["midd"][1] = p.goal()->y();
181 jvo["midd"][2] = p.goal()->h();
182 jvo["goal"][0] = p.slot_cusp().back()->x();
183 jvo["goal"][1] = p.slot_cusp().back()->y();
184 jvo["goal"][2] = p.slot_cusp().back()->h();
186 jvo["goal"][0] = p.goal()->x();
187 jvo["goal"][1] = p.goal()->y();
188 jvo["goal"][2] = p.goal()->h();
192 std::vector<RRTNode *> steered;
193 for (auto jn: jvi["traj"][0]) {
194 steered.push_back(new RRTNode(
201 std::reverse(steered.begin(), steered.end());
202 RRTNode *pn = p.root();
203 for (auto n: steered) {
206 pn->add_child(n, p.cost(pn, n));
210 pn->add_child(p.goal(), p.cost(pn, p.goal()));
219 #elif defined USE_INTERRUPT
220 signal(SIGINT, hint);
221 signal(SIGTERM, hint);
223 while (run_planner) {
230 #elif defined USE_TMAX
233 while (!p.goal_found() && p.elapsed() < TMAX) {
237 if (ps.cusp().size() > 0)
238 p.tlog(p.findt(p.slot_cusp().back()));
243 #elif defined USE_PTHREAD
245 RRTNode *ron = nullptr;
246 RRTNode *gon = nullptr;
248 pthread_t rt; // root thread
249 pthread_t gt; // goal thread
250 pthread_t ct; // connect thread
263 pthread_create(&rt, NULL, &next_run, (void *) &ra);
264 pthread_create(>, NULL, &next_run, (void *) &ga);
268 while (!gf && p.elapsed() < TMAX &&
269 p.p_root_.nodes().size() < NOFNODES &&
270 p.p_goal_.nodes().size() < NOFNODES) {
273 for (int i = 0; i < IXSIZE; i++) {
274 for (int j = 0; j < IYSIZE; j++) {
275 if (p.p_root_.ixy_[i][j].changed() &&
276 p.p_goal_.ixy_[i][j].changed()) {
278 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
279 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
280 if (rn->ccost() + gn->ccost() < mc &&
286 mc = rn->ccost() + gn->ccost();
294 if (p.elapsed() >= TMAX)
297 // end of overlap trees
301 pthread_join(rt, NULL);
302 pthread_join(gt, NULL);
303 float nodo = ((float) ndl / (float) tol);
304 std::cerr << "nothing done is " << 100.0 * nodo;
305 std::cerr << "%" << std::endl;
306 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
307 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
308 //std::cerr << "cgf is " << p.goal_found() << std::endl;
309 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
310 ron = p.p_root_.goal()->parent();
311 gon = p.p_root_.goal();
312 mc = p.p_root_.goal()->ccost();
314 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
315 ron = p.p_goal_.goal();
316 gon = p.p_goal_.goal()->parent();
317 mc = p.p_goal_.goal()->ccost();
319 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
321 p.goal()->children().clear();
324 while (gon != p.goal()) {
325 p.p_root_.nodes().push_back(new RRTNode(
330 p.p_root_.nodes().back(),
333 p.p_root_.nodes().back()));
334 ron = p.p_root_.nodes().back();
337 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
339 // end of connect trees
347 jvo["rrte"] = ELAPSED;
352 // statistics to error output
353 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
354 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
355 std::cerr << "Goal found is " << p.goal_found() << std::endl;
356 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
357 std::cerr << "#samples is " << p.samples().size() << std::endl;
358 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
359 std::cerr << "trajectories costs:" << std::endl;
360 for (j = 0; j < p.clog().size(); j++)
361 std::cerr << "- " << p.clog()[j] << std::endl;
362 std::cerr << "RRT #nodes:" << std::endl;
363 for (j = 0; j < p.nlog().size(); j++)
364 std::cerr << "- " << p.nlog()[j] << std::endl;
365 std::cerr << "trajectories seconds:" << std::endl;
366 for (j = 0; j < p.slog().size(); j++)
367 std::cerr << "- " << p.slog()[j] << std::endl;
368 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
369 std::cerr << std::endl;
370 for (auto edges: p.rlog())
371 std::cerr << "- " << edges.size() << std::endl;
374 jvo["elap"] = TELAPSED;
376 jvo["nodo"][0] = nodo;
379 for (j = 0; j < p.clog().size(); j++)
380 jvo["cost"][j] = p.clog()[j];
382 for (j = 0; j < p.nlog().size(); j++)
383 jvo["node"][j] = p.nlog()[j];
385 for (j = 0; j < p.slog().size(); j++)
386 jvo["secs"][j] = p.slog()[j];
390 for (auto traj: p.tlog()) {
393 jvo["traj"][j][i][0] = n->x();
394 jvo["traj"][j][i][1] = n->y();
395 jvo["traj"][j][i][2] = n->h();
396 jvo["traj"][j][i][3] = n->t();
397 jvo["traj"][j][i][4] = n->s();
405 for (auto edges: p.rlog()) {
407 for (auto e: edges) {
408 jvo["edge"][i][j][0][0] = e->init()->x();
409 jvo["edge"][i][j][0][1] = e->init()->y();
410 jvo["edge"][i][j][0][2] = e->init()->h();
411 jvo["edge"][i][j][1][0] = e->goal()->x();
412 jvo["edge"][i][j][1][1] = e->goal()->y();
413 jvo["edge"][i][j][1][2] = e->goal()->h();
419 #ifdef JSONLOGSAMPLES
422 for (auto s: p.samples()) {
423 jvo["samp"][j][0] = s->x();
424 jvo["samp"][j][1] = s->y();
425 jvo["samp"][j][2] = s->h();
430 std::cout << jvo << std::endl;
433 SDL_DestroyWindow(gw);
447 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
448 std::cerr << "SDL could not initialize! SDL_Error: ";
449 std::cerr << SDL_GetError();
450 std::cerr << std::endl;
453 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
454 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
455 gw = SDL_CreateWindow(
457 SDL_WINDOWPOS_UNDEFINED,
458 SDL_WINDOWPOS_UNDEFINED,
461 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
463 std::cerr << "Window could not be created! SDL_Error: ";
464 std::cerr << SDL_GetError();
465 std::cerr << std::endl;
468 gc = SDL_GL_CreateContext(gw);
470 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
471 std::cerr << SDL_GetError();
472 std::cerr << std::endl;
475 if (SDL_GL_SetSwapInterval(1) < 0) {
476 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
477 std::cerr << SDL_GetError();
478 std::cerr << std::endl;
482 std::cerr << "Unable to initialize OpenGL!";
483 std::cerr << std::endl;
491 GLenum error = GL_NO_ERROR;
492 glMatrixMode(GL_PROJECTION);
494 error = glGetError();
495 if (error != GL_NO_ERROR) {
496 std::cerr << "Error initializing OpenGL! ";
497 std::cerr << gluErrorString(error);
498 std::cerr << std::endl;
501 glMatrixMode(GL_MODELVIEW);
503 error = glGetError();
504 if (error != GL_NO_ERROR) {
505 std::cerr << "Error initializing OpenGL! ";
506 std::cerr << gluErrorString(error);
507 std::cerr << std::endl;
510 glClearColor(1, 1, 1, 1);
511 error = glGetError();
512 if (error != GL_NO_ERROR) {
513 std::cerr << "Error initializing OpenGL! ";
514 std::cerr << gluErrorString(error);
515 std::cerr << std::endl;