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();
143 jvi["init"][0].asFloat(),
144 jvi["init"][1].asFloat(),
145 jvi["init"][2].asFloat()),
147 jvi["goal"][0].asFloat(),
148 jvi["goal"][1].asFloat(),
149 jvi["goal"][2].asFloat()));
150 jvo["init"][0] = p.root()->x();
151 jvo["init"][1] = p.root()->y();
152 jvo["init"][2] = p.root()->h();
153 std::vector<CircleObstacle> co;
154 std::vector<SegmentObstacle> so;
155 p.HMIN = p.root()->x();
156 p.HMAX = p.root()->x();
157 p.VMIN = p.root()->y();
158 p.VMAX = p.root()->y();
159 for (auto o: jvi["obst"]) {
164 if (o["circle"] != Json::nullValue) {
165 tmpx = o["circle"][0].asFloat();
166 tmpy = o["circle"][1].asFloat();
167 tmpr = o["circle"][2].asFloat();
168 co.push_back(CircleObstacle(tmpx, tmpy, tmpr));
169 if (tmpx < p.HMIN) p.HMIN = tmpx;
170 if (tmpx > p.HMAX) p.HMAX = tmpx;
171 if (tmpy < p.VMIN) p.VMIN = tmpy;
172 if (tmpy > p.VMAX) p.VMAX = tmpy;
174 if (o["segment"] != Json::nullValue) {
175 tmpx = o["segment"][0][0].asFloat();
176 tmpy = o["segment"][0][1].asFloat();
177 tmpr = o["segment"][1][0].asFloat();
178 tmps = o["segment"][1][1].asFloat();
179 so.push_back(SegmentObstacle(
180 new RRTNode(tmpx, tmpy, 0),
181 new RRTNode(tmpr, tmps, 0)
183 p.frame().add_bnode(so.back().init());
184 if (tmpx < p.HMIN) p.HMIN = tmpx;
185 if (tmpx > p.HMAX) p.HMAX = tmpx;
186 if (tmpy < p.VMIN) p.VMIN = tmpy;
187 if (tmpy > p.VMAX) p.VMAX = tmpy;
188 if (tmpr < p.HMIN) p.HMIN = tmpr;
189 if (tmpr > p.HMAX) p.HMAX = tmpr;
190 if (tmps < p.VMIN) p.VMIN = tmps;
191 if (tmps > p.VMAX) p.VMAX = tmps;
194 p.link_obstacles(&co, &so);
198 ParallelSlot ps = ParallelSlot();
200 jvi["slot"] != Json::nullValue &&
201 jvi["slot"]["polygon"] != Json::nullValue
203 for (auto xy: jvi["slot"]["polygon"]) {
204 ps.slot().add_bnode(new RRTNode(
209 for (auto e: ps.slot().frame())
210 so.push_back(SegmentObstacle(e->init(), e->goal()));
212 #ifdef USE_SLOTPLANNER
214 if (ps.slot().bnodes().size() > 0)
217 jvo["ppse"] = ELAPSED;
218 TPRINT("ParallelSlot");
220 if (ps.slot().bnodes().size() > 0) {
222 //if (ps.getMidd() != nullptr)
223 // p.setSamplingInfo(ps.getSamplingInfo());
225 if (ps.cusp().size() > 0) {
226 p.goal(ps.getMidd());
227 p.slot_cusp(ps.cusp().front()); // use first found solution
229 jvo["midd"][0] = p.goal()->x();
230 jvo["midd"][1] = p.goal()->y();
231 jvo["midd"][2] = p.goal()->h();
232 jvo["goal"][0] = p.slot_cusp().back()->x();
233 jvo["goal"][1] = p.slot_cusp().back()->y();
234 jvo["goal"][2] = p.slot_cusp().back()->h();
236 jvo["goal"][0] = p.goal()->x();
237 jvo["goal"][1] = p.goal()->y();
238 jvo["goal"][2] = p.goal()->h();
241 std::cerr << "Slot Info:" << std::endl;
242 if (ps.slotSide() == LEFT)
243 std::cerr << "- LEFT" << std::endl;
245 std::cerr << "- RIGHT" << std::endl;
246 if (ps.slotType() == PARALLEL)
247 std::cerr << "- PARALLEL" << std::endl;
249 std::cerr << "- PERPENDICULAR" << std::endl;
251 std::vector<RRTNode *> steered;
252 for (auto jn: jvi["traj"][0]) {
253 steered.push_back(new RRTNode(
260 std::reverse(steered.begin(), steered.end());
261 RRTNode *pn = p.root();
262 for (auto n: steered) {
265 pn->add_child(n, p.cost(pn, n));
269 pn->add_child(p.goal(), p.cost(pn, p.goal()));
278 #elif defined USE_INTERRUPT
279 signal(SIGINT, hint);
280 signal(SIGTERM, hint);
282 while (run_planner) {
289 #elif defined USE_TMAX
292 while (!p.goal_found() && p.elapsed() < TMAX) {
299 if (p.goal_found() && ps.slotType() == PARALLEL)
300 p.tlog(p.findt(p.slot_cusp().back()));
301 #elif defined USE_PTHREAD
303 RRTNode *ron = nullptr;
304 RRTNode *gon = nullptr;
306 pthread_t rt; // root thread
307 pthread_t gt; // goal thread
308 pthread_t ct; // connect thread
321 pthread_create(&rt, NULL, &next_run, (void *) &ra);
322 pthread_create(>, NULL, &next_run, (void *) &ga);
326 while (!gf && p.elapsed() < TMAX &&
327 p.p_root_.nodes().size() < NOFNODES &&
328 p.p_goal_.nodes().size() < NOFNODES) {
331 for (int i = 0; i < IXSIZE; i++) {
332 for (int j = 0; j < IYSIZE; j++) {
333 if (p.p_root_.ixy_[i][j].changed() &&
334 p.p_goal_.ixy_[i][j].changed()) {
336 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
337 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
338 if (rn->ccost() + gn->ccost() < mc &&
344 mc = rn->ccost() + gn->ccost();
352 if (p.elapsed() >= TMAX)
355 // end of overlap trees
359 pthread_join(rt, NULL);
360 pthread_join(gt, NULL);
361 float nodo = ((float) ndl / (float) tol);
362 std::cerr << "nothing done is " << 100.0 * nodo;
363 std::cerr << "%" << std::endl;
364 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
365 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
366 //std::cerr << "cgf is " << p.goal_found() << std::endl;
367 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
368 ron = p.p_root_.goal()->parent();
369 gon = p.p_root_.goal();
370 mc = p.p_root_.goal()->ccost();
372 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
373 ron = p.p_goal_.goal();
374 gon = p.p_goal_.goal()->parent();
375 mc = p.p_goal_.goal()->ccost();
377 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
379 p.goal()->children().clear();
382 while (gon != p.goal()) {
383 p.p_root_.nodes().push_back(new RRTNode(
388 p.p_root_.nodes().back(),
391 p.p_root_.nodes().back()));
392 ron = p.p_root_.nodes().back();
395 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
397 // end of connect trees
405 jvo["rrte"] = ELAPSED;
410 // statistics to error output
411 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
412 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
413 std::cerr << "Goal found is " << p.goal_found() << std::endl;
414 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
415 std::cerr << "#samples is " << p.samples().size() << std::endl;
416 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
417 std::cerr << "trajectories costs:" << std::endl;
418 for (j = 0; j < p.clog().size(); j++)
419 std::cerr << "- " << p.clog()[j] << std::endl;
420 std::cerr << "RRT #nodes:" << std::endl;
421 for (j = 0; j < p.nlog().size(); j++)
422 std::cerr << "- " << p.nlog()[j] << std::endl;
423 std::cerr << "trajectories seconds:" << std::endl;
424 for (j = 0; j < p.slog().size(); j++)
425 std::cerr << "- " << p.slog()[j] << std::endl;
426 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
427 std::cerr << std::endl;
428 for (auto edges: p.rlog())
429 std::cerr << "- " << edges.size() << std::endl;
432 jvo["elap"] = TELAPSED;
434 jvo["nodo"][0] = nodo;
437 for (j = 0; j < p.clog().size(); j++)
438 jvo["cost"][j] = p.clog()[j];
440 for (j = 0; j < p.nlog().size(); j++)
441 jvo["node"][j] = p.nlog()[j];
443 for (j = 0; j < p.slog().size(); j++)
444 jvo["secs"][j] = p.slog()[j];
448 for (auto traj: p.tlog()) {
451 jvo["traj"][j][i][0] = n->x();
452 jvo["traj"][j][i][1] = n->y();
453 jvo["traj"][j][i][2] = n->h();
454 jvo["traj"][j][i][3] = n->t();
455 jvo["traj"][j][i][4] = n->s();
463 for (auto edges: p.rlog()) {
465 for (auto e: edges) {
466 jvo["edge"][i][j][0][0] = e->init()->x();
467 jvo["edge"][i][j][0][1] = e->init()->y();
468 jvo["edge"][i][j][0][2] = e->init()->h();
469 jvo["edge"][i][j][1][0] = e->goal()->x();
470 jvo["edge"][i][j][1][1] = e->goal()->y();
471 jvo["edge"][i][j][1][2] = e->goal()->h();
477 #ifdef JSONLOGSAMPLES
480 for (auto s: p.samples()) {
481 jvo["samp"][j][0] = s->x();
482 jvo["samp"][j][1] = s->y();
483 jvo["samp"][j][2] = s->h();
488 std::cout << jvo << std::endl;
491 SDL_DestroyWindow(gw);
506 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
507 std::cerr << "SDL could not initialize! SDL_Error: ";
508 std::cerr << SDL_GetError();
509 std::cerr << std::endl;
512 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
513 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
514 gw = SDL_CreateWindow(
516 SDL_WINDOWPOS_UNDEFINED,
517 SDL_WINDOWPOS_UNDEFINED,
520 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
522 std::cerr << "Window could not be created! SDL_Error: ";
523 std::cerr << SDL_GetError();
524 std::cerr << std::endl;
527 gc = SDL_GL_CreateContext(gw);
529 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
530 std::cerr << SDL_GetError();
531 std::cerr << std::endl;
534 if (SDL_GL_SetSwapInterval(1) < 0) {
535 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
536 std::cerr << SDL_GetError();
537 std::cerr << std::endl;
541 std::cerr << "Unable to initialize OpenGL!";
542 std::cerr << std::endl;
550 GLenum error = GL_NO_ERROR;
551 glMatrixMode(GL_PROJECTION);
553 error = glGetError();
554 if (error != GL_NO_ERROR) {
555 std::cerr << "Error initializing OpenGL! ";
556 std::cerr << gluErrorString(error);
557 std::cerr << std::endl;
560 glMatrixMode(GL_MODELVIEW);
562 error = glGetError();
563 if (error != GL_NO_ERROR) {
564 std::cerr << "Error initializing OpenGL! ";
565 std::cerr << gluErrorString(error);
566 std::cerr << std::endl;
569 glClearColor(1, 1, 1, 1);
570 error = glGetError();
571 if (error != GL_NO_ERROR) {
572 std::cerr << "Error initializing OpenGL! ";
573 std::cerr << gluErrorString(error);
574 std::cerr << std::endl;