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) {
78 lp->tlog(lp->findt());
91 Json::Value jvi; // JSON input
92 Json::Value jvo; // JSON output
96 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
100 jvi["init"][0].asFloat(),
101 jvi["init"][1].asFloat(),
102 jvi["init"][2].asFloat()),
104 jvi["goal"][0].asFloat(),
105 jvi["goal"][1].asFloat(),
106 jvi["goal"][2].asFloat()));
107 std::vector<CircleObstacle> co;
108 std::vector<SegmentObstacle> so;
109 for (auto o: jvi["obst"]) {
110 if (o["circle"] != Json::nullValue) {
111 co.push_back(CircleObstacle(
112 o["circle"][0].asFloat(),
113 o["circle"][1].asFloat(),
114 o["circle"][2].asFloat()));
116 if (o["segment"] != Json::nullValue) {
117 so.push_back(SegmentObstacle(
119 o["segment"][0][0].asFloat(),
120 o["segment"][0][1].asFloat(),
123 o["segment"][1][0].asFloat(),
124 o["segment"][1][1].asFloat(),
128 p.link_obstacles(&co, &so);
133 std::vector<RRTNode *> steered;
134 for (auto jn: jvi["traj"][0]) {
135 steered.push_back(new RRTNode(
142 std::reverse(steered.begin(), steered.end());
143 RRTNode *pn = p.root();
144 for (auto n: steered) {
147 pn->add_child(n, p.cost(pn, n));
151 pn->add_child(p.goal(), p.cost(pn, p.goal()));
160 #elif defined USE_INTERRUPT
161 signal(SIGINT, hint);
162 signal(SIGTERM, hint);
164 while (run_planner) {
171 #elif defined USE_TMAX
174 while (!p.goal_found() && p.elapsed() < TMAX) {
180 #elif defined USE_PTHREAD
182 pthread_t rt; // root thread
183 pthread_t gt; // goal thread
184 pthread_t ct; // connect thread
197 pthread_create(&rt, NULL, &next_run, (void *) &ra);
198 pthread_create(>, NULL, &next_run, (void *) &ga);
199 while (!gf && p.elapsed() < TMAX) {
203 pthread_join(rt, NULL);
204 pthread_join(gt, NULL);
214 // statistics to error output
215 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
216 std::cerr << "Goal found is " << p.goal_found() << std::endl;
217 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
218 std::cerr << "#samples is " << p.samples().size() << std::endl;
219 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
220 std::cerr << "trajectories costs:" << std::endl;
221 for (j = 0; j < p.clog().size(); j++)
222 std::cerr << "- " << p.clog()[j] << std::endl;
223 std::cerr << "RRT #nodes:" << std::endl;
224 for (j = 0; j < p.nlog().size(); j++)
225 std::cerr << "- " << p.nlog()[j] << std::endl;
226 std::cerr << "trajectories seconds:" << std::endl;
227 for (j = 0; j < p.slog().size(); j++)
228 std::cerr << "- " << p.slog()[j] << std::endl;
229 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
230 std::cerr << std::endl;
231 for (auto edges: p.rlog())
232 std::cerr << "- " << edges.size() << std::endl;
235 jvo["elap"] = p.elapsed();
237 for (j = 0; j < p.clog().size(); j++)
238 jvo["cost"][j] = p.clog()[j];
240 for (j = 0; j < p.nlog().size(); j++)
241 jvo["node"][j] = p.nlog()[j];
243 for (j = 0; j < p.slog().size(); j++)
244 jvo["secs"][j] = p.slog()[j];
248 for (auto traj: p.tlog()) {
251 jvo["traj"][j][i][0] = n->x();
252 jvo["traj"][j][i][1] = n->y();
253 jvo["traj"][j][i][2] = n->h();
254 jvo["traj"][j][i][3] = n->t();
255 jvo["traj"][j][i][4] = n->s();
263 for (auto edges: p.rlog()) {
265 for (auto e: edges) {
266 jvo["edge"][i][j][0][0] = e->init()->x();
267 jvo["edge"][i][j][0][1] = e->init()->y();
268 jvo["edge"][i][j][0][2] = e->init()->h();
269 jvo["edge"][i][j][1][0] = e->goal()->x();
270 jvo["edge"][i][j][1][1] = e->goal()->y();
271 jvo["edge"][i][j][1][2] = e->goal()->h();
277 #ifdef JSONLOGSAMPLES
280 for (auto s: p.samples()) {
281 jvo["samp"][j][0] = s->x();
282 jvo["samp"][j][1] = s->y();
283 jvo["samp"][j][2] = s->h();
288 std::cout << jvo << std::endl;
291 SDL_DestroyWindow(gw);
305 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
306 std::cerr << "SDL could not initialize! SDL_Error: ";
307 std::cerr << SDL_GetError();
308 std::cerr << std::endl;
311 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
312 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
313 gw = SDL_CreateWindow(
315 SDL_WINDOWPOS_UNDEFINED,
316 SDL_WINDOWPOS_UNDEFINED,
319 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
321 std::cerr << "Window could not be created! SDL_Error: ";
322 std::cerr << SDL_GetError();
323 std::cerr << std::endl;
326 gc = SDL_GL_CreateContext(gw);
328 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
329 std::cerr << SDL_GetError();
330 std::cerr << std::endl;
333 if (SDL_GL_SetSwapInterval(1) < 0) {
334 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
335 std::cerr << SDL_GetError();
336 std::cerr << std::endl;
340 std::cerr << "Unable to initialize OpenGL!";
341 std::cerr << std::endl;
349 GLenum error = GL_NO_ERROR;
350 glMatrixMode(GL_PROJECTION);
352 error = glGetError();
353 if (error != GL_NO_ERROR) {
354 std::cerr << "Error initializing OpenGL! ";
355 std::cerr << gluErrorString(error);
356 std::cerr << std::endl;
359 glMatrixMode(GL_MODELVIEW);
361 error = glGetError();
362 if (error != GL_NO_ERROR) {
363 std::cerr << "Error initializing OpenGL! ";
364 std::cerr << gluErrorString(error);
365 std::cerr << std::endl;
368 glClearColor(1, 1, 1, 1);
369 error = glGetError();
370 if (error != GL_NO_ERROR) {
371 std::cerr << "Error initializing OpenGL! ";
372 std::cerr << gluErrorString(error);
373 std::cerr << std::endl;