-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapf.py
104 lines (72 loc) · 2.68 KB
/
apf.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
MPS_TO_KMH = 18/5
KMH_TO_MPS = 5/18
ROAD_WIDTH = 3.7*2
ROAD_LENGTH = 200 # meters
class APF():
def __init__(self, width = ROAD_WIDTH, length = ROAD_LENGTH):
self.width = width
self.center = width/2
self.length = length
# Road potential params
self.yc1 = width/4
self.yc2 = 3*width/4
self.pm = 0.5
# Obstacle potential params
self.delta_x = 50 * 0.1
self.delta_y = 2.5 * 0.1
self.epsilon = 10
self.Uobs = 0
# Grid params for plotting
self.xx = None
self.yy = None
self.U = None
# Two-way lane road potential
def Road_potential_2way_opp(self, y):
Amp = np.array(y > self.yc2, dtype=int) * (1 - self.pm) + self.pm
w = 2*np.pi*y / (self.width/2)
Uroad = np.cos(w) - (np.array(y < self.center, dtype=int)) * ((np.cos(w) - 1) - (1-self.pm)*(self.center-y))
Uroad = 0.5 * (Amp*(Uroad + 1)) #+ (1-self.pm)*(np.array(y < self.yc1, dtype=int)) * (np.cos(w) + 1))
return Uroad
def Obstacle_potential(self, ego, obs, plot = False):
sole = ego.x_simulated
U_obs = 0
if plot:
ue = 40*KMH_TO_MPS
ve = 0
Xe = self.xx
Ye = self.yy
else:
ue = sole[0][-1]
ve = sole[1][-1]
Xe = sole[4][-1]
Ye = sole[5][-1]
vele = np.sqrt( ue**2 + ve**2 )
for ob in obs:
sol = ob.x_simulated
uo = sol[0][-1]
vo = sol[1][-1]
Xo = sol[4][-1]
Yo = sol[5][-1]
velo = np.sqrt( uo**2 + vo**2)
v_rel = vele - velo
U_obs = np.exp( - (1 / (v_rel + self.epsilon)) * ( (Xe-Xo)**2 / self.delta_x**2 + (Ye-Yo)**2 / self.delta_y**2 ) )
return U_obs
def plot_potential(self, ego, obs):
x = np.linspace(0, self.length, int(self.length/0.5))
y = np.linspace(0, self.width, int(self.length/0.1))
self.xx, self.yy = np.meshgrid(x,y)
self.U = np.zeros(self.xx.shape)
self.U += self.Road_potential_2way_opp(self.yy)
self.U += self.Obstacle_potential(ego, obs, plot = True)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(self.xx, self.yy, self.U, cmap = cm.jet)
ax.set_box_aspect((np.ptp(self.xx)/10, np.ptp(self.yy), np.ptp(self.U)))
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('U Label')
plt.show()