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())
26 double heading = atan2(
27 this->ps().y2() - this->ps().y1(),
28 this->ps().x2() - this->ps().x1()
30 while (heading < 0) heading += 2 * M_PI;
31 double h = this->gc().h();
32 while (h < 0) h += 2 * M_PI;
33 if (std::abs(heading - h) < M_PI / 4)
38 void PSPlanner::guess_gc()
40 double x = this->ps().x1();
41 double y = this->ps().y1();
42 double h = this->ps().heading();
43 double dts = + M_PI / 2; // direction to slot
44 if (this->ps().right())
46 dts *= 0.99; // precision workaround
47 if (this->ps().parallel()) {
48 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
49 x += (this->gc().dr() + 0.01) * cos(h);
50 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
51 y += (this->gc().dr() + 0.01) * sin(h);
54 this->ps().y2() - this->ps().y1(),
55 this->ps().x2() - this->ps().x1()
57 dts *= 1.01; // precision workaround
58 if (std::abs(dts - this->ps().heading()) < M_PI / 2) {
62 x += (this->gc().dr() + 0.01) * cos(h);
63 y += (this->gc().dr() + 0.01) * sin(h);
64 if (this->ps().right())
68 x += (this->gc().w() / 2 + 0.01) * cos(dts);
69 y += (this->gc().w() / 2 + 0.01) * sin(dts);
72 x += -(this->gc().df() + 0.01) * cos(h);
73 y += -(this->gc().df() + 0.01) * sin(h);
74 if (this->ps().right())
78 x += (this->gc().w() / 2 + 0.01) * cos(dts);
79 y += (this->gc().w() / 2 + 0.01) * sin(dts);
91 bool PSPlanner::left()
93 double lfx = this->cc().lfx();
94 double lfy = this->cc().lfy();
95 double lrx = this->cc().lrx();
96 double lry = this->cc().lry();
97 double rrx = this->cc().rrx();
98 double rry = this->cc().rry();
99 double rfx = this->cc().rfx();
100 double rfy = this->cc().rfy();
102 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
103 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
106 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
107 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
110 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
111 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
114 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
115 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
117 if (this->ps().parallel())
118 return lfs == rfs && (lfs != lrs || lfs != rrs);
119 else if (!this->forward())
120 return lfs == rfs && (lfs != lrs || lfs != rrs);
122 return lrs == rrs && (lrs != lfs || lrs != rfs);
125 bool PSPlanner::parked()
127 std::vector<std::tuple<double, double>> slot;
128 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
129 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
130 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
131 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
132 return inside(this->gc().lfx(), this->gc().lfy(), slot)
133 && inside(this->gc().lrx(), this->gc().lry(), slot)
134 && inside(this->gc().rrx(), this->gc().rry(), slot)
135 && inside(this->gc().rfx(), this->gc().rfy(), slot);
138 std::vector<BicycleCar> PSPlanner::possible_inits(
143 std::vector<BicycleCar> pi;
144 if (!this->ps().parallel()) {
145 this->cc().sp(this->cc().sp() * -dist);
146 this->cc().st(this->cc().st() * -1);
148 this->cc().sp(this->cc().sp() * dist);
149 this->cc().st(this->cc().st() * 1);
151 BicycleCar orig_cc(this->cc());
152 for (unsigned int i = 0; i < cnt; i++) {
154 pi.push_back(BicycleCar(this->cc()));
156 this->cc() = BicycleCar(orig_cc);
163 if (this->ps().parallel())
164 return this->fe_parallel();
166 return this->fe_perpendicular();
169 void PSPlanner::fe_parallel()
171 // angle for distance from "entry" corner
172 double dist_angl = this->ps().heading() + M_PI;
173 dist_angl += (this->ps().right()) ? - M_PI / 4 : + M_PI / 4;
174 // set bicycle car `bci` basic dimensions and heading
175 BicycleCar bci = BicycleCar(this->gc());
176 BicycleCar bco = BicycleCar(this->gc());
177 bci.h(this->ps().heading());
178 // move 0.01 from the "entry" corner
179 bci.x(this->ps().x4() + 0.01 * cos(dist_angl));
180 bci.y(this->ps().y4() + 0.01 * sin(dist_angl));
181 // align with parking "top" of slot (move backward)
182 dist_angl = bci.h() + M_PI;
183 bci.x(bci.x() + bci.df() * cos(dist_angl));
184 bci.y(bci.y() + bci.df() * sin(dist_angl));
185 // align with "entry" to pakring slot (move outside)
186 dist_angl = this->ps().heading();
187 dist_angl += (this->ps().right()) ? + M_PI / 2 : - M_PI / 2;
188 bci.x(bci.x() + bci.w() / 2 * cos(dist_angl));
189 bci.y(bci.y() + bci.w() / 2 * sin(dist_angl));
190 // set default speed, steer
191 bci.st(bci.wb() / bci.mtr());
192 if (!this->ps().right())
193 bci.st(bci.st() * -1);
195 // BFS - init all starts
196 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
197 double dist_diag = sqrt(pow(bci.w() / 2, 2) + pow(bci.df(), 2));
198 if (this->ps().right())
199 dist_angl = atan2(bci.y() - bci.rfy(), bci.x() - bci.rfx());
201 dist_angl = atan2(bci.y() - bci.lfy(), bci.x() - bci.lfx());
202 double DIST_ANGL = dist_angl;
203 std::queue<BicycleCar, std::list<BicycleCar>> q;
207 && dist_angl < DIST_ANGL + 3 * M_PI / 4
211 && dist_angl > DIST_ANGL - 3 * M_PI / 4
214 this->cc() = BicycleCar(bci);
215 if (this->ps().right()) {
216 this->cc().x(bci.rfx() + dist_diag * cos(dist_angl));
217 this->cc().y(bci.rfy() + dist_diag * sin(dist_angl));
219 this->cc().x(bci.lfx() + dist_diag * cos(dist_angl));
220 this->cc().y(bci.lfy() + dist_diag * sin(dist_angl));
222 this->cc().h(this->ps().heading() + dist_angl - DIST_ANGL);
223 if (!this->collide()) {
224 q.push(BicycleCar(this->cc()));
226 dist_angl += (this->ps().right()) ? + 0.01 : - 0.01;
228 // BFS - find entry current car `cc` and corresponding goal car `gc`
229 unsigned int iter_cntr = 0;
230 while (!q.empty() && iter_cntr < 9) {
231 this->cc() = BicycleCar(q.front());
236 this->cc().h() - this->ps().heading()
239 this->cc().h() - this->ps().heading()
243 this->cc().sp(this->cc().sp() * -1);
245 this->gc() = BicycleCar(this->cc());
248 this->cc().st(this->cc().st() * -1);
249 q.push(BicycleCar(this->cc()));
250 if (sgn(this->cc().st()) == sgn(q.front().st()))
254 this->gc() = BicycleCar(bco);
256 return this->fer_parallel();
259 void PSPlanner::fe_perpendicular()
261 // TODO Try multiple angles when going from parking slot.
263 // Do not use just the maximum steer angle. Test angles
264 // until the whole current car `cc` is out of the parking
267 // Another approach could be testing angles from the
268 // beginning of the escape parkig slot maneuver.
269 return fer_perpendicular();
272 void PSPlanner::fer()
274 if (this->ps().parallel())
275 return this->fer_parallel();
277 return this->fer_perpendicular();
280 void PSPlanner::fer_parallel()
282 this->cc().st(this->cc().wb() / this->cc().mtr());
283 if (!this->ps().right())
284 this->cc().st(this->cc().st() * -1);
286 while (!this->left()) {
287 while (!this->collide() && !this->left())
289 if (this->left() && !this->collide()) {
292 this->cc().sp(this->cc().sp() * -1);
294 this->cc().st(this->cc().st() * -1);
299 void PSPlanner::fer_perpendicular()
301 bool delta_use[] = {true, true, true};
302 double cc_h = this->cc().h();
305 // check inner radius
306 if (this->forward()) {
315 if (this->ps().right()) {
316 x1 = this->cc().ccr().x();
317 y1 = this->cc().ccr().y();
319 x1 = this->cc().ccl().x();
320 y1 = this->cc().ccl().y();
322 double IR = this->cc().iradi();
326 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
328 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
329 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
330 double D = pow(b, 2) - 4 * a * c;
332 delta = -b - sqrt(D);
334 double delta_1 = delta;
336 delta_use[0] = false;
337 // check outer radius
338 if (this->forward()) {
345 IR = this->cc().ofradi();
348 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
350 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
351 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
352 D = pow(b, 2) - 4 * a * c;
353 if (this->forward()) {
354 delta = -b + sqrt(D);
357 double delta_2 = delta;
359 delta_use[1] = false;
360 delta = -b - sqrt(D);
362 double delta_3 = delta;
364 delta_use[2] = false;
365 if (delta_use[0] && delta_use[1] && delta_use[22])
366 delta = std::max(delta_1, std::max(delta_2, delta_3));
367 else if (delta_use[0] && delta_use[1])
368 delta = std::max(delta_1, delta_2);
369 else if (delta_use[0] && delta_use[2])
370 delta = std::max(delta_1, delta_3);
371 else if (delta_use[1] && delta_use[2])
372 delta = std::max(delta_2, delta_3);
373 else if (delta_use[0])
375 else if (delta_use[1])
377 else if (delta_use[2])
381 // current car `cc` can get out of slot with max steer
382 this->cc().x(this->cc().x() + delta * cos(cc_h));
383 this->cc().y(this->cc().y() + delta * sin(cc_h));
385 // get current car `cc` out of slot
387 this->cc().sp(-0.01);
390 this->cc().st(this->cc().wb() / this->cc().mtr());
391 if (this->ps().right())
392 this->cc().st(this->cc().st() * -1);
393 while (!this->left()) {
394 while (!this->collide() && !this->left())
396 if (this->left() && !this->collide()) {
399 this->cc().sp(this->cc().sp() * -1);
401 this->cc().st(this->cc().st() * -1);
406 PSPlanner::PSPlanner()