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_;
53 void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
54 void TEND() {TEND_ = std::chrono::high_resolution_clock::now();}
55 void TPRINT(const char *what) {
56 std::chrono::duration<float> DT_;
57 DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
60 std::cerr << what << ": " << DT_.count() << std::endl;
63 bool run_planner = true;
65 SDL_Window* gw = NULL;
82 void *next_run(void *arg)
84 struct next_arg *na = (struct next_arg *) arg;
85 T2 *lp = (T2 *) na->p;
87 while (!*gf && lp->elapsed() < TMAX) {
103 Json::Value jvi; // JSON input
104 Json::Value jvo; // JSON output
108 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
112 jvi["init"][0].asFloat(),
113 jvi["init"][1].asFloat(),
114 jvi["init"][2].asFloat()),
116 jvi["goal"][0].asFloat(),
117 jvi["goal"][1].asFloat(),
118 jvi["goal"][2].asFloat()));
119 std::vector<CircleObstacle> co;
120 std::vector<SegmentObstacle> so;
121 for (auto o: jvi["obst"]) {
122 if (o["circle"] != Json::nullValue) {
123 co.push_back(CircleObstacle(
124 o["circle"][0].asFloat(),
125 o["circle"][1].asFloat(),
126 o["circle"][2].asFloat()));
128 if (o["segment"] != Json::nullValue) {
129 so.push_back(SegmentObstacle(
131 o["segment"][0][0].asFloat(),
132 o["segment"][0][1].asFloat(),
135 o["segment"][1][0].asFloat(),
136 o["segment"][1][1].asFloat(),
138 p.frame().add_bnode(so.back().init());
141 p.link_obstacles(&co, &so);
146 ParallelSlot ps = ParallelSlot();
147 for (auto xy: jvi["slot"]["polygon"]) {
148 ps.slot().add_bnode(new RRTNode(
153 if (ps.slot().bnodes().size() > 0)
156 TPRINT("ParallelSlot");
158 if (ps.cusp().size() > 0) {
159 p.goal(ps.cusp().front().front());
160 p.slot_cusp(ps.cusp().front()); // use first found solution
161 jvo["midd"][0] = p.goal()->x();
162 jvo["midd"][1] = p.goal()->y();
163 jvo["midd"][2] = p.goal()->h();
164 jvo["goal"][0] = p.slot_cusp().back()->x();
165 jvo["goal"][1] = p.slot_cusp().back()->y();
166 jvo["goal"][2] = p.slot_cusp().back()->h();
168 jvo["goal"][0] = p.goal()->x();
169 jvo["goal"][1] = p.goal()->y();
170 jvo["goal"][2] = p.goal()->h();
174 std::vector<RRTNode *> steered;
175 for (auto jn: jvi["traj"][0]) {
176 steered.push_back(new RRTNode(
183 std::reverse(steered.begin(), steered.end());
184 RRTNode *pn = p.root();
185 for (auto n: steered) {
188 pn->add_child(n, p.cost(pn, n));
192 pn->add_child(p.goal(), p.cost(pn, p.goal()));
201 #elif defined USE_INTERRUPT
202 signal(SIGINT, hint);
203 signal(SIGTERM, hint);
205 while (run_planner) {
212 #elif defined USE_TMAX
215 while (!p.goal_found() && p.elapsed() < TMAX) {
219 if (ps.cusp().size() > 0)
220 p.tlog(p.findt(p.slot_cusp().back()));
225 #elif defined USE_PTHREAD
227 RRTNode *ron = nullptr;
228 RRTNode *gon = nullptr;
230 pthread_t rt; // root thread
231 pthread_t gt; // goal thread
232 pthread_t ct; // connect thread
245 pthread_create(&rt, NULL, &next_run, (void *) &ra);
246 pthread_create(>, NULL, &next_run, (void *) &ga);
250 while (!gf && p.elapsed() < TMAX &&
251 p.p_root_.nodes().size() < NOFNODES &&
252 p.p_goal_.nodes().size() < NOFNODES) {
255 for (int i = 0; i < IXSIZE; i++) {
256 for (int j = 0; j < IYSIZE; j++) {
257 if (p.p_root_.ixy_[i][j].changed() &&
258 p.p_goal_.ixy_[i][j].changed()) {
260 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
261 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
262 if (rn->ccost() + gn->ccost() < mc &&
268 mc = rn->ccost() + gn->ccost();
276 if (p.elapsed() >= TMAX)
279 // end of overlap trees
283 pthread_join(rt, NULL);
284 pthread_join(gt, NULL);
285 float nodo = ((float) ndl / (float) tol);
286 std::cerr << "nothing done is " << 100.0 * nodo;
287 std::cerr << "%" << std::endl;
288 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
289 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
290 //std::cerr << "cgf is " << p.goal_found() << std::endl;
291 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
292 ron = p.p_root_.goal()->parent();
293 gon = p.p_root_.goal();
294 mc = p.p_root_.goal()->ccost();
296 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
297 ron = p.p_goal_.goal();
298 gon = p.p_goal_.goal()->parent();
299 mc = p.p_goal_.goal()->ccost();
301 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
303 p.goal()->children().clear();
306 while (gon != p.goal()) {
307 p.p_root_.nodes().push_back(new RRTNode(
312 p.p_root_.nodes().back(),
315 p.p_root_.nodes().back()));
316 ron = p.p_root_.nodes().back();
319 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
321 // end of connect trees
333 // statistics to error output
334 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
335 std::cerr << "Goal found is " << p.goal_found() << std::endl;
336 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
337 std::cerr << "#samples is " << p.samples().size() << std::endl;
338 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
339 std::cerr << "trajectories costs:" << std::endl;
340 for (j = 0; j < p.clog().size(); j++)
341 std::cerr << "- " << p.clog()[j] << std::endl;
342 std::cerr << "RRT #nodes:" << std::endl;
343 for (j = 0; j < p.nlog().size(); j++)
344 std::cerr << "- " << p.nlog()[j] << std::endl;
345 std::cerr << "trajectories seconds:" << std::endl;
346 for (j = 0; j < p.slog().size(); j++)
347 std::cerr << "- " << p.slog()[j] << std::endl;
348 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
349 std::cerr << std::endl;
350 for (auto edges: p.rlog())
351 std::cerr << "- " << edges.size() << std::endl;
354 jvo["elap"] = p.elapsed();
356 jvo["nodo"][0] = nodo;
359 for (j = 0; j < p.clog().size(); j++)
360 jvo["cost"][j] = p.clog()[j];
362 for (j = 0; j < p.nlog().size(); j++)
363 jvo["node"][j] = p.nlog()[j];
365 for (j = 0; j < p.slog().size(); j++)
366 jvo["secs"][j] = p.slog()[j];
370 for (auto traj: p.tlog()) {
373 jvo["traj"][j][i][0] = n->x();
374 jvo["traj"][j][i][1] = n->y();
375 jvo["traj"][j][i][2] = n->h();
376 jvo["traj"][j][i][3] = n->t();
377 jvo["traj"][j][i][4] = n->s();
385 for (auto edges: p.rlog()) {
387 for (auto e: edges) {
388 jvo["edge"][i][j][0][0] = e->init()->x();
389 jvo["edge"][i][j][0][1] = e->init()->y();
390 jvo["edge"][i][j][0][2] = e->init()->h();
391 jvo["edge"][i][j][1][0] = e->goal()->x();
392 jvo["edge"][i][j][1][1] = e->goal()->y();
393 jvo["edge"][i][j][1][2] = e->goal()->h();
399 #ifdef JSONLOGSAMPLES
402 for (auto s: p.samples()) {
403 jvo["samp"][j][0] = s->x();
404 jvo["samp"][j][1] = s->y();
405 jvo["samp"][j][2] = s->h();
410 std::cout << jvo << std::endl;
413 SDL_DestroyWindow(gw);
427 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
428 std::cerr << "SDL could not initialize! SDL_Error: ";
429 std::cerr << SDL_GetError();
430 std::cerr << std::endl;
433 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
434 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
435 gw = SDL_CreateWindow(
437 SDL_WINDOWPOS_UNDEFINED,
438 SDL_WINDOWPOS_UNDEFINED,
441 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
443 std::cerr << "Window could not be created! SDL_Error: ";
444 std::cerr << SDL_GetError();
445 std::cerr << std::endl;
448 gc = SDL_GL_CreateContext(gw);
450 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
451 std::cerr << SDL_GetError();
452 std::cerr << std::endl;
455 if (SDL_GL_SetSwapInterval(1) < 0) {
456 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
457 std::cerr << SDL_GetError();
458 std::cerr << std::endl;
462 std::cerr << "Unable to initialize OpenGL!";
463 std::cerr << std::endl;
471 GLenum error = GL_NO_ERROR;
472 glMatrixMode(GL_PROJECTION);
474 error = glGetError();
475 if (error != GL_NO_ERROR) {
476 std::cerr << "Error initializing OpenGL! ";
477 std::cerr << gluErrorString(error);
478 std::cerr << std::endl;
481 glMatrixMode(GL_MODELVIEW);
483 error = glGetError();
484 if (error != GL_NO_ERROR) {
485 std::cerr << "Error initializing OpenGL! ";
486 std::cerr << gluErrorString(error);
487 std::cerr << std::endl;
490 glClearColor(1, 1, 1, 1);
491 error = glGetError();
492 if (error != GL_NO_ERROR) {
493 std::cerr << "Error initializing OpenGL! ";
494 std::cerr << gluErrorString(error);
495 std::cerr << std::endl;