6 bool PSPlanner::collide()
8 std::vector<std::tuple<double, double>> bc;
9 bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
10 bc.push_back(std::make_tuple(this->cc().lrx(), this->cc().lry()));
11 bc.push_back(std::make_tuple(this->cc().rrx(), this->cc().rry()));
12 bc.push_back(std::make_tuple(this->cc().rfx(), this->cc().rfy()));
13 bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
14 std::vector<std::tuple<double, double>> ps;
15 ps.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
16 ps.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
17 ps.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
18 ps.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
19 return std::get<0>(::collide(bc, ps));
22 bool PSPlanner::forward()
24 if (this->ps().parallel())
28 double heading = atan2(
29 this->ps().y2() - this->ps().y1(),
30 this->ps().x2() - this->ps().x1()
32 while (heading < 0) heading += 2 * M_PI;
33 double h = this->gc().h();
34 while (h < 0) h += 2 * M_PI;
35 if (std::abs(heading - h) < M_PI / 4)
40 void PSPlanner::gc_to_4()
42 double angl_slot = atan2(
43 this->ps().y3() - this->ps().y4(),
44 this->ps().x3() - this->ps().x4()
46 double angl_delta = M_PI / 2;
47 if (this->ps().right())
48 angl_delta = -M_PI / 2;
49 double x = this->ps().x4();
50 double y = this->ps().y4();
51 x += (this->gc().dr() + 0.01) * cos(angl_slot);
52 y += (this->gc().dr() + 0.01) * sin(angl_slot);
53 x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
54 y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
57 this->gc().h(angl_slot);
60 std::tuple<double, double, double, double> circle_line_intersection(
61 double cx, double cy, double r,
66 double t = (y2 - y1) / (x2 - x1);
67 //double a = 1 + pow(t, 2);
68 //double b = - 2 * cx - 2 * pow(t, 2) * x1 + 2 * t * y1 - 2 * t * cy;
69 //double c = pow(cx, 2) + pow(t, 2) * pow(x1, 2) - 2 * t * y1 * x1
70 // + pow(y1, 2) + 2 * t * cy * x1 - 2 * y1 * cy + pow(cy, 2)
72 double a = 1 + pow(t, 2);
73 double b = - 2 * cx + 2 * t * (-t * x1 + y1) - 2 * cy * t;
74 double c = pow(cx, 2) + pow(cy, 2) - pow(r, 2);
75 c += pow(-t * x1 + y1, 2);
76 c += 2 * cy * t * x1 - 2 * cy * y1;
77 double D = pow(b, 2) - 4 * a * c;
79 return std::make_tuple(cx, cy, cx, cy);
80 double res_x1 = (-b + sqrt(D)) / (2 * a);
81 double res_y1 = t * (res_x1 - x1) + y1;
82 double res_x2 = (-b - sqrt(D)) / (2 * a);
83 double res_y2 = t * (res_x2 - x1) + y1;
84 return std::make_tuple(res_x1, res_y1, res_x2, res_y2);
87 double edist(double x1, double y1, double x2, double y2)
89 return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
92 void PSPlanner::guess_gc()
94 double x = this->ps().x1();
95 double y = this->ps().y1();
96 double h = this->ps().heading();
97 double dts = + M_PI / 2; // direction to slot
98 if (this->ps().right())
100 if (this->ps().parallel()) {
101 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
102 x += (this->gc().dr() + 0.01) * cos(h);
103 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
104 y += (this->gc().dr() + 0.01) * sin(h);
107 double entry_width = edist(
108 this->ps().x1(), this->ps().y1(),
109 this->ps().x4(), this->ps().y4()
111 x += entry_width / 2 * cos(h);
112 y += entry_width / 2 * sin(h);
114 this->ps().y2() - this->ps().y1(),
115 this->ps().x2() - this->ps().x1()
117 while (h < 0) h += 2 * M_PI;
121 //// This is for backward parking only.
122 //double entry_width = edist(
123 // this->ps().x1(), this->ps().y1(),
124 // this->ps().x4(), this->ps().y4()
127 // this->gc().orradi()
128 // - (this->gc().mtr() + this->gc().w() / 2)
130 //double move1 = dist_l + this->gc().w() / 2;
131 //double dist_r = entry_width - this->gc().w() - dist_l;
132 //double move2 = sqrt(
133 // pow(this->gc().iradi(), 2)
134 // - pow(this->gc().iradi() - dist_r, 2)
136 //move2 -= this->gc().dr() / 2; // workaround
137 //x += move1 * cos(h);
138 //y += move1 * sin(h);
140 // this->ps().y2() - this->ps().y1(),
141 // this->ps().x2() - this->ps().x1()
143 //while (dts < 0) dts += 2 * M_PI;
144 //x += move2 * cos(h + dts);
145 //y += move2 * sin(h + dts);
146 //h += dts - M_PI / 2;
157 bool PSPlanner::left()
159 double lfx = this->cc().lfx();
160 double lfy = this->cc().lfy();
161 double lrx = this->cc().lrx();
162 double lry = this->cc().lry();
163 double rrx = this->cc().rrx();
164 double rry = this->cc().rry();
165 double rfx = this->cc().rfx();
166 double rfy = this->cc().rfy();
168 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
169 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
172 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
173 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
176 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
177 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
180 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
181 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
183 if (this->ps().parallel())
184 return lfs == rfs && (lfs != lrs || lfs != rrs);
185 else if (!this->forward())
186 return lfs == rfs && (lfs != lrs || lfs != rrs);
188 return lrs == rrs && (lrs != lfs || lrs != rfs);
191 bool PSPlanner::parked()
193 std::vector<std::tuple<double, double>> slot;
194 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
195 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
196 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
197 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
198 return inside(this->gc().lfx(), this->gc().lfy(), slot)
199 && inside(this->gc().lrx(), this->gc().lry(), slot)
200 && inside(this->gc().rrx(), this->gc().rry(), slot)
201 && inside(this->gc().rfx(), this->gc().rfy(), slot);
204 std::vector<BicycleCar> PSPlanner::possible_goals(
209 std::vector<BicycleCar> pi;
210 if (this->cc().sp() > 0)
214 this->cc().sp(this->cc().sp() * dist);
215 this->cc().st(this->cc().st() * 1);
216 BicycleCar orig_cc(this->cc());
217 for (unsigned int i = 0; i < cnt; i++) {
219 pi.push_back(BicycleCar(this->cc()));
221 this->cc() = BicycleCar(orig_cc);
229 if (this->ps().parallel()) {
230 return this->fe_parallel();
233 this->cc() = BicycleCar(this->gc());
234 //this->cc().set_max_steer();
235 //if (this->ps().right())
236 // this->cc().st(this->cc().st() * -1);
241 void PSPlanner::fe_parallel()
243 BicycleCar bco = BicycleCar(this->gc());
244 this->cc() = BicycleCar();
245 this->cc().sp(-0.01);
246 this->cc().set_max_steer();
247 if (!this->ps().right())
248 this->cc().st(this->cc().st() * -1);
249 this->cc().h(this->ps().heading());
250 double angl_in_slot = this->ps().heading() - M_PI / 4;
251 if (!this->ps().right())
252 angl_in_slot += M_PI / 2;
255 + this->cc().w()/2 * cos(
257 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
259 + (this->cc().df() + 0.01) * cos(
260 this->ps().heading() + M_PI
265 + this->cc().w()/2 * sin(
267 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
269 + (this->cc().df() + 0.01) * sin(
270 this->ps().heading() + M_PI
274 std::queue<BicycleCar, std::list<BicycleCar>> q;
275 while (!this->collide()) {
279 this->ps().y4() - 0.01,
280 ((this->ps().right()) ? 0.001 : -0.001)
283 // BFS - find entry current car `cc` and corresponding goal car `gc`
284 unsigned int iter_cntr = 0;
285 while (!q.empty() && iter_cntr < 30) {
286 this->cc() = BicycleCar(q.front());
291 this->cc().h() - this->ps().heading()
294 this->cc().h() - this->ps().heading()
298 this->cc().sp(this->cc().sp() * -1);
300 this->gc() = BicycleCar(this->cc());
303 this->cc().st(this->cc().st() * -1);
304 q.push(BicycleCar(this->cc()));
305 if (sgn(this->cc().st()) == sgn(q.front().st()))
309 this->gc() = BicycleCar(bco);
311 return this->fer_parallel();
314 void PSPlanner::fe_perpendicular()
316 // TODO Try multiple angles when going from parking slot.
318 // Do not use just the maximum steer angle. Test angles
319 // until the whole current car `cc` is out of the parking
322 // Another approach could be testing angles from the
323 // beginning of the escape parkig slot maneuver.
325 this->cc().sp(-0.01);
328 while (!this->left())
333 void PSPlanner::fer()
336 if (this->ps().parallel()) {
338 this->cc() = BicycleCar(this->gc());
339 this->cc().set_max_steer();
340 if (!this->ps().right())
341 this->cc().st(this->cc().st() * -1);
343 return this->fer_parallel();
345 return this->fer_perpendicular();
349 void PSPlanner::fer_parallel()
351 this->cusps_.clear();
352 while (!this->left()) {
353 while (!this->collide() && !this->left())
355 if (this->left() && !this->collide()) {
358 this->cc().sp(this->cc().sp() * -1);
360 this->cc().st(this->cc().st() * -1);
362 this->cusps_.push_back(this->cc());
365 if (this->cc().st() < 0) {
367 this->cusps_.push_back(this->cc());
371 void PSPlanner::fer_perpendicular()
373 bool delta_use[] = {true, true, true};
374 double cc_h = this->cc().h();
377 // check inner radius
378 if (this->forward()) {
387 if (this->ps().right()) {
388 x1 = this->cc().ccr().x();
389 y1 = this->cc().ccr().y();
391 x1 = this->cc().ccl().x();
392 y1 = this->cc().ccl().y();
394 double IR = this->cc().iradi();
398 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
400 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
401 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
402 double D = pow(b, 2) - 4 * a * c;
404 delta = -b - sqrt(D);
406 double delta_1 = delta;
408 delta_use[0] = false;
409 // check outer radius
410 if (this->forward()) {
417 IR = this->cc().ofradi();
420 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
422 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
423 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
424 D = pow(b, 2) - 4 * a * c;
425 if (this->forward()) {
426 delta = -b + sqrt(D);
429 double delta_2 = delta;
431 delta_use[1] = false;
432 delta = -b - sqrt(D);
434 double delta_3 = delta;
436 delta_use[2] = false;
437 if (delta_use[0] && delta_use[1] && delta_use[2])
438 delta = std::max(delta_1, std::max(delta_2, delta_3));
439 else if (delta_use[0] && delta_use[1])
440 delta = std::max(delta_1, delta_2);
441 else if (delta_use[0] && delta_use[2])
442 delta = std::max(delta_1, delta_3);
443 else if (delta_use[1] && delta_use[2])
444 delta = std::max(delta_2, delta_3);
445 else if (delta_use[0])
447 else if (delta_use[1])
449 else if (delta_use[2])
453 // current car `cc` can get out of slot with max steer
454 this->cc().x(this->cc().x() + delta * cos(cc_h));
455 this->cc().y(this->cc().y() + delta * sin(cc_h));
457 // get current car `cc` out of slot
459 this->cc().sp(-0.01);
462 this->cc().set_max_steer();
463 if (this->ps().right())
464 this->cc().st(this->cc().st() * -1);
465 while (!this->left()) {
466 while (!this->collide() && !this->left())
468 if (this->left() && !this->collide()) {
471 this->cc().sp(this->cc().sp() * -1);
473 this->cc().st(this->cc().st() * -1);
478 PSPlanner::PSPlanner()