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::gc_to_4()
40 double angl_slot = atan2(
41 this->ps().y3() - this->ps().y4(),
42 this->ps().x3() - this->ps().x4()
44 double angl_delta = M_PI / 2;
45 if (this->ps().right())
46 angl_delta = -M_PI / 2;
47 double x = this->ps().x4();
48 double y = this->ps().y4();
49 x += (this->gc().dr() + 0.01) * cos(angl_slot);
50 y += (this->gc().dr() + 0.01) * sin(angl_slot);
51 x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
52 y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
55 this->gc().h(angl_slot);
58 void PSPlanner::guess_gc()
60 double x = this->ps().x1();
61 double y = this->ps().y1();
62 double h = this->ps().heading();
63 double dts = + M_PI / 2; // direction to slot
64 if (this->ps().right())
66 if (this->ps().parallel()) {
67 dts *= 0.99; // precision workaround
68 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
69 x += (this->gc().dr() + 0.01) * cos(h);
70 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
71 y += (this->gc().dr() + 0.01) * sin(h);
74 this->ps().y2() - this->ps().y1(),
75 this->ps().x2() - this->ps().x1()
77 dts *= 1.01; // precision workaround
78 if (std::abs(dts - this->ps().heading()) < M_PI / 2) {
82 x += (this->gc().dr() + 0.01) * cos(h);
83 y += (this->gc().dr() + 0.01) * sin(h);
84 if (this->ps().right())
88 x += (this->gc().w() / 2 + 0.01) * cos(dts);
89 y += (this->gc().w() / 2 + 0.01) * sin(dts);
92 x += -(this->gc().df() + 0.01) * cos(h);
93 y += -(this->gc().df() + 0.01) * sin(h);
94 if (this->ps().right())
98 x += (this->gc().w() / 2 + 0.01) * cos(dts);
99 y += (this->gc().w() / 2 + 0.01) * sin(dts);
111 bool PSPlanner::left()
113 double lfx = this->cc().lfx();
114 double lfy = this->cc().lfy();
115 double lrx = this->cc().lrx();
116 double lry = this->cc().lry();
117 double rrx = this->cc().rrx();
118 double rry = this->cc().rry();
119 double rfx = this->cc().rfx();
120 double rfy = this->cc().rfy();
122 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
123 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
126 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
127 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
130 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
131 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
134 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
135 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
137 if (this->ps().parallel())
138 return lfs == rfs && (lfs != lrs || lfs != rrs);
139 else if (!this->forward())
140 return lfs == rfs && (lfs != lrs || lfs != rrs);
142 return lrs == rrs && (lrs != lfs || lrs != rfs);
145 bool PSPlanner::parked()
147 std::vector<std::tuple<double, double>> slot;
148 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
149 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
150 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
151 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
152 return inside(this->gc().lfx(), this->gc().lfy(), slot)
153 && inside(this->gc().lrx(), this->gc().lry(), slot)
154 && inside(this->gc().rrx(), this->gc().rry(), slot)
155 && inside(this->gc().rfx(), this->gc().rfy(), slot);
158 std::vector<BicycleCar> PSPlanner::possible_inits(
163 std::vector<BicycleCar> pi;
164 this->cc().sp(this->cc().sp() * dist);
165 this->cc().st(this->cc().st() * 1);
166 BicycleCar orig_cc(this->cc());
167 for (unsigned int i = 0; i < cnt; i++) {
169 pi.push_back(BicycleCar(this->cc()));
171 this->cc() = BicycleCar(orig_cc);
178 if (this->ps().parallel())
179 return this->fe_parallel();
181 return this->fe_perpendicular();
184 void PSPlanner::fe_parallel()
186 // angle for distance from "entry" corner
187 double dist_angl = this->ps().heading() + M_PI;
188 dist_angl += (this->ps().right()) ? - M_PI / 4 : + M_PI / 4;
189 // set bicycle car `bci` basic dimensions and heading
190 BicycleCar bci = BicycleCar(this->gc());
191 BicycleCar bco = BicycleCar(this->gc());
192 bci.h(this->ps().heading());
193 // move 0.01 from the "entry" corner
194 bci.x(this->ps().x4() + 0.01 * cos(dist_angl));
195 bci.y(this->ps().y4() + 0.01 * sin(dist_angl));
196 // align with parking "top" of slot (move backward)
197 dist_angl = bci.h() + M_PI;
198 bci.x(bci.x() + bci.df() * cos(dist_angl));
199 bci.y(bci.y() + bci.df() * sin(dist_angl));
200 // align with "entry" to pakring slot (move outside)
201 dist_angl = this->ps().heading();
202 dist_angl += (this->ps().right()) ? + M_PI / 2 : - M_PI / 2;
203 bci.x(bci.x() + bci.w() / 2 * cos(dist_angl));
204 bci.y(bci.y() + bci.w() / 2 * sin(dist_angl));
205 // set default speed, steer
206 bci.st(bci.wb() / bci.mtr());
207 if (!this->ps().right())
208 bci.st(bci.st() * -1);
210 // BFS - init all starts
211 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
212 double dist_diag = sqrt(pow(bci.w() / 2, 2) + pow(bci.df(), 2));
213 if (this->ps().right())
214 dist_angl = atan2(bci.y() - bci.rfy(), bci.x() - bci.rfx());
216 dist_angl = atan2(bci.y() - bci.lfy(), bci.x() - bci.lfx());
217 double DIST_ANGL = dist_angl;
218 std::queue<BicycleCar, std::list<BicycleCar>> q;
222 && dist_angl < DIST_ANGL + 3 * M_PI / 4
226 && dist_angl > DIST_ANGL - 3 * M_PI / 4
229 this->cc() = BicycleCar(bci);
230 if (this->ps().right()) {
231 this->cc().x(bci.rfx() + dist_diag * cos(dist_angl));
232 this->cc().y(bci.rfy() + dist_diag * sin(dist_angl));
234 this->cc().x(bci.lfx() + dist_diag * cos(dist_angl));
235 this->cc().y(bci.lfy() + dist_diag * sin(dist_angl));
237 this->cc().h(this->ps().heading() + dist_angl - DIST_ANGL);
238 if (!this->collide()) {
239 q.push(BicycleCar(this->cc()));
241 dist_angl += (this->ps().right()) ? + 0.01 : - 0.01;
243 // BFS - find entry current car `cc` and corresponding goal car `gc`
244 unsigned int iter_cntr = 0;
245 while (!q.empty() && iter_cntr < 9) {
246 this->cc() = BicycleCar(q.front());
251 this->cc().h() - this->ps().heading()
254 this->cc().h() - this->ps().heading()
258 this->cc().sp(this->cc().sp() * -1);
260 this->gc() = BicycleCar(this->cc());
263 this->cc().st(this->cc().st() * -1);
264 q.push(BicycleCar(this->cc()));
265 if (sgn(this->cc().st()) == sgn(q.front().st()))
269 this->gc() = BicycleCar(bco);
271 return this->fer_parallel();
274 void PSPlanner::fe_perpendicular()
276 // TODO Try multiple angles when going from parking slot.
278 // Do not use just the maximum steer angle. Test angles
279 // until the whole current car `cc` is out of the parking
282 // Another approach could be testing angles from the
283 // beginning of the escape parkig slot maneuver.
285 this->cc().sp(-0.01);
288 while (!this->left())
293 void PSPlanner::fer()
295 if (this->ps().parallel())
296 return this->fer_parallel();
298 return this->fer_perpendicular();
301 void PSPlanner::fer_parallel()
303 this->cc().st(this->cc().wb() / this->cc().mtr());
304 if (!this->ps().right())
305 this->cc().st(this->cc().st() * -1);
307 while (!this->left()) {
308 while (!this->collide() && !this->left())
310 if (this->left() && !this->collide()) {
313 this->cc().sp(this->cc().sp() * -1);
315 this->cc().st(this->cc().st() * -1);
320 void PSPlanner::fer_perpendicular()
322 bool delta_use[] = {true, true, true};
323 double cc_h = this->cc().h();
326 // check inner radius
327 if (this->forward()) {
336 if (this->ps().right()) {
337 x1 = this->cc().ccr().x();
338 y1 = this->cc().ccr().y();
340 x1 = this->cc().ccl().x();
341 y1 = this->cc().ccl().y();
343 double IR = this->cc().iradi();
347 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
349 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
350 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
351 double D = pow(b, 2) - 4 * a * c;
353 delta = -b - sqrt(D);
355 double delta_1 = delta;
357 delta_use[0] = false;
358 // check outer radius
359 if (this->forward()) {
366 IR = this->cc().ofradi();
369 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
371 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
372 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
373 D = pow(b, 2) - 4 * a * c;
374 if (this->forward()) {
375 delta = -b + sqrt(D);
378 double delta_2 = delta;
380 delta_use[1] = false;
381 delta = -b - sqrt(D);
383 double delta_3 = delta;
385 delta_use[2] = false;
386 if (delta_use[0] && delta_use[1] && delta_use[22])
387 delta = std::max(delta_1, std::max(delta_2, delta_3));
388 else if (delta_use[0] && delta_use[1])
389 delta = std::max(delta_1, delta_2);
390 else if (delta_use[0] && delta_use[2])
391 delta = std::max(delta_1, delta_3);
392 else if (delta_use[1] && delta_use[2])
393 delta = std::max(delta_2, delta_3);
394 else if (delta_use[0])
396 else if (delta_use[1])
398 else if (delta_use[2])
402 // current car `cc` can get out of slot with max steer
403 this->cc().x(this->cc().x() + delta * cos(cc_h));
404 this->cc().y(this->cc().y() + delta * sin(cc_h));
406 // get current car `cc` out of slot
408 this->cc().sp(-0.01);
411 this->cc().st(this->cc().wb() / this->cc().mtr());
412 if (this->ps().right())
413 this->cc().st(this->cc().st() * -1);
414 while (!this->left()) {
415 while (!this->collide() && !this->left())
417 if (this->left() && !this->collide()) {
420 this->cc().sp(this->cc().sp() * -1);
422 this->cc().st(this->cc().st() * -1);
427 PSPlanner::PSPlanner()