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/>.
20 #include <jsoncpp/json/json.h>
26 #include "rrtplanner.h"
27 #include "slotplanner.h"
34 //#define JSONLOGEDGES
35 //#define JSONLOGSAMPLES
38 //#define USE_INTERRUPT
50 bool run_planner = true;
52 SDL_Window* gw = NULL;
69 void *next_run(void *arg)
71 struct next_arg *na = (struct next_arg *) arg;
72 T2 *lp = (T2 *) na->p;
74 while (!*gf && lp->elapsed() < TMAX) {
90 Json::Value jvi; // JSON input
91 Json::Value jvo; // JSON output
95 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
99 jvi["init"][0].asFloat(),
100 jvi["init"][1].asFloat(),
101 jvi["init"][2].asFloat()),
103 jvi["goal"][0].asFloat(),
104 jvi["goal"][1].asFloat(),
105 jvi["goal"][2].asFloat()));
106 std::vector<CircleObstacle> co;
107 std::vector<SegmentObstacle> so;
108 for (auto o: jvi["obst"]) {
109 if (o["circle"] != Json::nullValue) {
110 co.push_back(CircleObstacle(
111 o["circle"][0].asFloat(),
112 o["circle"][1].asFloat(),
113 o["circle"][2].asFloat()));
115 if (o["segment"] != Json::nullValue) {
116 so.push_back(SegmentObstacle(
118 o["segment"][0][0].asFloat(),
119 o["segment"][0][1].asFloat(),
122 o["segment"][1][0].asFloat(),
123 o["segment"][1][1].asFloat(),
125 p.frame().add_bnode(so.back().init());
128 p.link_obstacles(&co, &so);
132 ParallelSlot ps = ParallelSlot();
133 for (auto xy: jvi["slot"]["polygon"]) {
134 ps.slot().add_bnode(new RRTNode(
141 std::vector<RRTNode *> steered;
142 for (auto jn: jvi["traj"][0]) {
143 steered.push_back(new RRTNode(
150 std::reverse(steered.begin(), steered.end());
151 RRTNode *pn = p.root();
152 for (auto n: steered) {
155 pn->add_child(n, p.cost(pn, n));
159 pn->add_child(p.goal(), p.cost(pn, p.goal()));
168 #elif defined USE_INTERRUPT
169 signal(SIGINT, hint);
170 signal(SIGTERM, hint);
172 while (run_planner) {
179 #elif defined USE_TMAX
182 while (!p.goal_found() && p.elapsed() < TMAX) {
188 #elif defined USE_PTHREAD
190 RRTNode *ron = nullptr;
191 RRTNode *gon = nullptr;
193 pthread_t rt; // root thread
194 pthread_t gt; // goal thread
195 pthread_t ct; // connect thread
208 pthread_create(&rt, NULL, &next_run, (void *) &ra);
209 pthread_create(>, NULL, &next_run, (void *) &ga);
213 while (!gf && p.elapsed() < TMAX &&
214 p.p_root_.nodes().size() < NOFNODES &&
215 p.p_goal_.nodes().size() < NOFNODES) {
218 for (int i = 0; i < IXSIZE; i++) {
219 for (int j = 0; j < IYSIZE; j++) {
220 if (p.p_root_.ixy_[i][j].changed() &&
221 p.p_goal_.ixy_[i][j].changed()) {
223 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
224 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
225 if (rn->ccost() + gn->ccost() < mc &&
231 mc = rn->ccost() + gn->ccost();
239 if (p.elapsed() >= TMAX)
242 // end of overlap trees
246 pthread_join(rt, NULL);
247 pthread_join(gt, NULL);
248 float nodo = ((float) ndl / (float) tol);
249 std::cerr << "nothing done is " << 100.0 * nodo;
250 std::cerr << "%" << std::endl;
251 //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
252 //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
253 //std::cerr << "cgf is " << p.goal_found() << std::endl;
254 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
255 ron = p.p_root_.goal()->parent();
256 gon = p.p_root_.goal();
257 mc = p.p_root_.goal()->ccost();
259 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
260 ron = p.p_goal_.goal();
261 gon = p.p_goal_.goal()->parent();
262 mc = p.p_goal_.goal()->ccost();
264 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
266 p.goal()->children().clear();
269 while (gon != p.goal()) {
270 p.p_root_.nodes().push_back(new RRTNode(
275 p.p_root_.nodes().back(),
278 p.p_root_.nodes().back()));
279 ron = p.p_root_.nodes().back();
282 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
284 // end of connect trees
294 // statistics to error output
295 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
296 std::cerr << "Goal found is " << p.goal_found() << std::endl;
297 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
298 std::cerr << "#samples is " << p.samples().size() << std::endl;
299 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
300 std::cerr << "trajectories costs:" << std::endl;
301 for (j = 0; j < p.clog().size(); j++)
302 std::cerr << "- " << p.clog()[j] << std::endl;
303 std::cerr << "RRT #nodes:" << std::endl;
304 for (j = 0; j < p.nlog().size(); j++)
305 std::cerr << "- " << p.nlog()[j] << std::endl;
306 std::cerr << "trajectories seconds:" << std::endl;
307 for (j = 0; j < p.slog().size(); j++)
308 std::cerr << "- " << p.slog()[j] << std::endl;
309 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
310 std::cerr << std::endl;
311 for (auto edges: p.rlog())
312 std::cerr << "- " << edges.size() << std::endl;
315 jvo["elap"] = p.elapsed();
317 jvo["nodo"][0] = nodo;
320 for (j = 0; j < p.clog().size(); j++)
321 jvo["cost"][j] = p.clog()[j];
323 for (j = 0; j < p.nlog().size(); j++)
324 jvo["node"][j] = p.nlog()[j];
326 for (j = 0; j < p.slog().size(); j++)
327 jvo["secs"][j] = p.slog()[j];
331 for (auto traj: p.tlog()) {
334 jvo["traj"][j][i][0] = n->x();
335 jvo["traj"][j][i][1] = n->y();
336 jvo["traj"][j][i][2] = n->h();
337 jvo["traj"][j][i][3] = n->t();
338 jvo["traj"][j][i][4] = n->s();
346 for (auto edges: p.rlog()) {
348 for (auto e: edges) {
349 jvo["edge"][i][j][0][0] = e->init()->x();
350 jvo["edge"][i][j][0][1] = e->init()->y();
351 jvo["edge"][i][j][0][2] = e->init()->h();
352 jvo["edge"][i][j][1][0] = e->goal()->x();
353 jvo["edge"][i][j][1][1] = e->goal()->y();
354 jvo["edge"][i][j][1][2] = e->goal()->h();
360 #ifdef JSONLOGSAMPLES
363 for (auto s: p.samples()) {
364 jvo["samp"][j][0] = s->x();
365 jvo["samp"][j][1] = s->y();
366 jvo["samp"][j][2] = s->h();
371 std::cout << jvo << std::endl;
374 SDL_DestroyWindow(gw);
388 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
389 std::cerr << "SDL could not initialize! SDL_Error: ";
390 std::cerr << SDL_GetError();
391 std::cerr << std::endl;
394 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
395 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
396 gw = SDL_CreateWindow(
398 SDL_WINDOWPOS_UNDEFINED,
399 SDL_WINDOWPOS_UNDEFINED,
402 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
404 std::cerr << "Window could not be created! SDL_Error: ";
405 std::cerr << SDL_GetError();
406 std::cerr << std::endl;
409 gc = SDL_GL_CreateContext(gw);
411 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
412 std::cerr << SDL_GetError();
413 std::cerr << std::endl;
416 if (SDL_GL_SetSwapInterval(1) < 0) {
417 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
418 std::cerr << SDL_GetError();
419 std::cerr << std::endl;
423 std::cerr << "Unable to initialize OpenGL!";
424 std::cerr << std::endl;
432 GLenum error = GL_NO_ERROR;
433 glMatrixMode(GL_PROJECTION);
435 error = glGetError();
436 if (error != GL_NO_ERROR) {
437 std::cerr << "Error initializing OpenGL! ";
438 std::cerr << gluErrorString(error);
439 std::cerr << std::endl;
442 glMatrixMode(GL_MODELVIEW);
444 error = glGetError();
445 if (error != GL_NO_ERROR) {
446 std::cerr << "Error initializing OpenGL! ";
447 std::cerr << gluErrorString(error);
448 std::cerr << std::endl;
451 glClearColor(1, 1, 1, 1);
452 error = glGetError();
453 if (error != GL_NO_ERROR) {
454 std::cerr << "Error initializing OpenGL! ";
455 std::cerr << gluErrorString(error);
456 std::cerr << std::endl;