-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinetic_Model.py
69 lines (48 loc) · 1.96 KB
/
Kinetic_Model.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from math import cos, sin, tan, pi
import numpy as np
from scipy.spatial.transform import Rotation as Rot
'''
Modify these vehicle parameters before use
'''
WB = 2.875 # wheel base length
W = 1.85 # width of car
LF = 3.7975 # distance from rear to vehicle front end
LB = 0.9225 # distance from rear to vehicle back end
MAX_STEER = pi/3 # [rad] maximum steering angle
BUBBLE_DIST = (LF - LB) / 2.0 # distance from rear to center of vehicle.
BUBBLE_R = np.hypot((LF + LB) / 2.0, W / 2.0) # bubble radius
# vehicle rectangle vertices
VRX = [LF, LF, -LB, -LB, LF]
VRY = [W / 2, -W / 2, -W / 2, W / 2, W / 2]
def rot_mat_2d(angle):
#Create 2D rotation matrix from an angle
return Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
def check_car_collision(x_list, y_list, yaw_list, ox, oy, kd_tree):
for i_x, i_y, i_yaw in zip(x_list, y_list, yaw_list):
cx = i_x + BUBBLE_DIST * cos(i_yaw)
cy = i_y + BUBBLE_DIST * sin(i_yaw)
ids = kd_tree.query_ball_point([cx, cy], BUBBLE_R)
if not ids:
continue
if not rectangle_check(i_x, i_y, i_yaw,
[ox[i] for i in ids], [oy[i] for i in ids]):
return False # collision
return True # no collision
def rectangle_check(x, y, yaw, ox, oy):
# transform obstacles to base link frame
rot = rot_mat_2d(yaw)
for iox, ioy in zip(ox, oy):
tx = iox - x
ty = ioy - y
converted_xy = np.stack([tx, ty]).T @ rot
rx, ry = converted_xy[0], converted_xy[1]
if not (rx > LF or rx < -LB or ry > W / 2.0 or ry < -W / 2.0):
return False # no collision
return True # collision
def pi_2_pi(angle):
return (angle + pi) % (2 * pi) - pi
def move(x, y, yaw, distance, steer, L=WB):
x += distance * cos(yaw)
y += distance * sin(yaw)
yaw += pi_2_pi(distance * tan(steer) / L) # distance/2
return x, y, yaw