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)
25 bool RRTS::collide(std::vector<std::tuple<double, double>> &poly)
27 for (auto &o: this->obstacles())
28 if (std::get<0>(::collide(poly, o.poly())))
33 bool RRTS::collide_steered_from(RRTNode &f)
35 std::vector<std::tuple<double, double>> s;
36 s.push_back(std::make_tuple(f.x(), f.y()));
37 for (auto &n: this->steered()) {
38 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
39 s.push_back(std::make_tuple(n.lrx(), n.lry()));
40 s.push_back(std::make_tuple(n.rrx(), n.rry()));
41 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
43 return this->collide(s);
46 bool RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
48 std::vector<std::tuple<double, double>> p;
49 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
50 p.push_back(std::make_tuple(f.lrx(), f.lry()));
51 p.push_back(std::make_tuple(f.rrx(), f.rry()));
52 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
53 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
54 p.push_back(std::make_tuple(t.lrx(), t.lry()));
55 p.push_back(std::make_tuple(t.rrx(), t.rry()));
56 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
57 return this->collide(p);
60 double RRTS::cost(RRTNode &f, RRTNode &t)
63 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
67 double RRTS::cost_build(RRTNode &f, RRTNode &t)
69 double q0[] = {f.x(), f.y(), f.h()};
70 double q1[] = {t.x(), t.y(), t.h()};
71 ReedsSheppStateSpace rsss(f.mtr());
72 return rsss.distance(q0, q1);
75 double RRTS::cost_search(RRTNode &f, RRTNode &t)
78 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
79 double heur = std::min(
80 std::abs(t.h() - f.h()),
81 2 * M_PI - std::abs(t.h() - f.h())
84 cost = std::max(cost, heur);
90 double x = this->ndx_(this->gen_);
91 double y = this->ndy_(this->gen_);
92 double h = this->ndh_(this->gen_);
93 this->samples().push_back(RRTNode());
94 this->samples().back().x(x);
95 this->samples().back().y(y);
96 this->samples().back().h(h);
99 RRTNode *RRTS::nn(RRTNode &t)
101 RRTNode *nn = &this->nodes().front();
102 double cost = this->cost(*nn, t);
103 for (auto &f: this->nodes()) {
104 if (this->cost(f, t) < cost) {
106 cost = this->cost(f, t);
112 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
114 std::vector<RRTNode *> nv;
115 double cost = std::min(GAMMA(this->nodes().size()), ETA);
116 for (auto &f: this->nodes())
117 if (this->cost(f, t) < cost)
122 int cb_rs_steer(double q[3], void *user_data)
124 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
125 nodes->push_back(RRTNode());
126 nodes->back().x(q[0]);
127 nodes->back().y(q[1]);
128 nodes->back().h(q[2]);
132 void RRTS::steer(RRTNode &f, RRTNode &t)
134 this->steered().clear();
135 double q0[] = {f.x(), f.y(), f.h()};
136 double q1[] = {t.x(), t.y(), t.h()};
137 ReedsSheppStateSpace rsss(f.mtr());
138 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
145 RRTNode *t = &this->steered().front();
146 RRTNode *f = this->nn(this->samples().back());
147 double cost = this->cost(*f, *t);
148 for (auto n: this->nv(*t)) {
150 !this->collide_two_nodes(*n, *t)
151 && this->cost(*n, *t) < cost
154 cost = this->cost(*n, *t);
157 this->nodes().push_back(this->steered().front());
158 t = &this->nodes().back();
160 t->c(this->cost(*f, *t));
167 RRTNode *f = &this->nodes().back();
168 for (auto n: this->nv(*f)) {
170 !this->collide_two_nodes(*f, *n)
171 && cc(*f) + this->cost(*f, *n) < cc(*n)
178 std::vector<RRTNode *> RRTS::path()
180 std::vector<RRTNode *> path;
181 if (this->goals().size() == 0)
183 RRTNode *goal = &this->goals().front();
184 for (auto &n: this->goals()) {
187 && (n.c() < goal->c() || goal->p() == nullptr)
192 if (goal->p() == nullptr)
194 while (goal != nullptr) {
195 path.push_back(goal);
198 std::reverse(path.begin(), path.end());
208 *this->nn(this->samples().back()),
209 this->samples().back()
211 if (this->collide_steered_from(*this->nn(this->samples().back())))
213 if (!this->connect())
216 unsigned scnt = this->steered().size();
217 this->steered().erase(this->steered().begin());
218 while (this->steered().size() > 0) {
219 RRTNode *f = &this->nodes().back();
220 this->nodes().push_back(this->steered().front());
221 RRTNode *t = &this->nodes().back();
223 t->c(this->cost(*f, *t));
224 this->steered().erase(this->steered().begin());
226 RRTNode *just_added = &this->nodes().back();
229 for (auto &g: this->goals()) {
230 this->steer(*just_added, g);
231 if (this->collide_steered_from(*just_added))
233 RRTNode *f = just_added;
234 while (this->steered().size() > 0) {
235 RRTNode s = this->steered().front();
236 this->nodes().push_back(s);
237 RRTNode *t = &this->nodes().back();
239 t->c(this->cost(*f, *t));
240 this->steered().erase(this->steered().begin());
244 just_added = just_added->p();
246 for (auto &g: this->goals()) {
247 double cost = this->cost(this->nodes().back(), g);
249 pow(this->nodes().back().x() - g.x(), 2)
250 + pow(this->nodes().back().y() - g.y(), 2)
252 double adist = std::abs(this->nodes().back().h() - g.h());
253 if (edist < 0.05 && adist < M_PI / 32) {
257 || cc(this->nodes().back()) + cost < cc(g)
259 g.p(&this->nodes().back());
264 if (this->icnt_ > 999)
269 void RRTS::set_sample(
270 double mx, double dx,
271 double my, double dy,
275 this->ndx_ = std::normal_distribution<double>(mx, dx);
276 this->ndy_ = std::normal_distribution<double>(my, dy);
277 this->ndh_ = std::normal_distribution<double>(mh, dh);
281 : gen_(std::random_device{}())
283 this->goals().reserve(1);
284 this->nodes().reserve(40000);
285 this->samples().reserve(1000);
286 this->steered().reserve(200);
287 this->nodes().push_back(RRTNode()); // root
290 double cc(RRTNode &t)
294 while (n != nullptr) {