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"
38 //#define JSONLOGEDGES
39 //#define JSONLOGSAMPLES
45 //#define USE_INTERRUPT
54 std::chrono::high_resolution_clock::time_point TSTART_;
55 std::chrono::high_resolution_clock::time_point TEND_;
58 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
60 std::chrono::duration<float> DT_;
61 TEND_ = std::chrono::high_resolution_clock::now();
62 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
65 TELAPSED += DT_.count();
66 ELAPSED = DT_.count();
68 void TPRINT(const char *what) {
69 std::chrono::duration<float> DT_;
70 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
73 std::cerr << what << ": " << DT_.count() << std::endl;
76 bool run_planner = true;
79 SDL_Window* gw = NULL;
97 void *next_run(void *arg)
99 struct next_arg *na = (struct next_arg *) arg;
100 T2 *lp = (T2 *) na->p;
102 while (!*gf && lp->elapsed() < TMAX) {
118 Json::Value jvi; // JSON input
119 Json::Value jvo; // JSON output
123 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
127 jvi["init"][0].asFloat(),
128 jvi["init"][1].asFloat(),
129 jvi["init"][2].asFloat()),
131 jvi["goal"][0].asFloat(),
132 jvi["goal"][1].asFloat(),
133 jvi["goal"][2].asFloat()));
134 std::vector<CircleObstacle> co;
135 std::vector<SegmentObstacle> so;
136 for (auto o: jvi["obst"]) {
137 if (o["circle"] != Json::nullValue) {
138 co.push_back(CircleObstacle(
139 o["circle"][0].asFloat(),
140 o["circle"][1].asFloat(),
141 o["circle"][2].asFloat()));
143 if (o["segment"] != Json::nullValue) {
144 so.push_back(SegmentObstacle(
146 o["segment"][0][0].asFloat(),
147 o["segment"][0][1].asFloat(),
150 o["segment"][1][0].asFloat(),
151 o["segment"][1][1].asFloat(),
153 p.frame().add_bnode(so.back().init());
156 p.link_obstacles(&co, &so);
160 ParallelSlot ps = ParallelSlot();
162 jvi["slot"] != Json::nullValue &&
163 jvi["slot"]["polygon"] != Json::nullValue
165 for (auto xy: jvi["slot"]["polygon"]) {
166 ps.slot().add_bnode(new RRTNode(
172 if (ps.slot().bnodes().size() > 0) {
174 p.samplingInfo_ = ps.getSamplingInfo();
175 p.useSamplingInfo_ = true;
177 #ifdef USE_SLOTPLANNER
179 if (ps.slot().bnodes().size() > 0)
182 jvo["ppse"] = ELAPSED;
183 TPRINT("ParallelSlot");
185 if (ps.cusp().size() > 0) {
186 p.goal(ps.cusp().front().front());
187 p.slot_cusp(ps.cusp().front()); // use first found solution
188 jvo["midd"][0] = p.goal()->x();
189 jvo["midd"][1] = p.goal()->y();
190 jvo["midd"][2] = p.goal()->h();
191 jvo["goal"][0] = p.slot_cusp().back()->x();
192 jvo["goal"][1] = p.slot_cusp().back()->y();
193 jvo["goal"][2] = p.slot_cusp().back()->h();
195 jvo["goal"][0] = p.goal()->x();
196 jvo["goal"][1] = p.goal()->y();
197 jvo["goal"][2] = p.goal()->h();
201 std::vector<RRTNode *> steered;
202 for (auto jn: jvi["traj"][0]) {
203 steered.push_back(new RRTNode(
210 std::reverse(steered.begin(), steered.end());
211 RRTNode *pn = p.root();
212 for (auto n: steered) {
215 pn->add_child(n, p.cost(pn, n));
219 pn->add_child(p.goal(), p.cost(pn, p.goal()));
228 #elif defined USE_INTERRUPT
229 signal(SIGINT, hint);
230 signal(SIGTERM, hint);
232 while (run_planner) {
239 #elif defined USE_TMAX
242 while (!p.goal_found() && p.elapsed() < TMAX) {
246 if (ps.cusp().size() > 0)
247 p.tlog(p.findt(p.slot_cusp().back()));
252 #elif defined USE_PTHREAD
254 RRTNode *ron = nullptr;
255 RRTNode *gon = nullptr;
257 pthread_t rt; // root thread
258 pthread_t gt; // goal thread
259 pthread_t ct; // connect thread
272 pthread_create(&rt, NULL, &next_run, (void *) &ra);
273 pthread_create(>, NULL, &next_run, (void *) &ga);
277 while (!gf && p.elapsed() < TMAX &&
278 p.p_root_.nodes().size() < NOFNODES &&
279 p.p_goal_.nodes().size() < NOFNODES) {
282 for (int i = 0; i < IXSIZE; i++) {
283 for (int j = 0; j < IYSIZE; j++) {
284 if (p.p_root_.ixy_[i][j].changed() &&
285 p.p_goal_.ixy_[i][j].changed()) {
287 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
288 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
289 if (rn->ccost() + gn->ccost() < mc &&
295 mc = rn->ccost() + gn->ccost();
303 if (p.elapsed() >= TMAX)
306 // end of overlap trees
310 pthread_join(rt, NULL);
311 pthread_join(gt, NULL);
312 float nodo = ((float) ndl / (float) tol);
313 std::cerr << "nothing done is " << 100.0 * nodo;
314 std::cerr << "%" << std::endl;
315 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
316 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
317 //std::cerr << "cgf is " << p.goal_found() << std::endl;
318 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
319 ron = p.p_root_.goal()->parent();
320 gon = p.p_root_.goal();
321 mc = p.p_root_.goal()->ccost();
323 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
324 ron = p.p_goal_.goal();
325 gon = p.p_goal_.goal()->parent();
326 mc = p.p_goal_.goal()->ccost();
328 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
330 p.goal()->children().clear();
333 while (gon != p.goal()) {
334 p.p_root_.nodes().push_back(new RRTNode(
339 p.p_root_.nodes().back(),
342 p.p_root_.nodes().back()));
343 ron = p.p_root_.nodes().back();
346 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
348 // end of connect trees
356 jvo["rrte"] = ELAPSED;
361 // statistics to error output
362 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
363 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
364 std::cerr << "Goal found is " << p.goal_found() << std::endl;
365 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
366 std::cerr << "#samples is " << p.samples().size() << std::endl;
367 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
368 std::cerr << "trajectories costs:" << std::endl;
369 for (j = 0; j < p.clog().size(); j++)
370 std::cerr << "- " << p.clog()[j] << std::endl;
371 std::cerr << "RRT #nodes:" << std::endl;
372 for (j = 0; j < p.nlog().size(); j++)
373 std::cerr << "- " << p.nlog()[j] << std::endl;
374 std::cerr << "trajectories seconds:" << std::endl;
375 for (j = 0; j < p.slog().size(); j++)
376 std::cerr << "- " << p.slog()[j] << std::endl;
377 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
378 std::cerr << std::endl;
379 for (auto edges: p.rlog())
380 std::cerr << "- " << edges.size() << std::endl;
383 jvo["elap"] = TELAPSED;
385 jvo["nodo"][0] = nodo;
388 for (j = 0; j < p.clog().size(); j++)
389 jvo["cost"][j] = p.clog()[j];
391 for (j = 0; j < p.nlog().size(); j++)
392 jvo["node"][j] = p.nlog()[j];
394 for (j = 0; j < p.slog().size(); j++)
395 jvo["secs"][j] = p.slog()[j];
399 for (auto traj: p.tlog()) {
402 jvo["traj"][j][i][0] = n->x();
403 jvo["traj"][j][i][1] = n->y();
404 jvo["traj"][j][i][2] = n->h();
405 jvo["traj"][j][i][3] = n->t();
406 jvo["traj"][j][i][4] = n->s();
414 for (auto edges: p.rlog()) {
416 for (auto e: edges) {
417 jvo["edge"][i][j][0][0] = e->init()->x();
418 jvo["edge"][i][j][0][1] = e->init()->y();
419 jvo["edge"][i][j][0][2] = e->init()->h();
420 jvo["edge"][i][j][1][0] = e->goal()->x();
421 jvo["edge"][i][j][1][1] = e->goal()->y();
422 jvo["edge"][i][j][1][2] = e->goal()->h();
428 #ifdef JSONLOGSAMPLES
431 for (auto s: p.samples()) {
432 jvo["samp"][j][0] = s->x();
433 jvo["samp"][j][1] = s->y();
434 jvo["samp"][j][2] = s->h();
439 std::cout << jvo << std::endl;
442 SDL_DestroyWindow(gw);
457 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
458 std::cerr << "SDL could not initialize! SDL_Error: ";
459 std::cerr << SDL_GetError();
460 std::cerr << std::endl;
463 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
464 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
465 gw = SDL_CreateWindow(
467 SDL_WINDOWPOS_UNDEFINED,
468 SDL_WINDOWPOS_UNDEFINED,
471 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
473 std::cerr << "Window could not be created! SDL_Error: ";
474 std::cerr << SDL_GetError();
475 std::cerr << std::endl;
478 gc = SDL_GL_CreateContext(gw);
480 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
481 std::cerr << SDL_GetError();
482 std::cerr << std::endl;
485 if (SDL_GL_SetSwapInterval(1) < 0) {
486 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
487 std::cerr << SDL_GetError();
488 std::cerr << std::endl;
492 std::cerr << "Unable to initialize OpenGL!";
493 std::cerr << std::endl;
501 GLenum error = GL_NO_ERROR;
502 glMatrixMode(GL_PROJECTION);
504 error = glGetError();
505 if (error != GL_NO_ERROR) {
506 std::cerr << "Error initializing OpenGL! ";
507 std::cerr << gluErrorString(error);
508 std::cerr << std::endl;
511 glMatrixMode(GL_MODELVIEW);
513 error = glGetError();
514 if (error != GL_NO_ERROR) {
515 std::cerr << "Error initializing OpenGL! ";
516 std::cerr << gluErrorString(error);
517 std::cerr << std::endl;
520 glClearColor(1, 1, 1, 1);
521 error = glGetError();
522 if (error != GL_NO_ERROR) {
523 std::cerr << "Error initializing OpenGL! ";
524 std::cerr << gluErrorString(error);
525 std::cerr << std::endl;