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/>.
19 #include <jsoncpp/json/json.h>
23 #include "rrtplanner.h"
33 bool run_planner = true;
35 SDL_Window* gw = NULL;
52 Json::Value jvi; // JSON input
53 Json::Value jvo; // JSON output
57 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
61 jvi["init"][0].asFloat(),
62 jvi["init"][1].asFloat(),
63 jvi["init"][2].asFloat()),
65 jvi["goal"][0].asFloat(),
66 jvi["goal"][1].asFloat(),
67 jvi["goal"][2].asFloat()));
68 std::vector<CircleObstacle> co;
69 std::vector<SegmentObstacle> so;
70 for (auto o: jvi["obst"]) {
71 if (o["circle"] != Json::nullValue) {
72 co.push_back(CircleObstacle(
73 o["circle"][0].asFloat(),
74 o["circle"][1].asFloat(),
75 o["circle"][2].asFloat()));
77 if (o["segment"] != Json::nullValue) {
78 so.push_back(SegmentObstacle(
80 o["segment"][0][0].asFloat(),
81 o["segment"][0][1].asFloat(),
84 o["segment"][1][0].asFloat(),
85 o["segment"][1][1].asFloat(),
89 p.link_obstacles(&co, &so);
94 std::vector<RRTNode *> tr;
98 signal(SIGTERM, hint);
100 while (run_planner) {
109 if (!p.goal_found()) {
111 nn = p.nn(p.iy_, p.goal(), p.cost);
113 nn = p.nn(p.nodes(), p.goal(), p.cost);
123 #elif defined USE_TMAX // USE TMAX
126 while (p.elapsed() < TMAX) {
132 if (!p.goal_found()) {
134 nn = p.nn(p.iy_, p.goal(), p.cost);
136 nn = p.nn(p.nodes(), p.goal(), p.cost);
149 #else // NOT USE_INTERRUPT
151 while (!p.goal_found() && i < 20) {
154 while (p.elapsed() < TMAX) {
158 if (!p.goal_found()) {
160 nn = p.nn(p.iy_, p.goal(), p.cost);
162 nn = p.nn(p.nodes(), p.goal(), p.cost);
175 if (!p.goal_found()) {
176 nn = p.tlog().back().back();
177 for (j = p.tlog().back().size() - 1; j > 0; j--) {
178 float s1 = p.tlog().back()[j]->s();
179 float s2 = p.tlog().back()[j - 1]->s();
180 if (sgn(s1) != sgn(s2)) {
181 nn = p.tlog().back()[j - 1];
190 std::cerr << "#iterations is " << i << std::endl;
191 #endif // USE_INTERRUPT
193 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
194 std::cerr << "Goal found is " << p.goal_found() << std::endl;
195 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
196 std::cerr << "#samples is " << p.samples().size() << std::endl;
197 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
198 std::cerr << "trajectories costs:" << std::endl;
199 for (j = 0; j < p.clog().size(); j++)
200 std::cerr << "- " << p.clog()[j] << std::endl;
201 std::cerr << "RRT #nodes:" << std::endl;
202 for (j = 0; j < p.nlog().size(); j++)
203 std::cerr << "- " << p.nlog()[j] << std::endl;
204 std::cerr << "trajectories seconds:" << std::endl;
205 for (j = 0; j < p.slog().size(); j++)
206 std::cerr << "- " << p.slog()[j] << std::endl;
207 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
208 std::cerr << std::endl;
209 for (auto edges: p.rlog())
210 std::cerr << "- " << edges.size() << std::endl;
213 jvo["elap"] = p.elapsed();
215 for (j = 0; j < p.clog().size(); j++)
216 jvo["cost"][j] = p.clog()[j];
218 for (j = 0; j < p.nlog().size(); j++)
219 jvo["node"][j] = p.nlog()[j];
221 for (j = 0; j < p.slog().size(); j++)
222 jvo["secs"][j] = p.slog()[j];
226 for (auto traj: p.tlog()) {
229 jvo["traj"][j][i][0] = n->x();
230 jvo["traj"][j][i][1] = n->y();
231 jvo["traj"][j][i][2] = n->h();
232 jvo["traj"][j][i][3] = n->t();
233 jvo["traj"][j][i][4] = n->s();
242 for (auto edges: p.rlog()) {
244 for (auto e: edges) {
245 jvo["edge"][i][j][0][0] = e->init()->x();
246 jvo["edge"][i][j][0][1] = e->init()->y();
247 jvo["edge"][i][j][1][0] = e->goal()->x();
248 jvo["edge"][i][j][1][1] = e->goal()->y();
254 #if JSONLOGSAMPLES > 0
258 for (auto s: p.samples()) {
259 jvo["samp"][j][0] = s->x();
260 jvo["samp"][j][1] = s->y();
261 jvo["samp"][j][2] = s->h();
266 std::cout << jvo << std::endl;
269 SDL_DestroyWindow(gw);
283 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
284 std::cerr << "SDL could not initialize! SDL_Error: ";
285 std::cerr << SDL_GetError();
286 std::cerr << std::endl;
289 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
290 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
291 gw = SDL_CreateWindow(
293 SDL_WINDOWPOS_UNDEFINED,
294 SDL_WINDOWPOS_UNDEFINED,
297 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
299 std::cerr << "Window could not be created! SDL_Error: ";
300 std::cerr << SDL_GetError();
301 std::cerr << std::endl;
304 gc = SDL_GL_CreateContext(gw);
306 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
307 std::cerr << SDL_GetError();
308 std::cerr << std::endl;
311 if (SDL_GL_SetSwapInterval(1) < 0) {
312 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
313 std::cerr << SDL_GetError();
314 std::cerr << std::endl;
318 std::cerr << "Unable to initialize OpenGL!";
319 std::cerr << std::endl;
327 GLenum error = GL_NO_ERROR;
328 glMatrixMode(GL_PROJECTION);
330 error = glGetError();
331 if (error != GL_NO_ERROR) {
332 std::cerr << "Error initializing OpenGL! ";
333 std::cerr << gluErrorString(error);
334 std::cerr << std::endl;
337 glMatrixMode(GL_MODELVIEW);
339 error = glGetError();
340 if (error != GL_NO_ERROR) {
341 std::cerr << "Error initializing OpenGL! ";
342 std::cerr << gluErrorString(error);
343 std::cerr << std::endl;
346 glClearColor(1, 1, 1, 1);
347 error = glGetError();
348 if (error != GL_NO_ERROR) {
349 std::cerr << "Error initializing OpenGL! ";
350 std::cerr << gluErrorString(error);
351 std::cerr << std::endl;