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