Skip to content

Commit 2b00207

Browse files
authored
Fix Hybrid A* (AtsushiSakai#327)
1 parent ada734f commit 2b00207

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

PathPlanning/HybridAStar/hybrid_a_star.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
import math
1313
import matplotlib.pyplot as plt
1414
import sys
15-
sys.path.append("../ReedsSheppPath/")
15+
import os
16+
17+
sys.path.append(os.path.dirname(os.path.abspath(__file__))
18+
+ "/../ReedsSheppPath")
1619
try:
1720
from a_star_heuristic import dp_planning # , calc_obstacle_map
1821
import reeds_shepp_path_planning as rs
1922
from car import move, check_car_collision, MAX_STEER, WB, plot_car
20-
except:
23+
except Exception:
2124
raise
2225

2326

2427
XY_GRID_RESOLUTION = 2.0 # [m]
2528
YAW_GRID_RESOLUTION = np.deg2rad(15.0) # [rad]
2629
MOTION_RESOLUTION = 0.1 # [m] path interporate resolution
27-
N_STEER = 20.0 # number of steer command
30+
N_STEER = 20 # number of steer command
2831
H_COST = 1.0
2932
VR = 1.0 # robot radius
3033

@@ -131,7 +134,8 @@ def __init__(self, ox, oy, xyreso, yawreso):
131134

132135
def calc_motion_inputs():
133136

134-
for steer in np.concatenate((np.linspace(-MAX_STEER, MAX_STEER, N_STEER),[0.0])):
137+
for steer in np.concatenate((np.linspace(-MAX_STEER, MAX_STEER,
138+
N_STEER), [0.0])):
135139
for d in [1, -1]:
136140
yield [steer, d]
137141

@@ -225,7 +229,8 @@ def update_node_with_analystic_expantion(current, goal,
225229
apath = analytic_expantion(current, goal, c, ox, oy, kdtree)
226230

227231
if apath:
228-
plt.plot(apath.x, apath.y)
232+
if show_animation:
233+
plt.plot(apath.x, apath.y)
229234
fx = apath.x[1:]
230235
fy = apath.y[1:]
231236
fyaw = apath.yaw[1:]
@@ -337,6 +342,7 @@ def hybrid_a_star_planning(start, goal, ox, oy, xyreso, yawreso):
337342
current, ngoal, config, ox, oy, obkdtree)
338343

339344
if isupdated:
345+
print("path found")
340346
break
341347

342348
for neighbor in get_neighbors(current, config, ox, oy, obkdtree):
@@ -437,12 +443,16 @@ def main():
437443
start = [10.0, 10.0, np.deg2rad(90.0)]
438444
goal = [50.0, 50.0, np.deg2rad(-90.0)]
439445

440-
plt.plot(ox, oy, ".k")
441-
rs.plot_arrow(start[0], start[1], start[2], fc='g')
442-
rs.plot_arrow(goal[0], goal[1], goal[2])
446+
print("start : ", start)
447+
print("goal : ", goal)
448+
449+
if show_animation:
450+
plt.plot(ox, oy, ".k")
451+
rs.plot_arrow(start[0], start[1], start[2], fc='g')
452+
rs.plot_arrow(goal[0], goal[1], goal[2])
443453

444-
plt.grid(True)
445-
plt.axis("equal")
454+
plt.grid(True)
455+
plt.axis("equal")
446456

447457
path = hybrid_a_star_planning(
448458
start, goal, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION)
@@ -451,14 +461,15 @@ def main():
451461
y = path.ylist
452462
yaw = path.yawlist
453463

454-
for ix, iy, iyaw in zip(x, y, yaw):
455-
plt.cla()
456-
plt.plot(ox, oy, ".k")
457-
plt.plot(x, y, "-r", label="Hybrid A* path")
458-
plt.grid(True)
459-
plt.axis("equal")
460-
plot_car(ix, iy, iyaw)
461-
plt.pause(0.0001)
464+
if show_animation:
465+
for ix, iy, iyaw in zip(x, y, yaw):
466+
plt.cla()
467+
plt.plot(ox, oy, ".k")
468+
plt.plot(x, y, "-r", label="Hybrid A* path")
469+
plt.grid(True)
470+
plt.axis("equal")
471+
plot_car(ix, iy, iyaw)
472+
plt.pause(0.0001)
462473

463474
print(__file__ + " done!!")
464475

tests/test_hybrid_a_star.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from unittest import TestCase
2+
import sys
3+
import os
4+
sys.path.append(os.path.dirname(__file__) + "/../")
5+
sys.path.append(os.path.dirname(os.path.abspath(__file__))
6+
+ "/../PathPlanning/HybridAStar")
7+
try:
8+
from PathPlanning.HybridAStar import hybrid_a_star as m
9+
except Exception:
10+
raise
11+
12+
13+
class Test(TestCase):
14+
15+
def test1(self):
16+
m.show_animation = False
17+
m.main()
18+
19+
20+
if __name__ == '__main__': # pragma: no cover
21+
test = Test()
22+
test.test1()

0 commit comments

Comments
 (0)