Skip to content

Commit 7473364

Browse files
committed
code clean up
1 parent 5fd8974 commit 7473364

File tree

3 files changed

+141
-122
lines changed

3 files changed

+141
-122
lines changed

PathPlanning/HybridAStar/a_star.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
33
A* grid based planning
44
5-
author: Atsushi Sakai(@Atsushi_twi)
6-
Nikos Kanargias ([email protected])
5+
author: Nikos Kanargias ([email protected])
76
87
See Wikipedia article (https://en.wikipedia.org/wiki/A*_search_algorithm)
98
109
"""
1110

12-
import matplotlib.pyplot as plt
1311
import math
1412
import heapq
15-
16-
# _round = round
17-
# def round(x):
18-
# return int(_round(x))
13+
import matplotlib.pyplot as plt
1914

2015
show_animation = False
2116

17+
2218
class Node:
2319

2420
def __init__(self, x, y, cost, pind):
@@ -67,7 +63,6 @@ def dp_planning(sx, sy, gx, gy, ox, oy, reso, rr):
6763
openset[calc_index(ngoal, xw, minx, miny)] = ngoal
6864
pq = []
6965
pq.append((0, calc_index(ngoal, xw, minx, miny)))
70-
7166

7267
while 1:
7368
if not pq:
@@ -87,7 +82,6 @@ def dp_planning(sx, sy, gx, gy, ox, oy, reso, rr):
8782
plt.pause(0.001)
8883

8984
# Remove the item from the open set
90-
9185

9286
# expand search grid based on motion model
9387
for i, _ in enumerate(motion):
@@ -104,15 +98,17 @@ def dp_planning(sx, sy, gx, gy, ox, oy, reso, rr):
10498

10599
if n_id not in openset:
106100
openset[n_id] = node # Discover a new node
107-
heapq.heappush(pq, (node.cost, calc_index(node, xw, minx, miny)))
101+
heapq.heappush(
102+
pq, (node.cost, calc_index(node, xw, minx, miny)))
108103
else:
109104
if openset[n_id].cost >= node.cost:
110105
# This path is the best until now. record it!
111106
openset[n_id] = node
112-
heapq.heappush(pq, (node.cost, calc_index(node, xw, minx, miny)))
113-
107+
heapq.heappush(
108+
pq, (node.cost, calc_index(node, xw, minx, miny)))
114109

115-
rx, ry = calc_final_path(closedset[calc_index(nstart, xw, minx, miny)], closedset, reso)
110+
rx, ry = calc_final_path(closedset[calc_index(
111+
nstart, xw, minx, miny)], closedset, reso)
116112

117113
return rx, ry, closedset
118114

PathPlanning/HybridAStar/car.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,60 @@
1+
"""
2+
3+
Car model for Hybrid A* path planning
4+
5+
author: Zheng Zh (@Zhengzh)
6+
7+
"""
8+
19

210
import matplotlib.pyplot as plt
311
from math import sqrt, cos, sin, tan, pi
412

5-
WB = 3. # rear to front wheel
6-
W = 2. # width of car
7-
LF = 3.3 # distance from rear to vehicle front end
8-
LB = 1.0 # distance from rear to vehicle back end
9-
MAX_STEER = 0.6 #[rad] maximum steering angle
13+
WB = 3. # rear to front wheel
14+
W = 2. # width of car
15+
LF = 3.3 # distance from rear to vehicle front end
16+
LB = 1.0 # distance from rear to vehicle back end
17+
MAX_STEER = 0.6 # [rad] maximum steering angle
1018

11-
WBUBBLE_DIST = (LF-LB)/2.0
12-
WBUBBLE_R = sqrt(((LF+LB)/2.0)**2+1)
19+
WBUBBLE_DIST = (LF - LB) / 2.0
20+
WBUBBLE_R = sqrt(((LF + LB) / 2.0)**2 + 1)
1321

1422
# vehicle rectangle verticles
1523
VRX = [LF, LF, -LB, -LB, LF]
16-
VRY = [W/2,-W/2,-W/2,W/2,W/2]
24+
VRY = [W / 2, -W / 2, -W / 2, W / 2, W / 2]
1725

1826

1927
def check_car_collision(xlist, ylist, yawlist, ox, oy, kdtree):
2028
for x, y, yaw in zip(xlist, ylist, yawlist):
21-
cx = x + WBUBBLE_DIST*cos(yaw)
22-
cy = y + WBUBBLE_DIST*sin(yaw)
29+
cx = x + WBUBBLE_DIST * cos(yaw)
30+
cy = y + WBUBBLE_DIST * sin(yaw)
2331

2432
ids = kdtree.search_in_distance([cx, cy], WBUBBLE_R)
2533

2634
if not ids:
2735
continue
28-
36+
2937
if not rectangle_check(x, y, yaw,
30-
[ox[i] for i in ids], [oy[i] for i in ids]):
31-
return False # collision
32-
33-
return True # no collision
38+
[ox[i] for i in ids], [oy[i] for i in ids]):
39+
return False # collision
40+
41+
return True # no collision
42+
3443

3544
def rectangle_check(x, y, yaw, ox, oy):
3645
# transform obstacles to base link frame
3746
c, s = cos(-yaw), sin(-yaw)
3847
for iox, ioy in zip(ox, oy):
3948
tx = iox - x
4049
ty = ioy - y
41-
rx = c*tx - s*ty
42-
ry = s*tx + c*ty
50+
rx = c * tx - s * ty
51+
ry = s * tx + c * ty
4352

44-
if not (rx > LF or rx < -LB or ry > W/2.0 or ry < -W/2.0):
53+
if not (rx > LF or rx < -LB or ry > W / 2.0 or ry < -W / 2.0):
4554
# print (x, y, yaw, iox, ioy, c, s, rx, ry)
46-
return False # no collision
47-
48-
return True # collision
55+
return False # no collision
56+
57+
return True # collision
4958

5059

5160
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
@@ -58,35 +67,38 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
5867
fc=fc, ec=ec, head_width=width, head_length=width, alpha=0.4)
5968
# plt.plot(x, y)
6069

70+
6171
def plot_car(x, y, yaw):
6272
car_color = '-k'
6373
c, s = cos(yaw), sin(yaw)
64-
74+
6575
car_outline_x, car_outline_y = [], []
6676
for rx, ry in zip(VRX, VRY):
67-
tx = c*rx-s*ry + x
68-
ty = s*rx+c*ry + y
77+
tx = c * rx - s * ry + x
78+
ty = s * rx + c * ry + y
6979
car_outline_x.append(tx)
7080
car_outline_y.append(ty)
71-
72-
arrow_x, arrow_y, arrow_yaw = c*1.5+x, s*1.5+y, yaw
81+
82+
arrow_x, arrow_y, arrow_yaw = c * 1.5 + x, s * 1.5 + y, yaw
7383
plot_arrow(arrow_x, arrow_y, arrow_yaw)
74-
84+
7585
plt.plot(car_outline_x, car_outline_y, car_color)
7686

87+
7788
def pi_2_pi(angle):
7889
return (angle + pi) % (2 * pi) - pi
7990

91+
8092
def move(x, y, yaw, distance, steer, L=WB):
8193
x += distance * cos(yaw)
8294
y += distance * sin(yaw)
83-
yaw += pi_2_pi(distance * tan(steer) / L) # distance/2
95+
yaw += pi_2_pi(distance * tan(steer) / L) # distance/2
8496

8597
return x, y, yaw
8698

99+
87100
if __name__ == '__main__':
88101
x, y, yaw = 0., 0., 1.
89102
plt.axis('equal')
90103
plot_car(x, y, yaw)
91-
plt.show()
92-
104+
plt.show()

0 commit comments

Comments
 (0)