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
56 std::chrono::high_resolution_clock::time_point TSTART_;
57 std::chrono::high_resolution_clock::time_point TEND_;
60 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
62 std::chrono::duration<float> DT_;
63 TEND_ = std::chrono::high_resolution_clock::now();
64 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
67 TELAPSED += DT_.count();
68 ELAPSED = DT_.count();
70 void TPRINT(const char *what) {
71 std::chrono::duration<float> DT_;
72 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
75 std::cerr << what << ": " << DT_.count() << std::endl;
78 bool run_planner = true;
81 SDL_Window* gw = NULL;
99 void *next_run(void *arg)
101 struct next_arg *na = (struct next_arg *) arg;
102 T2 *lp = (T2 *) na->p;
104 while (!*gf && lp->elapsed() < TMAX) {
116 float new_x = 1 + static_cast<float>(rand()) /
117 static_cast<float>(RAND_MAX / (6.6 - 1 - 1));
119 float new_h = M_PI / 2;
120 return new RRTNode(new_x, new_y, new_h);
125 srand(static_cast<unsigned>(time(0)));
130 Json::Value jvi; // JSON input
131 Json::Value jvo; // JSON output
135 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
139 jvi["init"][0].asFloat(),
140 jvi["init"][1].asFloat(),
141 jvi["init"][2].asFloat()),
143 jvi["goal"][0].asFloat(),
144 jvi["goal"][1].asFloat(),
145 jvi["goal"][2].asFloat()));
146 jvo["init"][0] = p.root()->x();
147 jvo["init"][1] = p.root()->y();
148 jvo["init"][2] = p.root()->h();
149 std::vector<CircleObstacle> co;
150 std::vector<SegmentObstacle> so;
151 for (auto o: jvi["obst"]) {
152 if (o["circle"] != Json::nullValue) {
153 co.push_back(CircleObstacle(
154 o["circle"][0].asFloat(),
155 o["circle"][1].asFloat(),
156 o["circle"][2].asFloat()));
158 if (o["segment"] != Json::nullValue) {
159 so.push_back(SegmentObstacle(
161 o["segment"][0][0].asFloat(),
162 o["segment"][0][1].asFloat(),
165 o["segment"][1][0].asFloat(),
166 o["segment"][1][1].asFloat(),
168 p.frame().add_bnode(so.back().init());
171 p.link_obstacles(&co, &so);
175 ParallelSlot ps = ParallelSlot();
177 jvi["slot"] != Json::nullValue &&
178 jvi["slot"]["polygon"] != Json::nullValue
180 for (auto xy: jvi["slot"]["polygon"]) {
181 ps.slot().add_bnode(new RRTNode(
186 for (auto e: ps.slot().frame())
187 so.push_back(SegmentObstacle(e->init(), e->goal()));
189 #ifdef USE_SLOTPLANNER
191 if (ps.slot().bnodes().size() > 0)
194 jvo["ppse"] = ELAPSED;
195 TPRINT("ParallelSlot");
197 if (ps.slot().bnodes().size() > 0) {
199 //p.setSamplingInfo(ps.getSamplingInfo());
201 if (ps.cusp().size() > 0) {
202 p.goal(ps.getMidd());
203 p.slot_cusp(ps.cusp().front()); // use first found solution
205 jvo["midd"][0] = p.goal()->x();
206 jvo["midd"][1] = p.goal()->y();
207 jvo["midd"][2] = p.goal()->h();
208 jvo["goal"][0] = p.slot_cusp().back()->x();
209 jvo["goal"][1] = p.slot_cusp().back()->y();
210 jvo["goal"][2] = p.slot_cusp().back()->h();
212 jvo["goal"][0] = p.goal()->x();
213 jvo["goal"][1] = p.goal()->y();
214 jvo["goal"][2] = p.goal()->h();
217 std::cerr << "Slot Info:" << std::endl;
218 if (ps.slotSide() == LEFT)
219 std::cerr << "- LEFT" << std::endl;
221 std::cerr << "- RIGHT" << std::endl;
222 if (ps.slotType() == PARALLEL)
223 std::cerr << "- PARALLEL" << std::endl;
225 std::cerr << "- PERPENDICULAR" << std::endl;
227 std::vector<RRTNode *> steered;
228 for (auto jn: jvi["traj"][0]) {
229 steered.push_back(new RRTNode(
236 std::reverse(steered.begin(), steered.end());
237 RRTNode *pn = p.root();
238 for (auto n: steered) {
241 pn->add_child(n, p.cost(pn, n));
245 pn->add_child(p.goal(), p.cost(pn, p.goal()));
254 #elif defined USE_INTERRUPT
255 signal(SIGINT, hint);
256 signal(SIGTERM, hint);
258 while (run_planner) {
265 #elif defined USE_TMAX
268 while (!p.goal_found() && p.elapsed() < TMAX) {
275 #elif defined USE_PTHREAD
277 RRTNode *ron = nullptr;
278 RRTNode *gon = nullptr;
280 pthread_t rt; // root thread
281 pthread_t gt; // goal thread
282 pthread_t ct; // connect thread
295 pthread_create(&rt, NULL, &next_run, (void *) &ra);
296 pthread_create(>, NULL, &next_run, (void *) &ga);
300 while (!gf && p.elapsed() < TMAX &&
301 p.p_root_.nodes().size() < NOFNODES &&
302 p.p_goal_.nodes().size() < NOFNODES) {
305 for (int i = 0; i < IXSIZE; i++) {
306 for (int j = 0; j < IYSIZE; j++) {
307 if (p.p_root_.ixy_[i][j].changed() &&
308 p.p_goal_.ixy_[i][j].changed()) {
310 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
311 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
312 if (rn->ccost() + gn->ccost() < mc &&
318 mc = rn->ccost() + gn->ccost();
326 if (p.elapsed() >= TMAX)
329 // end of overlap trees
333 pthread_join(rt, NULL);
334 pthread_join(gt, NULL);
335 float nodo = ((float) ndl / (float) tol);
336 std::cerr << "nothing done is " << 100.0 * nodo;
337 std::cerr << "%" << std::endl;
338 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
339 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
340 //std::cerr << "cgf is " << p.goal_found() << std::endl;
341 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
342 ron = p.p_root_.goal()->parent();
343 gon = p.p_root_.goal();
344 mc = p.p_root_.goal()->ccost();
346 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
347 ron = p.p_goal_.goal();
348 gon = p.p_goal_.goal()->parent();
349 mc = p.p_goal_.goal()->ccost();
351 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
353 p.goal()->children().clear();
356 while (gon != p.goal()) {
357 p.p_root_.nodes().push_back(new RRTNode(
362 p.p_root_.nodes().back(),
365 p.p_root_.nodes().back()));
366 ron = p.p_root_.nodes().back();
369 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
371 // end of connect trees
379 jvo["rrte"] = ELAPSED;
384 // statistics to error output
385 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
386 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
387 std::cerr << "Goal found is " << p.goal_found() << std::endl;
388 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
389 std::cerr << "#samples is " << p.samples().size() << std::endl;
390 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
391 std::cerr << "trajectories costs:" << std::endl;
392 for (j = 0; j < p.clog().size(); j++)
393 std::cerr << "- " << p.clog()[j] << std::endl;
394 std::cerr << "RRT #nodes:" << std::endl;
395 for (j = 0; j < p.nlog().size(); j++)
396 std::cerr << "- " << p.nlog()[j] << std::endl;
397 std::cerr << "trajectories seconds:" << std::endl;
398 for (j = 0; j < p.slog().size(); j++)
399 std::cerr << "- " << p.slog()[j] << std::endl;
400 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
401 std::cerr << std::endl;
402 for (auto edges: p.rlog())
403 std::cerr << "- " << edges.size() << std::endl;
406 jvo["elap"] = TELAPSED;
408 jvo["nodo"][0] = nodo;
411 for (j = 0; j < p.clog().size(); j++)
412 jvo["cost"][j] = p.clog()[j];
414 for (j = 0; j < p.nlog().size(); j++)
415 jvo["node"][j] = p.nlog()[j];
417 for (j = 0; j < p.slog().size(); j++)
418 jvo["secs"][j] = p.slog()[j];
422 for (auto traj: p.tlog()) {
425 jvo["traj"][j][i][0] = n->x();
426 jvo["traj"][j][i][1] = n->y();
427 jvo["traj"][j][i][2] = n->h();
428 jvo["traj"][j][i][3] = n->t();
429 jvo["traj"][j][i][4] = n->s();
437 for (auto edges: p.rlog()) {
439 for (auto e: edges) {
440 jvo["edge"][i][j][0][0] = e->init()->x();
441 jvo["edge"][i][j][0][1] = e->init()->y();
442 jvo["edge"][i][j][0][2] = e->init()->h();
443 jvo["edge"][i][j][1][0] = e->goal()->x();
444 jvo["edge"][i][j][1][1] = e->goal()->y();
445 jvo["edge"][i][j][1][2] = e->goal()->h();
451 #ifdef JSONLOGSAMPLES
454 for (auto s: p.samples()) {
455 jvo["samp"][j][0] = s->x();
456 jvo["samp"][j][1] = s->y();
457 jvo["samp"][j][2] = s->h();
462 std::cout << jvo << std::endl;
465 SDL_DestroyWindow(gw);
480 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
481 std::cerr << "SDL could not initialize! SDL_Error: ";
482 std::cerr << SDL_GetError();
483 std::cerr << std::endl;
486 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
487 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
488 gw = SDL_CreateWindow(
490 SDL_WINDOWPOS_UNDEFINED,
491 SDL_WINDOWPOS_UNDEFINED,
494 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
496 std::cerr << "Window could not be created! SDL_Error: ";
497 std::cerr << SDL_GetError();
498 std::cerr << std::endl;
501 gc = SDL_GL_CreateContext(gw);
503 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
504 std::cerr << SDL_GetError();
505 std::cerr << std::endl;
508 if (SDL_GL_SetSwapInterval(1) < 0) {
509 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
510 std::cerr << SDL_GetError();
511 std::cerr << std::endl;
515 std::cerr << "Unable to initialize OpenGL!";
516 std::cerr << std::endl;
524 GLenum error = GL_NO_ERROR;
525 glMatrixMode(GL_PROJECTION);
527 error = glGetError();
528 if (error != GL_NO_ERROR) {
529 std::cerr << "Error initializing OpenGL! ";
530 std::cerr << gluErrorString(error);
531 std::cerr << std::endl;
534 glMatrixMode(GL_MODELVIEW);
536 error = glGetError();
537 if (error != GL_NO_ERROR) {
538 std::cerr << "Error initializing OpenGL! ";
539 std::cerr << gluErrorString(error);
540 std::cerr << std::endl;
543 glClearColor(1, 1, 1, 1);
544 error = glGetError();
545 if (error != GL_NO_ERROR) {
546 std::cerr << "Error initializing OpenGL! ";
547 std::cerr << gluErrorString(error);
548 std::cerr << std::endl;