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>
24 #include "rrtplanner.h"
33 #define USE_TMAX_1ST_CUSPS
35 bool run_planner = true;
37 SDL_Window* gw = NULL;
54 Json::Value jvi; // JSON input
55 Json::Value jvo; // JSON output
59 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
63 jvi["init"][0].asFloat(),
64 jvi["init"][1].asFloat(),
65 jvi["init"][2].asFloat()),
67 jvi["goal"][0].asFloat(),
68 jvi["goal"][1].asFloat(),
69 jvi["goal"][2].asFloat()));
70 std::vector<CircleObstacle> co;
71 std::vector<SegmentObstacle> so;
72 for (auto o: jvi["obst"]) {
73 if (o["circle"] != Json::nullValue) {
74 co.push_back(CircleObstacle(
75 o["circle"][0].asFloat(),
76 o["circle"][1].asFloat(),
77 o["circle"][2].asFloat()));
79 if (o["segment"] != Json::nullValue) {
80 so.push_back(SegmentObstacle(
82 o["segment"][0][0].asFloat(),
83 o["segment"][0][1].asFloat(),
86 o["segment"][1][0].asFloat(),
87 o["segment"][1][1].asFloat(),
91 p.link_obstacles(&co, &so);
96 std::vector<RRTNode *> tr;
99 std::vector<RRTNode *> steered;
100 for (auto jn: jvi["traj"][0]) {
101 steered.push_back(new RRTNode(
108 std::reverse(steered.begin(), steered.end());
109 RRTNode *pn = p.root();
110 for (auto n: steered) {
111 pn->add_child(n, p.cost(pn, n));
114 pn->add_child(p.goal(), p.cost(pn, p.goal()));
120 signal(SIGINT, hint);
121 signal(SIGTERM, hint);
123 while (run_planner) {
135 #elif defined USE_TMAX // USE TMAX
138 while (!p.goal_found() && p.elapsed() < TMAX) {
147 #else // NOT USE_INTERRUPT
149 while (!p.goal_found() && i < 20) {
152 while (p.elapsed() < TMAX) {
156 if (!p.goal_found()) {
158 nn = p.nn(p.iy_, p.goal(), p.cost);
160 nn = p.nn(p.nodes(), p.goal(), p.cost);
173 if (!p.goal_found()) {
174 nn = p.tlog().back().back();
175 for (j = p.tlog().back().size() - 1; j > 0; j--) {
176 float s1 = p.tlog().back()[j]->s();
177 float s2 = p.tlog().back()[j - 1]->s();
178 if (sgn(s1) != sgn(s2)) {
179 nn = p.tlog().back()[j - 1];
188 std::cerr << "#iterations is " << i << std::endl;
189 #endif // USE_INTERRUPT
192 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
193 std::cerr << "Goal found is " << p.goal_found() << std::endl;
194 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
195 std::cerr << "#samples is " << p.samples().size() << std::endl;
196 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
197 std::cerr << "trajectories costs:" << std::endl;
198 for (j = 0; j < p.clog().size(); j++)
199 std::cerr << "- " << p.clog()[j] << std::endl;
200 std::cerr << "RRT #nodes:" << std::endl;
201 for (j = 0; j < p.nlog().size(); j++)
202 std::cerr << "- " << p.nlog()[j] << std::endl;
203 std::cerr << "trajectories seconds:" << std::endl;
204 for (j = 0; j < p.slog().size(); j++)
205 std::cerr << "- " << p.slog()[j] << std::endl;
206 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
207 std::cerr << std::endl;
208 for (auto edges: p.rlog())
209 std::cerr << "- " << edges.size() << std::endl;
212 jvo["elap"] = p.elapsed();
214 for (j = 0; j < p.clog().size(); j++)
215 jvo["cost"][j] = p.clog()[j];
217 for (j = 0; j < p.nlog().size(); j++)
218 jvo["node"][j] = p.nlog()[j];
220 for (j = 0; j < p.slog().size(); j++)
221 jvo["secs"][j] = p.slog()[j];
225 for (auto traj: p.tlog()) {
228 jvo["traj"][j][i][0] = n->x();
229 jvo["traj"][j][i][1] = n->y();
230 jvo["traj"][j][i][2] = n->h();
231 jvo["traj"][j][i][3] = n->t();
232 jvo["traj"][j][i][4] = n->s();
241 for (auto edges: p.rlog()) {
243 for (auto e: edges) {
244 jvo["edge"][i][j][0][0] = e->init()->x();
245 jvo["edge"][i][j][0][1] = e->init()->y();
246 jvo["edge"][i][j][0][2] = e->init()->h();
247 jvo["edge"][i][j][1][0] = e->goal()->x();
248 jvo["edge"][i][j][1][1] = e->goal()->y();
249 jvo["edge"][i][j][1][2] = e->goal()->h();
255 #if JSONLOGSAMPLES > 0
259 for (auto s: p.samples()) {
260 jvo["samp"][j][0] = s->x();
261 jvo["samp"][j][1] = s->y();
262 jvo["samp"][j][2] = s->h();
267 std::cout << jvo << std::endl;
270 SDL_DestroyWindow(gw);
284 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
285 std::cerr << "SDL could not initialize! SDL_Error: ";
286 std::cerr << SDL_GetError();
287 std::cerr << std::endl;
290 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
291 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
292 gw = SDL_CreateWindow(
294 SDL_WINDOWPOS_UNDEFINED,
295 SDL_WINDOWPOS_UNDEFINED,
298 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
300 std::cerr << "Window could not be created! SDL_Error: ";
301 std::cerr << SDL_GetError();
302 std::cerr << std::endl;
305 gc = SDL_GL_CreateContext(gw);
307 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
308 std::cerr << SDL_GetError();
309 std::cerr << std::endl;
312 if (SDL_GL_SetSwapInterval(1) < 0) {
313 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
314 std::cerr << SDL_GetError();
315 std::cerr << std::endl;
319 std::cerr << "Unable to initialize OpenGL!";
320 std::cerr << std::endl;
328 GLenum error = GL_NO_ERROR;
329 glMatrixMode(GL_PROJECTION);
331 error = glGetError();
332 if (error != GL_NO_ERROR) {
333 std::cerr << "Error initializing OpenGL! ";
334 std::cerr << gluErrorString(error);
335 std::cerr << std::endl;
338 glMatrixMode(GL_MODELVIEW);
340 error = glGetError();
341 if (error != GL_NO_ERROR) {
342 std::cerr << "Error initializing OpenGL! ";
343 std::cerr << gluErrorString(error);
344 std::cerr << std::endl;
347 glClearColor(1, 1, 1, 1);
348 error = glGetError();
349 if (error != GL_NO_ERROR) {
350 std::cerr << "Error initializing OpenGL! ";
351 std::cerr << gluErrorString(error);
352 std::cerr << std::endl;