6 #include <jsoncpp/json/json.h>
11 #define ETA 1.0 // for steer, nv
12 #define GAMMA(cV) ({ \
13 __typeof__ (cV) _cV = (cV); \
14 pow(log(_cV) / _cV, 1.0 / 3.0); \
17 /*! \brief Possible type of RRT node.
19 \param cusp The node that is cusp (change in direction).
20 \param connected The node that branches generated steered path.
24 static const unsigned int cusp = 1 << 0;
25 static const unsigned int connected = 1 << 1;
28 /*! \brief RRT node basic class.
30 \param c Cumulative cost from RRT data structure root.
31 \param p Pointer to parent RRT node.
32 \param t Type of the RRT node (RRTNodeType).
34 \param x Horizontal coordinate of rear axle center.
35 \param y Vertical coordinate of rear axle center.
36 \param h Heading of the car in the interval [-pi,+pi] radians.
37 \param sp Speed of the car.
38 \param st Steering of the car.
42 RRTNode *p_ = nullptr;
56 double c() const { return this->c_; }
60 this->cc = this->p_->cc + this->c_;
63 RRTNode *p() const { return this->p_; }
64 void p(RRTNode *p) { this->p_ = p; }
66 bool t(unsigned int flag) { return this->t_ & flag; }
67 void set_t(unsigned int flag) { this->t_ |= flag; }
68 void clear_t(unsigned int flag) { this->t_ &= ~flag; }
72 double x() const { return this->x_; }
73 void x(double x) { this->x_ = x; }
75 double y() const { return this->y_; }
76 void y(double y) { this->y_ = y; }
78 double h() const { return this->h_; }
88 double sp() const { return this->sp_; }
89 void sp(double sp) { this->sp_ = sp; }
91 double st() const { return this->st_; }
92 void st(double st) { this->st_ = st; }
95 RRTNode(const BicycleCar &bc);
96 bool operator==(const RRTNode& n);
97 friend std::ostream &operator<<(
102 out << "[" << bc.x();
103 out << "," << bc.y();
104 out << "," << bc.h();
110 /*! \brief Polygon obstacle basic class.
112 \param poly Border polygon of the obstacle.
116 std::vector<std::tuple<double, double>> poly_;
119 std::vector<std::tuple<double, double>> &poly()
127 /*! \brief RRT* algorithm basic class.
129 \param icnt RRT algorithm iterations counter.
130 \param goals The vector of goal nodes.
131 \param nodes The vector of all nodes in RRT data structure.
132 \param samples The vector of all samples of RRT algorithm.
133 \param sample_dist_type Random distribution type for sampling function (0 -
134 normal, 1 - uniform, 2 - uniform circle)
138 unsigned int icnt_ = 0;
139 std::chrono::high_resolution_clock::time_point tstart_;
143 int sample_dist_type_ = 0;
145 std::vector<RRTNode> goals_;
146 std::vector<RRTNode> nodes_;
147 std::vector<Obstacle> obstacles_;
148 std::vector<RRTNode> samples_;
149 std::vector<RRTNode> steered_;
150 double log_path_time_ = 0.1;
151 unsigned int log_path_iter_ = 20;
153 /*! \brief Update and return elapsed time.
156 /*! \brief Log current path cost.
158 void log_path_cost();
159 /*! \brief Set normal distribution for sampling.
161 void set_sample_normal(
162 double x1, double x2,
163 double y1, double y2,
166 /*! \brief Set uniform distribution for sampling.
168 void set_sample_uniform(
169 double x1, double x2,
170 double y1, double y2,
173 /*! \brief Set uniform circle distribution for sampling.
175 void set_sample_uniform_circle();
176 RRTNode* use_nn; // Used for RRTExt12.
177 std::vector<RRTNode> tmp_steered_;
178 bool finishit = false;
179 double path_cost_before_opt_ = 9999;
182 /*! \brief Store RRT node to tree data structure.
184 virtual void store_node(RRTNode n);
187 std::tuple<bool, unsigned int, unsigned int>
188 collide(std::vector<std::tuple<double, double>> &poly);
189 virtual std::tuple<bool, unsigned int, unsigned int>
190 collide_steered_from(RRTNode &f);
191 virtual std::tuple<bool, unsigned int, unsigned int>
192 collide_tmp_steered_from(RRTNode &f);
193 virtual std::tuple<bool, unsigned int, unsigned int>
194 collide_two_nodes(RRTNode &f, RRTNode &t);
196 std::default_random_engine gen_;
197 std::normal_distribution<double> ndx_;
198 std::normal_distribution<double> ndy_;
199 std::normal_distribution<double> ndh_;
200 std::uniform_real_distribution<double> udx_;
201 std::uniform_real_distribution<double> udy_;
202 std::uniform_real_distribution<double> udh_;
203 std::uniform_int_distribution<unsigned int> udi1_;
204 std::uniform_int_distribution<unsigned int> udi2_;
205 virtual RRTNode *nn(RRTNode &t);
206 virtual std::vector<RRTNode *> nv(RRTNode &t);
207 void steer(RRTNode &f, RRTNode &t);
208 void tmp_steer(RRTNode &f, RRTNode &t);
209 virtual void steer1(RRTNode &f, RRTNode &t);
210 virtual void steer2(RRTNode &f, RRTNode &t);
211 /*! \brief Join steered nodes to RRT data structure
213 \param f RRT node to join steered nodes to.
215 void join_steered(RRTNode *f);
216 void join_tmp_steered(RRTNode *f);
217 virtual bool goal_found(RRTNode &f);
219 virtual bool connect();
223 std::vector<double> log_opt_time_;
224 std::vector<double> log_path_cost_;
225 struct { double x=0; double y=0; double b=0; double e=0; } entry;
226 bool entry_set = false;
227 struct { double x=0; double y=0; double h=0; } entry1;
228 struct { double x=0; double y=0; double h=0; } entry2;
229 bool entries_set = false;
230 std::vector<RRTNode *> steered1_;
231 std::vector<RRTNode *> steered2_;
233 /*! \brief Initialize RRT algorithm if needed.
236 /*! \brief Deinitialize RRT algorithm if needed.
238 virtual void deinit();
239 /*! \brief Return path found by RRT*.
241 virtual std::vector<RRTNode *> path();
242 /*! \brief Return ``true`` if algorithm should stop.
244 Update counters (iteration, seconds, ...) and return if
245 the current iteration should be the last one.
248 /*! \brief Return ``true`` if the algorithm should finish.
250 Finish means that the algorithm will not be resumed.
252 bool should_finish();
253 /*! \brief Return ``true`` if the algorithm shoud break.
255 Break means that the algorithm can be resumed.
258 /*! \brief Return ``true`` if algorithm should continue.
260 `pcnt_` is set to `scnt_`, so the difference is 0 and it can
261 start from scratch. After the `should_continue` is called,
262 there must be `while (rrts.next()) {}` loop.
264 bool should_continue();
265 /*! \brief Run next RRT* iteration.
268 /*! \brief Set sampling info.
270 Based on `sample_dist_type`, set proper distribution
271 parameters. The distribution parameters are relative to `front`
272 node in `nodes` (init).
275 \param x1 Mean x value.
276 \param x2 Standard deviation of x.
277 \param y1 Mean y value.
278 \param y2 Standard deviation of y.
279 \param h1 Mean h value.
280 \param h2 Standard deviation of h.
282 For uniform sampling:
283 \param x1 Minimum x value.
284 \param x2 Maximum x value.
285 \param y1 Minimum y value.
286 \param y2 Maximum y value.
287 \param h1 Minimum h value.
288 \param h2 Maximum h value.
290 For uniform circle sampling:
299 double x1, double x2,
300 double y1, double y2,
303 /*! \brief Generate JSON output.
306 /*! \brief Load JSON input.
308 void json(Json::Value jvi);
311 virtual double cost_build(RRTNode &f, RRTNode &t);
312 virtual double cost_search(RRTNode &f, RRTNode &t);
315 unsigned int icnt() const { return this->icnt_; }
316 void icnt(unsigned int i) { this->icnt_ = i; }
317 double scnt() const { return this->scnt_; }
318 bool gf() const { return this->gf_; }
319 void gf(bool f) { this->gf_ = f; }
320 int sample_dist_type() const { return this->sample_dist_type_;}
321 void sample_dist_type(int t) { this->sample_dist_type_ = t; }
322 std::vector<RRTNode> &goals() { return this->goals_; }
323 std::vector<RRTNode> &nodes() { return this->nodes_; }
324 std::vector<Obstacle> &obstacles() { return this->obstacles_; }
325 std::vector<RRTNode> &samples() { return this->samples_; }
326 std::vector<RRTNode> &steered() { return this->steered_; }
331 /*! \brief Compute cumulative cost of RRT node.
333 \param t RRT node to compute cumulative cost to.
335 double cc(RRTNode &t);