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);
145 ParallelSlot ps = ParallelSlot();
146 for (auto xy: jvi["slot"]["polygon"]) {
147 ps.slot().add_bnode(new RRTNode(
154 std::vector<RRTNode *> steered;
155 for (auto jn: jvi["traj"][0]) {
156 steered.push_back(new RRTNode(
163 std::reverse(steered.begin(), steered.end());
164 RRTNode *pn = p.root();
165 for (auto n: steered) {
168 pn->add_child(n, p.cost(pn, n));
172 pn->add_child(p.goal(), p.cost(pn, p.goal()));
181 #elif defined USE_INTERRUPT
182 signal(SIGINT, hint);
183 signal(SIGTERM, hint);
185 while (run_planner) {
192 #elif defined USE_TMAX
195 while (!p.goal_found() && p.elapsed() < TMAX) {
201 #elif defined USE_PTHREAD
203 RRTNode *ron = nullptr;
204 RRTNode *gon = nullptr;
206 pthread_t rt; // root thread
207 pthread_t gt; // goal thread
208 pthread_t ct; // connect thread
221 pthread_create(&rt, NULL, &next_run, (void *) &ra);
222 pthread_create(>, NULL, &next_run, (void *) &ga);
226 while (!gf && p.elapsed() < TMAX &&
227 p.p_root_.nodes().size() < NOFNODES &&
228 p.p_goal_.nodes().size() < NOFNODES) {
231 for (int i = 0; i < IXSIZE; i++) {
232 for (int j = 0; j < IYSIZE; j++) {
233 if (p.p_root_.ixy_[i][j].changed() &&
234 p.p_goal_.ixy_[i][j].changed()) {
236 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
237 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
238 if (rn->ccost() + gn->ccost() < mc &&
244 mc = rn->ccost() + gn->ccost();
252 if (p.elapsed() >= TMAX)
255 // end of overlap trees
259 pthread_join(rt, NULL);
260 pthread_join(gt, NULL);
261 float nodo = ((float) ndl / (float) tol);
262 std::cerr << "nothing done is " << 100.0 * nodo;
263 std::cerr << "%" << std::endl;
264 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
265 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
266 //std::cerr << "cgf is " << p.goal_found() << std::endl;
267 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
268 ron = p.p_root_.goal()->parent();
269 gon = p.p_root_.goal();
270 mc = p.p_root_.goal()->ccost();
272 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
273 ron = p.p_goal_.goal();
274 gon = p.p_goal_.goal()->parent();
275 mc = p.p_goal_.goal()->ccost();
277 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
279 p.goal()->children().clear();
282 while (gon != p.goal()) {
283 p.p_root_.nodes().push_back(new RRTNode(
288 p.p_root_.nodes().back(),
291 p.p_root_.nodes().back()));
292 ron = p.p_root_.nodes().back();
295 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
297 // end of connect trees
307 // statistics to error output
308 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
309 std::cerr << "Goal found is " << p.goal_found() << std::endl;
310 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
311 std::cerr << "#samples is " << p.samples().size() << std::endl;
312 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
313 std::cerr << "trajectories costs:" << std::endl;
314 for (j = 0; j < p.clog().size(); j++)
315 std::cerr << "- " << p.clog()[j] << std::endl;
316 std::cerr << "RRT #nodes:" << std::endl;
317 for (j = 0; j < p.nlog().size(); j++)
318 std::cerr << "- " << p.nlog()[j] << std::endl;
319 std::cerr << "trajectories seconds:" << std::endl;
320 for (j = 0; j < p.slog().size(); j++)
321 std::cerr << "- " << p.slog()[j] << std::endl;
322 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
323 std::cerr << std::endl;
324 for (auto edges: p.rlog())
325 std::cerr << "- " << edges.size() << std::endl;
328 jvo["elap"] = p.elapsed();
330 jvo["nodo"][0] = nodo;
333 for (j = 0; j < p.clog().size(); j++)
334 jvo["cost"][j] = p.clog()[j];
336 for (j = 0; j < p.nlog().size(); j++)
337 jvo["node"][j] = p.nlog()[j];
339 for (j = 0; j < p.slog().size(); j++)
340 jvo["secs"][j] = p.slog()[j];
344 for (auto traj: p.tlog()) {
347 jvo["traj"][j][i][0] = n->x();
348 jvo["traj"][j][i][1] = n->y();
349 jvo["traj"][j][i][2] = n->h();
350 jvo["traj"][j][i][3] = n->t();
351 jvo["traj"][j][i][4] = n->s();
359 for (auto edges: p.rlog()) {
361 for (auto e: edges) {
362 jvo["edge"][i][j][0][0] = e->init()->x();
363 jvo["edge"][i][j][0][1] = e->init()->y();
364 jvo["edge"][i][j][0][2] = e->init()->h();
365 jvo["edge"][i][j][1][0] = e->goal()->x();
366 jvo["edge"][i][j][1][1] = e->goal()->y();
367 jvo["edge"][i][j][1][2] = e->goal()->h();
373 #ifdef JSONLOGSAMPLES
376 for (auto s: p.samples()) {
377 jvo["samp"][j][0] = s->x();
378 jvo["samp"][j][1] = s->y();
379 jvo["samp"][j][2] = s->h();
384 std::cout << jvo << std::endl;
387 SDL_DestroyWindow(gw);
401 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
402 std::cerr << "SDL could not initialize! SDL_Error: ";
403 std::cerr << SDL_GetError();
404 std::cerr << std::endl;
407 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
408 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
409 gw = SDL_CreateWindow(
411 SDL_WINDOWPOS_UNDEFINED,
412 SDL_WINDOWPOS_UNDEFINED,
415 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
417 std::cerr << "Window could not be created! SDL_Error: ";
418 std::cerr << SDL_GetError();
419 std::cerr << std::endl;
422 gc = SDL_GL_CreateContext(gw);
424 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
425 std::cerr << SDL_GetError();
426 std::cerr << std::endl;
429 if (SDL_GL_SetSwapInterval(1) < 0) {
430 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
431 std::cerr << SDL_GetError();
432 std::cerr << std::endl;
436 std::cerr << "Unable to initialize OpenGL!";
437 std::cerr << std::endl;
445 GLenum error = GL_NO_ERROR;
446 glMatrixMode(GL_PROJECTION);
448 error = glGetError();
449 if (error != GL_NO_ERROR) {
450 std::cerr << "Error initializing OpenGL! ";
451 std::cerr << gluErrorString(error);
452 std::cerr << std::endl;
455 glMatrixMode(GL_MODELVIEW);
457 error = glGetError();
458 if (error != GL_NO_ERROR) {
459 std::cerr << "Error initializing OpenGL! ";
460 std::cerr << gluErrorString(error);
461 std::cerr << std::endl;
464 glClearColor(1, 1, 1, 1);
465 error = glGetError();
466 if (error != GL_NO_ERROR) {
467 std::cerr << "Error initializing OpenGL! ";
468 std::cerr << gluErrorString(error);
469 std::cerr << std::endl;