|
1 | 1 | """
|
2 | 2 |
|
3 |
| -Path Planning with B-Spline |
| 3 | +Path Planner with B-Spline |
4 | 4 |
|
5 | 5 | author: Atsushi Sakai (@Atsushi_twi)
|
6 | 6 |
|
|
11 | 11 | import scipy.interpolate as scipy_interpolate
|
12 | 12 |
|
13 | 13 |
|
14 |
| -def b_spline_planning(x, y, sn, degree=3): |
| 14 | +def approximate_b_spline_path(x: list, y: list, n_path_points: int, |
| 15 | + degree: int = 3) -> tuple: |
| 16 | + """ |
| 17 | + approximate points with a B-Spline path |
| 18 | +
|
| 19 | + :param x: x position list of approximated points |
| 20 | + :param y: y position list of approximated points |
| 21 | + :param n_path_points: number of path points |
| 22 | + :param degree: (Optional) B Spline curve degree |
| 23 | + :return: x and y position list of the result path |
| 24 | + """ |
15 | 25 | t = range(len(x))
|
16 | 26 | x_tup = scipy_interpolate.splrep(t, x, k=degree)
|
17 | 27 | y_tup = scipy_interpolate.splrep(t, y, k=degree)
|
18 | 28 |
|
19 | 29 | x_list = list(x_tup)
|
20 |
| - xl = x.tolist() |
21 |
| - x_list[1] = xl + [0.0, 0.0, 0.0, 0.0] |
| 30 | + x_list[1] = x + [0.0, 0.0, 0.0, 0.0] |
22 | 31 |
|
23 | 32 | y_list = list(y_tup)
|
24 |
| - yl = y.tolist() |
25 |
| - y_list[1] = yl + [0.0, 0.0, 0.0, 0.0] |
| 33 | + y_list[1] = y + [0.0, 0.0, 0.0, 0.0] |
26 | 34 |
|
27 |
| - ipl_t = np.linspace(0.0, len(x) - 1, sn) |
| 35 | + ipl_t = np.linspace(0.0, len(x) - 1, n_path_points) |
28 | 36 | rx = scipy_interpolate.splev(ipl_t, x_list)
|
29 | 37 | ry = scipy_interpolate.splev(ipl_t, y_list)
|
30 | 38 |
|
31 | 39 | return rx, ry
|
32 | 40 |
|
33 | 41 |
|
| 42 | +def interpolate_b_spline_path(x: list, y: list, n_path_points: int, |
| 43 | + degree: int = 3) -> tuple: |
| 44 | + """ |
| 45 | + interpolate points with a B-Spline path |
| 46 | +
|
| 47 | + :param x: x positions of interpolated points |
| 48 | + :param y: y positions of interpolated points |
| 49 | + :param n_path_points: number of path points |
| 50 | + :param degree: B-Spline degree |
| 51 | + :return: x and y position list of the result path |
| 52 | + """ |
| 53 | + ipl_t = np.linspace(0.0, len(x) - 1, len(x)) |
| 54 | + spl_i_x = scipy_interpolate.make_interp_spline(ipl_t, x, k=degree) |
| 55 | + spl_i_y = scipy_interpolate.make_interp_spline(ipl_t, y, k=degree) |
| 56 | + |
| 57 | + travel = np.linspace(0.0, len(x) - 1, n_path_points) |
| 58 | + return spl_i_x(travel), spl_i_y(travel) |
| 59 | + |
| 60 | + |
34 | 61 | def main():
|
35 | 62 | print(__file__ + " start!!")
|
36 | 63 | # way points
|
37 |
| - way_x = np.array([-1.0, 3.0, 4.0, 2.0, 1.0]) |
38 |
| - way_y = np.array([0.0, -3.0, 1.0, 1.0, 3.0]) |
39 |
| - sn = 100 # sampling number |
| 64 | + way_point_x = [-1.0, 3.0, 4.0, 2.0, 1.0] |
| 65 | + way_point_y = [0.0, -3.0, 1.0, 1.0, 3.0] |
| 66 | + n_course_point = 100 # sampling number |
40 | 67 |
|
41 |
| - rx, ry = b_spline_planning(way_x, way_y, sn) |
| 68 | + rax, ray = approximate_b_spline_path(way_point_x, way_point_y, |
| 69 | + n_course_point) |
| 70 | + rix, riy = interpolate_b_spline_path(way_point_x, way_point_y, |
| 71 | + n_course_point) |
42 | 72 |
|
43 | 73 | # show results
|
44 |
| - plt.plot(way_x, way_y, '-og', label="way points") |
45 |
| - plt.plot(rx, ry, '-r', label="B-Spline path") |
| 74 | + plt.plot(way_point_x, way_point_y, '-og', label="way points") |
| 75 | + plt.plot(rax, ray, '-r', label="Approximated B-Spline path") |
| 76 | + plt.plot(rix, riy, '-b', label="Interpolated B-Spline path") |
46 | 77 | plt.grid(True)
|
47 | 78 | plt.legend()
|
48 | 79 | plt.axis("equal")
|
|
0 commit comments