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"
32 bool run_planner = true;
34 SDL_Window* gw = NULL;
51 Json::Value jvi; // JSON input
52 Json::Value jvo; // JSON output
56 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
60 jvi["init"][0].asFloat(),
61 jvi["init"][1].asFloat(),
62 jvi["init"][2].asFloat()),
64 jvi["goal"][0].asFloat(),
65 jvi["goal"][1].asFloat(),
66 jvi["goal"][2].asFloat()));
67 std::vector<CircleObstacle> co;
68 std::vector<SegmentObstacle> so;
69 for (auto o: jvi["obst"]) {
70 if (o["circle"] != Json::nullValue) {
71 co.push_back(CircleObstacle(
72 o["circle"][0].asFloat(),
73 o["circle"][1].asFloat(),
74 o["circle"][2].asFloat()));
76 if (o["segment"] != Json::nullValue) {
77 so.push_back(SegmentObstacle(
79 o["segment"][0][0].asFloat(),
80 o["segment"][0][1].asFloat(),
83 o["segment"][1][0].asFloat(),
84 o["segment"][1][1].asFloat(),
88 p.link_obstacles(&co, &so);
93 std::vector<RRTNode *> tr;
97 signal(SIGTERM, hint);
108 if (!p.goal_found()) {
110 nn = p.nn(p.iy_, p.goal(), p.cost);
112 nn = p.nn(p.nodes(), p.goal(), p.cost);
122 #else // NOT USE_INTERRUPT
124 while (!p.goal_found() && i < 20) {
127 while (p.elapsed() < TMAX) {
131 if (!p.goal_found()) {
133 nn = p.nn(p.iy_, p.goal(), p.cost);
135 nn = p.nn(p.nodes(), p.goal(), p.cost);
149 std::cerr << "#iterations is " << i << std::endl;
150 #endif // USE_INTERRUPT
152 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
153 std::cerr << "Goal found is " << p.goal_found() << std::endl;
154 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
155 std::cerr << "#samples is " << p.samples().size() << std::endl;
156 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
157 std::cerr << "trajectories costs:" << std::endl;
158 for (j = 0; j < p.clog().size(); j++)
159 std::cerr << "- " << p.clog()[j] << std::endl;
160 std::cerr << "RRT #nodes:" << std::endl;
161 for (j = 0; j < p.nlog().size(); j++)
162 std::cerr << "- " << p.nlog()[j] << std::endl;
163 std::cerr << "trajectories seconds:" << std::endl;
164 for (j = 0; j < p.slog().size(); j++)
165 std::cerr << "- " << p.slog()[j] << std::endl;
166 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
167 std::cerr << std::endl;
168 for (auto edges: p.rlog())
169 std::cerr << "- " << edges.size() << std::endl;
172 jvo["elap"] = p.elapsed();
174 for (j = 0; j < p.clog().size(); j++)
175 jvo["cost"][j] = p.clog()[j];
177 for (j = 0; j < p.nlog().size(); j++)
178 jvo["node"][j] = p.nlog()[j];
180 for (j = 0; j < p.slog().size(); j++)
181 jvo["secs"][j] = p.slog()[j];
185 for (auto traj: p.tlog()) {
188 jvo["traj"][j][i][0] = n->x();
189 jvo["traj"][j][i][1] = n->y();
190 jvo["traj"][j][i][2] = n->h();
191 jvo["traj"][j][i][3] = n->t();
192 jvo["traj"][j][i][4] = n->s();
201 for (auto edges: p.rlog()) {
203 for (auto e: edges) {
204 jvo["edge"][i][j][0][0] = e->init()->x();
205 jvo["edge"][i][j][0][1] = e->init()->y();
206 jvo["edge"][i][j][1][0] = e->goal()->x();
207 jvo["edge"][i][j][1][1] = e->goal()->y();
213 #if JSONLOGSAMPLES > 0
217 for (auto s: p.samples()) {
218 jvo["samp"][j][0] = s->x();
219 jvo["samp"][j][1] = s->y();
220 jvo["samp"][j][2] = s->h();
225 std::cout << jvo << std::endl;
228 SDL_DestroyWindow(gw);
242 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
243 std::cerr << "SDL could not initialize! SDL_Error: ";
244 std::cerr << SDL_GetError();
245 std::cerr << std::endl;
248 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
249 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
250 gw = SDL_CreateWindow(
252 SDL_WINDOWPOS_UNDEFINED,
253 SDL_WINDOWPOS_UNDEFINED,
256 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
258 std::cerr << "Window could not be created! SDL_Error: ";
259 std::cerr << SDL_GetError();
260 std::cerr << std::endl;
263 gc = SDL_GL_CreateContext(gw);
265 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
266 std::cerr << SDL_GetError();
267 std::cerr << std::endl;
270 if (SDL_GL_SetSwapInterval(1) < 0) {
271 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
272 std::cerr << SDL_GetError();
273 std::cerr << std::endl;
277 std::cerr << "Unable to initialize OpenGL!";
278 std::cerr << std::endl;
286 GLenum error = GL_NO_ERROR;
287 glMatrixMode(GL_PROJECTION);
289 error = glGetError();
290 if (error != GL_NO_ERROR) {
291 std::cerr << "Error initializing OpenGL! ";
292 std::cerr << gluErrorString(error);
293 std::cerr << std::endl;
296 glMatrixMode(GL_MODELVIEW);
298 error = glGetError();
299 if (error != GL_NO_ERROR) {
300 std::cerr << "Error initializing OpenGL! ";
301 std::cerr << gluErrorString(error);
302 std::cerr << std::endl;
305 glClearColor(1, 1, 1, 1);
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;