4 #include "reeds_shepp.h"
6 template <typename T> int sgn(T val) {
7 return (T(0) < val) - (val < T(0));
14 RRTNode::RRTNode(const BicycleCar &bc)
23 bool RRTNode::operator==(const RRTNode& n)
34 double RRTS::elapsed()
36 std::chrono::duration<double> dt;
37 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
38 std::chrono::high_resolution_clock::now()
41 this->scnt_ = dt.count();
45 bool RRTS::should_stop()
47 // the following counters must be updated, do not comment
50 // current iteration stop conditions
51 if (this->should_finish()) return true;
52 if (this->should_break()) return true;
53 // but continue by default
57 bool RRTS::should_finish()
59 // decide finish conditions (maybe comment some lines)
60 //if (this->icnt_ > 999) return true;
61 if (this->scnt_ > 10) return true;
62 if (this->gf()) return true;
63 // but continue by default
67 bool RRTS::should_break()
69 // decide break conditions (maybe comment some lines)
70 //if (this->scnt_ - this->pcnt_ > 2) return true;
71 // but continue by default
75 bool RRTS::should_continue()
77 // decide the stop conditions (maybe comment some lines)
78 // it is exact opposite of `should_stop`
79 //if (this->icnt_ > 999) return false;
80 if (this->scnt_ > 10) return false;
81 if (this->gf()) return false;
82 // and reset pause counter if should continue
83 this->pcnt_ = this->scnt_;
87 void RRTS::store_node(RRTNode n)
89 this->nodes().push_back(n);
93 std::tuple<bool, unsigned int, unsigned int>
94 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
96 for (auto &o: this->obstacles())
97 if (std::get<0>(::collide(poly, o.poly())))
98 return ::collide(poly, o.poly());
99 return std::make_tuple(false, 0, 0);
102 std::tuple<bool, unsigned int, unsigned int>
103 RRTS::collide_steered_from(RRTNode &f)
105 auto fbc = BicycleCar();
109 std::vector<std::tuple<double, double>> s;
110 s.push_back(std::make_tuple(fbc.x(), fbc.y()));
111 for (auto &n: this->steered()) {
112 auto nbc = BicycleCar();
116 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
117 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
118 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
119 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
121 auto col = this->collide(s);
122 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
123 if (std::get<0>(col) && strip_from > 0) {
124 while (strip_from-- > 0) {
125 this->steered().pop_back();
127 return this->collide_steered_from(f);
132 std::tuple<bool, unsigned int, unsigned int>
133 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
135 auto fbc = BicycleCar();
139 auto tbc = BicycleCar();
143 std::vector<std::tuple<double, double>> p;
144 p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
145 p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
146 p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
147 p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
148 p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
149 p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
150 p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
151 p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
152 return this->collide(p);
155 double RRTS::cost_build(RRTNode &f, RRTNode &t)
158 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
162 double RRTS::cost_search(RRTNode &f, RRTNode &t)
165 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
174 switch (this->sample_dist_type()) {
176 x = this->udx_(this->gen_);
177 y = this->udy_(this->gen_);
178 h = this->udh_(this->gen_);
181 x = this->ndx_(this->gen_);
182 y = this->ndy_(this->gen_);
183 h = this->ndh_(this->gen_);
185 this->samples().push_back(RRTNode());
186 this->samples().back().x(x);
187 this->samples().back().y(y);
188 this->samples().back().h(h);
191 RRTNode *RRTS::nn(RRTNode &t)
193 RRTNode *nn = &this->nodes().front();
194 double cost = this->cost_search(*nn, t);
195 for (auto &f: this->nodes()) {
196 if (this->cost_search(f, t) < cost) {
198 cost = this->cost_search(f, t);
204 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
206 std::vector<RRTNode *> nv;
207 double cost = std::min(GAMMA(this->nodes().size()), ETA);
208 for (auto &f: this->nodes())
209 if (this->cost_search(f, t) < cost)
214 int cb_rs_steer(double q[4], void *user_data)
216 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
217 RRTNode *ln = nullptr;
218 if (nodes->size() > 0)
220 nodes->push_back(RRTNode());
221 nodes->back().x(q[0]);
222 nodes->back().y(q[1]);
223 nodes->back().h(q[2]);
224 nodes->back().sp(q[3]);
225 if (nodes->back().sp() == 0)
226 nodes->back().set_t(RRTNodeType::cusp);
227 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
228 ln->set_t(RRTNodeType::cusp);
232 void RRTS::steer(RRTNode &f, RRTNode &t)
234 this->steered().clear();
235 double q0[] = {f.x(), f.y(), f.h()};
236 double q1[] = {t.x(), t.y(), t.h()};
237 ReedsSheppStateSpace rsss(this->bc.mtr());
238 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
241 void RRTS::join_steered(RRTNode *f)
243 while (this->steered().size() > 0) {
244 this->store_node(this->steered().front());
245 RRTNode *t = &this->nodes().back();
247 t->c(this->cost_build(*f, *t));
248 this->steered().erase(this->steered().begin());
253 bool RRTS::goal_found(RRTNode &f)
255 auto &g = this->goals().front();
256 double cost = this->cost_build(f, g);
258 pow(f.x() - g.x(), 2)
259 + pow(f.y() - g.y(), 2)
261 double adist = std::abs(f.h() - g.h());
262 if (edist < 0.05 && adist < M_PI / 32) {
263 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
275 RRTNode *t = &this->steered().front();
276 RRTNode *f = this->nn(this->samples().back());
277 double cost = this->cost_search(*f, *t);
278 for (auto n: this->nv(*t)) {
280 !std::get<0>(this->collide_two_nodes(*n, *t))
281 && this->cost_search(*n, *t) < cost
284 cost = this->cost_search(*n, *t);
287 this->store_node(this->steered().front());
288 t = &this->nodes().back();
290 t->c(this->cost_build(*f, *t));
291 t->set_t(RRTNodeType::connected);
297 RRTNode *f = &this->nodes().back();
298 for (auto n: this->nv(*f)) {
300 !std::get<0>(this->collide_two_nodes(*f, *n))
301 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
304 n->c(this->cost_build(*f, *n));
316 this->nodes().clear();
317 this->samples().clear();
318 this->steered().clear();
319 this->store_node(RRTNode()); // root
326 std::vector<RRTNode *> RRTS::path()
328 std::vector<RRTNode *> path;
329 if (this->goals().size() == 0)
331 RRTNode *goal = &this->goals().front();
332 if (goal->p() == nullptr)
334 while (goal != nullptr) {
335 path.push_back(goal);
338 std::reverse(path.begin(), path.end());
344 if (this->icnt_ == 0)
345 this->tstart_ = std::chrono::high_resolution_clock::now();
347 if (this->should_stop())
349 if (this->samples().size() == 0) {
350 this->samples().push_back(RRTNode());
351 this->samples().back().x(this->goals().front().x());
352 this->samples().back().y(this->goals().front().y());
353 this->samples().back().h(this->goals().front().h());
358 *this->nn(this->samples().back()),
359 this->samples().back()
361 if (std::get<0>(this->collide_steered_from(
362 *this->nn(this->samples().back())
365 if (!this->connect())
368 unsigned scnt = this->steered().size();
369 this->join_steered(&this->nodes().back());
370 RRTNode *just_added = &this->nodes().back();
373 auto &g = this->goals().front();
374 this->steer(*just_added, g);
375 if (std::get<0>(this->collide_steered_from(
379 this->join_steered(just_added);
380 this->gf(this->goal_found(this->nodes().back()));
381 just_added = just_added->p();
386 void RRTS::set_sample_normal(
387 double mx, double dx,
388 double my, double dy,
392 this->ndx_ = std::normal_distribution<double>(mx, dx);
393 this->ndy_ = std::normal_distribution<double>(my, dy);
394 this->ndh_ = std::normal_distribution<double>(mh, dh);
396 void RRTS::set_sample_uniform(
397 double xmin, double xmax,
398 double ymin, double ymax,
399 double hmin, double hmax
402 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
403 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
404 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
406 void RRTS::set_sample(
407 double x1, double x2,
408 double y1, double y2,
412 switch (this->sample_dist_type()) {
414 x1 += this->nodes().front().x();
415 x2 += this->nodes().front().x();
416 y1 += this->nodes().front().y();
417 y2 += this->nodes().front().y();
418 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
421 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
425 Json::Value RRTS::json()
429 jvo["time"] = this->scnt();
432 jvo["iterations"] = this->icnt();
435 jvo["init"][0] = this->nodes().front().x();
436 jvo["init"][1] = this->nodes().front().y();
437 jvo["init"][2] = this->nodes().front().h();
440 if (this->path().size() > 0) {
441 jvo["cost"] = cc(*this->path().back());
442 jvo["goal"][0] = this->path().back()->x();
443 jvo["goal"][1] = this->path().back()->y();
444 jvo["goal"][2] = this->path().back()->h();
450 unsigned int pcnt = 0;
451 for (auto n: this->path()) {
452 jvo["path"][pcnt][0] = n->x();
453 jvo["path"][pcnt][1] = n->y();
454 jvo["path"][pcnt][2] = n->h();
455 if (n->t(RRTNodeType::cusp))
457 if (n->t(RRTNodeType::connected))
461 jvo["cusps-in-path"] = cu;
462 jvo["connecteds-in-path"] = co;
465 unsigned int gcnt = 0;
466 for (auto g: this->goals()) {
467 jvo["goals"][gcnt][0] = g.x();
468 jvo["goals"][gcnt][1] = g.y();
469 jvo["goals"][gcnt][2] = g.h();
474 unsigned int ocnt = 0;
475 for (auto o: this->obstacles()) {
476 unsigned int ccnt = 0;
477 for (auto c: o.poly()) {
478 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
479 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
486 jvo["nodes"] = (unsigned int) this->nodes().size();
489 // unsigned int ncnt = 0;
490 // for (auto n: this->nodes()) {
491 // jvo["nodes_x"][ncnt] = n.x();
492 // jvo["nodes_y"][ncnt] = n.y();
493 // //jvo["nodes_h"][ncnt] = n.h();
500 void RRTS::json(Json::Value jvi)
502 assert(jvi["init"] != Json::nullValue);
503 assert(jvi["goal"] != Json::nullValue);
504 assert(jvi["goals"] != Json::nullValue);
505 assert(jvi["obst"] != Json::nullValue);
507 this->nodes().front().x(jvi["init"][0].asDouble());
508 this->nodes().front().y(jvi["init"][1].asDouble());
509 this->nodes().front().h(jvi["init"][2].asDouble());
512 tmp_node.x(jvi["goal"][0].asDouble());
513 tmp_node.y(jvi["goal"][1].asDouble());
514 tmp_node.h(jvi["goal"][2].asDouble());
515 this->goals().push_back(tmp_node);
516 for (auto g: jvi["goals"]) {
517 tmp_node.x(g[0].asDouble());
518 tmp_node.y(g[1].asDouble());
519 tmp_node.h(g[2].asDouble());
520 this->goals().push_back(tmp_node);
524 Obstacle tmp_obstacle;
525 for (auto o: jvi["obst"]) {
526 tmp_obstacle.poly().clear();
528 double tmp_x = c[0].asDouble();
529 double tmp_y = c[1].asDouble();
530 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
531 tmp_obstacle.poly().push_back(tmp_tuple);
533 this->obstacles().push_back(tmp_obstacle);
537 double edist_init_goal = sqrt(
539 this->nodes().front().x()
540 - this->goals().front().x(),
544 this->nodes().front().y()
545 - this->goals().front().y(),
550 this->nodes().front().x(), edist_init_goal,
551 this->nodes().front().y(), edist_init_goal,
558 : gen_(std::random_device{}())
560 this->goals().reserve(100);
561 this->nodes().reserve(4000000);
562 this->samples().reserve(1000);
563 this->steered().reserve(20000);
564 this->store_node(RRTNode()); // root
567 double cc(RRTNode &t)
571 while (n != nullptr) {