2 This file is part of I am car.
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.
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.
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/>.
40 extern SDL_Window* gw;
41 extern SDL_GLContext gc;
46 pthread_mutex_init(&this->m_, NULL);
51 pthread_mutex_lock(&this->m_);
52 bool ret = this->changed_;
53 pthread_mutex_unlock(&this->m_);
57 std::vector<RRTNode *> Cell::nodes()
59 pthread_mutex_lock(&this->m_);
60 std::vector<RRTNode *> ret(this->nodes_);
61 pthread_mutex_unlock(&this->m_);
65 void Cell::add_node(RRTNode *n)
67 pthread_mutex_lock(&this->m_);
68 this->nodes_.push_back(n);
69 this->changed_ = true;
70 pthread_mutex_unlock(&this->m_);
75 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
76 // please uncommend the following code:
78 for (auto n: this->nodes_)
81 for (auto n: this->dnodes_)
82 if (n != this->root_ && n != this->goal_)
84 for (auto s: this->samples_)
87 for (auto edges: this->rlog_)
95 : root_(new RRTNode())
96 , goal_(new RRTNode())
97 , gen_(std::random_device{}())
98 , ndx_(HMAX - HMIN, (HMAX - HMIN) / 4)
99 , ndy_(VMAX - VMIN, (VMAX - VMIN) / 4)
100 , ndh_(0, M_PI * 2 / 4)
102 this->nodes_.reserve(NOFNODES);
103 this->nodes_.push_back(this->root_);
104 this->add_iy(this->root_);
105 this->add_ixy(this->root_);
108 RRTBase::RRTBase(RRTNode *init, RRTNode *goal)
111 , gen_(std::random_device{}())
112 , ndx_(HMIN + (HMAX - HMIN) / 2, (HMAX - HMIN) / 4)
113 , ndy_(VMIN + (VMAX - VMIN) / 2, (VMAX - VMIN) / 4)
114 , ndh_(0, M_PI * 2 / 4)
116 this->nodes_.reserve(NOFNODES);
117 this->nodes_.push_back(init);
123 RRTNode *RRTBase::root()
128 RRTNode *RRTBase::goal()
133 std::vector<RRTNode *> &RRTBase::goals()
138 std::vector<RRTNode *> &RRTBase::nodes()
143 std::vector<RRTNode *> &RRTBase::dnodes()
145 return this->dnodes_;
148 std::queue<RRTNode *> &RRTBase::firsts()
150 return this->firsts_;
153 PolygonObstacle &RRTBase::frame()
158 std::vector<RRTNode *> &RRTBase::samples()
160 return this->samples_;
163 std::vector<CircleObstacle> *RRTBase::co()
165 return this->cobstacles_;
168 std::vector<SegmentObstacle> *RRTBase::so()
170 return this->sobstacles_;
173 std::vector<float> &RRTBase::clog()
178 std::vector<float> &RRTBase::nlog()
183 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
188 std::vector<float> &RRTBase::slog()
193 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
198 std::vector<RRTNode *> &RRTBase::slot_cusp()
200 return this->slot_cusp_;
203 bool RRTBase::goal_found()
205 return this->goal_found_;
208 float RRTBase::elapsed()
210 std::chrono::duration<float> dt;
211 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
212 this->tend_ - this->tstart_);
216 std::vector<RRTNode *> RRTBase::traj_cusp()
218 std::vector<RRTNode *> tmp_cusps;
219 for (auto n: this->tlog().back()) {
220 if (sgn(n->s()) == 0) {
221 tmp_cusps.push_back(n);
222 } else if (n->parent() &&
223 sgn(n->s()) != sgn(n->parent()->s())) {
224 tmp_cusps.push_back(n);
225 tmp_cusps.push_back(n->parent());
228 std::vector<RRTNode *> cusps;
229 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
230 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
231 cusps.push_back(tmp_cusps[i]);
237 void RRTBase::root(RRTNode *node)
242 void RRTBase::goal(RRTNode *node)
247 void RRTBase::goals(std::vector<RRTNode *> g)
250 std::reverse(this->goals_.begin(), this->goals_.end());
251 RRTNode *pn = this->goals_.front();
252 for (auto n: this->goals_) {
254 pn->add_child(n, this->cost(pn ,n));
260 bool RRTBase::logr(RRTNode *root)
262 std::vector<RRTEdge *> e; // Edges to log
263 std::vector<RRTNode *> s; // DFS stack
264 std::vector<RRTNode *> r; // reset visited_
267 while (s.size() > 0) {
272 for (auto ch: tmp->children()) {
274 e.push_back(new RRTEdge(tmp, ch));
280 this->rlog_.push_back(e);
284 float RRTBase::ocost(RRTNode *n)
287 for (auto o: *this->cobstacles_)
288 if (o.dist_to(n) < dist)
290 for (auto o: *this->sobstacles_)
291 if (o.dist_to(n) < dist)
293 return n->ocost(dist);
296 bool RRTBase::tlog(std::vector<RRTNode *> t)
299 this->slog_.push_back(this->elapsed());
300 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
301 this->nlog_.push_back(this->nodes_.size());
302 this->tlog_.push_back(t);
309 void RRTBase::tstart()
311 this->tstart_ = std::chrono::high_resolution_clock::now();
316 this->tend_ = std::chrono::high_resolution_clock::now();
319 bool RRTBase::link_obstacles(
320 std::vector<CircleObstacle> *cobstacles,
321 std::vector<SegmentObstacle> *sobstacles)
323 this->cobstacles_ = cobstacles;
324 this->sobstacles_ = sobstacles;
325 if (!this->cobstacles_ || !this->sobstacles_) {
331 bool RRTBase::add_iy(RRTNode *n)
338 this->iy_[i].push_back(n);
342 bool RRTBase::add_ixy(RRTNode *n)
344 int ix = this->XI(n);
349 int iy = this->YI(n);
354 this->ixy_[ix][iy].add_node(n);
358 bool RRTBase::goal_found(bool f)
360 this->goal_found_ = f;
364 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
366 for (unsigned int i = 0; i < sc.size() - 1; i++)
367 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
368 sc[0]->parent(this->goal());
369 this->slot_cusp_ = sc;
373 bool RRTBase::glplot()
376 glClear(GL_COLOR_BUFFER_BIT);
381 for (auto o: *this->sobstacles_) {
383 glVertex2f(GLVERTEX(o.init()));
384 glVertex2f(GLVERTEX(o.goal()));
391 glVertex2f(GLVERTEX(this->root_));
392 glVertex2f(GLVERTEX(this->goal_));
395 if (this->samples_.size() > 0) {
399 glVertex2f(GLVERTEX(this->samples_.back()));
402 // Plot nodes (position, orientation
403 std::vector<RRTNode *> s; // DFS stack
404 std::vector<RRTNode *> r; // reset visited_
407 s.push_back(this->root_);
408 while (s.size() > 0) {
413 for (auto ch: tmp->children()) {
415 glColor3f(0.5, 0.5, 0.5);
416 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
418 bc.lfx() * GLPLWSCALE,
419 bc.lfy() * GLPLHSCALE
422 bc.lrx() * GLPLWSCALE,
423 bc.lry() * GLPLHSCALE
426 bc.lrx() * GLPLWSCALE,
427 bc.lry() * GLPLHSCALE
430 bc.rrx() * GLPLWSCALE,
431 bc.rry() * GLPLHSCALE
434 bc.rrx() * GLPLWSCALE,
435 bc.rry() * GLPLHSCALE
438 bc.rfx() * GLPLWSCALE,
439 bc.rfy() * GLPLHSCALE
447 s.push_back(this->root_);
448 while (s.size() > 0) {
453 for (auto ch: tmp->children()) {
455 glColor3f(0.5, 0.5, 0.5);
456 glVertex2f(GLVERTEX(tmp));
457 glVertex2f(GLVERTEX(ch));
462 // Plot nodes (from goal)
464 s.push_back(this->goal_);
465 while (s.size() > 0) {
470 for (auto ch: tmp->children()) {
472 glColor3f(0.5, 0.5, 0.5);
473 glVertex2f(GLVERTEX(tmp));
474 glVertex2f(GLVERTEX(ch));
479 std::vector<RRTNode *> cusps;
480 // Plot last trajectory
481 if (this->tlog().size() > 0) {
484 for (auto n: this->tlog().back()) {
487 glVertex2f(GLVERTEX(n));
488 glVertex2f(GLVERTEX(n->parent()));
489 if (sgn(n->s()) != sgn(n->parent()->s()))
498 for (auto n: cusps) {
500 glVertex2f(GLVERTEX(n));
503 SDL_GL_SwapWindow(gw);
510 bool RRTBase::goal_found(
512 float (*cost)(RRTNode *, RRTNode* ))
514 if (GOAL_IS_NEAR(node, this->goal_)) {
515 if (this->goal_found_) {
516 if (node->ccost() + this->cost(node, this->goal_) <
517 this->goal_->ccost()) {
518 RRTNode *op; // old parent
519 float oc; // old cumulative cost
520 float od; // old direct cost
521 op = this->goal_->parent();
522 oc = this->goal_->ccost();
523 od = this->goal_->dcost();
524 node->add_child(this->goal_,
525 this->cost(node, this->goal_));
526 if (this->collide(node, this->goal_)) {
527 node->children().pop_back();
528 this->goal_->parent(op);
529 this->goal_->ccost(oc);
530 this->goal_->dcost(od);
532 op->rem_child(this->goal_);
541 this->cost(node, this->goal_));
542 if (this->collide(node, this->goal_)) {
543 node->children().pop_back();
544 this->goal_->remove_parent();
547 this->goal_found_ = true;
548 // Update ccost of goal's parents
549 if (this->goals().size() > 0) {
550 RRTNode *ch = this->goals().back();
551 RRTNode *pn = this->goals().back()->parent();
567 bool RRTBase::goal_found(
572 if (GOAL_IS_NEAR(node, goal)) {
573 if (this->goal_found_) {
576 && node->ccost() + this->cost(node, goal)
579 RRTNode *op; // old parent
580 float oc; // old cumulative cost
581 float od; // old direct cost
585 node->add_child(goal,
586 this->cost(node, goal));
587 if (this->collide(node, goal)) {
588 node->children().pop_back();
602 this->cost(node, goal)
604 if (this->collide(node, goal)) {
605 node->children().pop_back();
606 goal->remove_parent();
609 this->goal_found_ = true;
610 // Update ccost of goal's children
611 goal->update_ccost();
612 // Update ccost of goals
613 for (auto g: this->goals()) {
624 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
626 std::vector<RRTEdge *> edges;
628 volatile bool col = false;
630 while (tmp != init) {
631 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
632 std::vector<RRTEdge *> bcframe = bc.frame();
633 #pragma omp parallel for reduction(|: col)
634 for (i = 0; i < (*this->cobstacles_).size(); i++) {
635 if ((*this->cobstacles_)[i].collide(tmp)) {
638 for (auto &e: bcframe) {
639 if ((*this->cobstacles_)[i].collide(e)) {
645 for (auto e: bcframe) {
650 for (auto e: edges) {
655 #pragma omp parallel for reduction(|: col)
656 for (i = 0; i < (*this->sobstacles_).size(); i++) {
657 for (auto &e: bcframe) {
658 if ((*this->sobstacles_)[i].collide(e)) {
664 for (auto e: bcframe) {
669 for (auto e: edges) {
674 if (!tmp->parent()) {
677 edges.push_back(new RRTEdge(tmp, tmp->parent()));
679 for (auto e: bcframe) {
685 for (auto &e: edges) {
686 #pragma omp parallel for reduction(|: col)
687 for (i = 0; i < (*this->cobstacles_).size(); i++) {
688 if ((*this->cobstacles_)[i].collide(e)) {
693 for (auto e: edges) {
698 #pragma omp parallel for reduction(|: col)
699 for (i = 0; i < (*this->sobstacles_).size(); i++) {
700 if ((*this->sobstacles_)[i].collide(e)) {
705 for (auto e: edges) {
711 for (auto e: edges) {
717 class RRTNodeDijkstra {
719 RRTNodeDijkstra(int i):
725 RRTNodeDijkstra(int i, float c):
731 RRTNodeDijkstra(int i, int p, float c):
750 class RRTNodeDijkstraComparator {
753 const RRTNodeDijkstra& n1,
754 const RRTNodeDijkstra& n2)
760 bool RRTBase::optp_dijkstra(
761 std::vector<RRTNode *> &cusps,
762 std::vector<int> &npi)
764 std::vector<RRTNodeDijkstra> dnodes;
765 for (unsigned int i = 0; i < cusps.size(); i++)
767 dnodes.push_back(RRTNodeDijkstra(
772 dnodes.push_back(RRTNodeDijkstra(
778 std::vector<RRTNodeDijkstra>,
779 RRTNodeDijkstraComparator> pq;
780 RRTNodeDijkstra tmp = dnodes[0];
782 float ch_cost = 9999;
783 std::vector<RRTNode *> steered;
784 while (!pq.empty()) {
787 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
788 ch_cost = dnodes[tmp.ni].c +
789 this->cost(cusps[tmp.ni], cusps[i]);
790 steered = this->steer(cusps[tmp.ni], cusps[i]);
791 if (steered.size() <= 0)
793 for (unsigned int j = 0; j < steered.size() - 1; j++)
794 steered[j]->add_child(steered[j + 1], 1);
797 steered[steered.size() - 1])) {
798 for (auto n: steered)
802 if (ch_cost < dnodes[i].c) {
803 dnodes[i].c = ch_cost;
804 dnodes[i].pi = tmp.ni;
808 for (auto n: steered)
812 unsigned int tmpi = 0;
813 for (auto n: dnodes) {
814 if (n.v && n.ni > tmpi)
819 tmpi = dnodes[tmpi].pi;
822 std::reverse(npi.begin(), npi.end());
826 bool RRTBase::optp_rrp(
827 std::vector<RRTNode *> &cusps,
828 std::vector<int> &npi)
830 std::vector<RRTNode *> steered;
831 std::vector<int> candidates;
832 RRTNode *x_j = nullptr;
833 RRTNode *x_i = nullptr;
834 int j = cusps.size() - 1;
848 for (int i = 0; i < j; i++) {
849 steered = this->steer(cusps[i], x_j);
850 for (unsigned int k = 0; k < steered.size() - 1; k++)
851 steered[k]->add_child(steered[k + 1], 1);
854 steered[steered.size() - 1]))
855 candidates.push_back(i);
857 if (candidates.size() <= 0)
859 i_min = candidates[0];
862 for (auto c: candidates) {
864 dx = x_j->x() - x_i->x();
865 dy = x_j->y() - x_i->y();
866 ed = EDIST(x_i, x_j);
867 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
868 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
879 std::reverse(npi.begin(), npi.end());
883 bool RRTBase::optp_smart(
884 std::vector<RRTNode *> &cusps,
885 std::vector<int> &npi)
887 std::vector<RRTNode *> steered;
888 int li = cusps.size() - 1;
892 steered = this->steer(cusps[ai - 1], cusps[li]);
893 for (unsigned int j = 0; j < steered.size() - 1; j++)
894 steered[j]->add_child(steered[j + 1], 1);
895 if (this->collide(steered[0], steered[steered.size() - 1])) {
900 for (auto n: steered)
904 std::reverse(npi.begin(), npi.end());
908 bool RRTBase::opt_path()
910 if (this->tlog().size() == 0)
912 float oc = this->tlog().back().front()->ccost();
913 std::vector<RRTNode *> tmp_cusps;
914 for (auto n: this->tlog().back()) {
915 if (sgn(n->s()) == 0) {
916 tmp_cusps.push_back(n);
917 } else if (n->parent() &&
918 sgn(n->s()) != sgn(n->parent()->s())) {
919 tmp_cusps.push_back(n);
920 tmp_cusps.push_back(n->parent());
922 //tmp_cusps.push_back(n);
924 if (tmp_cusps.size() < 2)
926 std::vector<RRTNode *> cusps;
927 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
930 tmp_cusps[(i + 1) % tmp_cusps.size()]
932 cusps.push_back(tmp_cusps[i]);
934 std::reverse(cusps.begin(), cusps.end());
935 std::vector<int> npi; // new path indexes
936 if (!this->optp_dijkstra(cusps, npi))
938 RRTNode *pn = cusps[npi[0]];
939 RRTNode *tmp = nullptr;
941 for (unsigned int i = 0; i < npi.size() - 1; i++) {
943 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
946 } else if (IS_NEAR(cusps[npi[i]], ns)) {
948 while (tmp && tmp != cusps[npi[i]]) {
954 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
956 cusps[npi[i + 1]]->parent()->rem_child(
960 this->cost(pn, cusps[npi[i + 1]]));
962 } else if (IS_NEAR(pn, ns)) {
965 this->nodes().push_back(ns);
968 pn->add_child(ns, this->cost(pn, ns));
973 this->root()->update_ccost();
974 if (this->tlog().back().front()->ccost() < oc)
979 bool RRTBase::rebase(RRTNode *nr)
981 if (!nr || this->goal_ == nr || this->root_ == nr)
983 std::vector<RRTNode *> s; // DFS stack
986 unsigned int to_del = 0;
988 s.push_back(this->root_);
989 while (s.size() > 0) {
992 for (auto ch: tmp->children()) {
996 to_del = this->nodes_.size();
997 #pragma omp parallel for reduction(min: to_del)
998 for (i = 0; i < this->nodes_.size(); i++) {
999 if (this->nodes_[i] == tmp)
1002 if (to_del < this->nodes_.size())
1003 this->nodes_.erase(this->nodes_.begin() + to_del);
1005 to_del = this->iy_[iy].size();
1006 #pragma omp parallel for reduction(min: to_del)
1007 for (i = 0; i < this->iy_[iy].size(); i++) {
1008 if (this->iy_[iy][i] == tmp)
1011 if (to_del < this->iy_[iy].size())
1012 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
1013 this->dnodes().push_back(tmp);
1016 this->root_->remove_parent();
1020 std::vector<RRTNode *> RRTBase::findt()
1022 return this->findt(this->goal_);
1025 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
1027 std::vector<RRTNode *> nodes;
1028 if (!n || !n->parent())
1037 int RRTBase::XI(RRTNode *n)
1039 float step = (this->HMAX - this->HMIN) / IXSIZE;
1040 float index = (int) (floor(n->x() - this->HMIN) / step);
1044 int RRTBase::YI(RRTNode *n)
1046 float step = (this->VMAX - this->VMIN) / IYSIZE;
1047 float index = (int) (floor(n->y() - this->VMIN) / step);
1052 void RRTBase::setSamplingInfo(SamplingInfo si)
1054 this->ndx_ = std::normal_distribution<float>(si.x0, si.x);
1055 this->ndy_ = std::normal_distribution<float>(si.y0, si.y);
1056 this->ndh_ = std::normal_distribution<float>(si.h0, si.h);
1059 RRTNode *RRTBase::sample()
1061 float x = this->ndx_(this->gen_);
1062 float y = this->ndy_(this->gen_);
1063 float h = this->ndh_(this->gen_);
1064 return new RRTNode(x, y, h);
1067 float RRTBase::cost(RRTNode *init, RRTNode *goal)
1069 return co2(init, goal);
1072 RRTNode *RRTBase::nn(RRTNode *rs)
1074 int iy = this->YI(rs);
1078 unsigned int i = 0; // vector step
1079 unsigned int j = 0; // array step
1081 while (nn.mc > j * IYSTEP) {
1082 iyj = (int) (iy + j);
1085 #pragma omp parallel for reduction(minn: nn)
1086 for (i = 0; i < this->iy_[iyj].size(); i++) {
1087 if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
1088 nn.mc = EDIST(this->iy_[iyj][i], rs);
1089 nn.nn = this->iy_[iyj][i];
1093 iyj = (int) (iy - j);
1096 #pragma omp parallel for reduction(minn: nn)
1097 for (i = 0; i < this->iy_[iyj].size(); i++) {
1098 if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
1099 nn.mc = EDIST(this->iy_[iyj][i], rs);
1100 nn.nn = this->iy_[iyj][i];
1109 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
1111 std::vector<RRTNode *> nvs;
1112 unsigned int iy = this->YI(node);
1113 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
1114 unsigned int i = 0; // vector index
1115 unsigned int j = 0; // array index
1116 unsigned int jmin = 0; // minimal j index
1117 unsigned int jmax = 0; // maximal j index
1118 jmin = iy - iy_dist;
1119 jmin = (jmin > 0) ? jmin : 0;
1120 jmax = iy + iy_dist + 1;
1121 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
1122 #pragma omp parallel for reduction(merge: nvs)
1123 for (j = jmin; j < jmax; j++) {
1124 #pragma omp parallel for reduction(merge: nvs)
1125 for (i = 0; i < this->iy_[j].size(); i++) {
1126 if (this->cost(this->iy_[j][i], node) < dist) {
1127 nvs.push_back(this->iy_[j][i]);
1134 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
1136 return st3(init, goal);
1139 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
1141 return st3(init, goal, step);