rrt star code is clean up

This commit is contained in:
Atsushi Sakai
2018-01-10 11:39:13 -08:00
parent 03f0266fed
commit 947ff8b77a
4 changed files with 27 additions and 81 deletions

View File

@@ -1,59 +0,0 @@
"""
A simple Python module for recording matplotlib animation
This tool use convert command of ImageMagick
author: Atsushi Sakai
"""
import matplotlib.pyplot as plt
import subprocess
iframe = 0
donothing = False
def save_frame():
"""
Save a frame for movie
"""
if not donothing:
global iframe
plt.savefig("recoder" + '{0:04d}'.format(iframe) + '.png')
iframe += 1
def save_movie(fname, d_pause):
"""
Save movie as gif
"""
if not donothing:
cmd = "convert -delay " + str(int(d_pause * 100)) + \
" recoder*.png " + fname
subprocess.call(cmd, shell=True)
cmd = "rm recoder*.png"
subprocess.call(cmd, shell=True)
if __name__ == '__main__':
print("A sample recording start")
import math
time = range(50)
x1 = [math.cos(t / 10.0) for t in time]
y1 = [math.sin(t / 10.0) for t in time]
x2 = [math.cos(t / 10.0) + 2 for t in time]
y2 = [math.sin(t / 10.0) + 2 for t in time]
for ix1, iy1, ix2, iy2 in zip(x1, y1, x2, y2):
plt.plot(ix1, iy1, "xr")
plt.plot(ix2, iy2, "xb")
plt.axis("equal")
plt.pause(0.1)
save_frame() # save each frame
save_movie("animation.gif", 0.1)
# save_movie("animation.mp4", 0.1)

View File

@@ -1,11 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
u"""
@brief: Path Planning Sample Code with Randamized Rapidly-Exploring Random Trees (RRT)
"""
Path Planning Sample Code with RRT*
@author: AtsushiSakai(@Atsushi_twi)
@license: MIT
author: AtsushiSakai(@Atsushi_twi)
"""
@@ -13,16 +9,19 @@ import random
import math
import copy
import numpy as np
import matplotlib.pyplot as plt
show_animation = True
class RRT():
u"""
"""
Class for RRT Planning
"""
def __init__(self, start, goal, obstacleList, randArea,
expandDis=0.5, goalSampleRate=20, maxIter=1000):
u"""
"""
Setting Parameter
start:Start Position [x,y]
@@ -40,7 +39,7 @@ class RRT():
self.maxIter = maxIter
def Planning(self, animation=True):
u"""
"""
Pathplanning
animation: flag for animation on or off
@@ -186,7 +185,6 @@ class RRT():
u"""
Draw Graph
"""
import matplotlib.pyplot as plt
plt.clf()
if rnd is not None:
plt.plot(rnd[0], rnd[1], "^k")
@@ -203,7 +201,6 @@ class RRT():
plt.axis([-2, 15, -2, 15])
plt.grid(True)
plt.pause(0.01)
matplotrecorder.save_frame() # save each frame
def GetNearestListIndex(self, nodeList, rnd):
dlist = [(node.x - rnd[0]) ** 2 + (node.y - rnd[1])
@@ -224,7 +221,7 @@ class RRT():
class Node():
u"""
"""
RRT Node
"""
@@ -237,9 +234,6 @@ class Node():
if __name__ == '__main__':
print("Start rrt planning")
import matplotlib.pyplot as plt
import matplotrecorder
matplotrecorder.donothing = True
# ====Search Path with RRT====
obstacleList = [
@@ -254,16 +248,12 @@ if __name__ == '__main__':
# Set Initial parameters
rrt = RRT(start=[0, 0], goal=[5, 10],
randArea=[-2, 15], obstacleList=obstacleList)
path = rrt.Planning(animation=False)
path = rrt.Planning(animation=show_animation)
# Draw final path
rrt.DrawGraph()
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
plt.grid(True)
plt.pause(0.01) # Need for Mac
for i in range(10):
matplotrecorder.save_frame() # save each frame
plt.show()
matplotrecorder.save_movie("animation.gif", 0.1)