]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Add interrupt/terminate handling
[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 <signal.h>
21 #include "compile.h"
22 #include "obstacle.h"
23 #include "rrtplanner.h"
24
25 #define USE_INTERRUPT
26
27 bool run_planner = true;
28
29 void hint(int)
30 {
31         run_planner = false;
32 }
33
34 int main()
35 {
36         Json::Value jvi; // JSON input
37         Json::Value jvo; // JSON output
38         unsigned int i = 0;
39         unsigned int j = 0;
40         std::cin >> jvi;
41         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
42
43         PLANNER p(
44                         new RRTNode(
45                                 jvi["init"][0].asFloat(),
46                                 jvi["init"][1].asFloat(),
47                                 jvi["init"][2].asFloat()),
48                         new RRTNode(
49                                 jvi["goal"][0].asFloat(),
50                                 jvi["goal"][1].asFloat(),
51                                 jvi["goal"][2].asFloat()));
52         std::vector<CircleObstacle> co;
53         std::vector<SegmentObstacle> so;
54         for (auto o: jvi["obst"]) {
55                 if (o["circle"] != Json::nullValue) {
56                         co.push_back(CircleObstacle(
57                                                 o["circle"][0].asFloat(),
58                                                 o["circle"][1].asFloat(),
59                                                 o["circle"][2].asFloat()));
60                 }
61                 if (o["segment"] != Json::nullValue) {
62                         so.push_back(SegmentObstacle(
63                                 new RRTNode(
64                                         o["segment"][0][0].asFloat(),
65                                         o["segment"][0][1].asFloat(),
66                                         0),
67                                 new RRTNode(
68                                         o["segment"][1][0].asFloat(),
69                                         o["segment"][1][1].asFloat(),
70                                         0)));
71                 }
72         }
73         p.link_obstacles(&co, &so);
74         p.ocost(p.root());
75         p.ocost(p.goal());
76
77         RRTNode *nn;
78         std::vector<RRTNode *> tr;
79
80 #ifdef USE_INTERRUPT
81         signal(SIGINT, hint);
82         signal(SIGTERM, hint);
83         p.tstart();
84         while (run_planner) {
85                 p.next();
86                 p.tend();
87         }
88         if (!p.goal_found()) {
89 #if NNVERSION > 1
90                 nn = p.nn(p.iy_, p.goal(), p.cost);
91 #else
92                 nn = p.nn(p.nodes(), p.goal(), p.cost);
93 #endif
94                 tr = p.findt(nn);
95                 p.tlog(tr);
96 #if JSONLOGEDGES > 0
97                 p.logr(p.root());
98 #endif
99         }
100 #else // USE_INTERRUPT
101         while(!p.goal_found()) {
102                 p.tstart();
103                 p.tend();
104                 while (p.elapsed() < TMAX) {
105                         p.next();
106                         p.tend();
107                 }
108 #if NNVERSION > 1
109                 nn = p.nn(p.iy_, p.goal(), p.cost);
110 #else
111                 nn = p.nn(p.nodes(), p.goal(), p.cost);
112 #endif
113                 tr = p.findt(nn);
114                 p.tlog(tr);
115 #if JSONLOGEDGES > 0
116                 p.logr(p.root());
117 #endif
118                 p.rebase(nn);
119         }
120 #endif // USE_INTERRUPT
121
122         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
123         std::cerr << "Goal found is " << p.goal_found() << std::endl;
124         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
125         std::cerr << "#samples is " << p.samples().size() << std::endl;
126         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
127         std::cerr << "trajectories costs:" << std::endl;
128         for (j = 0; j < p.clog().size(); j++)
129                 std::cerr << "- " << p.clog()[j] << std::endl;
130         std::cerr << "RRT #nodes:" << std::endl;
131         for (j = 0; j < p.nlog().size(); j++)
132                 std::cerr << "- " << p.nlog()[j] << std::endl;
133         std::cerr << "trajectories seconds:" << std::endl;
134         for (j = 0; j < p.slog().size(); j++)
135                 std::cerr << "- " << p.slog()[j] << std::endl;
136         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
137         std::cerr << std::endl;
138         for (auto edges: p.rlog())
139                 std::cerr << "- " << edges.size() << std::endl;
140
141         // JSON output
142         jvo["elap"] = p.elapsed();
143         // log cost
144         for (j = 0; j < p.clog().size(); j++)
145                 jvo["cost"][j] = p.clog()[j];
146         // log #nodes
147         for (j = 0; j < p.nlog().size(); j++)
148                 jvo["node"][j] = p.nlog()[j];
149         // log seconds
150         for (j = 0; j < p.slog().size(); j++)
151                 jvo["secs"][j] = p.slog()[j];
152         // log traj
153         i = 0;
154         j = 0;
155         for (auto traj: p.tlog()) {
156                 i = 0;
157                 for (auto n: traj) {
158                         jvo["traj"][j][i][0] = n->x();
159                         jvo["traj"][j][i][1] = n->y();
160                         jvo["traj"][j][i][2] = n->h();
161                         i++;
162                 }
163                 j++;
164         }
165 #if JSONLOGEDGES > 0
166         // log edges
167         i = 0;
168         j = 0;
169         for (auto edges: p.rlog()) {
170                 j = 0;
171                 for (auto e: edges) {
172                         jvo["edge"][i][j][0][0] = e->init()->x();
173                         jvo["edge"][i][j][0][1] = e->init()->y();
174                         jvo["edge"][i][j][1][0] = e->goal()->x();
175                         jvo["edge"][i][j][1][1] = e->goal()->y();
176                         j++;
177                 }
178                 i++;
179         }
180 #endif
181 #if JSONLOGSAMPLES > 0
182         // log samples
183         i = 0;
184         j = 0;
185         for (auto s: p.samples()) {
186                 jvo["samp"][j][0] = s->x();
187                 jvo["samp"][j][1] = s->y();
188                 jvo["samp"][j][2] = s->h();
189                 j++;
190         }
191 #endif
192         // print output
193         std::cout << jvo << std::endl;
194
195         // free mem
196         for (auto o: so) {
197                 delete o.init();
198                 delete o.goal();
199         }
200         return 0;
201 }