|
@@ -5,30 +5,10 @@
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.pyplot as plt
|
|
|
-import environment
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def obs_detect(x, u, obs_map):
|
|
|
|
|
- """
|
|
|
|
|
- Detect if the next state is in obstacles using this input.
|
|
|
|
|
-
|
|
|
|
|
- :param x: current state
|
|
|
|
|
- :param u: input
|
|
|
|
|
- :param obs_map: map of obstacles
|
|
|
|
|
- :return: in obstacles: True / not in obstacles: False
|
|
|
|
|
- """
|
|
|
|
|
-
|
|
|
|
|
- x_next = [x[0] + u[0], x[1] + u[1]] # next state using input 'u'
|
|
|
|
|
- if u not in environment.motions or \
|
|
|
|
|
- obs_map[x_next[0]][x_next[1]] == 1: # if 'u' is feasible and next state is not in obstacles
|
|
|
|
|
- return True
|
|
|
|
|
- return False
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
def extract_path(xI, xG, parent, actions):
|
|
def extract_path(xI, xG, parent, actions):
|
|
|
"""
|
|
"""
|
|
|
Extract the path based on the relationship of nodes.
|
|
Extract the path based on the relationship of nodes.
|
|
|
-
|
|
|
|
|
:param xI: Starting node
|
|
:param xI: Starting node
|
|
|
:param xG: Goal node
|
|
:param xG: Goal node
|
|
|
:param parent: Relationship between nodes
|
|
:param parent: Relationship between nodes
|
|
@@ -47,28 +27,27 @@ def extract_path(xI, xG, parent, actions):
|
|
|
return list(reversed(path_back)), list(reversed(acts_back))
|
|
return list(reversed(path_back)), list(reversed(acts_back))
|
|
|
|
|
|
|
|
|
|
|
|
|
-def showPath(xI, xG, path, visited, name):
|
|
|
|
|
|
|
+def showPath(xI, xG, path):
|
|
|
"""
|
|
"""
|
|
|
Plot the path.
|
|
Plot the path.
|
|
|
-
|
|
|
|
|
:param xI: Starting node
|
|
:param xI: Starting node
|
|
|
:param xG: Goal node
|
|
:param xG: Goal node
|
|
|
:param path: Planning path
|
|
:param path: Planning path
|
|
|
- :param visited: Visited nodes
|
|
|
|
|
- :param name: Name of this figure
|
|
|
|
|
:return: A plot
|
|
:return: A plot
|
|
|
"""
|
|
"""
|
|
|
-
|
|
|
|
|
- background = environment.obstacles()
|
|
|
|
|
- fig, ax = plt.subplots()
|
|
|
|
|
- for k in range(len(visited)):
|
|
|
|
|
- background[visited[k][1]][visited[k][0]] = [.5, .5, .5] # visited nodes: gray color
|
|
|
|
|
- for k in range(len(path)):
|
|
|
|
|
- background[path[k][1]][path[k][0]] = [1., 0., 0.] # path: red color
|
|
|
|
|
- background[xI[1]][xI[0]] = [0., 0., 1.] # starting node: blue color
|
|
|
|
|
- background[xG[1]][xG[0]] = [0., 1., .5] # goal node: green color
|
|
|
|
|
- ax.imshow(background)
|
|
|
|
|
- ax.invert_yaxis() # put origin of coordinate to left-bottom
|
|
|
|
|
- plt.title(name, fontdict=None)
|
|
|
|
|
|
|
+ path.remove(xI)
|
|
|
|
|
+ path.remove(xG)
|
|
|
|
|
+ path_x = [path[i][0] for i in range(len(path))]
|
|
|
|
|
+ path_y = [path[i][1] for i in range(len(path))]
|
|
|
|
|
+ plt.plot(path_x, path_y, linewidth='5', color='r', linestyle='-')
|
|
|
|
|
+ plt.pause(0.001)
|
|
|
plt.show()
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+def plot_dots(x, length):
|
|
|
|
|
+ plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
|
|
|
|
|
+ plt.gcf().canvas.mpl_connect('key_release_event',
|
|
|
|
|
+ lambda event: [exit(0) if event.key == 'escape' else None])
|
|
|
|
|
+ if length % 15 == 0:
|
|
|
|
|
+ plt.pause(0.001)
|
|
|
|
|
+
|