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"
35 //#define JSONLOGEDGES
36 //#define JSONLOGSAMPLES
39 //#define USE_INTERRUPT
51 std::chrono::high_resolution_clock::time_point TSTART_;
52 std::chrono::high_resolution_clock::time_point TEND_;
55 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
57 std::chrono::duration<float> DT_;
58 TEND_ = std::chrono::high_resolution_clock::now();
59 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
62 TELAPSED += DT_.count();
63 ELAPSED = DT_.count();
65 void TPRINT(const char *what) {
66 std::chrono::duration<float> DT_;
67 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
70 std::cerr << what << ": " << DT_.count() << std::endl;
73 bool run_planner = true;
75 SDL_Window* gw = NULL;
92 void *next_run(void *arg)
94 struct next_arg *na = (struct next_arg *) arg;
95 T2 *lp = (T2 *) na->p;
97 while (!*gf && lp->elapsed() < TMAX) {
113 Json::Value jvi; // JSON input
114 Json::Value jvo; // JSON output
118 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
122 jvi["init"][0].asFloat(),
123 jvi["init"][1].asFloat(),
124 jvi["init"][2].asFloat()),
126 jvi["goal"][0].asFloat(),
127 jvi["goal"][1].asFloat(),
128 jvi["goal"][2].asFloat()));
129 std::vector<CircleObstacle> co;
130 std::vector<SegmentObstacle> so;
131 for (auto o: jvi["obst"]) {
132 if (o["circle"] != Json::nullValue) {
133 co.push_back(CircleObstacle(
134 o["circle"][0].asFloat(),
135 o["circle"][1].asFloat(),
136 o["circle"][2].asFloat()));
138 if (o["segment"] != Json::nullValue) {
139 so.push_back(SegmentObstacle(
141 o["segment"][0][0].asFloat(),
142 o["segment"][0][1].asFloat(),
145 o["segment"][1][0].asFloat(),
146 o["segment"][1][1].asFloat(),
148 p.frame().add_bnode(so.back().init());
151 p.link_obstacles(&co, &so);
156 ParallelSlot ps = ParallelSlot();
157 for (auto xy: jvi["slot"]["polygon"]) {
158 ps.slot().add_bnode(new RRTNode(
163 if (ps.slot().bnodes().size() > 0)
166 jvo["ppse"] = ELAPSED;
167 TPRINT("ParallelSlot");
169 if (ps.cusp().size() > 0) {
170 p.goal(ps.cusp().front().front());
171 p.slot_cusp(ps.cusp().front()); // use first found solution
172 jvo["midd"][0] = p.goal()->x();
173 jvo["midd"][1] = p.goal()->y();
174 jvo["midd"][2] = p.goal()->h();
175 jvo["goal"][0] = p.slot_cusp().back()->x();
176 jvo["goal"][1] = p.slot_cusp().back()->y();
177 jvo["goal"][2] = p.slot_cusp().back()->h();
179 jvo["goal"][0] = p.goal()->x();
180 jvo["goal"][1] = p.goal()->y();
181 jvo["goal"][2] = p.goal()->h();
185 std::vector<RRTNode *> steered;
186 for (auto jn: jvi["traj"][0]) {
187 steered.push_back(new RRTNode(
194 std::reverse(steered.begin(), steered.end());
195 RRTNode *pn = p.root();
196 for (auto n: steered) {
199 pn->add_child(n, p.cost(pn, n));
203 pn->add_child(p.goal(), p.cost(pn, p.goal()));
212 #elif defined USE_INTERRUPT
213 signal(SIGINT, hint);
214 signal(SIGTERM, hint);
216 while (run_planner) {
223 #elif defined USE_TMAX
226 while (!p.goal_found() && p.elapsed() < TMAX) {
230 if (ps.cusp().size() > 0)
231 p.tlog(p.findt(p.slot_cusp().back()));
236 #elif defined USE_PTHREAD
238 RRTNode *ron = nullptr;
239 RRTNode *gon = nullptr;
241 pthread_t rt; // root thread
242 pthread_t gt; // goal thread
243 pthread_t ct; // connect thread
256 pthread_create(&rt, NULL, &next_run, (void *) &ra);
257 pthread_create(>, NULL, &next_run, (void *) &ga);
261 while (!gf && p.elapsed() < TMAX &&
262 p.p_root_.nodes().size() < NOFNODES &&
263 p.p_goal_.nodes().size() < NOFNODES) {
266 for (int i = 0; i < IXSIZE; i++) {
267 for (int j = 0; j < IYSIZE; j++) {
268 if (p.p_root_.ixy_[i][j].changed() &&
269 p.p_goal_.ixy_[i][j].changed()) {
271 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
272 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
273 if (rn->ccost() + gn->ccost() < mc &&
279 mc = rn->ccost() + gn->ccost();
287 if (p.elapsed() >= TMAX)
290 // end of overlap trees
294 pthread_join(rt, NULL);
295 pthread_join(gt, NULL);
296 float nodo = ((float) ndl / (float) tol);
297 std::cerr << "nothing done is " << 100.0 * nodo;
298 std::cerr << "%" << std::endl;
299 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
300 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
301 //std::cerr << "cgf is " << p.goal_found() << std::endl;
302 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
303 ron = p.p_root_.goal()->parent();
304 gon = p.p_root_.goal();
305 mc = p.p_root_.goal()->ccost();
307 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
308 ron = p.p_goal_.goal();
309 gon = p.p_goal_.goal()->parent();
310 mc = p.p_goal_.goal()->ccost();
312 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
314 p.goal()->children().clear();
317 while (gon != p.goal()) {
318 p.p_root_.nodes().push_back(new RRTNode(
323 p.p_root_.nodes().back(),
326 p.p_root_.nodes().back()));
327 ron = p.p_root_.nodes().back();
330 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
332 // end of connect trees
340 jvo["rrte"] = ELAPSED;
345 // statistics to error output
346 std::cerr << "TELAPSED is " << TELAPSED << std::endl;
347 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
348 std::cerr << "Goal found is " << p.goal_found() << std::endl;
349 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
350 std::cerr << "#samples is " << p.samples().size() << std::endl;
351 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
352 std::cerr << "trajectories costs:" << std::endl;
353 for (j = 0; j < p.clog().size(); j++)
354 std::cerr << "- " << p.clog()[j] << std::endl;
355 std::cerr << "RRT #nodes:" << std::endl;
356 for (j = 0; j < p.nlog().size(); j++)
357 std::cerr << "- " << p.nlog()[j] << std::endl;
358 std::cerr << "trajectories seconds:" << std::endl;
359 for (j = 0; j < p.slog().size(); j++)
360 std::cerr << "- " << p.slog()[j] << std::endl;
361 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
362 std::cerr << std::endl;
363 for (auto edges: p.rlog())
364 std::cerr << "- " << edges.size() << std::endl;
367 jvo["elap"] = TELAPSED;
369 jvo["nodo"][0] = nodo;
372 for (j = 0; j < p.clog().size(); j++)
373 jvo["cost"][j] = p.clog()[j];
375 for (j = 0; j < p.nlog().size(); j++)
376 jvo["node"][j] = p.nlog()[j];
378 for (j = 0; j < p.slog().size(); j++)
379 jvo["secs"][j] = p.slog()[j];
383 for (auto traj: p.tlog()) {
386 jvo["traj"][j][i][0] = n->x();
387 jvo["traj"][j][i][1] = n->y();
388 jvo["traj"][j][i][2] = n->h();
389 jvo["traj"][j][i][3] = n->t();
390 jvo["traj"][j][i][4] = n->s();
398 for (auto edges: p.rlog()) {
400 for (auto e: edges) {
401 jvo["edge"][i][j][0][0] = e->init()->x();
402 jvo["edge"][i][j][0][1] = e->init()->y();
403 jvo["edge"][i][j][0][2] = e->init()->h();
404 jvo["edge"][i][j][1][0] = e->goal()->x();
405 jvo["edge"][i][j][1][1] = e->goal()->y();
406 jvo["edge"][i][j][1][2] = e->goal()->h();
412 #ifdef JSONLOGSAMPLES
415 for (auto s: p.samples()) {
416 jvo["samp"][j][0] = s->x();
417 jvo["samp"][j][1] = s->y();
418 jvo["samp"][j][2] = s->h();
423 std::cout << jvo << std::endl;
426 SDL_DestroyWindow(gw);
440 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
441 std::cerr << "SDL could not initialize! SDL_Error: ";
442 std::cerr << SDL_GetError();
443 std::cerr << std::endl;
446 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
447 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
448 gw = SDL_CreateWindow(
450 SDL_WINDOWPOS_UNDEFINED,
451 SDL_WINDOWPOS_UNDEFINED,
454 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
456 std::cerr << "Window could not be created! SDL_Error: ";
457 std::cerr << SDL_GetError();
458 std::cerr << std::endl;
461 gc = SDL_GL_CreateContext(gw);
463 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
464 std::cerr << SDL_GetError();
465 std::cerr << std::endl;
468 if (SDL_GL_SetSwapInterval(1) < 0) {
469 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
470 std::cerr << SDL_GetError();
471 std::cerr << std::endl;
475 std::cerr << "Unable to initialize OpenGL!";
476 std::cerr << std::endl;
484 GLenum error = GL_NO_ERROR;
485 glMatrixMode(GL_PROJECTION);
487 error = glGetError();
488 if (error != GL_NO_ERROR) {
489 std::cerr << "Error initializing OpenGL! ";
490 std::cerr << gluErrorString(error);
491 std::cerr << std::endl;
494 glMatrixMode(GL_MODELVIEW);
496 error = glGetError();
497 if (error != GL_NO_ERROR) {
498 std::cerr << "Error initializing OpenGL! ";
499 std::cerr << gluErrorString(error);
500 std::cerr << std::endl;
503 glClearColor(1, 1, 1, 1);
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;