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>
25 #include "rrtplanner.h"
32 //#define JSONLOGEDGES
33 //#define JSONLOGSAMPLES
46 bool run_planner = true;
48 SDL_Window* gw = NULL;
65 Json::Value jvi; // JSON input
66 Json::Value jvo; // JSON output
70 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
74 jvi["init"][0].asFloat(),
75 jvi["init"][1].asFloat(),
76 jvi["init"][2].asFloat()),
78 jvi["goal"][0].asFloat(),
79 jvi["goal"][1].asFloat(),
80 jvi["goal"][2].asFloat()));
81 std::vector<CircleObstacle> co;
82 std::vector<SegmentObstacle> so;
83 for (auto o: jvi["obst"]) {
84 if (o["circle"] != Json::nullValue) {
85 co.push_back(CircleObstacle(
86 o["circle"][0].asFloat(),
87 o["circle"][1].asFloat(),
88 o["circle"][2].asFloat()));
90 if (o["segment"] != Json::nullValue) {
91 so.push_back(SegmentObstacle(
93 o["segment"][0][0].asFloat(),
94 o["segment"][0][1].asFloat(),
97 o["segment"][1][0].asFloat(),
98 o["segment"][1][1].asFloat(),
102 p.link_obstacles(&co, &so);
107 std::vector<RRTNode *> steered;
108 for (auto jn: jvi["traj"][0]) {
109 steered.push_back(new RRTNode(
116 std::reverse(steered.begin(), steered.end());
117 RRTNode *pn = p.root();
118 for (auto n: steered) {
121 pn->add_child(n, p.cost(pn, n));
125 pn->add_child(p.goal(), p.cost(pn, p.goal()));
134 #elif defined USE_INTERRUPT
135 signal(SIGINT, hint);
136 signal(SIGTERM, hint);
138 while (run_planner) {
145 #elif defined USE_TMAX
148 while (!p.goal_found() && p.elapsed() < TMAX) {
159 // statistics to error output
160 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
161 std::cerr << "Goal found is " << p.goal_found() << std::endl;
162 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
163 std::cerr << "#samples is " << p.samples().size() << std::endl;
164 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
165 std::cerr << "trajectories costs:" << std::endl;
166 for (j = 0; j < p.clog().size(); j++)
167 std::cerr << "- " << p.clog()[j] << std::endl;
168 std::cerr << "RRT #nodes:" << std::endl;
169 for (j = 0; j < p.nlog().size(); j++)
170 std::cerr << "- " << p.nlog()[j] << std::endl;
171 std::cerr << "trajectories seconds:" << std::endl;
172 for (j = 0; j < p.slog().size(); j++)
173 std::cerr << "- " << p.slog()[j] << std::endl;
174 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
175 std::cerr << std::endl;
176 for (auto edges: p.rlog())
177 std::cerr << "- " << edges.size() << std::endl;
180 jvo["elap"] = p.elapsed();
182 for (j = 0; j < p.clog().size(); j++)
183 jvo["cost"][j] = p.clog()[j];
185 for (j = 0; j < p.nlog().size(); j++)
186 jvo["node"][j] = p.nlog()[j];
188 for (j = 0; j < p.slog().size(); j++)
189 jvo["secs"][j] = p.slog()[j];
193 for (auto traj: p.tlog()) {
196 jvo["traj"][j][i][0] = n->x();
197 jvo["traj"][j][i][1] = n->y();
198 jvo["traj"][j][i][2] = n->h();
199 jvo["traj"][j][i][3] = n->t();
200 jvo["traj"][j][i][4] = n->s();
208 for (auto edges: p.rlog()) {
210 for (auto e: edges) {
211 jvo["edge"][i][j][0][0] = e->init()->x();
212 jvo["edge"][i][j][0][1] = e->init()->y();
213 jvo["edge"][i][j][0][2] = e->init()->h();
214 jvo["edge"][i][j][1][0] = e->goal()->x();
215 jvo["edge"][i][j][1][1] = e->goal()->y();
216 jvo["edge"][i][j][1][2] = e->goal()->h();
222 #ifdef JSONLOGSAMPLES
225 for (auto s: p.samples()) {
226 jvo["samp"][j][0] = s->x();
227 jvo["samp"][j][1] = s->y();
228 jvo["samp"][j][2] = s->h();
233 std::cout << jvo << std::endl;
236 SDL_DestroyWindow(gw);
250 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
251 std::cerr << "SDL could not initialize! SDL_Error: ";
252 std::cerr << SDL_GetError();
253 std::cerr << std::endl;
256 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
257 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
258 gw = SDL_CreateWindow(
260 SDL_WINDOWPOS_UNDEFINED,
261 SDL_WINDOWPOS_UNDEFINED,
264 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
266 std::cerr << "Window could not be created! SDL_Error: ";
267 std::cerr << SDL_GetError();
268 std::cerr << std::endl;
271 gc = SDL_GL_CreateContext(gw);
273 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
274 std::cerr << SDL_GetError();
275 std::cerr << std::endl;
278 if (SDL_GL_SetSwapInterval(1) < 0) {
279 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
280 std::cerr << SDL_GetError();
281 std::cerr << std::endl;
285 std::cerr << "Unable to initialize OpenGL!";
286 std::cerr << std::endl;
294 GLenum error = GL_NO_ERROR;
295 glMatrixMode(GL_PROJECTION);
297 error = glGetError();
298 if (error != GL_NO_ERROR) {
299 std::cerr << "Error initializing OpenGL! ";
300 std::cerr << gluErrorString(error);
301 std::cerr << std::endl;
304 glMatrixMode(GL_MODELVIEW);
306 error = glGetError();
307 if (error != GL_NO_ERROR) {
308 std::cerr << "Error initializing OpenGL! ";
309 std::cerr << gluErrorString(error);
310 std::cerr << std::endl;
313 glClearColor(1, 1, 1, 1);
314 error = glGetError();
315 if (error != GL_NO_ERROR) {
316 std::cerr << "Error initializing OpenGL! ";
317 std::cerr << gluErrorString(error);
318 std::cerr << std::endl;