Skip to content

Commit 041ad9f

Browse files
committed
add a star test and dijkstra test
1 parent da82628 commit 041ad9f

File tree

4 files changed

+77
-48
lines changed

4 files changed

+77
-48
lines changed

PathPlanning/AStar/a_star.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
22
A* grid based planning
33
4-
author: Atsushi Sakai
4+
author: Atsushi Sakai(@Atsushi_twi)
55
"""
66

77
import matplotlib.pyplot as plt
88
import math
9-
from matplotrecorder import matplotrecorder
10-
matplotrecorder.donothing = True
9+
10+
show_animation = True
1111

1212

1313
class Node:
@@ -22,6 +22,19 @@ def __str__(self):
2222
return str(self.x) + "," + str(self.y) + "," + str(self.cost) + "," + str(self.pind)
2323

2424

25+
def calc_fianl_path(ngoal, closedset, reso):
26+
# generate final course
27+
rx, ry = [ngoal.x * reso], [ngoal.y * reso]
28+
pind = ngoal.pind
29+
while pind != -1:
30+
n = closedset[pind]
31+
rx.append(n.x * reso)
32+
ry.append(n.y * reso)
33+
pind = n.pind
34+
35+
return rx, ry
36+
37+
2538
def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
2639
"""
2740
gx: goal x position [m]
@@ -51,10 +64,10 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
5164
# print("current", current)
5265

5366
# show graph
54-
plt.plot(current.x * reso, current.y * reso, "xc")
55-
if len(closedset.keys()) % 10 == 0:
56-
plt.pause(0.001)
57-
matplotrecorder.save_frame()
67+
if show_animation:
68+
plt.plot(current.x * reso, current.y * reso, "xc")
69+
if len(closedset.keys()) % 10 == 0:
70+
plt.pause(0.001)
5871

5972
if current.x == ngoal.x and current.y == ngoal.y:
6073
print("Find goal")
@@ -86,14 +99,7 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
8699
else:
87100
openset[n_id] = node
88101

89-
# generate final course
90-
rx, ry = [ngoal.x * reso], [ngoal.y * reso]
91-
pind = ngoal.pind
92-
while pind != -1:
93-
n = closedset[pind]
94-
rx.append(n.x * reso)
95-
ry.append(n.y * reso)
96-
pind = n.pind
102+
rx, ry = calc_fianl_path(ngoal, closedset, reso)
97103

98104
return rx, ry
99105

@@ -182,8 +188,7 @@ def main():
182188
grid_size = 1.0 # [m]
183189
robot_size = 1.0 # [m]
184190

185-
ox = []
186-
oy = []
191+
ox, oy = [], []
187192

188193
for i in range(60):
189194
ox.append(i)
@@ -204,21 +209,18 @@ def main():
204209
ox.append(40.0)
205210
oy.append(60.0 - i)
206211

207-
plt.plot(ox, oy, ".k")
208-
plt.plot(sx, sy, "xr")
209-
plt.plot(gx, gy, "xb")
210-
plt.grid(True)
211-
plt.axis("equal")
212+
if show_animation:
213+
plt.plot(ox, oy, ".k")
214+
plt.plot(sx, sy, "xr")
215+
plt.plot(gx, gy, "xb")
216+
plt.grid(True)
217+
plt.axis("equal")
212218

213219
rx, ry = a_star_planning(sx, sy, gx, gy, ox, oy, grid_size, robot_size)
214220

215-
plt.plot(rx, ry, "-r")
216-
217-
for i in range(20):
218-
matplotrecorder.save_frame()
219-
plt.show()
220-
221-
matplotrecorder.save_movie("animation.gif", 0.1)
221+
if show_animation:
222+
plt.plot(rx, ry, "-r")
223+
plt.show()
222224

223225

224226
if __name__ == '__main__':

PathPlanning/Dijkstra/dijkstra.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
22
Dijkstra grid based planning
33
4-
author: Atsushi Sakai
4+
author: Atsushi Sakai(@Atsushi_twi)
55
"""
66

77
import matplotlib.pyplot as plt
88
import math
9-
from matplotrecorder import matplotrecorder
10-
matplotrecorder.donothing = True
9+
10+
show_animation = True
1111

1212

1313
class Node:
@@ -50,10 +50,10 @@ def dijkstra_planning(sx, sy, gx, gy, ox, oy, reso, rr):
5050
# print("current", current)
5151

5252
# show graph
53-
plt.plot(current.x * reso, current.y * reso, "xc")
54-
if len(closedset.keys()) % 10 == 0:
55-
plt.pause(0.001)
56-
matplotrecorder.save_frame()
53+
if show_animation:
54+
plt.plot(current.x * reso, current.y * reso, "xc")
55+
if len(closedset.keys()) % 10 == 0:
56+
plt.pause(0.001)
5757

5858
if current.x == ngoal.x and current.y == ngoal.y:
5959
print("Find goal")
@@ -85,6 +85,12 @@ def dijkstra_planning(sx, sy, gx, gy, ox, oy, reso, rr):
8585
else:
8686
openset[n_id] = node
8787

88+
rx, ry = calc_fianl_path(ngoal, closedset, reso)
89+
90+
return rx, ry
91+
92+
93+
def calc_fianl_path(ngoal, closedset, reso):
8894
# generate final course
8995
rx, ry = [ngoal.x * reso], [ngoal.y * reso]
9096
pind = ngoal.pind
@@ -197,21 +203,18 @@ def main():
197203
ox.append(40.0)
198204
oy.append(60.0 - i)
199205

200-
plt.plot(ox, oy, ".k")
201-
plt.plot(sx, sy, "xr")
202-
plt.plot(gx, gy, "xb")
203-
plt.grid(True)
204-
plt.axis("equal")
206+
if show_animation:
207+
plt.plot(ox, oy, ".k")
208+
plt.plot(sx, sy, "xr")
209+
plt.plot(gx, gy, "xb")
210+
plt.grid(True)
211+
plt.axis("equal")
205212

206213
rx, ry = dijkstra_planning(sx, sy, gx, gy, ox, oy, grid_size, robot_size)
207214

208-
plt.plot(rx, ry, "-r")
209-
210-
for i in range(10):
211-
matplotrecorder.save_frame()
212-
plt.show()
213-
214-
matplotrecorder.save_movie("animation.gif", 0.1)
215+
if show_animation:
216+
plt.plot(rx, ry, "-r")
217+
plt.show()
215218

216219

217220
if __name__ == '__main__':

tests/test_a_star.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from unittest import TestCase
2+
3+
from PathPlanning.AStar import a_star as m
4+
5+
print(__file__)
6+
7+
8+
class Test(TestCase):
9+
10+
def test1(self):
11+
m.show_animation = False
12+
m.main()

tests/test_dijkstra.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from unittest import TestCase
2+
3+
from PathPlanning.Dijkstra import dijkstra as m
4+
5+
print(__file__)
6+
7+
8+
class Test(TestCase):
9+
10+
def test1(self):
11+
m.show_animation = False
12+
m.main()

0 commit comments

Comments
 (0)