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