1+ """
2+
3+ Car model for Hybrid A* path planning
4+
5+ author: Zheng Zh (@Zhengzh)
6+
7+ """
8+
19
210import matplotlib .pyplot as plt
311from 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
1523VRX = [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
1927def 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
3544def 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
5160def 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+
6171def 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+
7788def pi_2_pi (angle ):
7889 return (angle + pi ) % (2 * pi ) - pi
7990
91+
8092def 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+
87100if __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