]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - incl/rrtbase.h
1f1b6f479334ff7491baace6a7cb3bc44ac27b59
[hubacji1/iamcar.git] / incl / rrtbase.h
1 /*
2 This file is part of I am car.
3
4 I am car is free 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 #ifndef RRTBASE_H
19 #define RRTBASE_H
20
21 #include <chrono>
22 #include <cmath>
23 #include <pthread.h>
24 #include <random>
25 #include <vector>
26 #include "obstacle.h"
27 #include "rrtnode.h"
28 #include "sample.h"
29 #include "slotplanner.h"
30
31 #define NOFNODES 20000
32
33 #define IXSIZE 100
34 #define IXSTEP (1.0 * ((VMAX) - (VMIN)) / IXSIZE)
35 #define IXI(x) ({ __typeof__ (x) _x = (x); \
36                 std::abs((int) floor(_x / IXSTEP)); })
37 #define IYSIZE 100
38 #define IYSTEP (1.0 * ((HMAX) - (HMIN)) / IYSIZE)
39 #define IYI(y) ({ __typeof__ (y) _y = (y); \
40                 std::abs((int) floor(_y / IYSTEP)); })
41
42 class Cell {
43         private:
44                 std::vector<RRTNode *> nodes_;
45                 pthread_mutex_t m_;
46                 bool changed_ = false;
47         public:
48                 Cell();
49
50                 // getter
51                 bool changed();
52                 std::vector<RRTNode *> nodes();
53
54                 // other
55                 void add_node(RRTNode *n);
56 };
57
58 class RRTBase {
59         private:
60                 RRTNode *root_ = nullptr;
61                 RRTNode *goal_ = nullptr;
62                 std::vector<RRTNode *> goals_;
63
64                 std::vector<RRTNode *> nodes_;
65                 std::vector<RRTNode *> dnodes_;
66                 PolygonObstacle frame_;
67                 std::vector<RRTNode *> samples_;
68                 std::vector<CircleObstacle> *cobstacles_;
69                 std::vector<SegmentObstacle> *sobstacles_;
70
71                 bool goal_found_ = false;
72                 std::chrono::high_resolution_clock::time_point tstart_;
73                 std::chrono::high_resolution_clock::time_point tend_;
74
75                 std::vector<float> clog_; // costs of trajectories
76                 std::vector<float> nlog_; // #nodes of RRT
77                 std::vector<std::vector<RRTEdge *>> rlog_; // log tree
78                 std::vector<float> slog_; // seconds of trajectories
79                 std::vector<std::vector<RRTNode *>> tlog_; // trajectories
80                 std::vector<RRTNode *> slot_cusp_; // cusp nodes in slot
81
82                 std::default_random_engine gen_;
83         public:
84                 const float GOAL_FOUND_DISTANCE = 0.2;
85                 const float GOAL_FOUND_ANGLE = M_PI / 32;
86
87                 ~RRTBase();
88                 RRTBase();
89                 RRTBase(RRTNode *init, RRTNode *goal);
90
91                 // getter
92                 RRTNode *root();
93                 RRTNode *goal();
94                 std::vector<RRTNode *> &goals();
95                 std::vector<RRTNode *> &nodes();
96                 std::vector<RRTNode *> &dnodes();
97                 PolygonObstacle &frame();
98                 std::vector<RRTNode *> &samples();
99                 std::vector<RRTNode *> iy_[IYSIZE];
100                 Cell ixy_[IXSIZE][IYSIZE];
101                 std::vector<CircleObstacle> *co();
102                 std::vector<SegmentObstacle> *so();
103                 std::vector<float> &clog();
104                 std::vector<float> &nlog();
105                 std::vector<std::vector<RRTEdge *>> &rlog();
106                 std::vector<float> &slog();
107                 std::vector<std::vector<RRTNode *>> &tlog();
108                 std::vector<RRTNode *> &slot_cusp();
109                 bool goal_found();
110                 float elapsed();
111                 std::vector<RRTNode *> traj_cusp();
112
113                 // setter
114                 void root(RRTNode *node);
115                 void goal(RRTNode *node);
116                 void goals(std::vector<RRTNode *> g);
117                 bool logr(RRTNode *root);
118                 float ocost(RRTNode *n);
119                 bool tlog(std::vector<RRTNode *> t);
120                 void tstart();
121                 void tend();
122                 bool link_obstacles(
123                                 std::vector<CircleObstacle> *cobstacles,
124                                 std::vector<SegmentObstacle> *sobstacles);
125                 bool add_iy(RRTNode *n);
126                 bool add_ixy(RRTNode *n);
127                 bool goal_found(bool f);
128                 void slot_cusp(std::vector<RRTNode *> sc);
129
130                 // other
131                 bool glplot();
132                 bool goal_found(
133                                 RRTNode *node,
134                                 float (*cost)(RRTNode *, RRTNode *));
135                 bool goal_found(
136                         RRTNode *node,
137                         RRTNode *goal
138                 );
139                 bool collide(RRTNode *init, RRTNode *goal);
140                 bool optp_dijkstra(
141                                 std::vector<RRTNode *> &cusps,
142                                 std::vector<int> &npi);
143                 bool optp_rrp(
144                                 std::vector<RRTNode *> &cusps,
145                                 std::vector<int> &npi);
146                 bool optp_smart(
147                                 std::vector<RRTNode *> &cusps,
148                                 std::vector<int> &npi);
149                 bool opt_path();
150                 bool rebase(RRTNode *nr);
151                 std::vector<RRTNode *> findt();
152                 std::vector<RRTNode *> findt(RRTNode *n);
153
154                 // RRT Framework
155                 SamplingInfo samplingInfo_;
156                 bool useSamplingInfo_ = false;
157                 RRTNode *sample();
158                 float cost(RRTNode *init, RRTNode *goal);
159                 RRTNode *nn(RRTNode *rs);
160                 std::vector<RRTNode *> nv(RRTNode *node, float dist);
161                 std::vector<RRTNode *> steer(RRTNode *init, RRTNode *goal);
162                 std::vector<RRTNode *> steer(
163                                 RRTNode *init,
164                                 RRTNode *goal,
165                                 float step);
166
167                 // virtuals - implemented by child classes
168                 virtual bool next() = 0;
169 };
170
171 #endif