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); \
12 template <typename T> int sgn(T val) {
13 return (T(0) < val) - (val < T(0));
20 RRTNode::RRTNode(const BicycleCar &bc) : BicycleCar(bc)
28 double RRTS::elapsed()
30 std::chrono::duration<double> dt;
31 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
32 std::chrono::high_resolution_clock::now()
35 this->scnt_ = dt.count();
39 bool RRTS::should_stop()
41 // the following counters must be updated, do not comment
44 // decide the stop conditions (maybe comment some lines)
45 if (this->icnt_ > 999) return true;
46 if (this->scnt_ > 10) return true;
47 if (this->gf()) return true;
48 // but continue by default
53 std::tuple<bool, unsigned int, unsigned int>
54 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
56 for (auto &o: this->obstacles())
57 if (std::get<0>(::collide(poly, o.poly())))
58 return ::collide(poly, o.poly());
59 return std::make_tuple(false, 0, 0);
62 std::tuple<bool, unsigned int, unsigned int>
63 RRTS::collide_steered_from(RRTNode &f)
65 std::vector<std::tuple<double, double>> s;
66 s.push_back(std::make_tuple(f.x(), f.y()));
67 for (auto &n: this->steered()) {
68 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
69 s.push_back(std::make_tuple(n.lrx(), n.lry()));
70 s.push_back(std::make_tuple(n.rrx(), n.rry()));
71 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
73 auto col = this->collide(s);
74 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
75 if (std::get<0>(col) && strip_from > 0) {
76 while (strip_from-- > 0) {
77 this->steered().pop_back();
79 return this->collide_steered_from(f);
84 std::tuple<bool, unsigned int, unsigned int>
85 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
87 std::vector<std::tuple<double, double>> p;
88 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
89 p.push_back(std::make_tuple(f.lrx(), f.lry()));
90 p.push_back(std::make_tuple(f.rrx(), f.rry()));
91 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
92 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
93 p.push_back(std::make_tuple(t.lrx(), t.lry()));
94 p.push_back(std::make_tuple(t.rrx(), t.rry()));
95 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
96 return this->collide(p);
99 double RRTS::cost_build(RRTNode &f, RRTNode &t)
102 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
106 double RRTS::cost_search(RRTNode &f, RRTNode &t)
109 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
115 double x = this->ndx_(this->gen_);
116 double y = this->ndy_(this->gen_);
117 double h = this->ndh_(this->gen_);
118 this->samples().push_back(RRTNode());
119 this->samples().back().x(x);
120 this->samples().back().y(y);
121 this->samples().back().h(h);
124 RRTNode *RRTS::nn(RRTNode &t)
126 RRTNode *nn = &this->nodes().front();
127 double cost = this->cost_search(*nn, t);
128 for (auto &f: this->nodes()) {
129 if (this->cost_search(f, t) < cost) {
131 cost = this->cost_search(f, t);
137 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
139 std::vector<RRTNode *> nv;
140 double cost = std::min(GAMMA(this->nodes().size()), ETA);
141 for (auto &f: this->nodes())
142 if (this->cost_search(f, t) < cost)
147 int cb_rs_steer(double q[4], void *user_data)
149 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
150 RRTNode *ln = nullptr;
151 if (nodes->size() > 0)
153 nodes->push_back(RRTNode());
154 nodes->back().x(q[0]);
155 nodes->back().y(q[1]);
156 nodes->back().h(q[2]);
157 nodes->back().sp(q[3]);
158 if (nodes->back().sp() == 0)
159 nodes->back().set_t(RRTNodeType::cusp);
160 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
161 ln->set_t(RRTNodeType::cusp);
165 void RRTS::steer(RRTNode &f, RRTNode &t)
167 this->steered().clear();
168 double q0[] = {f.x(), f.y(), f.h()};
169 double q1[] = {t.x(), t.y(), t.h()};
170 ReedsSheppStateSpace rsss(f.mtr());
171 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
174 void RRTS::join_steered(RRTNode *f)
176 while (this->steered().size() > 0) {
177 this->nodes().push_back(this->steered().front());
178 RRTNode *t = &this->nodes().back();
180 t->c(this->cost_build(*f, *t));
181 this->steered().erase(this->steered().begin());
186 bool RRTS::goal_found(RRTNode &f)
189 for (auto &g: this->goals()) {
190 double cost = this->cost_build(f, g);
192 pow(f.x() - g.x(), 2)
193 + pow(f.y() - g.y(), 2)
195 double adist = std::abs(f.h() - g.h());
196 if (edist < 0.05 && adist < M_PI / 32) {
198 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
210 RRTNode *t = &this->steered().front();
211 RRTNode *f = this->nn(this->samples().back());
212 double cost = this->cost_search(*f, *t);
213 for (auto n: this->nv(*t)) {
215 !std::get<0>(this->collide_two_nodes(*n, *t))
216 && this->cost_search(*n, *t) < cost
219 cost = this->cost_search(*n, *t);
222 this->nodes().push_back(this->steered().front());
223 t = &this->nodes().back();
225 t->c(this->cost_build(*f, *t));
226 t->set_t(RRTNodeType::connected);
232 RRTNode *f = &this->nodes().back();
233 for (auto n: this->nv(*f)) {
235 !std::get<0>(this->collide_two_nodes(*f, *n))
236 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
239 n->c(this->cost_build(*f, *n));
253 std::vector<RRTNode *> RRTS::path()
255 std::vector<RRTNode *> path;
256 if (this->goals().size() == 0)
258 RRTNode *goal = &this->goals().front();
259 for (auto &n: this->goals()) {
262 && (n.c() < goal->c() || goal->p() == nullptr)
267 if (goal->p() == nullptr)
269 while (goal != nullptr) {
270 path.push_back(goal);
273 std::reverse(path.begin(), path.end());
279 if (this->icnt_ == 0)
280 this->tstart_ = std::chrono::high_resolution_clock::now();
282 if (this->should_stop())
286 *this->nn(this->samples().back()),
287 this->samples().back()
289 if (std::get<0>(this->collide_steered_from(
290 *this->nn(this->samples().back())
293 if (!this->connect())
296 unsigned scnt = this->steered().size();
297 this->steered().erase(this->steered().begin());
298 this->join_steered(&this->nodes().back());
299 RRTNode *just_added = &this->nodes().back();
302 for (auto &g: this->goals()) {
303 this->steer(*just_added, g);
304 if (std::get<0>(this->collide_steered_from(
308 this->join_steered(just_added);
310 this->gf(this->goal_found(this->nodes().back()));
311 just_added = just_added->p();
316 void RRTS::set_sample(
317 double mx, double dx,
318 double my, double dy,
322 this->ndx_ = std::normal_distribution<double>(mx, dx);
323 this->ndy_ = std::normal_distribution<double>(my, dy);
324 this->ndh_ = std::normal_distribution<double>(mh, dh);
328 : gen_(std::random_device{}())
330 this->goals().reserve(100);
331 this->nodes().reserve(4000000);
332 this->samples().reserve(1000);
333 this->steered().reserve(20000);
334 this->nodes().push_back(RRTNode()); // root
337 double cc(RRTNode &t)
341 while (n != nullptr) {