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/>.
27 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
32 RRTNode *RRTBase::root()
37 RRTNode *RRTBase::goal()
42 std::vector<RRTNode *> &RRTBase::nodes()
47 std::vector<RRTNode *> &RRTBase::samples()
49 return this->samples_;
52 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
57 bool RRTBase::goal_found()
59 return this->goal_found_;
62 float RRTBase::elapsed()
64 std::chrono::duration<float> dt;
65 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
66 this->tend_ - this->tstart_);
70 bool RRTBase::tlog(std::vector<RRTNode *> t)
72 this->tlog_.push_back(t);
76 void RRTBase::tstart()
78 this->tstart_ = std::chrono::high_resolution_clock::now();
83 this->tend_ = std::chrono::high_resolution_clock::now();
86 bool RRTBase::link_obstacles(
87 std::vector<CircleObstacle> *cobstacles,
88 std::vector<SegmentObstacle> *sobstacles)
90 this->cobstacles_ = cobstacles;
91 this->sobstacles_ = sobstacles;
92 if (!this->cobstacles_ || !this->sobstacles_) {
98 bool RRTBase::goal_found(
100 float (*cost)(RRTNode *, RRTNode* ))
102 if (this->goal_found_)
104 float xx = pow(node->x() - this->goal_->x(), 2);
105 float yy = pow(node->y() - this->goal_->y(), 2);
106 float dh = std::abs(node->h() - this->goal_->h());
107 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
108 dh < this->GOAL_FOUND_ANGLE) {
109 node->add_child(this->goal_, (*cost)(node, this->goal_));
110 if (this->collide(node, this->goal_)) {
111 node->children().pop_back();
114 this->goal_found_ = true;
120 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
122 std::vector<RRTEdge *> edges;
124 while (tmp != init) {
125 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
126 for (auto &o: *this->cobstacles_) {
127 if (o.collide(tmp)) {
130 // TODO collide with car frame
132 for (auto &o: *this->sobstacles_) {
133 for (auto &e: bc.frame()) {
139 if (!tmp->parent()) {
142 edges.push_back(new RRTEdge(tmp, tmp->parent()));
145 for (auto &e: edges) {
146 for (auto &o: *this->cobstacles_) {
151 for (auto &o: *this->sobstacles_) {
160 std::vector<RRTNode *> RRTBase::findt()
162 std::vector<RRTNode *> nodes;
163 RRTNode *tmp = this->goal_;
164 while (tmp != this->root_) {
165 nodes.push_back(tmp);
170 nodes.push_back(this->root_);