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"
31 bool run_planner = true;
33 SDL_Window* gw = NULL;
48 Json::Value jvi; // JSON input
49 Json::Value jvo; // JSON output
53 std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
57 jvi["init"][0].asFloat(),
58 jvi["init"][1].asFloat(),
59 jvi["init"][2].asFloat()),
61 jvi["goal"][0].asFloat(),
62 jvi["goal"][1].asFloat(),
63 jvi["goal"][2].asFloat()));
64 std::vector<CircleObstacle> co;
65 std::vector<SegmentObstacle> so;
66 for (auto o: jvi["obst"]) {
67 if (o["circle"] != Json::nullValue) {
68 co.push_back(CircleObstacle(
69 o["circle"][0].asFloat(),
70 o["circle"][1].asFloat(),
71 o["circle"][2].asFloat()));
73 if (o["segment"] != Json::nullValue) {
74 so.push_back(SegmentObstacle(
76 o["segment"][0][0].asFloat(),
77 o["segment"][0][1].asFloat(),
80 o["segment"][1][0].asFloat(),
81 o["segment"][1][1].asFloat(),
85 p.link_obstacles(&co, &so);
90 std::vector<RRTNode *> tr;
94 signal(SIGTERM, hint);
100 if (!p.goal_found()) {
102 nn = p.nn(p.iy_, p.goal(), p.cost);
104 nn = p.nn(p.nodes(), p.goal(), p.cost);
112 #else // NOT USE_INTERRUPT
113 while(!p.goal_found()) {
116 while (p.elapsed() < TMAX) {
121 nn = p.nn(p.iy_, p.goal(), p.cost);
123 nn = p.nn(p.nodes(), p.goal(), p.cost);
132 #endif // USE_INTERRUPT
134 std::cerr << "Elapsed is " << p.elapsed() << std::endl;
135 std::cerr << "Goal found is " << p.goal_found() << std::endl;
136 std::cerr << "#nodes is " << p.nodes().size() << std::endl;
137 std::cerr << "#samples is " << p.samples().size() << std::endl;
138 std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
139 std::cerr << "trajectories costs:" << std::endl;
140 for (j = 0; j < p.clog().size(); j++)
141 std::cerr << "- " << p.clog()[j] << std::endl;
142 std::cerr << "RRT #nodes:" << std::endl;
143 for (j = 0; j < p.nlog().size(); j++)
144 std::cerr << "- " << p.nlog()[j] << std::endl;
145 std::cerr << "trajectories seconds:" << std::endl;
146 for (j = 0; j < p.slog().size(); j++)
147 std::cerr << "- " << p.slog()[j] << std::endl;
148 std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
149 std::cerr << std::endl;
150 for (auto edges: p.rlog())
151 std::cerr << "- " << edges.size() << std::endl;
154 jvo["elap"] = p.elapsed();
156 for (j = 0; j < p.clog().size(); j++)
157 jvo["cost"][j] = p.clog()[j];
159 for (j = 0; j < p.nlog().size(); j++)
160 jvo["node"][j] = p.nlog()[j];
162 for (j = 0; j < p.slog().size(); j++)
163 jvo["secs"][j] = p.slog()[j];
167 for (auto traj: p.tlog()) {
170 jvo["traj"][j][i][0] = n->x();
171 jvo["traj"][j][i][1] = n->y();
172 jvo["traj"][j][i][2] = n->h();
181 for (auto edges: p.rlog()) {
183 for (auto e: edges) {
184 jvo["edge"][i][j][0][0] = e->init()->x();
185 jvo["edge"][i][j][0][1] = e->init()->y();
186 jvo["edge"][i][j][1][0] = e->goal()->x();
187 jvo["edge"][i][j][1][1] = e->goal()->y();
193 #if JSONLOGSAMPLES > 0
197 for (auto s: p.samples()) {
198 jvo["samp"][j][0] = s->x();
199 jvo["samp"][j][1] = s->y();
200 jvo["samp"][j][2] = s->h();
205 std::cout << jvo << std::endl;
207 SDL_DestroyWindow(gw);
220 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
221 std::cerr << "SDL could not initialize! SDL_Error: ";
222 std::cerr << SDL_GetError();
223 std::cerr << std::endl;
226 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
227 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
228 gw = SDL_CreateWindow(
230 SDL_WINDOWPOS_UNDEFINED,
231 SDL_WINDOWPOS_UNDEFINED,
234 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
236 std::cerr << "Window could not be created! SDL_Error: ";
237 std::cerr << SDL_GetError();
238 std::cerr << std::endl;
241 gc = SDL_GL_CreateContext(gw);
243 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
244 std::cerr << SDL_GetError();
245 std::cerr << std::endl;
248 if (SDL_GL_SetSwapInterval(1) < 0) {
249 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
250 std::cerr << SDL_GetError();
251 std::cerr << std::endl;
255 std::cerr << "Unable to initialize OpenGL!";
256 std::cerr << std::endl;
264 GLenum error = GL_NO_ERROR;
265 glMatrixMode(GL_PROJECTION);
267 error = glGetError();
268 if (error != GL_NO_ERROR) {
269 std::cerr << "Error initializing OpenGL! ";
270 std::cerr << gluErrorString(error);
271 std::cerr << std::endl;
274 glMatrixMode(GL_MODELVIEW);
276 error = glGetError();
277 if (error != GL_NO_ERROR) {
278 std::cerr << "Error initializing OpenGL! ";
279 std::cerr << gluErrorString(error);
280 std::cerr << std::endl;
283 glClearColor(1, 1, 1, 1);
284 error = glGetError();
285 if (error != GL_NO_ERROR) {
286 std::cerr << "Error initializing OpenGL! ";
287 std::cerr << gluErrorString(error);
288 std::cerr << std::endl;