]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Compute obstacle cost for root/goal
[hubacji1/iamcar.git] / base / main.cc
1 /*
2 This file is part of I am car.
3
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.
8
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.
13
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/>.
16 */
17
18 #include <iostream>
19 #include <jsoncpp/json/json.h>
20 #include "compile.h"
21 #include "obstacle.h"
22 #include "rrtplanner.h"
23
24 int main()
25 {
26         Json::Value jvi; // JSON input
27         Json::Value jvo; // JSON output
28         unsigned int i = 0;
29         unsigned int j = 0;
30         std::cin >> jvi;
31         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
32
33         PLANNER p(
34                         new RRTNode(
35                                 jvi["init"][0].asFloat(),
36                                 jvi["init"][1].asFloat(),
37                                 jvi["init"][2].asFloat()),
38                         new RRTNode(
39                                 jvi["goal"][0].asFloat(),
40                                 jvi["goal"][1].asFloat(),
41                                 jvi["goal"][2].asFloat()));
42         std::vector<CircleObstacle> co;
43         std::vector<SegmentObstacle> so;
44         for (auto o: jvi["obst"]) {
45                 if (o["circle"] != Json::nullValue) {
46                         co.push_back(CircleObstacle(
47                                                 o["circle"][0].asFloat(),
48                                                 o["circle"][1].asFloat(),
49                                                 o["circle"][2].asFloat()));
50                 }
51                 if (o["segment"] != Json::nullValue) {
52                         so.push_back(SegmentObstacle(
53                                 new RRTNode(
54                                         o["segment"][0][0].asFloat(),
55                                         o["segment"][0][1].asFloat(),
56                                         0),
57                                 new RRTNode(
58                                         o["segment"][1][0].asFloat(),
59                                         o["segment"][1][1].asFloat(),
60                                         0)));
61                 }
62         }
63         p.link_obstacles(&co, &so);
64         p.ocost(p.root());
65         p.ocost(p.goal());
66
67         RRTNode *nn;
68         std::vector<RRTNode *> tr;
69         while(!p.goal_found()) {
70                 p.tstart();
71                 p.tend();
72                 while (p.elapsed() < TMAX) {
73                         p.next();
74                         p.tend();
75                 }
76                 nn = p.nn(p.iy_, p.goal(), p.cost);
77                 tr = p.findt(nn);
78                 p.tlog(tr);
79                 p.logr(p.root());
80                 p.rebase(nn);
81         }
82
83         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
84         std::cerr << "Goal found is " << p.goal_found() << std::endl;
85         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
86         std::cerr << "#samples is " << p.samples().size() << std::endl;
87         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
88         std::cerr << "trajectories costs:" << std::endl;
89         for (j = 0; j < p.clog().size(); j++)
90                 std::cerr << "- " << p.clog()[j] << std::endl;
91         std::cerr << "RRT #nodes:" << std::endl;
92         for (j = 0; j < p.nlog().size(); j++)
93                 std::cerr << "- " << p.nlog()[j] << std::endl;
94         std::cerr << "trajectories seconds:" << std::endl;
95         for (j = 0; j < p.slog().size(); j++)
96                 std::cerr << "- " << p.slog()[j] << std::endl;
97         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
98         std::cerr << std::endl;
99         for (auto edges: p.rlog())
100                 std::cerr << "- " << edges.size() << std::endl;
101
102         // JSON output
103         jvo["elap"] = p.elapsed();
104         // log cost
105         for (j = 0; j < p.clog().size(); j++)
106                 jvo["cost"][j] = p.clog()[j];
107         // log #nodes
108         for (j = 0; j < p.nlog().size(); j++)
109                 jvo["node"][j] = p.nlog()[j];
110         // log seconds
111         for (j = 0; j < p.slog().size(); j++)
112                 jvo["secs"][j] = p.slog()[j];
113         // log traj
114         i = 0;
115         j = 0;
116         for (auto traj: p.tlog()) {
117                 i = 0;
118                 for (auto n: traj) {
119                         jvo["traj"][j][i][0] = n->x();
120                         jvo["traj"][j][i][1] = n->y();
121                         jvo["traj"][j][i][2] = n->h();
122                         i++;
123                 }
124                 j++;
125         }
126 #if JSONLOGEDGES > 0
127         // log edges
128         i = 0;
129         j = 0;
130         for (auto edges: p.rlog()) {
131                 j = 0;
132                 for (auto e: edges) {
133                         jvo["edge"][i][j][0][0] = e->init()->x();
134                         jvo["edge"][i][j][0][1] = e->init()->y();
135                         jvo["edge"][i][j][1][0] = e->goal()->x();
136                         jvo["edge"][i][j][1][1] = e->goal()->y();
137                         j++;
138                 }
139                 i++;
140         }
141 #endif
142 #if JSONLOGSAMPLES > 0
143         // log samples
144         i = 0;
145         j = 0;
146         for (auto s: p.samples()) {
147                 jvo["samp"][j][0] = s->x();
148                 jvo["samp"][j][1] = s->y();
149                 jvo["samp"][j][2] = s->h();
150                 j++;
151         }
152 #endif
153         // print output
154         std::cout << jvo << std::endl;
155
156         // free mem
157         for (auto o: so) {
158                 delete o.init();
159                 delete o.goal();
160         }
161         return 0;
162 }