]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - incl/rrtbase.h
Merge branch 'hotfix/0.6.1'
[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
63                 std::vector<RRTNode *> nodes_;
64                 std::vector<RRTNode *> dnodes_;
65                 PolygonObstacle frame_;
66                 std::vector<RRTNode *> samples_;
67                 std::vector<CircleObstacle> *cobstacles_;
68                 std::vector<SegmentObstacle> *sobstacles_;
69
70                 bool goal_found_ = false;
71                 std::chrono::high_resolution_clock::time_point tstart_;
72                 std::chrono::high_resolution_clock::time_point tend_;
73
74                 std::vector<float> clog_; // costs of trajectories
75                 std::vector<float> nlog_; // #nodes of RRT
76                 std::vector<std::vector<RRTEdge *>> rlog_; // log tree
77                 std::vector<float> slog_; // seconds of trajectories
78                 std::vector<std::vector<RRTNode *>> tlog_; // trajectories
79                 std::vector<RRTNode *> slot_cusp_; // cusp nodes in slot
80
81                 std::default_random_engine gen_;
82         public:
83                 const float GOAL_FOUND_DISTANCE = 0.2;
84                 const float GOAL_FOUND_ANGLE = M_PI / 32;
85
86                 ~RRTBase();
87                 RRTBase();
88                 RRTBase(RRTNode *init, RRTNode *goal);
89
90                 // getter
91                 RRTNode *root();
92                 RRTNode *goal();
93                 std::vector<RRTNode *> &nodes();
94                 std::vector<RRTNode *> &dnodes();
95                 PolygonObstacle &frame();
96                 std::vector<RRTNode *> &samples();
97                 std::vector<RRTNode *> iy_[IYSIZE];
98                 Cell ixy_[IXSIZE][IYSIZE];
99                 std::vector<CircleObstacle> *co();
100                 std::vector<SegmentObstacle> *so();
101                 std::vector<float> &clog();
102                 std::vector<float> &nlog();
103                 std::vector<std::vector<RRTEdge *>> &rlog();
104                 std::vector<float> &slog();
105                 std::vector<std::vector<RRTNode *>> &tlog();
106                 std::vector<RRTNode *> &slot_cusp();
107                 bool goal_found();
108                 float elapsed();
109                 std::vector<RRTNode *> traj_cusp();
110
111                 // setter
112                 void root(RRTNode *node);
113                 void goal(RRTNode *node);
114                 bool logr(RRTNode *root);
115                 float ocost(RRTNode *n);
116                 bool tlog(std::vector<RRTNode *> t);
117                 void tstart();
118                 void tend();
119                 bool link_obstacles(
120                                 std::vector<CircleObstacle> *cobstacles,
121                                 std::vector<SegmentObstacle> *sobstacles);
122                 bool add_iy(RRTNode *n);
123                 bool add_ixy(RRTNode *n);
124                 bool goal_found(bool f);
125                 void slot_cusp(std::vector<RRTNode *> sc);
126
127                 // other
128                 bool glplot();
129                 bool goal_found(
130                                 RRTNode *node,
131                                 float (*cost)(RRTNode *, RRTNode *));
132                 bool collide(RRTNode *init, RRTNode *goal);
133                 bool optp_dijkstra(
134                                 std::vector<RRTNode *> &cusps,
135                                 std::vector<int> &npi);
136                 bool optp_rrp(
137                                 std::vector<RRTNode *> &cusps,
138                                 std::vector<int> &npi);
139                 bool optp_smart(
140                                 std::vector<RRTNode *> &cusps,
141                                 std::vector<int> &npi);
142                 bool opt_path();
143                 bool rebase(RRTNode *nr);
144                 std::vector<RRTNode *> findt();
145                 std::vector<RRTNode *> findt(RRTNode *n);
146
147                 // RRT Framework
148                 SamplingInfo samplingInfo_;
149                 bool useSamplingInfo_ = false;
150                 RRTNode *sample();
151                 float cost(RRTNode *init, RRTNode *goal);
152                 RRTNode *nn(RRTNode *rs);
153                 std::vector<RRTNode *> nv(RRTNode *node, float dist);
154                 std::vector<RRTNode *> steer(RRTNode *init, RRTNode *goal);
155                 std::vector<RRTNode *> steer(
156                                 RRTNode *init,
157                                 RRTNode *goal,
158                                 float step);
159
160                 // virtuals - implemented by child classes
161                 virtual bool next() = 0;
162 };
163
164 #endif