mirror of
https://github.com/AtsushiSakai/PythonRobotics.git
synced 2026-02-16 00:35:11 -05:00
add test and code clean up
This commit is contained in:
|
Before Width: | Height: | Size: 12 MiB After Width: | Height: | Size: 12 MiB |
@@ -10,6 +10,9 @@ import math
|
|||||||
import copy
|
import copy
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import dubins_path_planning
|
import dubins_path_planning
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
show_animation = True
|
||||||
|
|
||||||
|
|
||||||
class RRT():
|
class RRT():
|
||||||
@@ -18,7 +21,7 @@ class RRT():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, start, goal, obstacleList, randArea,
|
def __init__(self, start, goal, obstacleList, randArea,
|
||||||
goalSampleRate=10, maxIter=1000):
|
goalSampleRate=10, maxIter=100):
|
||||||
"""
|
"""
|
||||||
Setting Parameter
|
Setting Parameter
|
||||||
|
|
||||||
@@ -34,6 +37,7 @@ class RRT():
|
|||||||
self.maxrand = randArea[1]
|
self.maxrand = randArea[1]
|
||||||
self.goalSampleRate = goalSampleRate
|
self.goalSampleRate = goalSampleRate
|
||||||
self.maxIter = maxIter
|
self.maxIter = maxIter
|
||||||
|
self.obstacleList = obstacleList
|
||||||
|
|
||||||
def Planning(self, animation=True):
|
def Planning(self, animation=True):
|
||||||
"""
|
"""
|
||||||
@@ -50,7 +54,7 @@ class RRT():
|
|||||||
newNode = self.steer(rnd, nind)
|
newNode = self.steer(rnd, nind)
|
||||||
# print(newNode.cost)
|
# print(newNode.cost)
|
||||||
|
|
||||||
if self.CollisionCheck(newNode, obstacleList):
|
if self.CollisionCheck(newNode, self.obstacleList):
|
||||||
nearinds = self.find_near_nodes(newNode)
|
nearinds = self.find_near_nodes(newNode)
|
||||||
newNode = self.choose_parent(newNode, nearinds)
|
newNode = self.choose_parent(newNode, nearinds)
|
||||||
self.nodeList.append(newNode)
|
self.nodeList.append(newNode)
|
||||||
@@ -72,7 +76,7 @@ class RRT():
|
|||||||
dlist = []
|
dlist = []
|
||||||
for i in nearinds:
|
for i in nearinds:
|
||||||
tNode = self.steer(newNode, i)
|
tNode = self.steer(newNode, i)
|
||||||
if self.CollisionCheck(tNode, obstacleList):
|
if self.CollisionCheck(tNode, self.obstacleList):
|
||||||
dlist.append(tNode.cost)
|
dlist.append(tNode.cost)
|
||||||
else:
|
else:
|
||||||
dlist.append(float("inf"))
|
dlist.append(float("inf"))
|
||||||
@@ -190,7 +194,7 @@ class RRT():
|
|||||||
nearNode = self.nodeList[i]
|
nearNode = self.nodeList[i]
|
||||||
tNode = self.steer(nearNode, nnode - 1)
|
tNode = self.steer(nearNode, nnode - 1)
|
||||||
|
|
||||||
obstacleOK = self.CollisionCheck(tNode, obstacleList)
|
obstacleOK = self.CollisionCheck(tNode, self.obstacleList)
|
||||||
imporveCost = nearNode.cost > tNode.cost
|
imporveCost = nearNode.cost > tNode.cost
|
||||||
|
|
||||||
if obstacleOK and imporveCost:
|
if obstacleOK and imporveCost:
|
||||||
@@ -201,7 +205,6 @@ class RRT():
|
|||||||
u"""
|
u"""
|
||||||
Draw Graph
|
Draw Graph
|
||||||
"""
|
"""
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
plt.clf()
|
plt.clf()
|
||||||
if rnd is not None:
|
if rnd is not None:
|
||||||
plt.plot(rnd.x, rnd.y, "^k")
|
plt.plot(rnd.x, rnd.y, "^k")
|
||||||
@@ -211,7 +214,7 @@ class RRT():
|
|||||||
# plt.plot([node.x, self.nodeList[node.parent].x], [
|
# plt.plot([node.x, self.nodeList[node.parent].x], [
|
||||||
# node.y, self.nodeList[node.parent].y], "-g")
|
# node.y, self.nodeList[node.parent].y], "-g")
|
||||||
|
|
||||||
for (ox, oy, size) in obstacleList:
|
for (ox, oy, size) in self.obstacleList:
|
||||||
plt.plot(ox, oy, "ok", ms=30 * size)
|
plt.plot(ox, oy, "ok", ms=30 * size)
|
||||||
|
|
||||||
dubins_path_planning.plot_arrow(
|
dubins_path_planning.plot_arrow(
|
||||||
@@ -263,9 +266,8 @@ class Node():
|
|||||||
self.parent = None
|
self.parent = None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def main():
|
||||||
print("Start rrt start planning")
|
print("Start rrt star with dubins planning")
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
# ====Search Path with RRT====
|
# ====Search Path with RRT====
|
||||||
obstacleList = [
|
obstacleList = [
|
||||||
@@ -282,12 +284,17 @@ if __name__ == '__main__':
|
|||||||
goal = [10.0, 10.0, math.radians(0.0)]
|
goal = [10.0, 10.0, math.radians(0.0)]
|
||||||
|
|
||||||
rrt = RRT(start, goal, randArea=[-2.0, 15.0], obstacleList=obstacleList)
|
rrt = RRT(start, goal, randArea=[-2.0, 15.0], obstacleList=obstacleList)
|
||||||
path = rrt.Planning(animation=True)
|
path = rrt.Planning(animation=show_animation)
|
||||||
|
|
||||||
# Draw final path
|
# Draw final path
|
||||||
rrt.DrawGraph()
|
if show_animation:
|
||||||
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
|
rrt.DrawGraph()
|
||||||
plt.grid(True)
|
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
|
||||||
plt.pause(0.001)
|
plt.grid(True)
|
||||||
|
plt.pause(0.001)
|
||||||
|
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
15
tests/test_rrt_star_dubins.py
Normal file
15
tests/test_rrt_star_dubins.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.path.append("./PathPlanning/RRTStarDubins/")
|
||||||
|
|
||||||
|
from PathPlanning.RRTStarDubins import rrt_star_dubins as m
|
||||||
|
|
||||||
|
print(__file__)
|
||||||
|
|
||||||
|
|
||||||
|
class Test(TestCase):
|
||||||
|
|
||||||
|
def test1(self):
|
||||||
|
m.show_animation = False
|
||||||
|
m.main()
|
||||||
Reference in New Issue
Block a user