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"
33 //#define JSONLOGEDGES
34 //#define JSONLOGSAMPLES
37 //#define USE_INTERRUPT
49 bool run_planner = true;
51 SDL_Window* gw = NULL;
68 void *next_run(void *arg)
70 struct next_arg *na = (struct next_arg *) arg;
71 T2 *lp = (T2 *) na->p;
73 while (!*gf && lp->elapsed() < TMAX) {
89 Json::Value jvi; // JSON input
90 Json::Value jvo; // JSON output
94 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
98 jvi["init"][0].asFloat(),
99 jvi["init"][1].asFloat(),
100 jvi["init"][2].asFloat()),
102 jvi["goal"][0].asFloat(),
103 jvi["goal"][1].asFloat(),
104 jvi["goal"][2].asFloat()));
105 std::vector<CircleObstacle> co;
106 std::vector<SegmentObstacle> so;
107 for (auto o: jvi["obst"]) {
108 if (o["circle"] != Json::nullValue) {
109 co.push_back(CircleObstacle(
110 o["circle"][0].asFloat(),
111 o["circle"][1].asFloat(),
112 o["circle"][2].asFloat()));
114 if (o["segment"] != Json::nullValue) {
115 so.push_back(SegmentObstacle(
117 o["segment"][0][0].asFloat(),
118 o["segment"][0][1].asFloat(),
121 o["segment"][1][0].asFloat(),
122 o["segment"][1][1].asFloat(),
126 p.link_obstacles(&co, &so);
131 std::vector<RRTNode *> steered;
132 for (auto jn: jvi["traj"][0]) {
133 steered.push_back(new RRTNode(
140 std::reverse(steered.begin(), steered.end());
141 RRTNode *pn = p.root();
142 for (auto n: steered) {
145 pn->add_child(n, p.cost(pn, n));
149 pn->add_child(p.goal(), p.cost(pn, p.goal()));
158 #elif defined USE_INTERRUPT
159 signal(SIGINT, hint);
160 signal(SIGTERM, hint);
162 while (run_planner) {
169 #elif defined USE_TMAX
172 while (!p.goal_found() && p.elapsed() < TMAX) {
178 #elif defined USE_PTHREAD
180 RRTNode *ron = nullptr;
181 RRTNode *gon = nullptr;
183 pthread_t rt; // root thread
184 pthread_t gt; // goal thread
185 pthread_t ct; // connect thread
198 pthread_create(&rt, NULL, &next_run, (void *) &ra);
199 pthread_create(>, NULL, &next_run, (void *) &ga);
200 while (!gf && p.elapsed() < TMAX &&
201 p.p_root_.nodes().size() < NOFNODES &&
202 p.p_goal_.nodes().size() < NOFNODES) {
204 for (int i = 0; i < IXSIZE; i++) {
205 for (int j = 0; j < IYSIZE; j++) {
206 if (p.p_root_.ixy_[i][j].changed() &&
207 p.p_goal_.ixy_[i][j].changed()) {
208 for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
209 for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
210 if (rn->ccost() + gn->ccost() < mc &&
216 mc = rn->ccost() + gn->ccost();
221 if (p.elapsed() >= TMAX)
224 // end of overlap trees
228 pthread_join(rt, NULL);
229 pthread_join(gt, NULL);
230 if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
231 ron = p.p_root_.goal()->parent();
232 gon = p.p_root_.goal();
233 mc = p.p_root_.goal()->ccost();
235 if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
236 ron = p.p_goal_.goal();
237 gon = p.p_goal_.goal()->parent();
238 mc = p.p_goal_.goal()->ccost();
240 p.root()->remove_parent(); // needed if p.p_goal_.goal_found()
242 p.goal()->children().clear();
245 while (gon != p.goal()) {
246 p.p_root_.nodes().push_back(new RRTNode(
251 p.p_root_.nodes().back(),
254 p.p_root_.nodes().back()));
255 ron = p.p_root_.nodes().back();
258 ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
260 // end of connect trees
270 // statistics to error output
271 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
272 std::cerr << "Goal found is " << p.goal_found() << std::endl;
273 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
274 std::cerr << "#samples is " << p.samples().size() << std::endl;
275 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
276 std::cerr << "trajectories costs:" << std::endl;
277 for (j = 0; j < p.clog().size(); j++)
278 std::cerr << "- " << p.clog()[j] << std::endl;
279 std::cerr << "RRT #nodes:" << std::endl;
280 for (j = 0; j < p.nlog().size(); j++)
281 std::cerr << "- " << p.nlog()[j] << std::endl;
282 std::cerr << "trajectories seconds:" << std::endl;
283 for (j = 0; j < p.slog().size(); j++)
284 std::cerr << "- " << p.slog()[j] << std::endl;
285 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
286 std::cerr << std::endl;
287 for (auto edges: p.rlog())
288 std::cerr << "- " << edges.size() << std::endl;
291 jvo["elap"] = p.elapsed();
293 for (j = 0; j < p.clog().size(); j++)
294 jvo["cost"][j] = p.clog()[j];
296 for (j = 0; j < p.nlog().size(); j++)
297 jvo["node"][j] = p.nlog()[j];
299 for (j = 0; j < p.slog().size(); j++)
300 jvo["secs"][j] = p.slog()[j];
304 for (auto traj: p.tlog()) {
307 jvo["traj"][j][i][0] = n->x();
308 jvo["traj"][j][i][1] = n->y();
309 jvo["traj"][j][i][2] = n->h();
310 jvo["traj"][j][i][3] = n->t();
311 jvo["traj"][j][i][4] = n->s();
319 for (auto edges: p.rlog()) {
321 for (auto e: edges) {
322 jvo["edge"][i][j][0][0] = e->init()->x();
323 jvo["edge"][i][j][0][1] = e->init()->y();
324 jvo["edge"][i][j][0][2] = e->init()->h();
325 jvo["edge"][i][j][1][0] = e->goal()->x();
326 jvo["edge"][i][j][1][1] = e->goal()->y();
327 jvo["edge"][i][j][1][2] = e->goal()->h();
333 #ifdef JSONLOGSAMPLES
336 for (auto s: p.samples()) {
337 jvo["samp"][j][0] = s->x();
338 jvo["samp"][j][1] = s->y();
339 jvo["samp"][j][2] = s->h();
344 std::cout << jvo << std::endl;
347 SDL_DestroyWindow(gw);
361 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
362 std::cerr << "SDL could not initialize! SDL_Error: ";
363 std::cerr << SDL_GetError();
364 std::cerr << std::endl;
367 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
368 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
369 gw = SDL_CreateWindow(
371 SDL_WINDOWPOS_UNDEFINED,
372 SDL_WINDOWPOS_UNDEFINED,
375 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
377 std::cerr << "Window could not be created! SDL_Error: ";
378 std::cerr << SDL_GetError();
379 std::cerr << std::endl;
382 gc = SDL_GL_CreateContext(gw);
384 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
385 std::cerr << SDL_GetError();
386 std::cerr << std::endl;
389 if (SDL_GL_SetSwapInterval(1) < 0) {
390 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
391 std::cerr << SDL_GetError();
392 std::cerr << std::endl;
396 std::cerr << "Unable to initialize OpenGL!";
397 std::cerr << std::endl;
405 GLenum error = GL_NO_ERROR;
406 glMatrixMode(GL_PROJECTION);
408 error = glGetError();
409 if (error != GL_NO_ERROR) {
410 std::cerr << "Error initializing OpenGL! ";
411 std::cerr << gluErrorString(error);
412 std::cerr << std::endl;
415 glMatrixMode(GL_MODELVIEW);
417 error = glGetError();
418 if (error != GL_NO_ERROR) {
419 std::cerr << "Error initializing OpenGL! ";
420 std::cerr << gluErrorString(error);
421 std::cerr << std::endl;
424 glClearColor(1, 1, 1, 1);
425 error = glGetError();
426 if (error != GL_NO_ERROR) {
427 std::cerr << "Error initializing OpenGL! ";
428 std::cerr << gluErrorString(error);
429 std::cerr << std::endl;