|
| 1 | +""" |
| 2 | +
|
| 3 | +Path Planning with 4 point Beizer curve |
| 4 | +
|
| 5 | +author: Atsushi Sakai(@Atsushi_twi) |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import scipy.misc as scm |
| 10 | +import numpy as np |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import math |
| 13 | + |
| 14 | + |
| 15 | +def calc_4point_bezier_path(sx, sy, syaw, ex, ey, eyaw, offset): |
| 16 | + D = math.sqrt((sx - ex)**2 + (sy - ey)**2) / offset |
| 17 | + cp = np.array( |
| 18 | + [[sx, sy], |
| 19 | + [sx + D * math.cos(syaw), sy + D * math.sin(syaw)], |
| 20 | + [ex - D * math.cos(eyaw), ey - D * math.sin(eyaw)], |
| 21 | + [ex, ey]]) |
| 22 | + |
| 23 | + traj = [] |
| 24 | + for t in np.linspace(0, 1, 100): |
| 25 | + traj.append(bezier(3, t, cp)) |
| 26 | + P = np.array(traj) |
| 27 | + |
| 28 | + return P, cp |
| 29 | + |
| 30 | + |
| 31 | +def bernstein(n, i, t): |
| 32 | + return scm.comb(n, i) * t**i * (1 - t)**(n - i) |
| 33 | + |
| 34 | + |
| 35 | +def bezier(n, t, q): |
| 36 | + p = np.zeros(2) |
| 37 | + for i in range(n + 1): |
| 38 | + p += bernstein(n, i, t) * q[i] |
| 39 | + return p |
| 40 | + |
| 41 | + |
| 42 | +def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): |
| 43 | + u""" |
| 44 | + Plot arrow |
| 45 | + """ |
| 46 | + |
| 47 | + if not isinstance(x, float): |
| 48 | + for (ix, iy, iyaw) in zip(x, y, yaw): |
| 49 | + plot_arrow(ix, iy, iyaw) |
| 50 | + else: |
| 51 | + plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), |
| 52 | + fc=fc, ec=ec, head_width=width, head_length=width) |
| 53 | + plt.plot(x, y) |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + start_x = 10.0 # [m] |
| 58 | + start_y = 1.0 # [m] |
| 59 | + start_yaw = math.radians(180.0) # [rad] |
| 60 | + |
| 61 | + end_x = -0.0 # [m] |
| 62 | + end_y = -3.0 # [m] |
| 63 | + end_yaw = math.radians(-45.0) # [rad] |
| 64 | + offset = 3.0 |
| 65 | + |
| 66 | + P, cp = calc_4point_bezier_path( |
| 67 | + start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) |
| 68 | + |
| 69 | + plt.plot(P.T[0], P.T[1], label="Bezier Path") |
| 70 | + plt.plot(cp.T[0], cp.T[1], '--o', label="Control Points") |
| 71 | + plot_arrow(start_x, start_y, start_yaw) |
| 72 | + plot_arrow(end_x, end_y, end_yaw) |
| 73 | + plt.legend() |
| 74 | + plt.axis("equal") |
| 75 | + plt.grid(True) |
| 76 | + plt.show() |
| 77 | + |
| 78 | + |
| 79 | +def main2(): |
| 80 | + start_x = 10.0 # [m] |
| 81 | + start_y = 1.0 # [m] |
| 82 | + start_yaw = math.radians(180.0) # [rad] |
| 83 | + |
| 84 | + end_x = -0.0 # [m] |
| 85 | + end_y = -3.0 # [m] |
| 86 | + end_yaw = math.radians(-45.0) # [rad] |
| 87 | + offset = 3.0 |
| 88 | + |
| 89 | + for offset in np.arange(1.0, 5.0, 1.0): |
| 90 | + P, cp = calc_4point_bezier_path( |
| 91 | + start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) |
| 92 | + plt.plot(P.T[0], P.T[1], label="Offset=" + str(offset)) |
| 93 | + plot_arrow(start_x, start_y, start_yaw) |
| 94 | + plot_arrow(end_x, end_y, end_yaw) |
| 95 | + plt.legend() |
| 96 | + plt.axis("equal") |
| 97 | + plt.grid(True) |
| 98 | + plt.show() |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + main() |
| 103 | + # main2() |
0 commit comments