4 #include "reeds_shepp.h"
6 #define ETA 1.0 // for steer, nv
8 __typeof__ (cV) _cV = (cV); \
9 pow(log(_cV) / _cV, 1.0 / 3.0); \
16 RRTNode::RRTNode(const BicycleCar &bc) : BicycleCar(bc)
24 double RRTS::elapsed()
26 std::chrono::duration<double> dt;
27 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
28 std::chrono::high_resolution_clock::now()
31 this->scnt_ = dt.count();
35 bool RRTS::should_stop()
37 // the following counters must be updated, do not comment
40 // decide the stop conditions (maybe comment some lines)
41 if (this->icnt_ > 999) return true;
42 if (this->scnt_ > 10) return true;
43 if (this->gf()) return true;
44 // but continue by default
49 std::tuple<bool, unsigned int, unsigned int>
50 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
52 for (auto &o: this->obstacles())
53 if (std::get<0>(::collide(poly, o.poly())))
54 return ::collide(poly, o.poly());
55 return std::make_tuple(false, 0, 0);
58 std::tuple<bool, unsigned int, unsigned int>
59 RRTS::collide_steered_from(RRTNode &f)
61 std::vector<std::tuple<double, double>> s;
62 s.push_back(std::make_tuple(f.x(), f.y()));
63 for (auto &n: this->steered()) {
64 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
65 s.push_back(std::make_tuple(n.lrx(), n.lry()));
66 s.push_back(std::make_tuple(n.rrx(), n.rry()));
67 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
69 auto col = this->collide(s);
70 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
71 if (std::get<0>(col) && strip_from > 0) {
72 while (strip_from-- > 0) {
73 this->steered().pop_back();
75 return this->collide_steered_from(f);
80 std::tuple<bool, unsigned int, unsigned int>
81 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
83 std::vector<std::tuple<double, double>> p;
84 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
85 p.push_back(std::make_tuple(f.lrx(), f.lry()));
86 p.push_back(std::make_tuple(f.rrx(), f.rry()));
87 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
88 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
89 p.push_back(std::make_tuple(t.lrx(), t.lry()));
90 p.push_back(std::make_tuple(t.rrx(), t.rry()));
91 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
92 return this->collide(p);
95 double RRTS::cost_build(RRTNode &f, RRTNode &t)
98 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
102 double RRTS::cost_search(RRTNode &f, RRTNode &t)
105 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
111 double x = this->ndx_(this->gen_);
112 double y = this->ndy_(this->gen_);
113 double h = this->ndh_(this->gen_);
114 this->samples().push_back(RRTNode());
115 this->samples().back().x(x);
116 this->samples().back().y(y);
117 this->samples().back().h(h);
120 RRTNode *RRTS::nn(RRTNode &t)
122 RRTNode *nn = &this->nodes().front();
123 double cost = this->cost_search(*nn, t);
124 for (auto &f: this->nodes()) {
125 if (this->cost_search(f, t) < cost) {
127 cost = this->cost_search(f, t);
133 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
135 std::vector<RRTNode *> nv;
136 double cost = std::min(GAMMA(this->nodes().size()), ETA);
137 for (auto &f: this->nodes())
138 if (this->cost_search(f, t) < cost)
143 int cb_rs_steer(double q[3], void *user_data)
145 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
146 nodes->push_back(RRTNode());
147 nodes->back().x(q[0]);
148 nodes->back().y(q[1]);
149 nodes->back().h(q[2]);
153 void RRTS::steer(RRTNode &f, RRTNode &t)
155 this->steered().clear();
156 double q0[] = {f.x(), f.y(), f.h()};
157 double q1[] = {t.x(), t.y(), t.h()};
158 ReedsSheppStateSpace rsss(f.mtr());
159 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
162 void RRTS::join_steered(RRTNode *f)
164 while (this->steered().size() > 0) {
165 this->nodes().push_back(this->steered().front());
166 RRTNode *t = &this->nodes().back();
168 t->c(this->cost_build(*f, *t));
169 this->steered().erase(this->steered().begin());
174 bool RRTS::goal_found(RRTNode &f)
177 for (auto &g: this->goals()) {
178 double cost = this->cost_build(f, g);
180 pow(f.x() - g.x(), 2)
181 + pow(f.y() - g.y(), 2)
183 double adist = std::abs(f.h() - g.h());
184 if (edist < 0.05 && adist < M_PI / 32) {
186 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
198 RRTNode *t = &this->steered().front();
199 RRTNode *f = this->nn(this->samples().back());
200 double cost = this->cost_search(*f, *t);
201 for (auto n: this->nv(*t)) {
203 !std::get<0>(this->collide_two_nodes(*n, *t))
204 && this->cost_search(*n, *t) < cost
207 cost = this->cost_search(*n, *t);
210 this->nodes().push_back(this->steered().front());
211 t = &this->nodes().back();
213 t->c(this->cost_build(*f, *t));
214 t->set_t(RRTNodeType::connected);
220 RRTNode *f = &this->nodes().back();
221 for (auto n: this->nv(*f)) {
223 !std::get<0>(this->collide_two_nodes(*f, *n))
224 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
227 n->c(this->cost_build(*f, *n));
233 std::vector<RRTNode *> RRTS::path()
235 std::vector<RRTNode *> path;
236 if (this->goals().size() == 0)
238 RRTNode *goal = &this->goals().front();
239 for (auto &n: this->goals()) {
242 && (n.c() < goal->c() || goal->p() == nullptr)
247 if (goal->p() == nullptr)
249 while (goal != nullptr) {
250 path.push_back(goal);
253 std::reverse(path.begin(), path.end());
259 if (this->icnt_ == 0)
260 this->tstart_ = std::chrono::high_resolution_clock::now();
262 if (this->should_stop())
266 *this->nn(this->samples().back()),
267 this->samples().back()
269 if (std::get<0>(this->collide_steered_from(
270 *this->nn(this->samples().back())
273 if (!this->connect())
276 unsigned scnt = this->steered().size();
277 this->steered().erase(this->steered().begin());
278 this->join_steered(&this->nodes().back());
279 RRTNode *just_added = &this->nodes().back();
282 for (auto &g: this->goals()) {
283 this->steer(*just_added, g);
284 if (std::get<0>(this->collide_steered_from(
288 this->join_steered(just_added);
290 this->gf(this->goal_found(this->nodes().back()));
291 just_added = just_added->p();
296 void RRTS::set_sample(
297 double mx, double dx,
298 double my, double dy,
302 this->ndx_ = std::normal_distribution<double>(mx, dx);
303 this->ndy_ = std::normal_distribution<double>(my, dy);
304 this->ndh_ = std::normal_distribution<double>(mh, dh);
308 : gen_(std::random_device{}())
310 this->goals().reserve(100);
311 this->nodes().reserve(4000000);
312 this->samples().reserve(1000);
313 this->steered().reserve(20000);
314 this->nodes().push_back(RRTNode()); // root
317 double cc(RRTNode &t)
321 while (n != nullptr) {