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/>.
35 extern SDL_Window* gw;
36 extern SDL_GLContext gc;
40 pthread_mutex_init(&this->m_, NULL);
45 pthread_mutex_lock(&this->m_);
46 bool ret = this->changed_;
47 pthread_mutex_unlock(&this->m_);
51 std::vector<RRTNode *> Cell::nodes()
53 pthread_mutex_lock(&this->m_);
54 std::vector<RRTNode *> ret(this->nodes_);
55 pthread_mutex_unlock(&this->m_);
59 void Cell::add_node(RRTNode *n)
61 pthread_mutex_lock(&this->m_);
62 this->nodes_.push_back(n);
63 this->changed_ = true;
64 pthread_mutex_unlock(&this->m_);
69 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
70 // please uncommend the following code:
72 for (auto n: this->nodes_)
75 for (auto n: this->dnodes_)
76 if (n != this->root_ && n != this->goal_)
78 for (auto s: this->samples_)
81 for (auto edges: this->rlog_)
91 gen_(std::random_device{}())
93 this->nodes_.reserve(NOFNODES);
94 this->nodes_.push_back(this->root_);
95 this->add_iy(this->root_);
96 this->add_ixy(this->root_);
99 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
102 gen_(std::random_device{}())
104 this->nodes_.reserve(NOFNODES);
105 this->nodes_.push_back(init);
111 RRTNode *RRTBase::root()
116 RRTNode *RRTBase::goal()
121 std::vector<RRTNode *> &RRTBase::nodes()
126 std::vector<RRTNode *> &RRTBase::dnodes()
128 return this->dnodes_;
131 PolygonObstacle &RRTBase::frame()
136 std::vector<RRTNode *> &RRTBase::samples()
138 return this->samples_;
141 std::vector<CircleObstacle> *RRTBase::co()
143 return this->cobstacles_;
146 std::vector<SegmentObstacle> *RRTBase::so()
148 return this->sobstacles_;
151 std::vector<float> &RRTBase::clog()
156 std::vector<float> &RRTBase::nlog()
161 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
166 std::vector<float> &RRTBase::slog()
171 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
176 std::vector<RRTNode *> &RRTBase::slot_cusp()
178 return this->slot_cusp_;
181 bool RRTBase::goal_found()
183 return this->goal_found_;
186 float RRTBase::elapsed()
188 std::chrono::duration<float> dt;
189 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
190 this->tend_ - this->tstart_);
195 void RRTBase::root(RRTNode *node)
200 void RRTBase::goal(RRTNode *node)
205 bool RRTBase::logr(RRTNode *root)
207 std::vector<RRTEdge *> e; // Edges to log
208 std::vector<RRTNode *> s; // DFS stack
209 std::vector<RRTNode *> r; // reset visited_
212 while (s.size() > 0) {
217 for (auto ch: tmp->children()) {
219 e.push_back(new RRTEdge(tmp, ch));
225 this->rlog_.push_back(e);
229 float RRTBase::ocost(RRTNode *n)
232 for (auto o: *this->cobstacles_)
233 if (o.dist_to(n) < dist)
235 for (auto o: *this->sobstacles_)
236 if (o.dist_to(n) < dist)
238 return n->ocost(dist);
241 bool RRTBase::tlog(std::vector<RRTNode *> t)
244 this->slog_.push_back(this->elapsed());
245 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
246 this->nlog_.push_back(this->nodes_.size());
247 this->tlog_.push_back(t);
254 void RRTBase::tstart()
256 this->tstart_ = std::chrono::high_resolution_clock::now();
261 this->tend_ = std::chrono::high_resolution_clock::now();
264 bool RRTBase::link_obstacles(
265 std::vector<CircleObstacle> *cobstacles,
266 std::vector<SegmentObstacle> *sobstacles)
268 this->cobstacles_ = cobstacles;
269 this->sobstacles_ = sobstacles;
270 if (!this->cobstacles_ || !this->sobstacles_) {
276 bool RRTBase::add_iy(RRTNode *n)
283 this->iy_[i].push_back(n);
287 bool RRTBase::add_ixy(RRTNode *n)
289 int ix = IXI(n->x());
294 int iy = IYI(n->y());
299 this->ixy_[ix][iy].add_node(n);
303 bool RRTBase::goal_found(bool f)
305 this->goal_found_ = f;
309 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
311 for (unsigned int i = 0; i < sc.size() - 1; i++)
312 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
313 sc[0]->parent(this->goal());
314 this->slot_cusp_ = sc;
318 bool RRTBase::glplot()
321 glClear(GL_COLOR_BUFFER_BIT);
326 for (auto o: *this->sobstacles_) {
328 glVertex2f(GLVERTEX(o.init()));
329 glVertex2f(GLVERTEX(o.goal()));
336 glVertex2f(GLVERTEX(this->root_));
337 glVertex2f(GLVERTEX(this->goal_));
340 if (this->samples_.size() > 0) {
344 glVertex2f(GLVERTEX(this->samples_.back()));
348 std::vector<RRTNode *> s; // DFS stack
349 std::vector<RRTNode *> r; // reset visited_
352 s.push_back(this->root_);
353 while (s.size() > 0) {
358 for (auto ch: tmp->children()) {
360 glColor3f(0.5, 0.5, 0.5);
361 glVertex2f(GLVERTEX(tmp));
362 glVertex2f(GLVERTEX(ch));
367 // Plot nodes (from goal)
369 s.push_back(this->goal_);
370 while (s.size() > 0) {
375 for (auto ch: tmp->children()) {
377 glColor3f(0.5, 0.5, 0.5);
378 glVertex2f(GLVERTEX(tmp));
379 glVertex2f(GLVERTEX(ch));
384 std::vector<RRTNode *> cusps;
385 // Plot last trajectory
386 if (this->tlog().size() > 0) {
389 for (auto n: this->tlog().back()) {
392 glVertex2f(GLVERTEX(n));
393 glVertex2f(GLVERTEX(n->parent()));
394 if (sgn(n->s()) != sgn(n->parent()->s()))
403 for (auto n: cusps) {
405 glVertex2f(GLVERTEX(n));
408 SDL_GL_SwapWindow(gw);
415 bool RRTBase::goal_found(
417 float (*cost)(RRTNode *, RRTNode* ))
419 if (IS_NEAR(node, this->goal_)) {
420 if (this->goal_found_) {
421 if (node->ccost() + this->cost(node, this->goal_) <
422 this->goal_->ccost()) {
423 RRTNode *op; // old parent
424 float oc; // old cumulative cost
425 float od; // old direct cost
426 op = this->goal_->parent();
427 oc = this->goal_->ccost();
428 od = this->goal_->dcost();
429 node->add_child(this->goal_,
430 this->cost(node, this->goal_));
431 if (this->collide(node, this->goal_)) {
432 node->children().pop_back();
433 this->goal_->parent(op);
434 this->goal_->ccost(oc);
435 this->goal_->dcost(od);
437 op->rem_child(this->goal_);
446 this->cost(node, this->goal_));
447 if (this->collide(node, this->goal_)) {
448 node->children().pop_back();
449 this->goal_->remove_parent();
452 this->goal_found_ = true;
459 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
461 std::vector<RRTEdge *> edges;
463 volatile bool col = false;
465 while (tmp != init) {
466 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
467 std::vector<RRTEdge *> bcframe = bc.frame();
468 #pragma omp parallel for reduction(|: col)
469 for (i = 0; i < (*this->cobstacles_).size(); i++) {
470 if ((*this->cobstacles_)[i].collide(tmp)) {
473 for (auto &e: bcframe) {
474 if ((*this->cobstacles_)[i].collide(e)) {
480 for (auto e: bcframe) {
485 for (auto e: edges) {
490 #pragma omp parallel for reduction(|: col)
491 for (i = 0; i < (*this->sobstacles_).size(); i++) {
492 for (auto &e: bcframe) {
493 if ((*this->sobstacles_)[i].collide(e)) {
499 for (auto e: bcframe) {
504 for (auto e: edges) {
509 if (!tmp->parent()) {
512 edges.push_back(new RRTEdge(tmp, tmp->parent()));
514 for (auto e: bcframe) {
520 for (auto &e: edges) {
521 #pragma omp parallel for reduction(|: col)
522 for (i = 0; i < (*this->cobstacles_).size(); i++) {
523 if ((*this->cobstacles_)[i].collide(e)) {
528 for (auto e: edges) {
533 #pragma omp parallel for reduction(|: col)
534 for (i = 0; i < (*this->sobstacles_).size(); i++) {
535 if ((*this->sobstacles_)[i].collide(e)) {
540 for (auto e: edges) {
546 for (auto e: edges) {
552 class RRTNodeDijkstra {
554 RRTNodeDijkstra(int i):
560 RRTNodeDijkstra(int i, float c):
566 RRTNodeDijkstra(int i, int p, float c):
585 class RRTNodeDijkstraComparator {
588 const RRTNodeDijkstra& n1,
589 const RRTNodeDijkstra& n2)
595 bool RRTBase::optp_dijkstra(
596 std::vector<RRTNode *> &cusps,
597 std::vector<int> &npi)
599 std::vector<RRTNodeDijkstra> dnodes;
600 for (unsigned int i = 0; i < cusps.size(); i++)
602 dnodes.push_back(RRTNodeDijkstra(
607 dnodes.push_back(RRTNodeDijkstra(
613 std::vector<RRTNodeDijkstra>,
614 RRTNodeDijkstraComparator> pq;
615 RRTNodeDijkstra tmp = dnodes[0];
617 float ch_cost = 9999;
618 std::vector<RRTNode *> steered;
619 while (!pq.empty()) {
622 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
623 ch_cost = dnodes[tmp.ni].c +
624 this->cost(cusps[tmp.ni], cusps[i]);
625 steered = this->steer(cusps[tmp.ni], cusps[i]);
626 for (unsigned int j = 0; j < steered.size() - 1; j++)
627 steered[j]->add_child(steered[j + 1], 1);
630 steered[steered.size() - 1])) {
631 for (auto n: steered)
635 if (ch_cost < dnodes[i].c) {
636 dnodes[i].c = ch_cost;
637 dnodes[i].pi = tmp.ni;
641 for (auto n: steered)
645 unsigned int tmpi = 0;
646 for (auto n: dnodes) {
647 if (n.v && n.ni > tmpi)
652 tmpi = dnodes[tmpi].pi;
655 std::reverse(npi.begin(), npi.end());
659 bool RRTBase::optp_rrp(
660 std::vector<RRTNode *> &cusps,
661 std::vector<int> &npi)
663 std::vector<RRTNode *> steered;
664 std::vector<int> candidates;
665 RRTNode *x_j = nullptr;
666 RRTNode *x_i = nullptr;
667 int j = cusps.size() - 1;
681 for (int i = 0; i < j; i++) {
682 steered = this->steer(cusps[i], x_j);
683 for (unsigned int k = 0; k < steered.size() - 1; k++)
684 steered[k]->add_child(steered[k + 1], 1);
687 steered[steered.size() - 1]))
688 candidates.push_back(i);
690 if (candidates.size() <= 0)
692 i_min = candidates[0];
695 for (auto c: candidates) {
697 dx = x_j->x() - x_i->x();
698 dy = x_j->y() - x_i->y();
699 ed = EDIST(x_i, x_j);
700 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
701 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
712 std::reverse(npi.begin(), npi.end());
716 bool RRTBase::optp_smart(
717 std::vector<RRTNode *> &cusps,
718 std::vector<int> &npi)
720 std::vector<RRTNode *> steered;
721 int li = cusps.size() - 1;
725 steered = this->steer(cusps[ai - 1], cusps[li]);
726 for (unsigned int j = 0; j < steered.size() - 1; j++)
727 steered[j]->add_child(steered[j + 1], 1);
728 if (this->collide(steered[0], steered[steered.size() - 1])) {
733 for (auto n: steered)
737 std::reverse(npi.begin(), npi.end());
741 bool RRTBase::opt_path()
743 if (this->tlog().size() == 0)
745 float oc = this->tlog().back().front()->ccost();
746 std::vector<RRTNode *> tmp_cusps;
747 for (auto n: this->tlog().back()) {
748 if (sgn(n->s()) == 0) {
749 tmp_cusps.push_back(n);
750 } else if (n->parent() &&
751 sgn(n->s()) != sgn(n->parent()->s())) {
752 tmp_cusps.push_back(n);
753 tmp_cusps.push_back(n->parent());
755 //tmp_cusps.push_back(n);
757 if (tmp_cusps.size() < 2)
759 std::vector<RRTNode *> cusps;
760 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
761 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
762 cusps.push_back(tmp_cusps[i]);
764 std::reverse(cusps.begin(), cusps.end());
765 std::vector<int> npi; // new path indexes
766 if (!this->optp_dijkstra(cusps, npi))
768 RRTNode *pn = cusps[npi[0]];
769 RRTNode *tmp = nullptr;
771 for (unsigned int i = 0; i < npi.size() - 1; i++) {
773 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
776 } else if (IS_NEAR(cusps[npi[i]], ns)) {
778 while (tmp && tmp != cusps[npi[i]]) {
784 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
786 cusps[npi[i + 1]]->parent()->rem_child(
790 this->cost(pn, cusps[npi[i + 1]]));
792 } else if (IS_NEAR(pn, ns)) {
795 this->nodes().push_back(ns);
798 pn->add_child(ns, this->cost(pn, ns));
803 this->root()->update_ccost();
804 if (this->tlog().back().front()->ccost() < oc)
809 bool RRTBase::rebase(RRTNode *nr)
811 if (!nr || this->goal_ == nr || this->root_ == nr)
813 std::vector<RRTNode *> s; // DFS stack
816 unsigned int to_del = 0;
818 s.push_back(this->root_);
819 while (s.size() > 0) {
822 for (auto ch: tmp->children()) {
826 to_del = this->nodes_.size();
827 #pragma omp parallel for reduction(min: to_del)
828 for (i = 0; i < this->nodes_.size(); i++) {
829 if (this->nodes_[i] == tmp)
832 if (to_del < this->nodes_.size())
833 this->nodes_.erase(this->nodes_.begin() + to_del);
835 to_del = this->iy_[iy].size();
836 #pragma omp parallel for reduction(min: to_del)
837 for (i = 0; i < this->iy_[iy].size(); i++) {
838 if (this->iy_[iy][i] == tmp)
841 if (to_del < this->iy_[iy].size())
842 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
843 this->dnodes().push_back(tmp);
846 this->root_->remove_parent();
850 std::vector<RRTNode *> RRTBase::findt()
852 return this->findt(this->goal_);
855 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
857 std::vector<RRTNode *> nodes;
858 if (!n || !n->parent())
868 RRTNode *RRTBase::sample()
870 float x = this->samplingInfo_.x;
871 float y = this->samplingInfo_.y;
872 std::normal_distribution<float> xdist(
876 x += this->samplingInfo_.dx
877 * (BCAR_WIDTH / 2 + std::abs(xdist(this->gen_)))
878 * cos(this->samplingInfo_.sh + M_PI / 2);
879 y += this->samplingInfo_.dx
880 * (BCAR_WIDTH / 2 + std::abs(xdist(this->gen_)))
881 * sin(this->samplingInfo_.sh + M_PI / 2);
882 std::normal_distribution<float> ydist(
886 x += ydist(this->gen_) * cos(this->samplingInfo_.sh);
887 y += ydist(this->gen_) * sin(this->samplingInfo_.sh);
888 std::normal_distribution<float> hdist(
890 this->samplingInfo_.h / 3
892 float h = std::abs(hdist(this->gen_));
896 this->samplingInfo_.sh + this->samplingInfo_.dh * h
900 float RRTBase::cost(RRTNode *init, RRTNode *goal)
902 return co2(init, goal);
905 RRTNode *RRTBase::nn(RRTNode *rs)
907 return nn4(this->iy_, rs, nullptr);
908 //return nn3(this->iy_, rs, nullptr);
911 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
913 std::vector<RRTNode *> nvs;
914 unsigned int iy = IYI(node->y());
915 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
916 unsigned int i = 0; // vector index
917 unsigned int j = 0; // array index
918 unsigned int jmin = 0; // minimal j index
919 unsigned int jmax = 0; // maximal j index
921 jmin = (jmin > 0) ? jmin : 0;
922 jmax = iy + iy_dist + 1;
923 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
924 #pragma omp parallel for reduction(merge: nvs)
925 for (j = jmin; j < jmax; j++) {
926 #pragma omp parallel for reduction(merge: nvs)
927 for (i = 0; i < this->iy_[j].size(); i++) {
928 if (this->cost(this->iy_[j][i], node) < dist) {
929 nvs.push_back(this->iy_[j][i]);
936 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
938 return st3(init, goal);
941 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
943 return st3(init, goal, step);