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 for (auto o: jvi["obst"]) {
156 if (o["circle"] != Json::nullValue) {
157 co.push_back(CircleObstacle(
158 o["circle"][0].asFloat(),
159 o["circle"][1].asFloat(),
160 o["circle"][2].asFloat()));
162 if (o["segment"] != Json::nullValue) {
163 so.push_back(SegmentObstacle(
165 o["segment"][0][0].asFloat(),
166 o["segment"][0][1].asFloat(),
169 o["segment"][1][0].asFloat(),
170 o["segment"][1][1].asFloat(),
172 p.frame().add_bnode(so.back().init());
175 p.link_obstacles(&co, &so);
179 ParallelSlot ps = ParallelSlot();
181 jvi["slot"] != Json::nullValue &&
182 jvi["slot"]["polygon"] != Json::nullValue
184 for (auto xy: jvi["slot"]["polygon"]) {
185 ps.slot().add_bnode(new RRTNode(
190 for (auto e: ps.slot().frame())
191 so.push_back(SegmentObstacle(e->init(), e->goal()));
193 #ifdef USE_SLOTPLANNER
195 if (ps.slot().bnodes().size() > 0)
198 jvo["ppse"] = ELAPSED;
199 TPRINT("ParallelSlot");
201 if (ps.slot().bnodes().size() > 0) {
203 //if (ps.getMidd() != nullptr)
204 // p.setSamplingInfo(ps.getSamplingInfo());
206 if (ps.cusp().size() > 0) {
207 p.goal(ps.getMidd());
208 p.slot_cusp(ps.cusp().front()); // use first found solution
210 jvo["midd"][0] = p.goal()->x();
211 jvo["midd"][1] = p.goal()->y();
212 jvo["midd"][2] = p.goal()->h();
213 jvo["goal"][0] = p.slot_cusp().back()->x();
214 jvo["goal"][1] = p.slot_cusp().back()->y();
215 jvo["goal"][2] = p.slot_cusp().back()->h();
217 jvo["goal"][0] = p.goal()->x();
218 jvo["goal"][1] = p.goal()->y();
219 jvo["goal"][2] = p.goal()->h();
222 std::cerr << "Slot Info:" << std::endl;
223 if (ps.slotSide() == LEFT)
224 std::cerr << "- LEFT" << std::endl;
226 std::cerr << "- RIGHT" << std::endl;
227 if (ps.slotType() == PARALLEL)
228 std::cerr << "- PARALLEL" << std::endl;
230 std::cerr << "- PERPENDICULAR" << std::endl;
232 std::vector<RRTNode *> steered;
233 for (auto jn: jvi["traj"][0]) {
234 steered.push_back(new RRTNode(
241 std::reverse(steered.begin(), steered.end());
242 RRTNode *pn = p.root();
243 for (auto n: steered) {
246 pn->add_child(n, p.cost(pn, n));
250 pn->add_child(p.goal(), p.cost(pn, p.goal()));
259 #elif defined USE_INTERRUPT
260 signal(SIGINT, hint);
261 signal(SIGTERM, hint);
263 while (run_planner) {
270 #elif defined USE_TMAX
273 while (!p.goal_found() && p.elapsed() < TMAX) {
280 if (p.goal_found() && ps.slotType() == PARALLEL)
281 p.tlog(p.findt(p.slot_cusp().back()));
282 #elif defined USE_PTHREAD
284 RRTNode *ron = nullptr;
285 RRTNode *gon = nullptr;
287 pthread_t rt; // root thread
288 pthread_t gt; // goal thread
289 pthread_t ct; // connect thread
302 pthread_create(&rt, NULL, &next_run, (void *) &ra);
303 pthread_create(>, NULL, &next_run, (void *) &ga);
307 while (!gf && p.elapsed() < TMAX &&
308 p.p_root_.nodes().size() < NOFNODES &&
309 p.p_goal_.nodes().size() < NOFNODES) {
312 for (int i = 0; i < IXSIZE; i++) {
313 for (int j = 0; j < IYSIZE; j++) {
314 if (p.p_root_.ixy_[i][j].changed() &&
315 p.p_goal_.ixy_[i][j].changed()) {
317 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
318 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
319 if (rn->ccost() + gn->ccost() < mc &&
325 mc = rn->ccost() + gn->ccost();
333 if (p.elapsed() >= TMAX)
336 // end of overlap trees
340 pthread_join(rt, NULL);
341 pthread_join(gt, NULL);
342 float nodo = ((float) ndl / (float) tol);
343 std::cerr << "nothing done is " << 100.0 * nodo;
344 std::cerr << "%" << std::endl;
345 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
346 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
347 //std::cerr << "cgf is " << p.goal_found() << std::endl;
348 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
349 ron = p.p_root_.goal()->parent();
350 gon = p.p_root_.goal();
351 mc = p.p_root_.goal()->ccost();
353 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
354 ron = p.p_goal_.goal();
355 gon = p.p_goal_.goal()->parent();
356 mc = p.p_goal_.goal()->ccost();
358 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
360 p.goal()->children().clear();
363 while (gon != p.goal()) {
364 p.p_root_.nodes().push_back(new RRTNode(
369 p.p_root_.nodes().back(),
372 p.p_root_.nodes().back()));
373 ron = p.p_root_.nodes().back();
376 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
378 // end of connect trees
386 jvo["rrte"] = ELAPSED;
391 // statistics to error output
392 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
393 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
394 std::cerr << "Goal found is " << p.goal_found() << std::endl;
395 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
396 std::cerr << "#samples is " << p.samples().size() << std::endl;
397 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
398 std::cerr << "trajectories costs:" << std::endl;
399 for (j = 0; j < p.clog().size(); j++)
400 std::cerr << "- " << p.clog()[j] << std::endl;
401 std::cerr << "RRT #nodes:" << std::endl;
402 for (j = 0; j < p.nlog().size(); j++)
403 std::cerr << "- " << p.nlog()[j] << std::endl;
404 std::cerr << "trajectories seconds:" << std::endl;
405 for (j = 0; j < p.slog().size(); j++)
406 std::cerr << "- " << p.slog()[j] << std::endl;
407 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
408 std::cerr << std::endl;
409 for (auto edges: p.rlog())
410 std::cerr << "- " << edges.size() << std::endl;
413 jvo["elap"] = TELAPSED;
415 jvo["nodo"][0] = nodo;
418 for (j = 0; j < p.clog().size(); j++)
419 jvo["cost"][j] = p.clog()[j];
421 for (j = 0; j < p.nlog().size(); j++)
422 jvo["node"][j] = p.nlog()[j];
424 for (j = 0; j < p.slog().size(); j++)
425 jvo["secs"][j] = p.slog()[j];
429 for (auto traj: p.tlog()) {
432 jvo["traj"][j][i][0] = n->x();
433 jvo["traj"][j][i][1] = n->y();
434 jvo["traj"][j][i][2] = n->h();
435 jvo["traj"][j][i][3] = n->t();
436 jvo["traj"][j][i][4] = n->s();
444 for (auto edges: p.rlog()) {
446 for (auto e: edges) {
447 jvo["edge"][i][j][0][0] = e->init()->x();
448 jvo["edge"][i][j][0][1] = e->init()->y();
449 jvo["edge"][i][j][0][2] = e->init()->h();
450 jvo["edge"][i][j][1][0] = e->goal()->x();
451 jvo["edge"][i][j][1][1] = e->goal()->y();
452 jvo["edge"][i][j][1][2] = e->goal()->h();
458 #ifdef JSONLOGSAMPLES
461 for (auto s: p.samples()) {
462 jvo["samp"][j][0] = s->x();
463 jvo["samp"][j][1] = s->y();
464 jvo["samp"][j][2] = s->h();
469 std::cout << jvo << std::endl;
472 SDL_DestroyWindow(gw);
487 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
488 std::cerr << "SDL could not initialize! SDL_Error: ";
489 std::cerr << SDL_GetError();
490 std::cerr << std::endl;
493 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
494 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
495 gw = SDL_CreateWindow(
497 SDL_WINDOWPOS_UNDEFINED,
498 SDL_WINDOWPOS_UNDEFINED,
501 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
503 std::cerr << "Window could not be created! SDL_Error: ";
504 std::cerr << SDL_GetError();
505 std::cerr << std::endl;
508 gc = SDL_GL_CreateContext(gw);
510 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
511 std::cerr << SDL_GetError();
512 std::cerr << std::endl;
515 if (SDL_GL_SetSwapInterval(1) < 0) {
516 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
517 std::cerr << SDL_GetError();
518 std::cerr << std::endl;
522 std::cerr << "Unable to initialize OpenGL!";
523 std::cerr << std::endl;
531 GLenum error = GL_NO_ERROR;
532 glMatrixMode(GL_PROJECTION);
534 error = glGetError();
535 if (error != GL_NO_ERROR) {
536 std::cerr << "Error initializing OpenGL! ";
537 std::cerr << gluErrorString(error);
538 std::cerr << std::endl;
541 glMatrixMode(GL_MODELVIEW);
543 error = glGetError();
544 if (error != GL_NO_ERROR) {
545 std::cerr << "Error initializing OpenGL! ";
546 std::cerr << gluErrorString(error);
547 std::cerr << std::endl;
550 glClearColor(1, 1, 1, 1);
551 error = glGetError();
552 if (error != GL_NO_ERROR) {
553 std::cerr << "Error initializing OpenGL! ";
554 std::cerr << gluErrorString(error);
555 std::cerr << std::endl;