Skip to content

Commit

Permalink
add bezier path test
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsushiSakai committed Dec 31, 2017
1 parent 08ffab0 commit 4433fed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
50 changes: 33 additions & 17 deletions PathPlanning/BezierPath/bezier_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"""

import scipy.misc as scm
import scipy.special
import numpy as np
import matplotlib.pyplot as plt
import math

show_animation = True


def calc_4point_bezier_path(sx, sy, syaw, ex, ey, eyaw, offset):
D = math.sqrt((sx - ex)**2 + (sy - ey)**2) / offset
Expand All @@ -29,7 +31,7 @@ def calc_4point_bezier_path(sx, sy, syaw, ex, ey, eyaw, offset):


def bernstein(n, i, t):
return scm.comb(n, i) * t**i * (1 - t)**(n - i)
return scipy.special.comb(n, i) * t**i * (1 - t)**(n - i)


def bezier(n, t, q):
Expand Down Expand Up @@ -66,14 +68,20 @@ def main():
P, cp = calc_4point_bezier_path(
start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset)

plt.plot(P.T[0], P.T[1], label="Bezier Path")
plt.plot(cp.T[0], cp.T[1], '--o', label="Control Points")
plot_arrow(start_x, start_y, start_yaw)
plot_arrow(end_x, end_y, end_yaw)
plt.legend()
plt.axis("equal")
plt.grid(True)
plt.show()
assert P.T[0][0] == start_x, "path is invalid"
assert P.T[1][0] == start_y, "path is invalid"
assert P.T[0][-1] == end_x, "path is invalid"
assert P.T[1][-1] == end_y, "path is invalid"

if show_animation:
plt.plot(P.T[0], P.T[1], label="Bezier Path")
plt.plot(cp.T[0], cp.T[1], '--o', label="Control Points")
plot_arrow(start_x, start_y, start_yaw)
plot_arrow(end_x, end_y, end_yaw)
plt.legend()
plt.axis("equal")
plt.grid(True)
plt.show()


def main2():
Expand All @@ -89,13 +97,21 @@ def main2():
for offset in np.arange(1.0, 5.0, 1.0):
P, cp = calc_4point_bezier_path(
start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset)
plt.plot(P.T[0], P.T[1], label="Offset=" + str(offset))
plot_arrow(start_x, start_y, start_yaw)
plot_arrow(end_x, end_y, end_yaw)
plt.legend()
plt.axis("equal")
plt.grid(True)
plt.show()
assert P.T[0][0] == start_x, "path is invalid"
assert P.T[1][0] == start_y, "path is invalid"
assert P.T[0][-1] == end_x, "path is invalid"
assert P.T[1][-1] == end_y, "path is invalid"

if show_animation:
plt.plot(P.T[0], P.T[1], label="Offset=" + str(offset))

if show_animation:
plot_arrow(start_x, start_y, start_yaw)
plot_arrow(end_x, end_y, end_yaw)
plt.legend()
plt.axis("equal")
plt.grid(True)
plt.show()


if __name__ == '__main__':
Expand Down
16 changes: 16 additions & 0 deletions tests/test_bezier_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from unittest import TestCase

import sys
sys.path.append("./PathPlanning/BezierPath/")

from PathPlanning.BezierPath import bezier_path as m

print(__file__)


class Test(TestCase):

def test1(self):
m.show_animation = False
m.main()
m.main2()

0 comments on commit 4433fed

Please sign in to comment.