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