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/>.
23 #include <jsoncpp/json/json.h>
29 #include "rrtplanner.h"
30 #include "slotplanner.h"
40 //#define JSONLOGEDGES
41 //#define JSONLOGSAMPLES
47 //#define USE_INTERRUPT
57 #define SCREEN_WIDTH 1000
58 #define SCREEN_HEIGHT 1000
60 std::chrono::high_resolution_clock::time_point TSTART_;
61 std::chrono::high_resolution_clock::time_point TEND_;
64 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
66 std::chrono::duration<float> DT_;
67 TEND_ = std::chrono::high_resolution_clock::now();
68 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
71 TELAPSED += DT_.count();
72 ELAPSED = DT_.count();
74 void TPRINT(const char *what) {
75 std::chrono::duration<float> DT_;
76 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
79 std::cerr << what << ": " << DT_.count() << std::endl;
82 bool run_planner = true;
85 SDL_Window* gw = NULL;
103 void *next_run(void *arg)
105 struct next_arg *na = (struct next_arg *) arg;
106 T2 *lp = (T2 *) na->p;
108 while (!*gf && lp->elapsed() < TMAX) {
120 float new_x = 1 + static_cast<float>(rand()) /
121 static_cast<float>(RAND_MAX / (6.6 - 1 - 1));
123 float new_h = M_PI / 2;
124 return new RRTNode(new_x, new_y, new_h);
129 srand(static_cast<unsigned>(time(0)));
134 Json::Value jvi; // JSON input
135 Json::Value jvo; // JSON output
139 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
141 if (jvi["init"] == Json::nullValue) {
142 std::cerr << "I need `init` in JSON input scenario";
143 std::cerr << std::endl;
147 if (jvi["goal"] == Json::nullValue) {
148 std::cerr << "I need `goal` in JSON input scenario";
149 std::cerr << std::endl;
155 jvi["init"][0].asFloat(),
156 jvi["init"][1].asFloat(),
157 jvi["init"][2].asFloat()
160 jvi["goal"][0].asFloat(),
161 jvi["goal"][1].asFloat(),
162 jvi["goal"][2].asFloat()
165 jvo["init"][0] = p.root()->x();
166 jvo["init"][1] = p.root()->y();
167 jvo["init"][2] = p.root()->h();
168 jvo["goal"][0] = jvi["goal"][0].asFloat();
169 jvo["goal"][1] = jvi["goal"][1].asFloat();
170 jvo["goal"][2] = jvi["goal"][2].asFloat();
172 if (jvi["goals"] != Json::nullValue) {
173 for (auto g: jvi["goals"]) {
174 p.goals().push_back(new RRTNode(
182 if (jvi["slot"] != Json::nullValue) {
183 jvo["slot"] = jvi["slot"];
186 std::vector<CircleObstacle> co;
187 std::vector<SegmentObstacle> so;
188 p.HMIN = p.root()->x();
189 p.HMAX = p.root()->x();
190 p.VMIN = p.root()->y();
191 p.VMAX = p.root()->y();
193 for (auto o: jvi["obst"]) {
198 for (i = 0; i < o.size() - 1; i++) {
199 tmpx = o[i][0].asFloat();
200 tmpy = o[i][1].asFloat();
201 tmpr = o[i + 1][0].asFloat();
202 tmps = o[i + 1][1].asFloat();
203 so.push_back(SegmentObstacle(
204 new RRTNode(tmpx, tmpy, 0),
205 new RRTNode(tmpr, tmps, 0)
207 p.frame().add_bnode(so.back().init());
208 if (tmpx < p.HMIN) p.HMIN = tmpx;
209 if (tmpx > p.HMAX) p.HMAX = tmpx;
210 if (tmpy < p.VMIN) p.VMIN = tmpy;
211 if (tmpy > p.VMAX) p.VMAX = tmpy;
212 if (tmpr < p.HMIN) p.HMIN = tmpr;
213 if (tmpr > p.HMAX) p.HMAX = tmpr;
214 if (tmps < p.VMIN) p.VMIN = tmps;
215 if (tmps > p.VMAX) p.VMAX = tmps;
218 jvo["obst"][j][i][0] = tmpx;
219 jvo["obst"][j][i][1] = tmpy;
221 jvo["obst"][j][i][0] = tmpr;
222 jvo["obst"][j][i][1] = tmps;
225 p.defaultSamplingInfo();
226 p.link_obstacles(&co, &so);
231 std::vector<RRTNode *> steered;
232 for (auto jn: jvi["traj"][0]) {
233 steered.push_back(new RRTNode(
240 std::reverse(steered.begin(), steered.end());
241 RRTNode *pn = p.root();
242 for (auto n: steered) {
245 pn->add_child(n, p.cost(pn, n));
249 pn->add_child(p.goal(), p.cost(pn, p.goal()));
258 #elif defined USE_INTERRUPT
259 signal(SIGINT, hint);
260 signal(SIGTERM, hint);
262 while (run_planner) {
269 #elif defined USE_TMAX
272 while (!p.goal_found() && p.elapsed() < TMAX) {
279 #elif defined USE_PTHREAD
281 RRTNode *ron = nullptr;
282 RRTNode *gon = nullptr;
284 pthread_t rt; // root thread
285 pthread_t gt; // goal thread
286 pthread_t ct; // connect thread
299 pthread_create(&rt, NULL, &next_run, (void *) &ra);
300 pthread_create(>, NULL, &next_run, (void *) &ga);
304 while (!gf && p.elapsed() < TMAX &&
305 p.p_root_.nodes().size() < NOFNODES &&
306 p.p_goal_.nodes().size() < NOFNODES) {
309 for (int i = 0; i < IXSIZE; i++) {
310 for (int j = 0; j < IYSIZE; j++) {
311 if (p.p_root_.ixy_[i][j].changed() &&
312 p.p_goal_.ixy_[i][j].changed()) {
314 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
315 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
316 if (rn->ccost() + gn->ccost() < mc &&
322 mc = rn->ccost() + gn->ccost();
330 if (p.elapsed() >= TMAX)
333 // end of overlap trees
337 pthread_join(rt, NULL);
338 pthread_join(gt, NULL);
339 float nodo = ((float) ndl / (float) tol);
340 std::cerr << "nothing done is " << 100.0 * nodo;
341 std::cerr << "%" << std::endl;
342 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
343 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
344 //std::cerr << "cgf is " << p.goal_found() << std::endl;
345 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
346 ron = p.p_root_.goal()->parent();
347 gon = p.p_root_.goal();
348 mc = p.p_root_.goal()->ccost();
350 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
351 ron = p.p_goal_.goal();
352 gon = p.p_goal_.goal()->parent();
353 mc = p.p_goal_.goal()->ccost();
355 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
357 p.goal()->children().clear();
360 while (gon != p.goal()) {
361 p.p_root_.nodes().push_back(new RRTNode(
366 p.p_root_.nodes().back(),
369 p.p_root_.nodes().back()));
370 ron = p.p_root_.nodes().back();
373 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
375 // end of connect trees
383 jvo["rrte"] = ELAPSED;
388 // statistics to error output
389 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
390 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
391 std::cerr << "Goal found is " << p.goal_found() << std::endl;
392 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
393 std::cerr << "#samples is " << p.samples().size() << std::endl;
394 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
395 std::cerr << "trajectories costs:" << std::endl;
396 for (j = 0; j < p.clog().size(); j++)
397 std::cerr << "- " << p.clog()[j] << std::endl;
398 std::cerr << "RRT #nodes:" << std::endl;
399 for (j = 0; j < p.nlog().size(); j++)
400 std::cerr << "- " << p.nlog()[j] << std::endl;
401 std::cerr << "trajectories seconds:" << std::endl;
402 for (j = 0; j < p.slog().size(); j++)
403 std::cerr << "- " << p.slog()[j] << std::endl;
404 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
405 std::cerr << std::endl;
406 for (auto edges: p.rlog())
407 std::cerr << "- " << edges.size() << std::endl;
410 jvo["elap"] = TELAPSED;
412 jvo["nodo"][0] = nodo;
415 for (j = 0; j < p.clog().size(); j++)
416 jvo["cost"][j] = p.clog()[j];
418 for (j = 0; j < p.nlog().size(); j++)
419 jvo["node"][j] = p.nlog()[j];
421 for (j = 0; j < p.slog().size(); j++)
422 jvo["secs"][j] = p.slog()[j];
426 for (auto traj: p.tlog()) {
429 jvo["traj"][j][i][0] = n->x();
430 jvo["traj"][j][i][1] = n->y();
431 jvo["traj"][j][i][2] = n->h();
432 jvo["traj"][j][i][3] = n->t();
433 jvo["traj"][j][i][4] = n->s();
436 if (j == p.tlog().size() - 1) {
439 jvo["path"][i][0] = n->x();
440 jvo["path"][i][1] = n->y();
441 jvo["path"][i][2] = n->h();
450 for (auto edges: p.rlog()) {
452 for (auto e: edges) {
453 jvo["edge"][i][j][0][0] = e->init()->x();
454 jvo["edge"][i][j][0][1] = e->init()->y();
455 jvo["edge"][i][j][0][2] = e->init()->h();
456 jvo["edge"][i][j][1][0] = e->goal()->x();
457 jvo["edge"][i][j][1][1] = e->goal()->y();
458 jvo["edge"][i][j][1][2] = e->goal()->h();
464 #ifdef JSONLOGSAMPLES
467 for (auto s: p.samples()) {
468 jvo["samp"][j][0] = s->x();
469 jvo["samp"][j][1] = s->y();
470 jvo["samp"][j][2] = s->h();
475 std::cout << jvo << std::endl;
478 SDL_DestroyWindow(gw);
493 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
494 std::cerr << "SDL could not initialize! SDL_Error: ";
495 std::cerr << SDL_GetError();
496 std::cerr << std::endl;
499 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
500 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
501 gw = SDL_CreateWindow(
503 SDL_WINDOWPOS_UNDEFINED,
504 SDL_WINDOWPOS_UNDEFINED,
507 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
509 std::cerr << "Window could not be created! SDL_Error: ";
510 std::cerr << SDL_GetError();
511 std::cerr << std::endl;
514 gc = SDL_GL_CreateContext(gw);
516 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
517 std::cerr << SDL_GetError();
518 std::cerr << std::endl;
521 if (SDL_GL_SetSwapInterval(1) < 0) {
522 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
523 std::cerr << SDL_GetError();
524 std::cerr << std::endl;
528 std::cerr << "Unable to initialize OpenGL!";
529 std::cerr << std::endl;
537 GLenum error = GL_NO_ERROR;
538 glMatrixMode(GL_PROJECTION);
540 error = glGetError();
541 if (error != GL_NO_ERROR) {
542 std::cerr << "Error initializing OpenGL! ";
543 std::cerr << gluErrorString(error);
544 std::cerr << std::endl;
547 glMatrixMode(GL_MODELVIEW);
549 error = glGetError();
550 if (error != GL_NO_ERROR) {
551 std::cerr << "Error initializing OpenGL! ";
552 std::cerr << gluErrorString(error);
553 std::cerr << std::endl;
556 glClearColor(1, 1, 1, 1);
557 error = glGetError();
558 if (error != GL_NO_ERROR) {
559 std::cerr << "Error initializing OpenGL! ";
560 std::cerr << gluErrorString(error);
561 std::cerr << std::endl;