nwo
stringlengths
10
28
sha
stringlengths
40
40
path
stringlengths
11
97
identifier
stringlengths
1
64
parameters
stringlengths
2
2.24k
return_statement
stringlengths
0
2.17k
docstring
stringlengths
0
5.45k
docstring_summary
stringlengths
0
3.83k
func_begin
int64
1
13.4k
func_end
int64
2
13.4k
function
stringlengths
28
56.4k
url
stringlengths
106
209
project
int64
1
48
executed_lines
list
executed_lines_pc
float64
0
153
missing_lines
list
missing_lines_pc
float64
0
100
covered
bool
2 classes
filecoverage
float64
2.53
100
function_lines
int64
2
1.46k
mccabe
int64
1
253
coverage
float64
0
100
docstring_lines
int64
0
112
function_nodoc
stringlengths
9
56.4k
id
int64
0
29.8k
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ClothoidPath/clothoid_path_planner.py
get_axes_limits
(clothoids)
return x_min, x_max, y_min, y_max
124
141
def get_axes_limits(clothoids): x_vals = [p.x for clothoid in clothoids for p in clothoid] y_vals = [p.y for clothoid in clothoids for p in clothoid] x_min = min(x_vals) x_max = max(x_vals) y_min = min(y_vals) y_max = max(y_vals) x_offset = 0.1*(x_max - x_min) y_offset = 0.1*(y_max - y_min) x_min = x_min - x_offset x_max = x_max + x_offset y_min = y_min - y_offset y_max = y_max + y_offset return x_min, x_max, y_min, y_max
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ClothoidPath/clothoid_path_planner.py#L124-L141
2
[ 0 ]
5.555556
[ 1, 2, 4, 5, 6, 7, 9, 10, 12, 13, 14, 15, 17 ]
72.222222
false
63.265306
18
3
27.777778
0
def get_axes_limits(clothoids): x_vals = [p.x for clothoid in clothoids for p in clothoid] y_vals = [p.y for clothoid in clothoids for p in clothoid] x_min = min(x_vals) x_max = max(x_vals) y_min = min(y_vals) y_max = max(y_vals) x_offset = 0.1*(x_max - x_min) y_offset = 0.1*(y_max - y_min) x_min = x_min - x_offset x_max = x_max + x_offset y_min = y_min - y_offset y_max = y_max + y_offset return x_min, x_max, y_min, y_max
892
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ClothoidPath/clothoid_path_planner.py
draw_clothoids
(start, goal, num_steps, clothoidal_paths, save_animation=False)
144
172
def draw_clothoids(start, goal, num_steps, clothoidal_paths, save_animation=False): fig = plt.figure(figsize=(10, 10)) x_min, x_max, y_min, y_max = get_axes_limits(clothoidal_paths) axes = plt.axes(xlim=(x_min, x_max), ylim=(y_min, y_max)) axes.plot(start.x, start.y, 'ro') axes.plot(goal.x, goal.y, 'ro') lines = [axes.plot([], [], 'b-')[0] for _ in range(len(clothoidal_paths))] def animate(i): for line, clothoid_path in zip(lines, clothoidal_paths): x = [p.x for p in clothoid_path[:i]] y = [p.y for p in clothoid_path[:i]] line.set_data(x, y) return lines anim = animation.FuncAnimation( fig, animate, frames=num_steps, interval=25, blit=True ) if save_animation: anim.save('clothoid.gif', fps=30, writer="imagemagick") plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ClothoidPath/clothoid_path_planner.py#L144-L172
2
[ 0 ]
3.448276
[ 3, 4, 5, 7, 8, 9, 11, 12, 13, 14, 15, 17, 19, 26, 27, 28 ]
55.172414
false
63.265306
29
7
44.827586
0
def draw_clothoids(start, goal, num_steps, clothoidal_paths, save_animation=False): fig = plt.figure(figsize=(10, 10)) x_min, x_max, y_min, y_max = get_axes_limits(clothoidal_paths) axes = plt.axes(xlim=(x_min, x_max), ylim=(y_min, y_max)) axes.plot(start.x, start.y, 'ro') axes.plot(goal.x, goal.y, 'ro') lines = [axes.plot([], [], 'b-')[0] for _ in range(len(clothoidal_paths))] def animate(i): for line, clothoid_path in zip(lines, clothoidal_paths): x = [p.x for p in clothoid_path[:i]] y = [p.y for p in clothoid_path[:i]] line.set_data(x, y) return lines anim = animation.FuncAnimation( fig, animate, frames=num_steps, interval=25, blit=True ) if save_animation: anim.save('clothoid.gif', fps=30, writer="imagemagick") plt.show()
893
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ClothoidPath/clothoid_path_planner.py
main
()
175
188
def main(): start_point = Point(0, 0) start_orientation_list = [0.0] goal_point = Point(10, 0) goal_orientation_list = np.linspace(-pi, pi, 75) num_path_points = 100 clothoid_paths = generate_clothoid_paths( start_point, start_orientation_list, goal_point, goal_orientation_list, num_path_points) if show_animation: draw_clothoids(start_point, goal_point, num_path_points, clothoid_paths, save_animation=False)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ClothoidPath/clothoid_path_planner.py#L175-L188
2
[ 0, 1, 2, 3, 4, 5, 6, 10 ]
57.142857
[ 11 ]
7.142857
false
63.265306
14
2
92.857143
0
def main(): start_point = Point(0, 0) start_orientation_list = [0.0] goal_point = Point(10, 0) goal_orientation_list = np.linspace(-pi, pi, 75) num_path_points = 100 clothoid_paths = generate_clothoid_paths( start_point, start_orientation_list, goal_point, goal_orientation_list, num_path_points) if show_animation: draw_clothoids(start_point, goal_point, num_path_points, clothoid_paths, save_animation=False)
894
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
main
()
199
238
def main(): m = Map(100, 100) ox, oy = [], [] for i in range(-10, 60): ox.append(i) oy.append(-10) for i in range(-10, 60): ox.append(60) oy.append(i) for i in range(-10, 61): ox.append(i) oy.append(60) for i in range(-10, 61): ox.append(-10) oy.append(i) for i in range(-10, 40): ox.append(20) oy.append(i) for i in range(0, 40): ox.append(40) oy.append(60 - i) print([(i, j) for i, j in zip(ox, oy)]) m.set_obstacle([(i, j) for i, j in zip(ox, oy)]) start = [10, 10] goal = [50, 50] if show_animation: plt.plot(ox, oy, ".k") plt.plot(start[0], start[1], "og") plt.plot(goal[0], goal[1], "xb") plt.axis("equal") start = m.map[start[0]][start[1]] end = m.map[goal[0]][goal[1]] dstar = Dstar(m) rx, ry = dstar.run(start, end) if show_animation: plt.plot(rx, ry, "-r") plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L199-L238
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 37 ]
85
[ 27, 28, 29, 30, 38, 39 ]
15
false
79.775281
40
11
85
0
def main(): m = Map(100, 100) ox, oy = [], [] for i in range(-10, 60): ox.append(i) oy.append(-10) for i in range(-10, 60): ox.append(60) oy.append(i) for i in range(-10, 61): ox.append(i) oy.append(60) for i in range(-10, 61): ox.append(-10) oy.append(i) for i in range(-10, 40): ox.append(20) oy.append(i) for i in range(0, 40): ox.append(40) oy.append(60 - i) print([(i, j) for i, j in zip(ox, oy)]) m.set_obstacle([(i, j) for i, j in zip(ox, oy)]) start = [10, 10] goal = [50, 50] if show_animation: plt.plot(ox, oy, ".k") plt.plot(start[0], start[1], "og") plt.plot(goal[0], goal[1], "xb") plt.axis("equal") start = m.map[start[0]][start[1]] end = m.map[goal[0]][goal[1]] dstar = Dstar(m) rx, ry = dstar.run(start, end) if show_animation: plt.plot(rx, ry, "-r") plt.show()
895
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
State.__init__
(self, x, y)
21
28
def __init__(self, x, y): self.x = x self.y = y self.parent = None self.state = "." self.t = "new" # tag for state self.h = 0 self.k = 0
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L21-L28
2
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
100
[]
0
true
79.775281
8
1
100
0
def __init__(self, x, y): self.x = x self.y = y self.parent = None self.state = "." self.t = "new" # tag for state self.h = 0 self.k = 0
896
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
State.cost
(self, state)
return math.sqrt(math.pow((self.x - state.x), 2) + math.pow((self.y - state.y), 2))
30
35
def cost(self, state): if self.state == "#" or state.state == "#": return maxsize return math.sqrt(math.pow((self.x - state.x), 2) + math.pow((self.y - state.y), 2))
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L30-L35
2
[ 0, 1, 2, 3, 4 ]
83.333333
[]
0
false
79.775281
6
3
100
0
def cost(self, state): if self.state == "#" or state.state == "#": return maxsize return math.sqrt(math.pow((self.x - state.x), 2) + math.pow((self.y - state.y), 2))
897
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
State.set_state
(self, state)
.: new #: obstacle e: oparent of current state *: closed state s: current state
.: new #: obstacle e: oparent of current state *: closed state s: current state
37
47
def set_state(self, state): """ .: new #: obstacle e: oparent of current state *: closed state s: current state """ if state not in ["s", ".", "#", "e", "*"]: return self.state = state
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L37-L47
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 ]
90.909091
[ 9 ]
9.090909
false
79.775281
11
2
90.909091
5
def set_state(self, state): if state not in ["s", ".", "#", "e", "*"]: return self.state = state
898
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Map.__init__
(self, row, col)
52
55
def __init__(self, row, col): self.row = row self.col = col self.map = self.init_map()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L52-L55
2
[ 0, 1, 2, 3 ]
100
[]
0
true
79.775281
4
1
100
0
def __init__(self, row, col): self.row = row self.col = col self.map = self.init_map()
899
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Map.init_map
(self)
return map_list
57
64
def init_map(self): map_list = [] for i in range(self.row): tmp = [] for j in range(self.col): tmp.append(State(i, j)) map_list.append(tmp) return map_list
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L57-L64
2
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
100
[]
0
true
79.775281
8
3
100
0
def init_map(self): map_list = [] for i in range(self.row): tmp = [] for j in range(self.col): tmp.append(State(i, j)) map_list.append(tmp) return map_list
900
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Map.get_neighbors
(self, state)
return state_list
66
77
def get_neighbors(self, state): state_list = [] for i in [-1, 0, 1]: for j in [-1, 0, 1]: if i == 0 and j == 0: continue if state.x + i < 0 or state.x + i >= self.row: continue if state.y + j < 0 or state.y + j >= self.col: continue state_list.append(self.map[state.x + i][state.y + j]) return state_list
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L66-L77
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
100
[]
0
true
79.775281
12
9
100
0
def get_neighbors(self, state): state_list = [] for i in [-1, 0, 1]: for j in [-1, 0, 1]: if i == 0 and j == 0: continue if state.x + i < 0 or state.x + i >= self.row: continue if state.y + j < 0 or state.y + j >= self.col: continue state_list.append(self.map[state.x + i][state.y + j]) return state_list
901
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Map.set_obstacle
(self, point_list)
79
84
def set_obstacle(self, point_list): for x, y in point_list: if x < 0 or x >= self.row or y < 0 or y >= self.col: continue self.map[x][y].set_state("#")
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L79-L84
2
[ 0, 1, 2, 3, 4, 5 ]
100
[]
0
true
79.775281
6
6
100
0
def set_obstacle(self, point_list): for x, y in point_list: if x < 0 or x >= self.row or y < 0 or y >= self.col: continue self.map[x][y].set_state("#")
902
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.__init__
(self, maps)
88
90
def __init__(self, maps): self.map = maps self.open_list = set()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L88-L90
2
[ 0, 1, 2 ]
100
[]
0
true
79.775281
3
1
100
0
def __init__(self, maps): self.map = maps self.open_list = set()
903
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.process_state
(self)
return self.get_kmin()
92
124
def process_state(self): x = self.min_state() if x is None: return -1 k_old = self.get_kmin() self.remove(x) if k_old < x.h: for y in self.map.get_neighbors(x): if y.h <= k_old and x.h > y.h + x.cost(y): x.parent = y x.h = y.h + x.cost(y) elif k_old == x.h: for y in self.map.get_neighbors(x): if y.t == "new" or y.parent == x and y.h != x.h + x.cost(y) \ or y.parent != x and y.h > x.h + x.cost(y): y.parent = x self.insert(y, x.h + x.cost(y)) else: for y in self.map.get_neighbors(x): if y.t == "new" or y.parent == x and y.h != x.h + x.cost(y): y.parent = x self.insert(y, x.h + x.cost(y)) else: if y.parent != x and y.h > x.h + x.cost(y): self.insert(y, x.h) else: if y.parent != x and x.h > y.h + x.cost(y) \ and y.t == "close" and y.h > k_old: self.insert(y, y.h) return self.get_kmin()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L92-L124
2
[ 0, 1, 2, 3, 5, 6, 7, 8, 9, 14, 15, 16, 18, 19, 32 ]
45.454545
[ 4, 10, 11, 12, 13, 21, 22, 23, 24, 26, 27, 29, 31 ]
39.393939
false
79.775281
33
23
60.606061
0
def process_state(self): x = self.min_state() if x is None: return -1 k_old = self.get_kmin() self.remove(x) if k_old < x.h: for y in self.map.get_neighbors(x): if y.h <= k_old and x.h > y.h + x.cost(y): x.parent = y x.h = y.h + x.cost(y) elif k_old == x.h: for y in self.map.get_neighbors(x): if y.t == "new" or y.parent == x and y.h != x.h + x.cost(y) \ or y.parent != x and y.h > x.h + x.cost(y): y.parent = x self.insert(y, x.h + x.cost(y)) else: for y in self.map.get_neighbors(x): if y.t == "new" or y.parent == x and y.h != x.h + x.cost(y): y.parent = x self.insert(y, x.h + x.cost(y)) else: if y.parent != x and y.h > x.h + x.cost(y): self.insert(y, x.h) else: if y.parent != x and x.h > y.h + x.cost(y) \ and y.t == "close" and y.h > k_old: self.insert(y, y.h) return self.get_kmin()
904
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.min_state
(self)
return min_state
126
130
def min_state(self): if not self.open_list: return None min_state = min(self.open_list, key=lambda x: x.k) return min_state
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L126-L130
2
[ 0, 1, 3, 4 ]
80
[ 2 ]
20
false
79.775281
5
2
80
0
def min_state(self): if not self.open_list: return None min_state = min(self.open_list, key=lambda x: x.k) return min_state
905
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.get_kmin
(self)
return k_min
132
136
def get_kmin(self): if not self.open_list: return -1 k_min = min([x.k for x in self.open_list]) return k_min
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L132-L136
2
[ 0, 1, 3, 4 ]
80
[ 2 ]
20
false
79.775281
5
3
80
0
def get_kmin(self): if not self.open_list: return -1 k_min = min([x.k for x in self.open_list]) return k_min
906
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.insert
(self, state, h_new)
138
147
def insert(self, state, h_new): if state.t == "new": state.k = h_new elif state.t == "open": state.k = min(state.k, h_new) elif state.t == "close": state.k = min(state.h, h_new) state.h = h_new state.t = "open" self.open_list.add(state)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L138-L147
2
[ 0, 1, 2, 3, 4, 7, 8, 9 ]
80
[ 5, 6 ]
20
false
79.775281
10
4
80
0
def insert(self, state, h_new): if state.t == "new": state.k = h_new elif state.t == "open": state.k = min(state.k, h_new) elif state.t == "close": state.k = min(state.h, h_new) state.h = h_new state.t = "open" self.open_list.add(state)
907
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.remove
(self, state)
149
152
def remove(self, state): if state.t == "open": state.t = "close" self.open_list.remove(state)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L149-L152
2
[ 0, 1, 2, 3 ]
100
[]
0
true
79.775281
4
2
100
0
def remove(self, state): if state.t == "open": state.t = "close" self.open_list.remove(state)
908
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.modify_cost
(self, x)
154
156
def modify_cost(self, x): if x.t == "close": self.insert(x, x.parent.h + x.cost(x.parent))
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L154-L156
2
[ 0 ]
33.333333
[ 1, 2 ]
66.666667
false
79.775281
3
2
33.333333
0
def modify_cost(self, x): if x.t == "close": self.insert(x, x.parent.h + x.cost(x.parent))
909
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.run
(self, start, end)
return rx, ry
158
189
def run(self, start, end): rx = [] ry = [] self.insert(end, 0.0) while True: self.process_state() if start.t == "close": break start.set_state("s") s = start s = s.parent s.set_state("e") tmp = start while tmp != end: tmp.set_state("*") rx.append(tmp.x) ry.append(tmp.y) if show_animation: plt.plot(rx, ry, "-r") plt.pause(0.01) if tmp.parent.state == "#": self.modify(tmp) continue tmp = tmp.parent tmp.set_state("e") return rx, ry
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L158-L189
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 25, 28, 29, 30, 31 ]
87.5
[ 23, 24, 26, 27 ]
12.5
false
79.775281
32
6
87.5
0
def run(self, start, end): rx = [] ry = [] self.insert(end, 0.0) while True: self.process_state() if start.t == "close": break start.set_state("s") s = start s = s.parent s.set_state("e") tmp = start while tmp != end: tmp.set_state("*") rx.append(tmp.x) ry.append(tmp.y) if show_animation: plt.plot(rx, ry, "-r") plt.pause(0.01) if tmp.parent.state == "#": self.modify(tmp) continue tmp = tmp.parent tmp.set_state("e") return rx, ry
910
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DStar/dstar.py
Dstar.modify
(self, state)
191
196
def modify(self, state): self.modify_cost(state) while True: k_min = self.process_state() if k_min >= state.h: break
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DStar/dstar.py#L191-L196
2
[ 0 ]
16.666667
[ 1, 2, 3, 4, 5 ]
83.333333
false
79.775281
6
3
16.666667
0
def modify(self, state): self.modify_cost(state) while True: k_min = self.process_state() if k_min >= state.h: break
911
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
calc_frenet_paths
(c_speed, c_accel, c_d, c_d_d, c_d_dd, s0)
return frenet_paths
119
161
def calc_frenet_paths(c_speed, c_accel, c_d, c_d_d, c_d_dd, s0): frenet_paths = [] # generate path to each offset goal for di in np.arange(-MAX_ROAD_WIDTH, MAX_ROAD_WIDTH, D_ROAD_W): # Lateral motion planning for Ti in np.arange(MIN_T, MAX_T, DT): fp = FrenetPath() # lat_qp = quintic_polynomial(c_d, c_d_d, c_d_dd, di, 0.0, 0.0, Ti) lat_qp = QuinticPolynomial(c_d, c_d_d, c_d_dd, di, 0.0, 0.0, Ti) fp.t = [t for t in np.arange(0.0, Ti, DT)] fp.d = [lat_qp.calc_point(t) for t in fp.t] fp.d_d = [lat_qp.calc_first_derivative(t) for t in fp.t] fp.d_dd = [lat_qp.calc_second_derivative(t) for t in fp.t] fp.d_ddd = [lat_qp.calc_third_derivative(t) for t in fp.t] # Longitudinal motion planning (Velocity keeping) for tv in np.arange(TARGET_SPEED - D_T_S * N_S_SAMPLE, TARGET_SPEED + D_T_S * N_S_SAMPLE, D_T_S): tfp = copy.deepcopy(fp) lon_qp = QuarticPolynomial(s0, c_speed, c_accel, tv, 0.0, Ti) tfp.s = [lon_qp.calc_point(t) for t in fp.t] tfp.s_d = [lon_qp.calc_first_derivative(t) for t in fp.t] tfp.s_dd = [lon_qp.calc_second_derivative(t) for t in fp.t] tfp.s_ddd = [lon_qp.calc_third_derivative(t) for t in fp.t] Jp = sum(np.power(tfp.d_ddd, 2)) # square of jerk Js = sum(np.power(tfp.s_ddd, 2)) # square of jerk # square of diff from target speed ds = (TARGET_SPEED - tfp.s_d[-1]) ** 2 tfp.cd = K_J * Jp + K_T * Ti + K_D * tfp.d[-1] ** 2 tfp.cv = K_J * Js + K_T * Ti + K_D * ds tfp.cf = K_LAT * tfp.cd + K_LON * tfp.cv frenet_paths.append(tfp) return frenet_paths
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L119-L161
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 ]
97.674419
[]
0
false
97.311828
43
13
100
0
def calc_frenet_paths(c_speed, c_accel, c_d, c_d_d, c_d_dd, s0): frenet_paths = [] # generate path to each offset goal for di in np.arange(-MAX_ROAD_WIDTH, MAX_ROAD_WIDTH, D_ROAD_W): # Lateral motion planning for Ti in np.arange(MIN_T, MAX_T, DT): fp = FrenetPath() # lat_qp = quintic_polynomial(c_d, c_d_d, c_d_dd, di, 0.0, 0.0, Ti) lat_qp = QuinticPolynomial(c_d, c_d_d, c_d_dd, di, 0.0, 0.0, Ti) fp.t = [t for t in np.arange(0.0, Ti, DT)] fp.d = [lat_qp.calc_point(t) for t in fp.t] fp.d_d = [lat_qp.calc_first_derivative(t) for t in fp.t] fp.d_dd = [lat_qp.calc_second_derivative(t) for t in fp.t] fp.d_ddd = [lat_qp.calc_third_derivative(t) for t in fp.t] # Longitudinal motion planning (Velocity keeping) for tv in np.arange(TARGET_SPEED - D_T_S * N_S_SAMPLE, TARGET_SPEED + D_T_S * N_S_SAMPLE, D_T_S): tfp = copy.deepcopy(fp) lon_qp = QuarticPolynomial(s0, c_speed, c_accel, tv, 0.0, Ti) tfp.s = [lon_qp.calc_point(t) for t in fp.t] tfp.s_d = [lon_qp.calc_first_derivative(t) for t in fp.t] tfp.s_dd = [lon_qp.calc_second_derivative(t) for t in fp.t] tfp.s_ddd = [lon_qp.calc_third_derivative(t) for t in fp.t] Jp = sum(np.power(tfp.d_ddd, 2)) # square of jerk Js = sum(np.power(tfp.s_ddd, 2)) # square of jerk # square of diff from target speed ds = (TARGET_SPEED - tfp.s_d[-1]) ** 2 tfp.cd = K_J * Jp + K_T * Ti + K_D * tfp.d[-1] ** 2 tfp.cv = K_J * Js + K_T * Ti + K_D * ds tfp.cf = K_LAT * tfp.cd + K_LON * tfp.cv frenet_paths.append(tfp) return frenet_paths
912
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
calc_global_paths
(fplist, csp)
return fplist
164
193
def calc_global_paths(fplist, csp): for fp in fplist: # calc global positions for i in range(len(fp.s)): ix, iy = csp.calc_position(fp.s[i]) if ix is None: break i_yaw = csp.calc_yaw(fp.s[i]) di = fp.d[i] fx = ix + di * math.cos(i_yaw + math.pi / 2.0) fy = iy + di * math.sin(i_yaw + math.pi / 2.0) fp.x.append(fx) fp.y.append(fy) # calc yaw and ds for i in range(len(fp.x) - 1): dx = fp.x[i + 1] - fp.x[i] dy = fp.y[i + 1] - fp.y[i] fp.yaw.append(math.atan2(dy, dx)) fp.ds.append(math.hypot(dx, dy)) fp.yaw.append(fp.yaw[-1]) fp.ds.append(fp.ds[-1]) # calc curvature for i in range(len(fp.yaw) - 1): fp.c.append((fp.yaw[i + 1] - fp.yaw[i]) / fp.ds[i]) return fplist
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L164-L193
2
[ 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]
96.666667
[ 7 ]
3.333333
false
97.311828
30
6
96.666667
0
def calc_global_paths(fplist, csp): for fp in fplist: # calc global positions for i in range(len(fp.s)): ix, iy = csp.calc_position(fp.s[i]) if ix is None: break i_yaw = csp.calc_yaw(fp.s[i]) di = fp.d[i] fx = ix + di * math.cos(i_yaw + math.pi / 2.0) fy = iy + di * math.sin(i_yaw + math.pi / 2.0) fp.x.append(fx) fp.y.append(fy) # calc yaw and ds for i in range(len(fp.x) - 1): dx = fp.x[i + 1] - fp.x[i] dy = fp.y[i + 1] - fp.y[i] fp.yaw.append(math.atan2(dy, dx)) fp.ds.append(math.hypot(dx, dy)) fp.yaw.append(fp.yaw[-1]) fp.ds.append(fp.ds[-1]) # calc curvature for i in range(len(fp.yaw) - 1): fp.c.append((fp.yaw[i + 1] - fp.yaw[i]) / fp.ds[i]) return fplist
913
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
check_collision
(fp, ob)
return True
196
206
def check_collision(fp, ob): for i in range(len(ob[:, 0])): d = [((ix - ob[i, 0]) ** 2 + (iy - ob[i, 1]) ** 2) for (ix, iy) in zip(fp.x, fp.y)] collision = any([di <= ROBOT_RADIUS ** 2 for di in d]) if collision: return False return True
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L196-L206
2
[ 0, 1, 2, 4, 5, 6, 7, 8, 9, 10 ]
90.909091
[]
0
false
97.311828
11
5
100
0
def check_collision(fp, ob): for i in range(len(ob[:, 0])): d = [((ix - ob[i, 0]) ** 2 + (iy - ob[i, 1]) ** 2) for (ix, iy) in zip(fp.x, fp.y)] collision = any([di <= ROBOT_RADIUS ** 2 for di in d]) if collision: return False return True
914
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
check_paths
(fplist, ob)
return [fplist[i] for i in ok_ind]
209
225
def check_paths(fplist, ob): ok_ind = [] for i, _ in enumerate(fplist): if any([v > MAX_SPEED for v in fplist[i].s_d]): # Max speed check continue elif any([abs(a) > MAX_ACCEL for a in fplist[i].s_dd]): # Max accel check continue elif any([abs(c) > MAX_CURVATURE for c in fplist[i].c]): # Max curvature check continue elif not check_collision(fplist[i], ob): continue ok_ind.append(i) return [fplist[i] for i in ok_ind]
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L209-L225
2
[ 0, 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16 ]
82.352941
[ 4 ]
5.882353
false
97.311828
17
10
94.117647
0
def check_paths(fplist, ob): ok_ind = [] for i, _ in enumerate(fplist): if any([v > MAX_SPEED for v in fplist[i].s_d]): # Max speed check continue elif any([abs(a) > MAX_ACCEL for a in fplist[i].s_dd]): # Max accel check continue elif any([abs(c) > MAX_CURVATURE for c in fplist[i].c]): # Max curvature check continue elif not check_collision(fplist[i], ob): continue ok_ind.append(i) return [fplist[i] for i in ok_ind]
915
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
frenet_optimal_planning
(csp, s0, c_speed, c_accel, c_d, c_d_d, c_d_dd, ob)
return best_path
228
241
def frenet_optimal_planning(csp, s0, c_speed, c_accel, c_d, c_d_d, c_d_dd, ob): fplist = calc_frenet_paths(c_speed, c_accel, c_d, c_d_d, c_d_dd, s0) fplist = calc_global_paths(fplist, csp) fplist = check_paths(fplist, ob) # find minimum cost path min_cost = float("inf") best_path = None for fp in fplist: if min_cost >= fp.cf: min_cost = fp.cf best_path = fp return best_path
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L228-L241
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
100
[]
0
true
97.311828
14
3
100
0
def frenet_optimal_planning(csp, s0, c_speed, c_accel, c_d, c_d_d, c_d_dd, ob): fplist = calc_frenet_paths(c_speed, c_accel, c_d, c_d_d, c_d_dd, s0) fplist = calc_global_paths(fplist, csp) fplist = check_paths(fplist, ob) # find minimum cost path min_cost = float("inf") best_path = None for fp in fplist: if min_cost >= fp.cf: min_cost = fp.cf best_path = fp return best_path
916
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
generate_target_course
(x, y)
return rx, ry, ryaw, rk, csp
244
256
def generate_target_course(x, y): csp = cubic_spline_planner.CubicSpline2D(x, y) s = np.arange(0, csp.s[-1], 0.1) rx, ry, ryaw, rk = [], [], [], [] for i_s in s: ix, iy = csp.calc_position(i_s) rx.append(ix) ry.append(iy) ryaw.append(csp.calc_yaw(i_s)) rk.append(csp.calc_curvature(i_s)) return rx, ry, ryaw, rk, csp
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L244-L256
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
100
[]
0
true
97.311828
13
2
100
0
def generate_target_course(x, y): csp = cubic_spline_planner.CubicSpline2D(x, y) s = np.arange(0, csp.s[-1], 0.1) rx, ry, ryaw, rk = [], [], [], [] for i_s in s: ix, iy = csp.calc_position(i_s) rx.append(ix) ry.append(iy) ryaw.append(csp.calc_yaw(i_s)) rk.append(csp.calc_curvature(i_s)) return rx, ry, ryaw, rk, csp
917
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
main
()
259
320
def main(): print(__file__ + " start!!") # way points wx = [0.0, 10.0, 20.5, 35.0, 70.5] wy = [0.0, -6.0, 5.0, 6.5, 0.0] # obstacle lists ob = np.array([[20.0, 10.0], [30.0, 6.0], [30.0, 8.0], [35.0, 8.0], [50.0, 3.0] ]) tx, ty, tyaw, tc, csp = generate_target_course(wx, wy) # initial state c_speed = 10.0 / 3.6 # current speed [m/s] c_accel = 0.0 # current acceleration [m/ss] c_d = 2.0 # current lateral position [m] c_d_d = 0.0 # current lateral speed [m/s] c_d_dd = 0.0 # current lateral acceleration [m/s] s0 = 0.0 # current course position area = 20.0 # animation area length [m] for i in range(SIM_LOOP): path = frenet_optimal_planning( csp, s0, c_speed, c_accel, c_d, c_d_d, c_d_dd, ob) s0 = path.s[1] c_d = path.d[1] c_d_d = path.d_d[1] c_d_dd = path.d_dd[1] c_speed = path.s_d[1] c_accel = path.s_dd[1] if np.hypot(path.x[1] - tx[-1], path.y[1] - ty[-1]) <= 1.0: print("Goal") break if show_animation: # pragma: no cover plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.plot(tx, ty) plt.plot(ob[:, 0], ob[:, 1], "xk") plt.plot(path.x[1:], path.y[1:], "-or") plt.plot(path.x[1], path.y[1], "vc") plt.xlim(path.x[1] - area, path.x[1] + area) plt.ylim(path.y[1] - area, path.y[1] + area) plt.title("v[km/h]:" + str(c_speed * 3.6)[0:4]) plt.grid(True) plt.pause(0.0001) print("Finish") if show_animation: # pragma: no cover plt.grid(True) plt.pause(0.0001) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L259-L320
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 56, 57 ]
73.913043
[ 38, 39 ]
4.347826
false
97.311828
62
5
95.652174
0
def main(): print(__file__ + " start!!") # way points wx = [0.0, 10.0, 20.5, 35.0, 70.5] wy = [0.0, -6.0, 5.0, 6.5, 0.0] # obstacle lists ob = np.array([[20.0, 10.0], [30.0, 6.0], [30.0, 8.0], [35.0, 8.0], [50.0, 3.0] ]) tx, ty, tyaw, tc, csp = generate_target_course(wx, wy) # initial state c_speed = 10.0 / 3.6 # current speed [m/s] c_accel = 0.0 # current acceleration [m/ss] c_d = 2.0 # current lateral position [m] c_d_d = 0.0 # current lateral speed [m/s] c_d_dd = 0.0 # current lateral acceleration [m/s] s0 = 0.0 # current course position area = 20.0 # animation area length [m] for i in range(SIM_LOOP): path = frenet_optimal_planning( csp, s0, c_speed, c_accel, c_d, c_d_d, c_d_dd, ob) s0 = path.s[1] c_d = path.d[1] c_d_d = path.d_d[1] c_d_dd = path.d_dd[1] c_speed = path.s_d[1] c_accel = path.s_dd[1] if np.hypot(path.x[1] - tx[-1], path.y[1] - ty[-1]) <= 1.0: print("Goal") break if show_animation: # pragma: no cover plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.plot(tx, ty) plt.plot(ob[:, 0], ob[:, 1], "xk") plt.plot(path.x[1:], path.y[1:], "-or") plt.plot(path.x[1], path.y[1], "vc") plt.xlim(path.x[1] - area, path.x[1] + area) plt.ylim(path.y[1] - area, path.y[1] + area) plt.title("v[km/h]:" + str(c_speed * 3.6)[0:4]) plt.grid(True) plt.pause(0.0001) print("Finish") if show_animation: # pragma: no cover plt.grid(True) plt.pause(0.0001) plt.show()
918
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
QuarticPolynomial.__init__
(self, xs, vxs, axs, vxe, axe, time)
57
71
def __init__(self, xs, vxs, axs, vxe, axe, time): # calc coefficient of quartic polynomial self.a0 = xs self.a1 = vxs self.a2 = axs / 2.0 A = np.array([[3 * time ** 2, 4 * time ** 3], [6 * time, 12 * time ** 2]]) b = np.array([vxe - self.a1 - 2 * self.a2 * time, axe - 2 * self.a2]) x = np.linalg.solve(A, b) self.a3 = x[0] self.a4 = x[1]
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L57-L71
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14 ]
86.666667
[]
0
false
97.311828
15
1
100
0
def __init__(self, xs, vxs, axs, vxe, axe, time): # calc coefficient of quartic polynomial self.a0 = xs self.a1 = vxs self.a2 = axs / 2.0 A = np.array([[3 * time ** 2, 4 * time ** 3], [6 * time, 12 * time ** 2]]) b = np.array([vxe - self.a1 - 2 * self.a2 * time, axe - 2 * self.a2]) x = np.linalg.solve(A, b) self.a3 = x[0] self.a4 = x[1]
919
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
QuarticPolynomial.calc_point
(self, t)
return xt
73
77
def calc_point(self, t): xt = self.a0 + self.a1 * t + self.a2 * t ** 2 + \ self.a3 * t ** 3 + self.a4 * t ** 4 return xt
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L73-L77
2
[ 0, 1, 3, 4 ]
80
[]
0
false
97.311828
5
1
100
0
def calc_point(self, t): xt = self.a0 + self.a1 * t + self.a2 * t ** 2 + \ self.a3 * t ** 3 + self.a4 * t ** 4 return xt
920
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
QuarticPolynomial.calc_first_derivative
(self, t)
return xt
79
83
def calc_first_derivative(self, t): xt = self.a1 + 2 * self.a2 * t + \ 3 * self.a3 * t ** 2 + 4 * self.a4 * t ** 3 return xt
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L79-L83
2
[ 0, 1, 3, 4 ]
80
[]
0
false
97.311828
5
1
100
0
def calc_first_derivative(self, t): xt = self.a1 + 2 * self.a2 * t + \ 3 * self.a3 * t ** 2 + 4 * self.a4 * t ** 3 return xt
921
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
QuarticPolynomial.calc_second_derivative
(self, t)
return xt
85
88
def calc_second_derivative(self, t): xt = 2 * self.a2 + 6 * self.a3 * t + 12 * self.a4 * t ** 2 return xt
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L85-L88
2
[ 0, 1, 2, 3 ]
100
[]
0
true
97.311828
4
1
100
0
def calc_second_derivative(self, t): xt = 2 * self.a2 + 6 * self.a3 * t + 12 * self.a4 * t ** 2 return xt
922
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
QuarticPolynomial.calc_third_derivative
(self, t)
return xt
90
93
def calc_third_derivative(self, t): xt = 6 * self.a3 + 24 * self.a4 * t return xt
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L90-L93
2
[ 0, 1, 2, 3 ]
100
[]
0
true
97.311828
4
1
100
0
def calc_third_derivative(self, t): xt = 6 * self.a3 + 24 * self.a4 * t return xt
923
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py
FrenetPath.__init__
(self)
98
116
def __init__(self): self.t = [] self.d = [] self.d_d = [] self.d_dd = [] self.d_ddd = [] self.s = [] self.s_d = [] self.s_dd = [] self.s_ddd = [] self.cd = 0.0 self.cv = 0.0 self.cf = 0.0 self.x = [] self.y = [] self.yaw = [] self.ds = [] self.c = []
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/FrenetOptimalTrajectory/frenet_optimal_trajectory.py#L98-L116
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]
100
[]
0
true
97.311828
19
1
100
0
def __init__(self): self.t = [] self.d = [] self.d_d = [] self.d_dd = [] self.d_ddd = [] self.s = [] self.s_d = [] self.s_dd = [] self.s_ddd = [] self.cd = 0.0 self.cv = 0.0 self.cf = 0.0 self.x = [] self.y = [] self.yaw = [] self.ds = [] self.c = []
924
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
calc_4points_bezier_path
(sx, sy, syaw, ex, ey, eyaw, offset)
return path, control_points
Compute control points and path given start and end position. :param sx: (float) x-coordinate of the starting point :param sy: (float) y-coordinate of the starting point :param syaw: (float) yaw angle at start :param ex: (float) x-coordinate of the ending point :param ey: (float) y-coordinate of the ending point :param eyaw: (float) yaw angle at the end :param offset: (float) :return: (numpy array, numpy array)
Compute control points and path given start and end position.
16
38
def calc_4points_bezier_path(sx, sy, syaw, ex, ey, eyaw, offset): """ Compute control points and path given start and end position. :param sx: (float) x-coordinate of the starting point :param sy: (float) y-coordinate of the starting point :param syaw: (float) yaw angle at start :param ex: (float) x-coordinate of the ending point :param ey: (float) y-coordinate of the ending point :param eyaw: (float) yaw angle at the end :param offset: (float) :return: (numpy array, numpy array) """ dist = np.hypot(sx - ex, sy - ey) / offset control_points = np.array( [[sx, sy], [sx + dist * np.cos(syaw), sy + dist * np.sin(syaw)], [ex - dist * np.cos(eyaw), ey - dist * np.sin(eyaw)], [ex, ey]]) path = calc_bezier_path(control_points, n_points=100) return path, control_points
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L16-L38
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 ]
100
[]
0
true
98.507463
23
1
100
10
def calc_4points_bezier_path(sx, sy, syaw, ex, ey, eyaw, offset): dist = np.hypot(sx - ex, sy - ey) / offset control_points = np.array( [[sx, sy], [sx + dist * np.cos(syaw), sy + dist * np.sin(syaw)], [ex - dist * np.cos(eyaw), ey - dist * np.sin(eyaw)], [ex, ey]]) path = calc_bezier_path(control_points, n_points=100) return path, control_points
925
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
calc_bezier_path
(control_points, n_points=100)
return np.array(traj)
Compute bezier path (trajectory) given control points. :param control_points: (numpy array) :param n_points: (int) number of points in the trajectory :return: (numpy array)
Compute bezier path (trajectory) given control points.
41
53
def calc_bezier_path(control_points, n_points=100): """ Compute bezier path (trajectory) given control points. :param control_points: (numpy array) :param n_points: (int) number of points in the trajectory :return: (numpy array) """ traj = [] for t in np.linspace(0, 1, n_points): traj.append(bezier(t, control_points)) return np.array(traj)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L41-L53
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
100
[]
0
true
98.507463
13
2
100
5
def calc_bezier_path(control_points, n_points=100): traj = [] for t in np.linspace(0, 1, n_points): traj.append(bezier(t, control_points)) return np.array(traj)
926
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
bernstein_poly
(n, i, t)
return scipy.special.comb(n, i) * t ** i * (1 - t) ** (n - i)
Bernstein polynom. :param n: (int) polynom degree :param i: (int) :param t: (float) :return: (float)
Bernstein polynom.
56
65
def bernstein_poly(n, i, t): """ Bernstein polynom. :param n: (int) polynom degree :param i: (int) :param t: (float) :return: (float) """ return scipy.special.comb(n, i) * t ** i * (1 - t) ** (n - i)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L56-L65
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
100
[]
0
true
98.507463
10
1
100
6
def bernstein_poly(n, i, t): return scipy.special.comb(n, i) * t ** i * (1 - t) ** (n - i)
927
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
bezier
(t, control_points)
return np.sum([bernstein_poly(n, i, t) * control_points[i] for i in range(n + 1)], axis=0)
Return one point on the bezier curve. :param t: (float) number in [0, 1] :param control_points: (numpy array) :return: (numpy array) Coordinates of the point
Return one point on the bezier curve.
68
77
def bezier(t, control_points): """ Return one point on the bezier curve. :param t: (float) number in [0, 1] :param control_points: (numpy array) :return: (numpy array) Coordinates of the point """ n = len(control_points) - 1 return np.sum([bernstein_poly(n, i, t) * control_points[i] for i in range(n + 1)], axis=0)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L68-L77
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
100
[]
0
true
98.507463
10
2
100
5
def bezier(t, control_points): n = len(control_points) - 1 return np.sum([bernstein_poly(n, i, t) * control_points[i] for i in range(n + 1)], axis=0)
928
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
bezier_derivatives_control_points
(control_points, n_derivatives)
return w
Compute control points of the successive derivatives of a given bezier curve. A derivative of a bezier curve is a bezier curve. See https://pomax.github.io/bezierinfo/#derivatives for detailed explanations :param control_points: (numpy array) :param n_derivatives: (int) e.g., n_derivatives=2 -> compute control points for first and second derivatives :return: ([numpy array])
Compute control points of the successive derivatives of a given bezier curve.
80
98
def bezier_derivatives_control_points(control_points, n_derivatives): """ Compute control points of the successive derivatives of a given bezier curve. A derivative of a bezier curve is a bezier curve. See https://pomax.github.io/bezierinfo/#derivatives for detailed explanations :param control_points: (numpy array) :param n_derivatives: (int) e.g., n_derivatives=2 -> compute control points for first and second derivatives :return: ([numpy array]) """ w = {0: control_points} for i in range(n_derivatives): n = len(w[i]) w[i + 1] = np.array([(n - 1) * (w[i][j + 1] - w[i][j]) for j in range(n - 1)]) return w
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L80-L98
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]
100
[]
0
true
98.507463
19
3
100
10
def bezier_derivatives_control_points(control_points, n_derivatives): w = {0: control_points} for i in range(n_derivatives): n = len(w[i]) w[i + 1] = np.array([(n - 1) * (w[i][j + 1] - w[i][j]) for j in range(n - 1)]) return w
929
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
curvature
(dx, dy, ddx, ddy)
return (dx * ddy - dy * ddx) / (dx ** 2 + dy ** 2) ** (3 / 2)
Compute curvature at one point given first and second derivatives. :param dx: (float) First derivative along x axis :param dy: (float) :param ddx: (float) Second derivative along x axis :param ddy: (float) :return: (float)
Compute curvature at one point given first and second derivatives.
101
111
def curvature(dx, dy, ddx, ddy): """ Compute curvature at one point given first and second derivatives. :param dx: (float) First derivative along x axis :param dy: (float) :param ddx: (float) Second derivative along x axis :param ddy: (float) :return: (float) """ return (dx * ddy - dy * ddx) / (dx ** 2 + dy ** 2) ** (3 / 2)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L101-L111
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
100
[]
0
true
98.507463
11
1
100
7
def curvature(dx, dy, ddx, ddy): return (dx * ddy - dy * ddx) / (dx ** 2 + dy ** 2) ** (3 / 2)
930
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
plot_arrow
(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k")
Plot arrow.
Plot arrow.
114
122
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): # pragma: no cover """Plot arrow.""" if not isinstance(x, float): for (ix, iy, iyaw) in zip(x, y, yaw): plot_arrow(ix, iy, iyaw) else: plt.arrow(x, y, length * np.cos(yaw), length * np.sin(yaw), fc=fc, ec=ec, head_width=width, head_length=width) plt.plot(x, y)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L114-L122
2
[]
0
[]
0
false
98.507463
9
3
100
1
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): # pragma: no cover if not isinstance(x, float): for (ix, iy, iyaw) in zip(x, y, yaw): plot_arrow(ix, iy, iyaw) else: plt.arrow(x, y, length * np.cos(yaw), length * np.sin(yaw), fc=fc, ec=ec, head_width=width, head_length=width) plt.plot(x, y)
931
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
main
()
Plot an example bezier curve.
Plot an example bezier curve.
125
180
def main(): """Plot an example bezier curve.""" start_x = 10.0 # [m] start_y = 1.0 # [m] start_yaw = np.radians(180.0) # [rad] end_x = -0.0 # [m] end_y = -3.0 # [m] end_yaw = np.radians(-45.0) # [rad] offset = 3.0 path, control_points = calc_4points_bezier_path( start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) # Note: alternatively, instead of specifying start and end position # you can directly define n control points and compute the path: # control_points = np.array([[5., 1.], [-2.78, 1.], [-11.5, -4.5], [-6., -8.]]) # path = calc_bezier_path(control_points, n_points=100) # Display the tangent, normal and radius of cruvature at a given point t = 0.86 # Number in [0, 1] x_target, y_target = bezier(t, control_points) derivatives_cp = bezier_derivatives_control_points(control_points, 2) point = bezier(t, control_points) dt = bezier(t, derivatives_cp[1]) ddt = bezier(t, derivatives_cp[2]) # Radius of curvature radius = 1 / curvature(dt[0], dt[1], ddt[0], ddt[1]) # Normalize derivative dt /= np.linalg.norm(dt, 2) tangent = np.array([point, point + dt]) normal = np.array([point, point + [- dt[1], dt[0]]]) curvature_center = point + np.array([- dt[1], dt[0]]) * radius circle = plt.Circle(tuple(curvature_center), radius, color=(0, 0.8, 0.8), fill=False, linewidth=1) assert path.T[0][0] == start_x, "path is invalid" assert path.T[1][0] == start_y, "path is invalid" assert path.T[0][-1] == end_x, "path is invalid" assert path.T[1][-1] == end_y, "path is invalid" if show_animation: # pragma: no cover fig, ax = plt.subplots() ax.plot(path.T[0], path.T[1], label="Bezier Path") ax.plot(control_points.T[0], control_points.T[1], '--o', label="Control Points") ax.plot(x_target, y_target) ax.plot(tangent[:, 0], tangent[:, 1], label="Tangent") ax.plot(normal[:, 0], normal[:, 1], label="Normal") ax.add_artist(circle) plot_arrow(start_x, start_y, start_yaw) plot_arrow(end_x, end_y, end_yaw) ax.legend() ax.axis("equal") ax.grid(True) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L125-L180
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55 ]
133.333333
[]
0
true
98.507463
56
6
100
1
def main(): start_x = 10.0 # [m] start_y = 1.0 # [m] start_yaw = np.radians(180.0) # [rad] end_x = -0.0 # [m] end_y = -3.0 # [m] end_yaw = np.radians(-45.0) # [rad] offset = 3.0 path, control_points = calc_4points_bezier_path( start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) # Note: alternatively, instead of specifying start and end position # you can directly define n control points and compute the path: # control_points = np.array([[5., 1.], [-2.78, 1.], [-11.5, -4.5], [-6., -8.]]) # path = calc_bezier_path(control_points, n_points=100) # Display the tangent, normal and radius of cruvature at a given point t = 0.86 # Number in [0, 1] x_target, y_target = bezier(t, control_points) derivatives_cp = bezier_derivatives_control_points(control_points, 2) point = bezier(t, control_points) dt = bezier(t, derivatives_cp[1]) ddt = bezier(t, derivatives_cp[2]) # Radius of curvature radius = 1 / curvature(dt[0], dt[1], ddt[0], ddt[1]) # Normalize derivative dt /= np.linalg.norm(dt, 2) tangent = np.array([point, point + dt]) normal = np.array([point, point + [- dt[1], dt[0]]]) curvature_center = point + np.array([- dt[1], dt[0]]) * radius circle = plt.Circle(tuple(curvature_center), radius, color=(0, 0.8, 0.8), fill=False, linewidth=1) assert path.T[0][0] == start_x, "path is invalid" assert path.T[1][0] == start_y, "path is invalid" assert path.T[0][-1] == end_x, "path is invalid" assert path.T[1][-1] == end_y, "path is invalid" if show_animation: # pragma: no cover fig, ax = plt.subplots() ax.plot(path.T[0], path.T[1], label="Bezier Path") ax.plot(control_points.T[0], control_points.T[1], '--o', label="Control Points") ax.plot(x_target, y_target) ax.plot(tangent[:, 0], tangent[:, 1], label="Tangent") ax.plot(normal[:, 0], normal[:, 1], label="Normal") ax.add_artist(circle) plot_arrow(start_x, start_y, start_yaw) plot_arrow(end_x, end_y, end_yaw) ax.legend() ax.axis("equal") ax.grid(True) plt.show()
932
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/BezierPath/bezier_path.py
main2
()
Show the effect of the offset.
Show the effect of the offset.
183
210
def main2(): """Show the effect of the offset.""" start_x = 10.0 # [m] start_y = 1.0 # [m] start_yaw = np.radians(180.0) # [rad] end_x = -0.0 # [m] end_y = -3.0 # [m] end_yaw = np.radians(-45.0) # [rad] for offset in np.arange(1.0, 5.0, 1.0): path, control_points = calc_4points_bezier_path( start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) assert path.T[0][0] == start_x, "path is invalid" assert path.T[1][0] == start_y, "path is invalid" assert path.T[0][-1] == end_x, "path is invalid" assert path.T[1][-1] == end_y, "path is invalid" if show_animation: # pragma: no cover plt.plot(path.T[0], path.T[1], label="Offset=" + str(offset)) if show_animation: # pragma: no cover plot_arrow(start_x, start_y, start_yaw) plot_arrow(end_x, end_y, end_yaw) plt.legend() plt.axis("equal") plt.grid(True) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/BezierPath/bezier_path.py#L183-L210
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ]
147.368421
[]
0
true
98.507463
28
8
100
1
def main2(): start_x = 10.0 # [m] start_y = 1.0 # [m] start_yaw = np.radians(180.0) # [rad] end_x = -0.0 # [m] end_y = -3.0 # [m] end_yaw = np.radians(-45.0) # [rad] for offset in np.arange(1.0, 5.0, 1.0): path, control_points = calc_4points_bezier_path( start_x, start_y, start_yaw, end_x, end_y, end_yaw, offset) assert path.T[0][0] == start_x, "path is invalid" assert path.T[1][0] == start_y, "path is invalid" assert path.T[0][-1] == end_x, "path is invalid" assert path.T[1][-1] == end_y, "path is invalid" if show_animation: # pragma: no cover plt.plot(path.T[0], path.T[1], label="Offset=" + str(offset)) if show_animation: # pragma: no cover plot_arrow(start_x, start_y, start_yaw) plot_arrow(end_x, end_y, end_yaw) plt.legend() plt.axis("equal") plt.grid(True) plt.show()
933
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
main
(max_iter=100)
226
257
def main(max_iter=100): print("Start " + __file__) # ====Search Path with RRT==== obstacleList = [ (5, 5, 1), (4, 6, 1), (4, 8, 1), (4, 10, 1), (6, 5, 1), (7, 5, 1), (8, 6, 1), (8, 8, 1), (8, 10, 1) ] # [x,y,size(radius)] # Set Initial parameters start = [0.0, 0.0, np.deg2rad(0.0)] goal = [6.0, 7.0, np.deg2rad(90.0)] rrt_star_reeds_shepp = RRTStarReedsShepp(start, goal, obstacleList, [-2.0, 15.0], max_iter=max_iter) path = rrt_star_reeds_shepp.planning(animation=show_animation) # Draw final path if path and show_animation: # pragma: no cover rrt_star_reeds_shepp.draw_graph() plt.plot([x for (x, y, yaw) in path], [y for (x, y, yaw) in path], '-r') plt.grid(True) plt.pause(0.001) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L226-L257
2
[ 0, 1, 2, 3, 4, 16, 17, 18, 19, 20, 23, 24, 25 ]
50
[]
0
false
80.851064
32
5
100
0
def main(max_iter=100): print("Start " + __file__) # ====Search Path with RRT==== obstacleList = [ (5, 5, 1), (4, 6, 1), (4, 8, 1), (4, 10, 1), (6, 5, 1), (7, 5, 1), (8, 6, 1), (8, 8, 1), (8, 10, 1) ] # [x,y,size(radius)] # Set Initial parameters start = [0.0, 0.0, np.deg2rad(0.0)] goal = [6.0, 7.0, np.deg2rad(90.0)] rrt_star_reeds_shepp = RRTStarReedsShepp(start, goal, obstacleList, [-2.0, 15.0], max_iter=max_iter) path = rrt_star_reeds_shepp.planning(animation=show_animation) # Draw final path if path and show_animation: # pragma: no cover rrt_star_reeds_shepp.draw_graph() plt.plot([x for (x, y, yaw) in path], [y for (x, y, yaw) in path], '-r') plt.grid(True) plt.pause(0.001) plt.show()
934
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.__init__
(self, start, goal, obstacle_list, rand_area, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0 )
Setting Parameter start:Start Position [x,y] goal:Goal Position [x,y] obstacleList:obstacle Positions [[x,y,size],...] randArea:Random Sampling Area [min,max] robot_radius: robot body modeled as circle with given radius
Setting Parameter
38
64
def __init__(self, start, goal, obstacle_list, rand_area, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0 ): """ Setting Parameter start:Start Position [x,y] goal:Goal Position [x,y] obstacleList:obstacle Positions [[x,y,size],...] randArea:Random Sampling Area [min,max] robot_radius: robot body modeled as circle with given radius """ self.start = self.Node(start[0], start[1], start[2]) self.end = self.Node(goal[0], goal[1], goal[2]) self.min_rand = rand_area[0] self.max_rand = rand_area[1] self.max_iter = max_iter self.obstacle_list = obstacle_list self.connect_circle_dist = connect_circle_dist self.robot_radius = robot_radius self.curvature = 1.0 self.goal_yaw_th = np.deg2rad(1.0) self.goal_xy_th = 0.5
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L38-L64
2
[ 0, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 ]
51.851852
[]
0
false
80.851064
27
1
100
7
def __init__(self, start, goal, obstacle_list, rand_area, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0 ): self.start = self.Node(start[0], start[1], start[2]) self.end = self.Node(goal[0], goal[1], goal[2]) self.min_rand = rand_area[0] self.max_rand = rand_area[1] self.max_iter = max_iter self.obstacle_list = obstacle_list self.connect_circle_dist = connect_circle_dist self.robot_radius = robot_radius self.curvature = 1.0 self.goal_yaw_th = np.deg2rad(1.0) self.goal_xy_th = 0.5
935
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.planning
(self, animation=True, search_until_max_iter=True)
return None
planning animation: flag for animation on or off
planning
66
106
def planning(self, animation=True, search_until_max_iter=True): """ planning animation: flag for animation on or off """ self.node_list = [self.start] for i in range(self.max_iter): print("Iter:", i, ", number of nodes:", len(self.node_list)) rnd = self.get_random_node() nearest_ind = self.get_nearest_node_index(self.node_list, rnd) new_node = self.steer(self.node_list[nearest_ind], rnd) if self.check_collision( new_node, self.obstacle_list, self.robot_radius): near_indexes = self.find_near_nodes(new_node) new_node = self.choose_parent(new_node, near_indexes) if new_node: self.node_list.append(new_node) self.rewire(new_node, near_indexes) self.try_goal_path(new_node) if animation and i % 5 == 0: self.plot_start_goal_arrow() self.draw_graph(rnd) if (not search_until_max_iter) and new_node: # check reaching the goal last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) print("reached max iteration") last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) else: print("Cannot find path") return None
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L66-L106
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 26, 27, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ]
87.804878
[ 24, 25, 28, 29, 30 ]
12.195122
false
80.851064
41
10
87.804878
3
def planning(self, animation=True, search_until_max_iter=True): self.node_list = [self.start] for i in range(self.max_iter): print("Iter:", i, ", number of nodes:", len(self.node_list)) rnd = self.get_random_node() nearest_ind = self.get_nearest_node_index(self.node_list, rnd) new_node = self.steer(self.node_list[nearest_ind], rnd) if self.check_collision( new_node, self.obstacle_list, self.robot_radius): near_indexes = self.find_near_nodes(new_node) new_node = self.choose_parent(new_node, near_indexes) if new_node: self.node_list.append(new_node) self.rewire(new_node, near_indexes) self.try_goal_path(new_node) if animation and i % 5 == 0: self.plot_start_goal_arrow() self.draw_graph(rnd) if (not search_until_max_iter) and new_node: # check reaching the goal last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) print("reached max iteration") last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) else: print("Cannot find path") return None
936
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.try_goal_path
(self, node)
108
118
def try_goal_path(self, node): goal = self.Node(self.end.x, self.end.y, self.end.yaw) new_node = self.steer(node, goal) if new_node is None: return if self.check_collision( new_node, self.obstacle_list, self.robot_radius): self.node_list.append(new_node)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L108-L118
2
[ 0, 1, 2, 3, 4, 5, 7, 8, 10 ]
81.818182
[ 6 ]
9.090909
false
80.851064
11
3
90.909091
0
def try_goal_path(self, node): goal = self.Node(self.end.x, self.end.y, self.end.yaw) new_node = self.steer(node, goal) if new_node is None: return if self.check_collision( new_node, self.obstacle_list, self.robot_radius): self.node_list.append(new_node)
937
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.draw_graph
(self, rnd=None)
120
139
def draw_graph(self, rnd=None): plt.clf() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if rnd is not None: plt.plot(rnd.x, rnd.y, "^k") for node in self.node_list: if node.parent: plt.plot(node.path_x, node.path_y, "-g") for (ox, oy, size) in self.obstacle_list: plt.plot(ox, oy, "ok", ms=30 * size) plt.plot(self.start.x, self.start.y, "xr") plt.plot(self.end.x, self.end.y, "xr") plt.axis([-2, 15, -2, 15]) plt.grid(True) self.plot_start_goal_arrow() plt.pause(0.01)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L120-L139
2
[ 0 ]
5
[ 1, 3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 18, 19 ]
75
false
80.851064
20
5
25
0
def draw_graph(self, rnd=None): plt.clf() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if rnd is not None: plt.plot(rnd.x, rnd.y, "^k") for node in self.node_list: if node.parent: plt.plot(node.path_x, node.path_y, "-g") for (ox, oy, size) in self.obstacle_list: plt.plot(ox, oy, "ok", ms=30 * size) plt.plot(self.start.x, self.start.y, "xr") plt.plot(self.end.x, self.end.y, "xr") plt.axis([-2, 15, -2, 15]) plt.grid(True) self.plot_start_goal_arrow() plt.pause(0.01)
938
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.plot_start_goal_arrow
(self)
141
145
def plot_start_goal_arrow(self): reeds_shepp_path_planning.plot_arrow( self.start.x, self.start.y, self.start.yaw) reeds_shepp_path_planning.plot_arrow( self.end.x, self.end.y, self.end.yaw)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L141-L145
2
[ 0 ]
20
[ 1, 3 ]
40
false
80.851064
5
1
60
0
def plot_start_goal_arrow(self): reeds_shepp_path_planning.plot_arrow( self.start.x, self.start.y, self.start.yaw) reeds_shepp_path_planning.plot_arrow( self.end.x, self.end.y, self.end.yaw)
939
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.steer
(self, from_node, to_node)
return new_node
147
167
def steer(self, from_node, to_node): px, py, pyaw, mode, course_lengths = reeds_shepp_path_planning.reeds_shepp_path_planning( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if not px: return None new_node = copy.deepcopy(from_node) new_node.x = px[-1] new_node.y = py[-1] new_node.yaw = pyaw[-1] new_node.path_x = px new_node.path_y = py new_node.path_yaw = pyaw new_node.cost += sum([abs(l) for l in course_lengths]) new_node.parent = from_node return new_node
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L147-L167
2
[ 0, 1, 2, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
85.714286
[ 7 ]
4.761905
false
80.851064
21
3
95.238095
0
def steer(self, from_node, to_node): px, py, pyaw, mode, course_lengths = reeds_shepp_path_planning.reeds_shepp_path_planning( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if not px: return None new_node = copy.deepcopy(from_node) new_node.x = px[-1] new_node.y = py[-1] new_node.yaw = pyaw[-1] new_node.path_x = px new_node.path_y = py new_node.path_yaw = pyaw new_node.cost += sum([abs(l) for l in course_lengths]) new_node.parent = from_node return new_node
940
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.calc_new_cost
(self, from_node, to_node)
return from_node.cost + sum([abs(l) for l in course_lengths])
169
177
def calc_new_cost(self, from_node, to_node): _, _, _, _, course_lengths = reeds_shepp_path_planning.reeds_shepp_path_planning( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if not course_lengths: return float("inf") return from_node.cost + sum([abs(l) for l in course_lengths])
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L169-L177
2
[ 0, 1, 2, 5, 7, 8 ]
66.666667
[ 6 ]
11.111111
false
80.851064
9
3
88.888889
0
def calc_new_cost(self, from_node, to_node): _, _, _, _, course_lengths = reeds_shepp_path_planning.reeds_shepp_path_planning( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if not course_lengths: return float("inf") return from_node.cost + sum([abs(l) for l in course_lengths])
941
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.get_random_node
(self)
return rnd
179
186
def get_random_node(self): rnd = self.Node(random.uniform(self.min_rand, self.max_rand), random.uniform(self.min_rand, self.max_rand), random.uniform(-math.pi, math.pi) ) return rnd
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L179-L186
2
[ 0, 1, 2, 6, 7 ]
62.5
[]
0
false
80.851064
8
1
100
0
def get_random_node(self): rnd = self.Node(random.uniform(self.min_rand, self.max_rand), random.uniform(self.min_rand, self.max_rand), random.uniform(-math.pi, math.pi) ) return rnd
942
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.search_best_goal_node
(self)
return None
188
213
def search_best_goal_node(self): goal_indexes = [] for (i, node) in enumerate(self.node_list): if self.calc_dist_to_goal(node.x, node.y) <= self.goal_xy_th: goal_indexes.append(i) print("goal_indexes:", len(goal_indexes)) # angle check final_goal_indexes = [] for i in goal_indexes: if abs(self.node_list[i].yaw - self.end.yaw) <= self.goal_yaw_th: final_goal_indexes.append(i) print("final_goal_indexes:", len(final_goal_indexes)) if not final_goal_indexes: return None min_cost = min([self.node_list[i].cost for i in final_goal_indexes]) print("min_cost:", min_cost) for i in final_goal_indexes: if self.node_list[i].cost == min_cost: return i return None
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L188-L213
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]
96.153846
[ 25 ]
3.846154
false
80.851064
26
9
96.153846
0
def search_best_goal_node(self): goal_indexes = [] for (i, node) in enumerate(self.node_list): if self.calc_dist_to_goal(node.x, node.y) <= self.goal_xy_th: goal_indexes.append(i) print("goal_indexes:", len(goal_indexes)) # angle check final_goal_indexes = [] for i in goal_indexes: if abs(self.node_list[i].yaw - self.end.yaw) <= self.goal_yaw_th: final_goal_indexes.append(i) print("final_goal_indexes:", len(final_goal_indexes)) if not final_goal_indexes: return None min_cost = min([self.node_list[i].cost for i in final_goal_indexes]) print("min_cost:", min_cost) for i in final_goal_indexes: if self.node_list[i].cost == min_cost: return i return None
943
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py
RRTStarReedsShepp.generate_final_course
(self, goal_index)
return path
215
223
def generate_final_course(self, goal_index): path = [[self.end.x, self.end.y, self.end.yaw]] node = self.node_list[goal_index] while node.parent: for (ix, iy, iyaw) in zip(reversed(node.path_x), reversed(node.path_y), reversed(node.path_yaw)): path.append([ix, iy, iyaw]) node = node.parent path.append([self.start.x, self.start.y, self.start.yaw]) return path
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarReedsShepp/rrt_star_reeds_shepp.py#L215-L223
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
100
[]
0
true
80.851064
9
3
100
0
def generate_final_course(self, goal_index): path = [[self.end.x, self.end.y, self.end.yaw]] node = self.node_list[goal_index] while node.parent: for (ix, iy, iyaw) in zip(reversed(node.path_x), reversed(node.path_y), reversed(node.path_yaw)): path.append([ix, iy, iyaw]) node = node.parent path.append([self.start.x, self.start.y, self.start.yaw]) return path
944
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
plot_arrow
(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k")
Plot arrow
Plot arrow
27
34
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): # pragma: no cover """ Plot arrow """ plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), fc=fc, ec=ec, head_width=width, head_length=width) plt.plot(x, y) plt.plot(0, 0)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L27-L34
2
[]
0
[]
0
false
89.855072
8
1
100
1
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): # pragma: no cover plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), fc=fc, ec=ec, head_width=width, head_length=width) plt.plot(x, y) plt.plot(0, 0)
945
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
calc_diff
(target, x, y, yaw)
return d
37
42
def calc_diff(target, x, y, yaw): d = np.array([target.x - x[-1], target.y - y[-1], motion_model.pi_2_pi(target.yaw - yaw[-1])]) return d
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L37-L42
2
[ 0, 1, 4, 5 ]
66.666667
[]
0
false
89.855072
6
1
100
0
def calc_diff(target, x, y, yaw): d = np.array([target.x - x[-1], target.y - y[-1], motion_model.pi_2_pi(target.yaw - yaw[-1])]) return d
946
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
calc_j
(target, p, h, k0)
return J
45
72
def calc_j(target, p, h, k0): xp, yp, yawp = motion_model.generate_last_state( p[0, 0] + h[0], p[1, 0], p[2, 0], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0] - h[0], p[1, 0], p[2, 0], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d1 = np.array((dp - dn) / (2.0 * h[0])).reshape(3, 1) xp, yp, yawp = motion_model.generate_last_state( p[0, 0], p[1, 0] + h[1], p[2, 0], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0], p[1, 0] - h[1], p[2, 0], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d2 = np.array((dp - dn) / (2.0 * h[1])).reshape(3, 1) xp, yp, yawp = motion_model.generate_last_state( p[0, 0], p[1, 0], p[2, 0] + h[2], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0], p[1, 0], p[2, 0] - h[2], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d3 = np.array((dp - dn) / (2.0 * h[2])).reshape(3, 1) J = np.hstack((d1, d2, d3)) return J
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L45-L72
2
[ 0, 1, 3, 4, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 19, 20, 22, 23, 24, 25, 26, 27 ]
78.571429
[]
0
false
89.855072
28
1
100
0
def calc_j(target, p, h, k0): xp, yp, yawp = motion_model.generate_last_state( p[0, 0] + h[0], p[1, 0], p[2, 0], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0] - h[0], p[1, 0], p[2, 0], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d1 = np.array((dp - dn) / (2.0 * h[0])).reshape(3, 1) xp, yp, yawp = motion_model.generate_last_state( p[0, 0], p[1, 0] + h[1], p[2, 0], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0], p[1, 0] - h[1], p[2, 0], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d2 = np.array((dp - dn) / (2.0 * h[1])).reshape(3, 1) xp, yp, yawp = motion_model.generate_last_state( p[0, 0], p[1, 0], p[2, 0] + h[2], k0) dp = calc_diff(target, [xp], [yp], [yawp]) xn, yn, yawn = motion_model.generate_last_state( p[0, 0], p[1, 0], p[2, 0] - h[2], k0) dn = calc_diff(target, [xn], [yn], [yawn]) d3 = np.array((dp - dn) / (2.0 * h[2])).reshape(3, 1) J = np.hstack((d1, d2, d3)) return J
947
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
selection_learning_param
(dp, p, k0, target)
return mina
75
95
def selection_learning_param(dp, p, k0, target): mincost = float("inf") mina = 1.0 maxa = 2.0 da = 0.5 for a in np.arange(mina, maxa, da): tp = p + a * dp xc, yc, yawc = motion_model.generate_last_state( tp[0], tp[1], tp[2], k0) dc = calc_diff(target, [xc], [yc], [yawc]) cost = np.linalg.norm(dc) if cost <= mincost and a != 0.0: mina = a mincost = cost # print(mincost, mina) # input() return mina
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L75-L95
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
95.238095
[]
0
false
89.855072
21
4
100
0
def selection_learning_param(dp, p, k0, target): mincost = float("inf") mina = 1.0 maxa = 2.0 da = 0.5 for a in np.arange(mina, maxa, da): tp = p + a * dp xc, yc, yawc = motion_model.generate_last_state( tp[0], tp[1], tp[2], k0) dc = calc_diff(target, [xc], [yc], [yawc]) cost = np.linalg.norm(dc) if cost <= mincost and a != 0.0: mina = a mincost = cost # print(mincost, mina) # input() return mina
948
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
show_trajectory
(target, xc, yc)
98
104
def show_trajectory(target, xc, yc): # pragma: no cover plt.clf() plot_arrow(target.x, target.y, target.yaw) plt.plot(xc, yc, "-r") plt.axis("equal") plt.grid(True) plt.pause(0.1)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L98-L104
2
[]
0
[]
0
false
89.855072
7
1
100
0
def show_trajectory(target, xc, yc): # pragma: no cover plt.clf() plot_arrow(target.x, target.y, target.yaw) plt.plot(xc, yc, "-r") plt.axis("equal") plt.grid(True) plt.pause(0.1)
949
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
optimize_trajectory
(target, k0, p)
return xc, yc, yawc, p
107
135
def optimize_trajectory(target, k0, p): for i in range(max_iter): xc, yc, yawc = motion_model.generate_trajectory(p[0], p[1], p[2], k0) dc = np.array(calc_diff(target, xc, yc, yawc)).reshape(3, 1) cost = np.linalg.norm(dc) if cost <= cost_th: print("path is ok cost is:" + str(cost)) break J = calc_j(target, p, h, k0) try: dp = - np.linalg.inv(J) @ dc except np.linalg.linalg.LinAlgError: print("cannot calc path LinAlgError") xc, yc, yawc, p = None, None, None, None break alpha = selection_learning_param(dp, p, k0, target) p += alpha * np.array(dp) # print(p.T) if show_animation: # pragma: no cover show_trajectory(target, xc, yc) else: xc, yc, yawc, p = None, None, None, None print("cannot calc path") return xc, yc, yawc, p
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L107-L135
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 18, 19, 20, 21, 27, 28 ]
74.074074
[ 13, 14, 15, 16, 25, 26 ]
22.222222
false
89.855072
29
5
77.777778
0
def optimize_trajectory(target, k0, p): for i in range(max_iter): xc, yc, yawc = motion_model.generate_trajectory(p[0], p[1], p[2], k0) dc = np.array(calc_diff(target, xc, yc, yawc)).reshape(3, 1) cost = np.linalg.norm(dc) if cost <= cost_th: print("path is ok cost is:" + str(cost)) break J = calc_j(target, p, h, k0) try: dp = - np.linalg.inv(J) @ dc except np.linalg.linalg.LinAlgError: print("cannot calc path LinAlgError") xc, yc, yawc, p = None, None, None, None break alpha = selection_learning_param(dp, p, k0, target) p += alpha * np.array(dp) # print(p.T) if show_animation: # pragma: no cover show_trajectory(target, xc, yc) else: xc, yc, yawc, p = None, None, None, None print("cannot calc path") return xc, yc, yawc, p
950
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py
main
()
156
158
def main(): # pragma: no cover print(__file__ + " start!!") test_optimize_trajectory()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py#L156-L158
2
[]
0
[]
0
false
89.855072
3
1
100
0
def main(): # pragma: no cover print(__file__ + " start!!") test_optimize_trajectory()
951
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py
pi_2_pi
(angle)
return (angle + math.pi) % (2 * math.pi) - math.pi
20
21
def pi_2_pi(angle): return (angle + math.pi) % (2 * math.pi) - math.pi
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py#L20-L21
2
[ 0, 1 ]
100
[]
0
true
100
2
1
100
0
def pi_2_pi(angle): return (angle + math.pi) % (2 * math.pi) - math.pi
957
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py
update
(state, v, delta, dt, L)
return state
24
32
def update(state, v, delta, dt, L): state.v = v state.x = state.x + state.v * math.cos(state.yaw) * dt state.y = state.y + state.v * math.sin(state.yaw) * dt state.yaw = state.yaw + state.v / L * math.tan(delta) * dt state.yaw = pi_2_pi(state.yaw) return state
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py#L24-L32
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
100
[]
0
true
100
9
1
100
0
def update(state, v, delta, dt, L): state.v = v state.x = state.x + state.v * math.cos(state.yaw) * dt state.y = state.y + state.v * math.sin(state.yaw) * dt state.yaw = state.yaw + state.v / L * math.tan(delta) * dt state.yaw = pi_2_pi(state.yaw) return state
958
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py
generate_trajectory
(s, km, kf, k0)
return x, y, yaw
35
63
def generate_trajectory(s, km, kf, k0): n = s / ds time = s / v # [s] if isinstance(time, type(np.array([]))): time = time[0] if isinstance(km, type(np.array([]))): km = km[0] if isinstance(kf, type(np.array([]))): kf = kf[0] tk = np.array([0.0, time / 2.0, time]) kk = np.array([k0, km, kf]) t = np.arange(0.0, time, time / n) fkp = scipy.interpolate.interp1d(tk, kk, kind="quadratic") kp = [fkp(ti) for ti in t] dt = float(time / n) # plt.plot(t, kp) # plt.show() state = State() x, y, yaw = [state.x], [state.y], [state.yaw] for ikp in kp: state = update(state, v, ikp, dt, L) x.append(state.x) y.append(state.y) yaw.append(state.yaw) return x, y, yaw
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py#L35-L63
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]
100
[]
0
true
100
29
6
100
0
def generate_trajectory(s, km, kf, k0): n = s / ds time = s / v # [s] if isinstance(time, type(np.array([]))): time = time[0] if isinstance(km, type(np.array([]))): km = km[0] if isinstance(kf, type(np.array([]))): kf = kf[0] tk = np.array([0.0, time / 2.0, time]) kk = np.array([k0, km, kf]) t = np.arange(0.0, time, time / n) fkp = scipy.interpolate.interp1d(tk, kk, kind="quadratic") kp = [fkp(ti) for ti in t] dt = float(time / n) # plt.plot(t, kp) # plt.show() state = State() x, y, yaw = [state.x], [state.y], [state.yaw] for ikp in kp: state = update(state, v, ikp, dt, L) x.append(state.x) y.append(state.y) yaw.append(state.yaw) return x, y, yaw
959
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py
generate_last_state
(s, km, kf, k0)
return state.x, state.y, state.yaw
66
89
def generate_last_state(s, km, kf, k0): n = s / ds time = s / v # [s] if isinstance(time, type(np.array([]))): time = time[0] if isinstance(km, type(np.array([]))): km = km[0] if isinstance(kf, type(np.array([]))): kf = kf[0] tk = np.array([0.0, time / 2.0, time]) kk = np.array([k0, km, kf]) t = np.arange(0.0, time, time / n) fkp = scipy.interpolate.interp1d(tk, kk, kind="quadratic") kp = [fkp(ti) for ti in t] dt = time / n # plt.plot(t, kp) # plt.show() state = State() _ = [update(state, v, ikp, dt, L) for ikp in kp] return state.x, state.y, state.yaw
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py#L66-L89
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ]
100
[]
0
true
100
24
6
100
0
def generate_last_state(s, km, kf, k0): n = s / ds time = s / v # [s] if isinstance(time, type(np.array([]))): time = time[0] if isinstance(km, type(np.array([]))): km = km[0] if isinstance(kf, type(np.array([]))): kf = kf[0] tk = np.array([0.0, time / 2.0, time]) kk = np.array([k0, km, kf]) t = np.arange(0.0, time, time / n) fkp = scipy.interpolate.interp1d(tk, kk, kind="quadratic") kp = [fkp(ti) for ti in t] dt = time / n # plt.plot(t, kp) # plt.show() state = State() _ = [update(state, v, ikp, dt, L) for ikp in kp] return state.x, state.y, state.yaw
960
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py
State.__init__
(self, x=0.0, y=0.0, yaw=0.0, v=0.0)
13
17
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0): self.x = x self.y = y self.yaw = yaw self.v = v
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py#L13-L17
2
[ 0, 1, 2, 3, 4 ]
100
[]
0
true
100
5
1
100
0
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0): self.x = x self.y = y self.yaw = yaw self.v = v
961
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
main
()
229
274
def main(): print(__file__ + " start!!") # start and goal position sx = 10.0 # [m] sy = 10.0 # [m] gx = 50.0 # [m] gy = 50.0 # [m] grid_size = 2.0 # [m] robot_radius = 1.0 # [m] # set obstacle positions ox, oy = [], [] for i in range(-10, 60): ox.append(i) oy.append(-10.0) for i in range(-10, 60): ox.append(60.0) oy.append(i) for i in range(-10, 61): ox.append(i) oy.append(60.0) for i in range(-10, 61): ox.append(-10.0) oy.append(i) for i in range(-10, 40): ox.append(20.0) oy.append(i) for i in range(0, 40): ox.append(40.0) oy.append(60.0 - i) if show_animation: # pragma: no cover plt.plot(ox, oy, ".k") plt.plot(sx, sy, "og") plt.plot(gx, gy, "xb") plt.grid(True) plt.axis("equal") greedybestfirst = BestFirstSearchPlanner(ox, oy, grid_size, robot_radius) rx, ry = greedybestfirst.planning(sx, sy, gx, gy) if show_animation: # pragma: no cover plt.plot(rx, ry, "-r") plt.pause(0.01) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L229-L274
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 38, 39, 40, 41 ]
100
[]
0
true
95.070423
46
9
100
0
def main(): print(__file__ + " start!!") # start and goal position sx = 10.0 # [m] sy = 10.0 # [m] gx = 50.0 # [m] gy = 50.0 # [m] grid_size = 2.0 # [m] robot_radius = 1.0 # [m] # set obstacle positions ox, oy = [], [] for i in range(-10, 60): ox.append(i) oy.append(-10.0) for i in range(-10, 60): ox.append(60.0) oy.append(i) for i in range(-10, 61): ox.append(i) oy.append(60.0) for i in range(-10, 61): ox.append(-10.0) oy.append(i) for i in range(-10, 40): ox.append(20.0) oy.append(i) for i in range(0, 40): ox.append(40.0) oy.append(60.0 - i) if show_animation: # pragma: no cover plt.plot(ox, oy, ".k") plt.plot(sx, sy, "og") plt.plot(gx, gy, "xb") plt.grid(True) plt.axis("equal") greedybestfirst = BestFirstSearchPlanner(ox, oy, grid_size, robot_radius) rx, ry = greedybestfirst.planning(sx, sy, gx, gy) if show_animation: # pragma: no cover plt.plot(rx, ry, "-r") plt.pause(0.01) plt.show()
962
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.__init__
(self, ox, oy, reso, rr)
Initialize grid map for greedy best-first planning ox: x position list of Obstacles [m] oy: y position list of Obstacles [m] resolution: grid resolution [m] rr: robot radius[m]
Initialize grid map for greedy best-first planning
20
33
def __init__(self, ox, oy, reso, rr): """ Initialize grid map for greedy best-first planning ox: x position list of Obstacles [m] oy: y position list of Obstacles [m] resolution: grid resolution [m] rr: robot radius[m] """ self.reso = reso self.rr = rr self.calc_obstacle_map(ox, oy) self.motion = self.get_motion_model()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L20-L33
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
100
[]
0
true
95.070423
14
1
100
6
def __init__(self, ox, oy, reso, rr): self.reso = reso self.rr = rr self.calc_obstacle_map(ox, oy) self.motion = self.get_motion_model()
963
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.planning
(self, sx, sy, gx, gy)
return rx, ry
Greedy Best-First search input: s_x: start x position [m] s_y: start y position [m] gx: goal x position [m] gy: goal y position [m] output: rx: x position list of the final path ry: y position list of the final path
Greedy Best-First search
47
129
def planning(self, sx, sy, gx, gy): """ Greedy Best-First search input: s_x: start x position [m] s_y: start y position [m] gx: goal x position [m] gy: goal y position [m] output: rx: x position list of the final path ry: y position list of the final path """ nstart = self.Node(self.calc_xyindex(sx, self.minx), self.calc_xyindex(sy, self.miny), 0.0, -1, None) ngoal = self.Node(self.calc_xyindex(gx, self.minx), self.calc_xyindex(gy, self.miny), 0.0, -1, None) open_set, closed_set = dict(), dict() open_set[self.calc_grid_index(nstart)] = nstart while True: if len(open_set) == 0: print("Open set is empty..") break c_id = min( open_set, key=lambda o: self.calc_heuristic(ngoal, open_set[o])) current = open_set[c_id] # show graph if show_animation: # pragma: no cover plt.plot(self.calc_grid_position(current.x, self.minx), self.calc_grid_position(current.y, self.miny), "xc") # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if len(closed_set.keys()) % 10 == 0: plt.pause(0.001) # Remove the item from the open set del open_set[c_id] # Add it to the closed set closed_set[c_id] = current if current.x == ngoal.x and current.y == ngoal.y: print("Found goal") ngoal.pind = current.pind ngoal.cost = current.cost break # expand_grid search grid based on motion model for i, _ in enumerate(self.motion): node = self.Node(current.x + self.motion[i][0], current.y + self.motion[i][1], current.cost + self.motion[i][2], c_id, current) n_id = self.calc_grid_index(node) # If the node is not safe, do nothing if not self.verify_node(node): continue if n_id in closed_set: continue if n_id not in open_set: open_set[n_id] = node else: if open_set[n_id].cost > node.cost: open_set[n_id] = node closed_set[ngoal.pind] = current rx, ry = self.calc_final_path(ngoal, closed_set) return rx, ry
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L47-L129
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 ]
103.846154
[ 25, 26 ]
2.564103
false
95.070423
83
12
97.435897
11
def planning(self, sx, sy, gx, gy): nstart = self.Node(self.calc_xyindex(sx, self.minx), self.calc_xyindex(sy, self.miny), 0.0, -1, None) ngoal = self.Node(self.calc_xyindex(gx, self.minx), self.calc_xyindex(gy, self.miny), 0.0, -1, None) open_set, closed_set = dict(), dict() open_set[self.calc_grid_index(nstart)] = nstart while True: if len(open_set) == 0: print("Open set is empty..") break c_id = min( open_set, key=lambda o: self.calc_heuristic(ngoal, open_set[o])) current = open_set[c_id] # show graph if show_animation: # pragma: no cover plt.plot(self.calc_grid_position(current.x, self.minx), self.calc_grid_position(current.y, self.miny), "xc") # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if len(closed_set.keys()) % 10 == 0: plt.pause(0.001) # Remove the item from the open set del open_set[c_id] # Add it to the closed set closed_set[c_id] = current if current.x == ngoal.x and current.y == ngoal.y: print("Found goal") ngoal.pind = current.pind ngoal.cost = current.cost break # expand_grid search grid based on motion model for i, _ in enumerate(self.motion): node = self.Node(current.x + self.motion[i][0], current.y + self.motion[i][1], current.cost + self.motion[i][2], c_id, current) n_id = self.calc_grid_index(node) # If the node is not safe, do nothing if not self.verify_node(node): continue if n_id in closed_set: continue if n_id not in open_set: open_set[n_id] = node else: if open_set[n_id].cost > node.cost: open_set[n_id] = node closed_set[ngoal.pind] = current rx, ry = self.calc_final_path(ngoal, closed_set) return rx, ry
964
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_final_path
(self, ngoal, closedset)
return rx, ry
131
141
def calc_final_path(self, ngoal, closedset): # generate final course rx, ry = [self.calc_grid_position(ngoal.x, self.minx)], [ self.calc_grid_position(ngoal.y, self.miny)] n = closedset[ngoal.pind] while n is not None: rx.append(self.calc_grid_position(n.x, self.minx)) ry.append(self.calc_grid_position(n.y, self.miny)) n = n.parent return rx, ry
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L131-L141
2
[ 0, 1, 2, 4, 5, 6, 7, 8, 9, 10 ]
90.909091
[]
0
false
95.070423
11
2
100
0
def calc_final_path(self, ngoal, closedset): # generate final course rx, ry = [self.calc_grid_position(ngoal.x, self.minx)], [ self.calc_grid_position(ngoal.y, self.miny)] n = closedset[ngoal.pind] while n is not None: rx.append(self.calc_grid_position(n.x, self.minx)) ry.append(self.calc_grid_position(n.y, self.miny)) n = n.parent return rx, ry
965
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_heuristic
(n1, n2)
return d
144
147
def calc_heuristic(n1, n2): w = 1.0 # weight of heuristic d = w * math.hypot(n1.x - n2.x, n1.y - n2.y) return d
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L144-L147
2
[ 0, 1, 2, 3 ]
100
[]
0
true
95.070423
4
1
100
0
def calc_heuristic(n1, n2): w = 1.0 # weight of heuristic d = w * math.hypot(n1.x - n2.x, n1.y - n2.y) return d
966
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_grid_position
(self, index, minp)
return pos
calc grid position :param index: :param minp: :return:
calc grid position
149
158
def calc_grid_position(self, index, minp): """ calc grid position :param index: :param minp: :return: """ pos = index * self.reso + minp return pos
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L149-L158
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
100
[]
0
true
95.070423
10
1
100
5
def calc_grid_position(self, index, minp): pos = index * self.reso + minp return pos
967
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_xyindex
(self, position, min_pos)
return round((position - min_pos) / self.reso)
160
161
def calc_xyindex(self, position, min_pos): return round((position - min_pos) / self.reso)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L160-L161
2
[ 0, 1 ]
100
[]
0
true
95.070423
2
1
100
0
def calc_xyindex(self, position, min_pos): return round((position - min_pos) / self.reso)
968
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_grid_index
(self, node)
return (node.y - self.miny) * self.xwidth + (node.x - self.minx)
163
164
def calc_grid_index(self, node): return (node.y - self.miny) * self.xwidth + (node.x - self.minx)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L163-L164
2
[ 0, 1 ]
100
[]
0
true
95.070423
2
1
100
0
def calc_grid_index(self, node): return (node.y - self.miny) * self.xwidth + (node.x - self.minx)
969
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.verify_node
(self, node)
return True
166
183
def verify_node(self, node): px = self.calc_grid_position(node.x, self.minx) py = self.calc_grid_position(node.y, self.miny) if px < self.minx: return False elif py < self.miny: return False elif px >= self.maxx: return False elif py >= self.maxy: return False # collision check if self.obmap[node.x][node.y]: return False return True
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L166-L183
2
[ 0, 1, 2, 3, 4, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17 ]
83.333333
[ 5, 7, 9 ]
16.666667
false
95.070423
18
6
83.333333
0
def verify_node(self, node): px = self.calc_grid_position(node.x, self.minx) py = self.calc_grid_position(node.y, self.miny) if px < self.minx: return False elif py < self.miny: return False elif px >= self.maxx: return False elif py >= self.maxy: return False # collision check if self.obmap[node.x][node.y]: return False return True
970
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.calc_obstacle_map
(self, ox, oy)
185
212
def calc_obstacle_map(self, ox, oy): self.minx = round(min(ox)) self.miny = round(min(oy)) self.maxx = round(max(ox)) self.maxy = round(max(oy)) print("min_x:", self.minx) print("min_y:", self.miny) print("max_x:", self.maxx) print("max_y:", self.maxy) self.xwidth = round((self.maxx - self.minx) / self.reso) self.ywidth = round((self.maxy - self.miny) / self.reso) print("x_width:", self.xwidth) print("y_width:", self.ywidth) # obstacle map generation self.obmap = [[False for _ in range(self.ywidth)] for _ in range(self.xwidth)] for ix in range(self.xwidth): x = self.calc_grid_position(ix, self.minx) for iy in range(self.ywidth): y = self.calc_grid_position(iy, self.miny) for iox, ioy in zip(ox, oy): d = math.hypot(iox - x, ioy - y) if d <= self.rr: self.obmap[ix][iy] = True break
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L185-L212
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27 ]
96.428571
[]
0
false
95.070423
28
7
100
0
def calc_obstacle_map(self, ox, oy): self.minx = round(min(ox)) self.miny = round(min(oy)) self.maxx = round(max(ox)) self.maxy = round(max(oy)) print("min_x:", self.minx) print("min_y:", self.miny) print("max_x:", self.maxx) print("max_y:", self.maxy) self.xwidth = round((self.maxx - self.minx) / self.reso) self.ywidth = round((self.maxy - self.miny) / self.reso) print("x_width:", self.xwidth) print("y_width:", self.ywidth) # obstacle map generation self.obmap = [[False for _ in range(self.ywidth)] for _ in range(self.xwidth)] for ix in range(self.xwidth): x = self.calc_grid_position(ix, self.minx) for iy in range(self.ywidth): y = self.calc_grid_position(iy, self.miny) for iox, ioy in zip(ox, oy): d = math.hypot(iox - x, ioy - y) if d <= self.rr: self.obmap[ix][iy] = True break
971
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
BestFirstSearchPlanner.get_motion_model
()
return motion
215
226
def get_motion_model(): # dx, dy, cost motion = [[1, 0, 1], [0, 1, 1], [-1, 0, 1], [0, -1, 1], [-1, -1, math.sqrt(2)], [-1, 1, math.sqrt(2)], [1, -1, math.sqrt(2)], [1, 1, math.sqrt(2)]] return motion
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py#L215-L226
2
[ 0, 1, 2, 10, 11 ]
41.666667
[]
0
false
95.070423
12
1
100
0
def get_motion_model(): # dx, dy, cost motion = [[1, 0, 1], [0, 1, 1], [-1, 0, 1], [0, -1, 1], [-1, -1, math.sqrt(2)], [-1, 1, math.sqrt(2)], [1, -1, math.sqrt(2)], [1, 1, math.sqrt(2)]] return motion
972
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
dwa_control
(x, config, goal, ob)
return u, trajectory
Dynamic Window Approach control
Dynamic Window Approach control
18
26
def dwa_control(x, config, goal, ob): """ Dynamic Window Approach control """ dw = calc_dynamic_window(x, config) u, trajectory = calc_control_and_trajectory(x, dw, config, goal, ob) return u, trajectory
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L18-L26
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
100
[]
0
true
89.115646
9
1
100
1
def dwa_control(x, config, goal, ob): dw = calc_dynamic_window(x, config) u, trajectory = calc_control_and_trajectory(x, dw, config, goal, ob) return u, trajectory
973
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
motion
(x, u, dt)
return x
motion model
motion model
95
106
def motion(x, u, dt): """ motion model """ x[2] += u[1] * dt x[0] += u[0] * math.cos(x[2]) * dt x[1] += u[0] * math.sin(x[2]) * dt x[3] = u[0] x[4] = u[1] return x
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L95-L106
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
100
[]
0
true
89.115646
12
1
100
1
def motion(x, u, dt): x[2] += u[1] * dt x[0] += u[0] * math.cos(x[2]) * dt x[1] += u[0] * math.sin(x[2]) * dt x[3] = u[0] x[4] = u[1] return x
974
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
calc_dynamic_window
(x, config)
return dw
calculation dynamic window based on current state x
calculation dynamic window based on current state x
109
128
def calc_dynamic_window(x, config): """ calculation dynamic window based on current state x """ # Dynamic window from robot specification Vs = [config.min_speed, config.max_speed, -config.max_yaw_rate, config.max_yaw_rate] # Dynamic window from motion model Vd = [x[3] - config.max_accel * config.dt, x[3] + config.max_accel * config.dt, x[4] - config.max_delta_yaw_rate * config.dt, x[4] + config.max_delta_yaw_rate * config.dt] # [v_min, v_max, yaw_rate_min, yaw_rate_max] dw = [max(Vs[0], Vd[0]), min(Vs[1], Vd[1]), max(Vs[2], Vd[2]), min(Vs[3], Vd[3])] return dw
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L109-L128
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
100
[]
0
true
89.115646
20
1
100
1
def calc_dynamic_window(x, config): # Dynamic window from robot specification Vs = [config.min_speed, config.max_speed, -config.max_yaw_rate, config.max_yaw_rate] # Dynamic window from motion model Vd = [x[3] - config.max_accel * config.dt, x[3] + config.max_accel * config.dt, x[4] - config.max_delta_yaw_rate * config.dt, x[4] + config.max_delta_yaw_rate * config.dt] # [v_min, v_max, yaw_rate_min, yaw_rate_max] dw = [max(Vs[0], Vd[0]), min(Vs[1], Vd[1]), max(Vs[2], Vd[2]), min(Vs[3], Vd[3])] return dw
975
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
predict_trajectory
(x_init, v, y, config)
return trajectory
predict trajectory with an input
predict trajectory with an input
131
144
def predict_trajectory(x_init, v, y, config): """ predict trajectory with an input """ x = np.array(x_init) trajectory = np.array(x) time = 0 while time <= config.predict_time: x = motion(x, [v, y], config.dt) trajectory = np.vstack((trajectory, x)) time += config.dt return trajectory
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L131-L144
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
100
[]
0
true
89.115646
14
2
100
1
def predict_trajectory(x_init, v, y, config): x = np.array(x_init) trajectory = np.array(x) time = 0 while time <= config.predict_time: x = motion(x, [v, y], config.dt) trajectory = np.vstack((trajectory, x)) time += config.dt return trajectory
976
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
calc_control_and_trajectory
(x, dw, config, goal, ob)
return best_u, best_trajectory
calculation final input with dynamic window
calculation final input with dynamic window
147
181
def calc_control_and_trajectory(x, dw, config, goal, ob): """ calculation final input with dynamic window """ x_init = x[:] min_cost = float("inf") best_u = [0.0, 0.0] best_trajectory = np.array([x]) # evaluate all trajectory with sampled input in dynamic window for v in np.arange(dw[0], dw[1], config.v_resolution): for y in np.arange(dw[2], dw[3], config.yaw_rate_resolution): trajectory = predict_trajectory(x_init, v, y, config) # calc cost to_goal_cost = config.to_goal_cost_gain * calc_to_goal_cost(trajectory, goal) speed_cost = config.speed_cost_gain * (config.max_speed - trajectory[-1, 3]) ob_cost = config.obstacle_cost_gain * calc_obstacle_cost(trajectory, ob, config) final_cost = to_goal_cost + speed_cost + ob_cost # search minimum trajectory if min_cost >= final_cost: min_cost = final_cost best_u = [v, y] best_trajectory = trajectory if abs(best_u[0]) < config.robot_stuck_flag_cons \ and abs(x[3]) < config.robot_stuck_flag_cons: # to ensure the robot do not get stuck in # best v=0 m/s (in front of an obstacle) and # best omega=0 rad/s (heading to the goal with # angle difference of 0) best_u[1] = -config.max_delta_yaw_rate return best_u, best_trajectory
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L147-L181
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 ]
100
[]
0
true
89.115646
35
6
100
1
def calc_control_and_trajectory(x, dw, config, goal, ob): x_init = x[:] min_cost = float("inf") best_u = [0.0, 0.0] best_trajectory = np.array([x]) # evaluate all trajectory with sampled input in dynamic window for v in np.arange(dw[0], dw[1], config.v_resolution): for y in np.arange(dw[2], dw[3], config.yaw_rate_resolution): trajectory = predict_trajectory(x_init, v, y, config) # calc cost to_goal_cost = config.to_goal_cost_gain * calc_to_goal_cost(trajectory, goal) speed_cost = config.speed_cost_gain * (config.max_speed - trajectory[-1, 3]) ob_cost = config.obstacle_cost_gain * calc_obstacle_cost(trajectory, ob, config) final_cost = to_goal_cost + speed_cost + ob_cost # search minimum trajectory if min_cost >= final_cost: min_cost = final_cost best_u = [v, y] best_trajectory = trajectory if abs(best_u[0]) < config.robot_stuck_flag_cons \ and abs(x[3]) < config.robot_stuck_flag_cons: # to ensure the robot do not get stuck in # best v=0 m/s (in front of an obstacle) and # best omega=0 rad/s (heading to the goal with # angle difference of 0) best_u[1] = -config.max_delta_yaw_rate return best_u, best_trajectory
977
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
calc_obstacle_cost
(trajectory, ob, config)
return 1.0 / min_r
calc obstacle cost inf: collision
calc obstacle cost inf: collision
184
214
def calc_obstacle_cost(trajectory, ob, config): """ calc obstacle cost inf: collision """ ox = ob[:, 0] oy = ob[:, 1] dx = trajectory[:, 0] - ox[:, None] dy = trajectory[:, 1] - oy[:, None] r = np.hypot(dx, dy) if config.robot_type == RobotType.rectangle: yaw = trajectory[:, 2] rot = np.array([[np.cos(yaw), -np.sin(yaw)], [np.sin(yaw), np.cos(yaw)]]) rot = np.transpose(rot, [2, 0, 1]) local_ob = ob[:, None] - trajectory[:, 0:2] local_ob = local_ob.reshape(-1, local_ob.shape[-1]) local_ob = np.array([local_ob @ x for x in rot]) local_ob = local_ob.reshape(-1, local_ob.shape[-1]) upper_check = local_ob[:, 0] <= config.robot_length / 2 right_check = local_ob[:, 1] <= config.robot_width / 2 bottom_check = local_ob[:, 0] >= -config.robot_length / 2 left_check = local_ob[:, 1] >= -config.robot_width / 2 if (np.logical_and(np.logical_and(upper_check, right_check), np.logical_and(bottom_check, left_check))).any(): return float("Inf") elif config.robot_type == RobotType.circle: if np.array(r <= config.robot_radius).any(): return float("Inf") min_r = np.min(r) return 1.0 / min_r
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L184-L214
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30 ]
96.774194
[ 24 ]
3.225806
false
89.115646
31
6
96.774194
1
def calc_obstacle_cost(trajectory, ob, config): ox = ob[:, 0] oy = ob[:, 1] dx = trajectory[:, 0] - ox[:, None] dy = trajectory[:, 1] - oy[:, None] r = np.hypot(dx, dy) if config.robot_type == RobotType.rectangle: yaw = trajectory[:, 2] rot = np.array([[np.cos(yaw), -np.sin(yaw)], [np.sin(yaw), np.cos(yaw)]]) rot = np.transpose(rot, [2, 0, 1]) local_ob = ob[:, None] - trajectory[:, 0:2] local_ob = local_ob.reshape(-1, local_ob.shape[-1]) local_ob = np.array([local_ob @ x for x in rot]) local_ob = local_ob.reshape(-1, local_ob.shape[-1]) upper_check = local_ob[:, 0] <= config.robot_length / 2 right_check = local_ob[:, 1] <= config.robot_width / 2 bottom_check = local_ob[:, 0] >= -config.robot_length / 2 left_check = local_ob[:, 1] >= -config.robot_width / 2 if (np.logical_and(np.logical_and(upper_check, right_check), np.logical_and(bottom_check, left_check))).any(): return float("Inf") elif config.robot_type == RobotType.circle: if np.array(r <= config.robot_radius).any(): return float("Inf") min_r = np.min(r) return 1.0 / min_r
978
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
calc_to_goal_cost
(trajectory, goal)
return cost
calc to goal cost with angle difference
calc to goal cost with angle difference
217
228
def calc_to_goal_cost(trajectory, goal): """ calc to goal cost with angle difference """ dx = goal[0] - trajectory[-1, 0] dy = goal[1] - trajectory[-1, 1] error_angle = math.atan2(dy, dx) cost_angle = error_angle - trajectory[-1, 2] cost = abs(math.atan2(math.sin(cost_angle), math.cos(cost_angle))) return cost
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L217-L228
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
100
[]
0
true
89.115646
12
1
100
1
def calc_to_goal_cost(trajectory, goal): dx = goal[0] - trajectory[-1, 0] dy = goal[1] - trajectory[-1, 1] error_angle = math.atan2(dy, dx) cost_angle = error_angle - trajectory[-1, 2] cost = abs(math.atan2(math.sin(cost_angle), math.cos(cost_angle))) return cost
979
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
plot_arrow
(x, y, yaw, length=0.5, width=0.1)
231
234
def plot_arrow(x, y, yaw, length=0.5, width=0.1): # pragma: no cover plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), head_length=width, head_width=width) plt.plot(x, y)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L231-L234
2
[]
0
[]
0
false
89.115646
4
1
100
0
def plot_arrow(x, y, yaw, length=0.5, width=0.1): # pragma: no cover plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), head_length=width, head_width=width) plt.plot(x, y)
980
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
plot_robot
(x, y, yaw, config)
237
257
def plot_robot(x, y, yaw, config): # pragma: no cover if config.robot_type == RobotType.rectangle: outline = np.array([[-config.robot_length / 2, config.robot_length / 2, (config.robot_length / 2), -config.robot_length / 2, -config.robot_length / 2], [config.robot_width / 2, config.robot_width / 2, - config.robot_width / 2, -config.robot_width / 2, config.robot_width / 2]]) Rot1 = np.array([[math.cos(yaw), math.sin(yaw)], [-math.sin(yaw), math.cos(yaw)]]) outline = (outline.T.dot(Rot1)).T outline[0, :] += x outline[1, :] += y plt.plot(np.array(outline[0, :]).flatten(), np.array(outline[1, :]).flatten(), "-k") elif config.robot_type == RobotType.circle: circle = plt.Circle((x, y), config.robot_radius, color="b") plt.gcf().gca().add_artist(circle) out_x, out_y = (np.array([x, y]) + np.array([np.cos(yaw), np.sin(yaw)]) * config.robot_radius) plt.plot([x, out_x], [y, out_y], "-k")
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L237-L257
2
[]
0
[]
0
false
89.115646
21
3
100
0
def plot_robot(x, y, yaw, config): # pragma: no cover if config.robot_type == RobotType.rectangle: outline = np.array([[-config.robot_length / 2, config.robot_length / 2, (config.robot_length / 2), -config.robot_length / 2, -config.robot_length / 2], [config.robot_width / 2, config.robot_width / 2, - config.robot_width / 2, -config.robot_width / 2, config.robot_width / 2]]) Rot1 = np.array([[math.cos(yaw), math.sin(yaw)], [-math.sin(yaw), math.cos(yaw)]]) outline = (outline.T.dot(Rot1)).T outline[0, :] += x outline[1, :] += y plt.plot(np.array(outline[0, :]).flatten(), np.array(outline[1, :]).flatten(), "-k") elif config.robot_type == RobotType.circle: circle = plt.Circle((x, y), config.robot_radius, color="b") plt.gcf().gca().add_artist(circle) out_x, out_y = (np.array([x, y]) + np.array([np.cos(yaw), np.sin(yaw)]) * config.robot_radius) plt.plot([x, out_x], [y, out_y], "-k")
981
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
main
(gx=10.0, gy=10.0, robot_type=RobotType.circle)
260
304
def main(gx=10.0, gy=10.0, robot_type=RobotType.circle): print(__file__ + " start!!") # initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)] x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0]) # goal position [x(m), y(m)] goal = np.array([gx, gy]) # input [forward speed, yaw_rate] config.robot_type = robot_type trajectory = np.array(x) ob = config.ob while True: u, predicted_trajectory = dwa_control(x, config, goal, ob) x = motion(x, u, config.dt) # simulate robot trajectory = np.vstack((trajectory, x)) # store state history if show_animation: plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.plot(predicted_trajectory[:, 0], predicted_trajectory[:, 1], "-g") plt.plot(x[0], x[1], "xr") plt.plot(goal[0], goal[1], "xb") plt.plot(ob[:, 0], ob[:, 1], "ok") plot_robot(x[0], x[1], x[2], config) plot_arrow(x[0], x[1], x[2]) plt.axis("equal") plt.grid(True) plt.pause(0.0001) # check reaching goal dist_to_goal = math.hypot(x[0] - goal[0], x[1] - goal[1]) if dist_to_goal <= config.robot_radius: print("Goal!!") break print("Done") if show_animation: plt.plot(trajectory[:, 0], trajectory[:, 1], "-r") plt.pause(0.0001) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L260-L304
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 33, 34, 35, 36, 37, 38, 39, 40, 43, 44 ]
62.222222
[ 18, 20, 23, 24, 25, 26, 27, 28, 29, 30, 31, 41, 42 ]
28.888889
false
89.115646
45
5
71.111111
0
def main(gx=10.0, gy=10.0, robot_type=RobotType.circle): print(__file__ + " start!!") # initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)] x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0]) # goal position [x(m), y(m)] goal = np.array([gx, gy]) # input [forward speed, yaw_rate] config.robot_type = robot_type trajectory = np.array(x) ob = config.ob while True: u, predicted_trajectory = dwa_control(x, config, goal, ob) x = motion(x, u, config.dt) # simulate robot trajectory = np.vstack((trajectory, x)) # store state history if show_animation: plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.plot(predicted_trajectory[:, 0], predicted_trajectory[:, 1], "-g") plt.plot(x[0], x[1], "xr") plt.plot(goal[0], goal[1], "xb") plt.plot(ob[:, 0], ob[:, 1], "ok") plot_robot(x[0], x[1], x[2], config) plot_arrow(x[0], x[1], x[2]) plt.axis("equal") plt.grid(True) plt.pause(0.0001) # check reaching goal dist_to_goal = math.hypot(x[0] - goal[0], x[1] - goal[1]) if dist_to_goal <= config.robot_radius: print("Goal!!") break print("Done") if show_animation: plt.plot(trajectory[:, 0], trajectory[:, 1], "-r") plt.pause(0.0001) plt.show()
982
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
Config.__init__
(self)
39
79
def __init__(self): # robot parameter self.max_speed = 1.0 # [m/s] self.min_speed = -0.5 # [m/s] self.max_yaw_rate = 40.0 * math.pi / 180.0 # [rad/s] self.max_accel = 0.2 # [m/ss] self.max_delta_yaw_rate = 40.0 * math.pi / 180.0 # [rad/ss] self.v_resolution = 0.01 # [m/s] self.yaw_rate_resolution = 0.1 * math.pi / 180.0 # [rad/s] self.dt = 0.1 # [s] Time tick for motion prediction self.predict_time = 3.0 # [s] self.to_goal_cost_gain = 0.15 self.speed_cost_gain = 1.0 self.obstacle_cost_gain = 1.0 self.robot_stuck_flag_cons = 0.001 # constant to prevent robot stucked self.robot_type = RobotType.circle # if robot_type == RobotType.circle # Also used to check if goal is reached in both types self.robot_radius = 1.0 # [m] for collision check # if robot_type == RobotType.rectangle self.robot_width = 0.5 # [m] for collision check self.robot_length = 1.2 # [m] for collision check # obstacles [x(m) y(m), ....] self.ob = np.array([[-1, -1], [0, 2], [4.0, 2.0], [5.0, 4.0], [5.0, 5.0], [5.0, 6.0], [5.0, 9.0], [8.0, 9.0], [7.0, 9.0], [8.0, 10.0], [9.0, 11.0], [12.0, 13.0], [12.0, 12.0], [15.0, 15.0], [13.0, 13.0] ])
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L39-L79
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ]
63.414634
[]
0
false
89.115646
41
1
100
0
def __init__(self): # robot parameter self.max_speed = 1.0 # [m/s] self.min_speed = -0.5 # [m/s] self.max_yaw_rate = 40.0 * math.pi / 180.0 # [rad/s] self.max_accel = 0.2 # [m/ss] self.max_delta_yaw_rate = 40.0 * math.pi / 180.0 # [rad/ss] self.v_resolution = 0.01 # [m/s] self.yaw_rate_resolution = 0.1 * math.pi / 180.0 # [rad/s] self.dt = 0.1 # [s] Time tick for motion prediction self.predict_time = 3.0 # [s] self.to_goal_cost_gain = 0.15 self.speed_cost_gain = 1.0 self.obstacle_cost_gain = 1.0 self.robot_stuck_flag_cons = 0.001 # constant to prevent robot stucked self.robot_type = RobotType.circle # if robot_type == RobotType.circle # Also used to check if goal is reached in both types self.robot_radius = 1.0 # [m] for collision check # if robot_type == RobotType.rectangle self.robot_width = 0.5 # [m] for collision check self.robot_length = 1.2 # [m] for collision check # obstacles [x(m) y(m), ....] self.ob = np.array([[-1, -1], [0, 2], [4.0, 2.0], [5.0, 4.0], [5.0, 5.0], [5.0, 6.0], [5.0, 9.0], [8.0, 9.0], [7.0, 9.0], [8.0, 10.0], [9.0, 11.0], [12.0, 13.0], [12.0, 12.0], [15.0, 15.0], [13.0, 13.0] ])
983
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
Config.robot_type
(self)
return self._robot_type
82
83
def robot_type(self): return self._robot_type
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L82-L83
2
[ 0, 1 ]
100
[]
0
true
89.115646
2
1
100
0
def robot_type(self): return self._robot_type
984
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/DynamicWindowApproach/dynamic_window_approach.py
Config.robot_type
(self, value)
86
89
def robot_type(self, value): if not isinstance(value, RobotType): raise TypeError("robot_type must be an instance of RobotType") self._robot_type = value
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/DynamicWindowApproach/dynamic_window_approach.py#L86-L89
2
[ 0, 1, 3 ]
75
[ 2 ]
25
false
89.115646
4
2
75
0
def robot_type(self, value): if not isinstance(value, RobotType): raise TypeError("robot_type must be an instance of RobotType") self._robot_type = value
985
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py
transform
( grid_map, src, distance_type='chessboard', transform_type='path', alpha=0.01 )
return transform_matrix
transform calculating transform of transform_type from src in given distance_type :param grid_map: 2d binary map :param src: distance transform source :param distance_type: type of distance used :param transform_type: type of transform used :param alpha: weight of Obstacle Transform used when using path_transform
transform
20
91
def transform( grid_map, src, distance_type='chessboard', transform_type='path', alpha=0.01 ): """transform calculating transform of transform_type from src in given distance_type :param grid_map: 2d binary map :param src: distance transform source :param distance_type: type of distance used :param transform_type: type of transform used :param alpha: weight of Obstacle Transform used when using path_transform """ n_rows, n_cols = grid_map.shape if n_rows == 0 or n_cols == 0: sys.exit('Empty grid_map.') inc_order = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]] if distance_type == 'chessboard': cost = [1, 1, 1, 1, 1, 1, 1, 1] elif distance_type == 'eculidean': cost = [1, np.sqrt(2), 1, np.sqrt(2), 1, np.sqrt(2), 1, np.sqrt(2)] else: sys.exit('Unsupported distance type.') transform_matrix = float('inf') * np.ones_like(grid_map, dtype=float) transform_matrix[src[0], src[1]] = 0 if transform_type == 'distance': eT = np.zeros_like(grid_map) elif transform_type == 'path': eT = ndimage.distance_transform_cdt(1 - grid_map, distance_type) else: sys.exit('Unsupported transform type.') # set obstacle transform_matrix value to infinity for i in range(n_rows): for j in range(n_cols): if grid_map[i][j] == 1.0: transform_matrix[i][j] = float('inf') is_visited = np.zeros_like(transform_matrix, dtype=bool) is_visited[src[0], src[1]] = True traversal_queue = [src] calculated = [(src[0] - 1) * n_cols + src[1]] def is_valid_neighbor(g_i, g_j): return 0 <= g_i < n_rows and 0 <= g_j < n_cols \ and not grid_map[g_i][g_j] while traversal_queue: i, j = traversal_queue.pop(0) for k, inc in enumerate(inc_order): ni = i + inc[0] nj = j + inc[1] if is_valid_neighbor(ni, nj): is_visited[i][j] = True # update transform_matrix transform_matrix[i][j] = min( transform_matrix[i][j], transform_matrix[ni][nj] + cost[k] + alpha * eT[ni][nj]) if not is_visited[ni][nj] \ and ((ni - 1) * n_cols + nj) not in calculated: traversal_queue.append((ni, nj)) calculated.append((ni - 1) * n_cols + nj) return transform_matrix
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py#L20-L91
2
[ 0, 15, 16, 17, 18, 20, 21, 23, 24, 29, 30, 31, 32, 33, 34, 35, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 65, 66, 68, 69, 70, 71 ]
62.5
[ 19, 25, 26, 28, 37 ]
6.944444
false
80.952381
72
18
93.055556
10
def transform( grid_map, src, distance_type='chessboard', transform_type='path', alpha=0.01 ): n_rows, n_cols = grid_map.shape if n_rows == 0 or n_cols == 0: sys.exit('Empty grid_map.') inc_order = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]] if distance_type == 'chessboard': cost = [1, 1, 1, 1, 1, 1, 1, 1] elif distance_type == 'eculidean': cost = [1, np.sqrt(2), 1, np.sqrt(2), 1, np.sqrt(2), 1, np.sqrt(2)] else: sys.exit('Unsupported distance type.') transform_matrix = float('inf') * np.ones_like(grid_map, dtype=float) transform_matrix[src[0], src[1]] = 0 if transform_type == 'distance': eT = np.zeros_like(grid_map) elif transform_type == 'path': eT = ndimage.distance_transform_cdt(1 - grid_map, distance_type) else: sys.exit('Unsupported transform type.') # set obstacle transform_matrix value to infinity for i in range(n_rows): for j in range(n_cols): if grid_map[i][j] == 1.0: transform_matrix[i][j] = float('inf') is_visited = np.zeros_like(transform_matrix, dtype=bool) is_visited[src[0], src[1]] = True traversal_queue = [src] calculated = [(src[0] - 1) * n_cols + src[1]] def is_valid_neighbor(g_i, g_j): return 0 <= g_i < n_rows and 0 <= g_j < n_cols \ and not grid_map[g_i][g_j] while traversal_queue: i, j = traversal_queue.pop(0) for k, inc in enumerate(inc_order): ni = i + inc[0] nj = j + inc[1] if is_valid_neighbor(ni, nj): is_visited[i][j] = True # update transform_matrix transform_matrix[i][j] = min( transform_matrix[i][j], transform_matrix[ni][nj] + cost[k] + alpha * eT[ni][nj]) if not is_visited[ni][nj] \ and ((ni - 1) * n_cols + nj) not in calculated: traversal_queue.append((ni, nj)) calculated.append((ni - 1) * n_cols + nj) return transform_matrix
986
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py
get_search_order_increment
(start, goal)
return order
94
110
def get_search_order_increment(start, goal): if start[0] >= goal[0] and start[1] >= goal[1]: order = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]] elif start[0] <= goal[0] and start[1] >= goal[1]: order = [[-1, 0], [0, 1], [1, 0], [0, -1], [-1, 1], [-1, -1], [1, 1], [1, -1]] elif start[0] >= goal[0] and start[1] <= goal[1]: order = [[1, 0], [0, -1], [-1, 0], [0, 1], [1, -1], [-1, -1], [1, 1], [-1, 1]] elif start[0] <= goal[0] and start[1] <= goal[1]: order = [[-1, 0], [0, -1], [0, 1], [1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]] else: sys.exit('get_search_order_increment: cannot determine \ start=>goal increment order') return order
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py#L94-L110
2
[ 0, 1, 2, 4, 7, 8, 10, 11, 16 ]
52.941176
[ 5, 14 ]
11.764706
false
80.952381
17
9
88.235294
0
def get_search_order_increment(start, goal): if start[0] >= goal[0] and start[1] >= goal[1]: order = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]] elif start[0] <= goal[0] and start[1] >= goal[1]: order = [[-1, 0], [0, 1], [1, 0], [0, -1], [-1, 1], [-1, -1], [1, 1], [1, -1]] elif start[0] >= goal[0] and start[1] <= goal[1]: order = [[1, 0], [0, -1], [-1, 0], [0, 1], [1, -1], [-1, -1], [1, 1], [-1, 1]] elif start[0] <= goal[0] and start[1] <= goal[1]: order = [[-1, 0], [0, -1], [0, 1], [1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]] else: sys.exit('get_search_order_increment: cannot determine \ start=>goal increment order') return order
987
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py
wavefront
(transform_matrix, start, goal)
return path
wavefront performing wavefront coverage path planning :param transform_matrix: the transform matrix :param start: start point of planning :param goal: goal point of planning
wavefront
113
167
def wavefront(transform_matrix, start, goal): """wavefront performing wavefront coverage path planning :param transform_matrix: the transform matrix :param start: start point of planning :param goal: goal point of planning """ path = [] n_rows, n_cols = transform_matrix.shape def is_valid_neighbor(g_i, g_j): is_i_valid_bounded = 0 <= g_i < n_rows is_j_valid_bounded = 0 <= g_j < n_cols if is_i_valid_bounded and is_j_valid_bounded: return not is_visited[g_i][g_j] and \ transform_matrix[g_i][g_j] != float('inf') return False inc_order = get_search_order_increment(start, goal) current_node = start is_visited = np.zeros_like(transform_matrix, dtype=bool) while current_node != goal: i, j = current_node path.append((i, j)) is_visited[i][j] = True max_T = float('-inf') i_max = (-1, -1) i_last = 0 for i_last in range(len(path)): current_node = path[-1 - i_last] # get latest node in path for ci, cj in inc_order: ni, nj = current_node[0] + ci, current_node[1] + cj if is_valid_neighbor(ni, nj) and \ transform_matrix[ni][nj] > max_T: i_max = (ni, nj) max_T = transform_matrix[ni][nj] if i_max != (-1, -1): break if i_max == (-1, -1): break else: current_node = i_max if i_last != 0: print('backtracing to', current_node) path.append(goal) return path
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py#L113-L167
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54 ]
98.181818
[ 47 ]
1.818182
false
80.952381
55
13
98.181818
7
def wavefront(transform_matrix, start, goal): path = [] n_rows, n_cols = transform_matrix.shape def is_valid_neighbor(g_i, g_j): is_i_valid_bounded = 0 <= g_i < n_rows is_j_valid_bounded = 0 <= g_j < n_cols if is_i_valid_bounded and is_j_valid_bounded: return not is_visited[g_i][g_j] and \ transform_matrix[g_i][g_j] != float('inf') return False inc_order = get_search_order_increment(start, goal) current_node = start is_visited = np.zeros_like(transform_matrix, dtype=bool) while current_node != goal: i, j = current_node path.append((i, j)) is_visited[i][j] = True max_T = float('-inf') i_max = (-1, -1) i_last = 0 for i_last in range(len(path)): current_node = path[-1 - i_last] # get latest node in path for ci, cj in inc_order: ni, nj = current_node[0] + ci, current_node[1] + cj if is_valid_neighbor(ni, nj) and \ transform_matrix[ni][nj] > max_T: i_max = (ni, nj) max_T = transform_matrix[ni][nj] if i_max != (-1, -1): break if i_max == (-1, -1): break else: current_node = i_max if i_last != 0: print('backtracing to', current_node) path.append(goal) return path
988
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py
visualize_path
(grid_map, start, goal, path)
170
195
def visualize_path(grid_map, start, goal, path): # pragma: no cover oy, ox = start gy, gx = goal px, py = np.transpose(np.flipud(np.fliplr(path))) if not do_animation: plt.imshow(grid_map, cmap='Greys') plt.plot(ox, oy, "-xy") plt.plot(px, py, "-r") plt.plot(gx, gy, "-pg") plt.show() else: for ipx, ipy in zip(px, py): plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.imshow(grid_map, cmap='Greys') plt.plot(ox, oy, "-xb") plt.plot(px, py, "-r") plt.plot(gx, gy, "-pg") plt.plot(ipx, ipy, "or") plt.axis("equal") plt.grid(True) plt.pause(0.1)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py#L170-L195
2
[]
0
[]
0
false
80.952381
26
3
100
0
def visualize_path(grid_map, start, goal, path): # pragma: no cover oy, ox = start gy, gx = goal px, py = np.transpose(np.flipud(np.fliplr(path))) if not do_animation: plt.imshow(grid_map, cmap='Greys') plt.plot(ox, oy, "-xy") plt.plot(px, py, "-r") plt.plot(gx, gy, "-pg") plt.show() else: for ipx, ipy in zip(px, py): plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.imshow(grid_map, cmap='Greys') plt.plot(ox, oy, "-xb") plt.plot(px, py, "-r") plt.plot(gx, gy, "-pg") plt.plot(ipx, ipy, "or") plt.axis("equal") plt.grid(True) plt.pause(0.1)
989
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py
main
()
198
214
def main(): dir_path = os.path.dirname(os.path.realpath(__file__)) img = plt.imread(os.path.join(dir_path, 'map', 'test.png')) img = 1 - img # revert pixel values start = (43, 0) goal = (0, 0) # distance transform wavefront DT = transform(img, goal, transform_type='distance') DT_path = wavefront(DT, start, goal) visualize_path(img, start, goal, DT_path) # path transform wavefront PT = transform(img, goal, transform_type='path', alpha=0.01) PT_path = wavefront(PT, start, goal) visualize_path(img, start, goal, PT_path)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/WavefrontCPP/wavefront_coverage_path_planner.py#L198-L214
2
[ 0 ]
5.882353
[ 1, 2, 3, 5, 6, 9, 10, 11, 14, 15, 16 ]
64.705882
false
80.952381
17
1
35.294118
0
def main(): dir_path = os.path.dirname(os.path.realpath(__file__)) img = plt.imread(os.path.join(dir_path, 'map', 'test.png')) img = 1 - img # revert pixel values start = (43, 0) goal = (0, 0) # distance transform wavefront DT = transform(img, goal, transform_type='distance') DT_path = wavefront(DT, start, goal) visualize_path(img, start, goal, DT_path) # path transform wavefront PT = transform(img, goal, transform_type='path', alpha=0.01) PT_path = wavefront(PT, start, goal) visualize_path(img, start, goal, PT_path)
990
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
main
()
215
242
def main(): print("Start rrt star with dubins planning") # ====Search Path with RRT==== obstacleList = [ (5, 5, 1), (3, 6, 2), (3, 8, 2), (3, 10, 2), (7, 5, 2), (9, 5, 2) ] # [x,y,size(radius)] # Set Initial parameters start = [0.0, 0.0, np.deg2rad(0.0)] goal = [10.0, 10.0, np.deg2rad(0.0)] rrtstar_dubins = RRTStarDubins(start, goal, rand_area=[-2.0, 15.0], obstacle_list=obstacleList) path = rrtstar_dubins.planning(animation=show_animation) # Draw final path if show_animation: # pragma: no cover rrtstar_dubins.draw_graph() plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r') plt.grid(True) plt.pause(0.001) plt.show()
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L215-L242
2
[ 0, 1, 2, 3, 4, 13, 14, 15, 16, 17, 18, 19, 20 ]
59.090909
[]
0
false
80
28
4
100
0
def main(): print("Start rrt star with dubins planning") # ====Search Path with RRT==== obstacleList = [ (5, 5, 1), (3, 6, 2), (3, 8, 2), (3, 10, 2), (7, 5, 2), (9, 5, 2) ] # [x,y,size(radius)] # Set Initial parameters start = [0.0, 0.0, np.deg2rad(0.0)] goal = [10.0, 10.0, np.deg2rad(0.0)] rrtstar_dubins = RRTStarDubins(start, goal, rand_area=[-2.0, 15.0], obstacle_list=obstacleList) path = rrtstar_dubins.planning(animation=show_animation) # Draw final path if show_animation: # pragma: no cover rrtstar_dubins.draw_graph() plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r') plt.grid(True) plt.pause(0.001) plt.show()
991
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
RRTStarDubins.__init__
(self, start, goal, obstacle_list, rand_area, goal_sample_rate=10, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0, )
Setting Parameter start:Start Position [x,y] goal:Goal Position [x,y] obstacleList:obstacle Positions [[x,y,size],...] randArea:Random Sampling Area [min,max] robot_radius: robot body modeled as circle with given radius
Setting Parameter
39
67
def __init__(self, start, goal, obstacle_list, rand_area, goal_sample_rate=10, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0, ): """ Setting Parameter start:Start Position [x,y] goal:Goal Position [x,y] obstacleList:obstacle Positions [[x,y,size],...] randArea:Random Sampling Area [min,max] robot_radius: robot body modeled as circle with given radius """ self.start = self.Node(start[0], start[1], start[2]) self.end = self.Node(goal[0], goal[1], goal[2]) self.min_rand = rand_area[0] self.max_rand = rand_area[1] self.goal_sample_rate = goal_sample_rate self.max_iter = max_iter self.obstacle_list = obstacle_list self.connect_circle_dist = connect_circle_dist self.curvature = 1.0 # for dubins path self.goal_yaw_th = np.deg2rad(1.0) self.goal_xy_th = 0.5 self.robot_radius = robot_radius
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L39-L67
2
[ 0, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]
51.724138
[]
0
false
80
29
1
100
7
def __init__(self, start, goal, obstacle_list, rand_area, goal_sample_rate=10, max_iter=200, connect_circle_dist=50.0, robot_radius=0.0, ): self.start = self.Node(start[0], start[1], start[2]) self.end = self.Node(goal[0], goal[1], goal[2]) self.min_rand = rand_area[0] self.max_rand = rand_area[1] self.goal_sample_rate = goal_sample_rate self.max_iter = max_iter self.obstacle_list = obstacle_list self.connect_circle_dist = connect_circle_dist self.curvature = 1.0 # for dubins path self.goal_yaw_th = np.deg2rad(1.0) self.goal_xy_th = 0.5 self.robot_radius = robot_radius
992
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
RRTStarDubins.planning
(self, animation=True, search_until_max_iter=True)
return None
RRT Star planning animation: flag for animation on or off
RRT Star planning
69
108
def planning(self, animation=True, search_until_max_iter=True): """ RRT Star planning animation: flag for animation on or off """ self.node_list = [self.start] for i in range(self.max_iter): print("Iter:", i, ", number of nodes:", len(self.node_list)) rnd = self.get_random_node() nearest_ind = self.get_nearest_node_index(self.node_list, rnd) new_node = self.steer(self.node_list[nearest_ind], rnd) if self.check_collision( new_node, self.obstacle_list, self.robot_radius): near_indexes = self.find_near_nodes(new_node) new_node = self.choose_parent(new_node, near_indexes) if new_node: self.node_list.append(new_node) self.rewire(new_node, near_indexes) if animation and i % 5 == 0: self.plot_start_goal_arrow() self.draw_graph(rnd) if (not search_until_max_iter) and new_node: # check reaching the goal last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) print("reached max iteration") last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) else: print("Cannot find path") return None
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L69-L108
2
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 25, 26, 30, 31, 32, 33, 34, 35, 36 ]
80
[ 23, 24, 27, 28, 29, 37, 39 ]
17.5
false
80
40
10
82.5
3
def planning(self, animation=True, search_until_max_iter=True): self.node_list = [self.start] for i in range(self.max_iter): print("Iter:", i, ", number of nodes:", len(self.node_list)) rnd = self.get_random_node() nearest_ind = self.get_nearest_node_index(self.node_list, rnd) new_node = self.steer(self.node_list[nearest_ind], rnd) if self.check_collision( new_node, self.obstacle_list, self.robot_radius): near_indexes = self.find_near_nodes(new_node) new_node = self.choose_parent(new_node, near_indexes) if new_node: self.node_list.append(new_node) self.rewire(new_node, near_indexes) if animation and i % 5 == 0: self.plot_start_goal_arrow() self.draw_graph(rnd) if (not search_until_max_iter) and new_node: # check reaching the goal last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) print("reached max iteration") last_index = self.search_best_goal_node() if last_index: return self.generate_final_course(last_index) else: print("Cannot find path") return None
993
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
RRTStarDubins.draw_graph
(self, rnd=None)
110
129
def draw_graph(self, rnd=None): plt.clf() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if rnd is not None: plt.plot(rnd.x, rnd.y, "^k") for node in self.node_list: if node.parent: plt.plot(node.path_x, node.path_y, "-g") for (ox, oy, size) in self.obstacle_list: plt.plot(ox, oy, "ok", ms=30 * size) plt.plot(self.start.x, self.start.y, "xr") plt.plot(self.end.x, self.end.y, "xr") plt.axis([-2, 15, -2, 15]) plt.grid(True) self.plot_start_goal_arrow() plt.pause(0.01)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L110-L129
2
[ 0 ]
5
[ 1, 3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 18, 19 ]
75
false
80
20
5
25
0
def draw_graph(self, rnd=None): plt.clf() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect('key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if rnd is not None: plt.plot(rnd.x, rnd.y, "^k") for node in self.node_list: if node.parent: plt.plot(node.path_x, node.path_y, "-g") for (ox, oy, size) in self.obstacle_list: plt.plot(ox, oy, "ok", ms=30 * size) plt.plot(self.start.x, self.start.y, "xr") plt.plot(self.end.x, self.end.y, "xr") plt.axis([-2, 15, -2, 15]) plt.grid(True) self.plot_start_goal_arrow() plt.pause(0.01)
994
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
RRTStarDubins.plot_start_goal_arrow
(self)
131
133
def plot_start_goal_arrow(self): plot_arrow(self.start.x, self.start.y, self.start.yaw) plot_arrow(self.end.x, self.end.y, self.end.yaw)
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L131-L133
2
[ 0 ]
33.333333
[ 1, 2 ]
66.666667
false
80
3
1
33.333333
0
def plot_start_goal_arrow(self): plot_arrow(self.start.x, self.start.y, self.start.yaw) plot_arrow(self.end.x, self.end.y, self.end.yaw)
995
AtsushiSakai/PythonRobotics
15ab19688b2f6c03ee91a853f1f8cc9def84d162
PathPlanning/RRTStarDubins/rrt_star_dubins.py
RRTStarDubins.steer
(self, from_node, to_node)
return new_node
135
156
def steer(self, from_node, to_node): px, py, pyaw, mode, course_lengths = \ dubins_path_planner.plan_dubins_path( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if len(px) <= 1: # cannot find a dubins path return None new_node = copy.deepcopy(from_node) new_node.x = px[-1] new_node.y = py[-1] new_node.yaw = pyaw[-1] new_node.path_x = px new_node.path_y = py new_node.path_yaw = pyaw new_node.cost += sum([abs(c) for c in course_lengths]) new_node.parent = from_node return new_node
https://github.com/AtsushiSakai/PythonRobotics/blob/15ab19688b2f6c03ee91a853f1f8cc9def84d162/project2/PathPlanning/RRTStarDubins/rrt_star_dubins.py#L135-L156
2
[ 0, 1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 ]
86.363636
[]
0
false
80
22
3
100
0
def steer(self, from_node, to_node): px, py, pyaw, mode, course_lengths = \ dubins_path_planner.plan_dubins_path( from_node.x, from_node.y, from_node.yaw, to_node.x, to_node.y, to_node.yaw, self.curvature) if len(px) <= 1: # cannot find a dubins path return None new_node = copy.deepcopy(from_node) new_node.x = px[-1] new_node.y = py[-1] new_node.yaw = pyaw[-1] new_node.path_x = px new_node.path_y = py new_node.path_yaw = pyaw new_node.cost += sum([abs(c) for c in course_lengths]) new_node.parent = from_node return new_node
996