-
Notifications
You must be signed in to change notification settings - Fork 42
/
RVO.py
198 lines (184 loc) · 7.41 KB
/
RVO.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from math import ceil, floor, sqrt
import copy
import numpy
from math import cos, sin, tan, atan2, asin
from math import pi as PI
def distance(pose1, pose2):
""" compute Euclidean distance for 2D """
return sqrt((pose1[0]-pose2[0])**2+(pose1[1]-pose2[1])**2)+0.001
def RVO_update(X, V_des, V_current, ws_model):
""" compute best velocity given the desired velocity, current velocity and workspace model"""
ROB_RAD = ws_model['robot_radius']+0.1
V_opt = list(V_current)
for i in range(len(X)):
vA = [V_current[i][0], V_current[i][1]]
pA = [X[i][0], X[i][1]]
RVO_BA_all = []
for j in range(len(X)):
if i!=j:
vB = [V_current[j][0], V_current[j][1]]
pB = [X[j][0], X[j][1]]
# use RVO
transl_vB_vA = [pA[0]+0.5*(vB[0]+vA[0]), pA[1]+0.5*(vB[1]+vA[1])]
# use VO
#transl_vB_vA = [pA[0]+vB[0], pA[1]+vB[1]]
dist_BA = distance(pA, pB)
theta_BA = atan2(pB[1]-pA[1], pB[0]-pA[0])
if 2*ROB_RAD > dist_BA:
dist_BA = 2*ROB_RAD
theta_BAort = asin(2*ROB_RAD/dist_BA)
theta_ort_left = theta_BA+theta_BAort
bound_left = [cos(theta_ort_left), sin(theta_ort_left)]
theta_ort_right = theta_BA-theta_BAort
bound_right = [cos(theta_ort_right), sin(theta_ort_right)]
# use HRVO
# dist_dif = distance([0.5*(vB[0]-vA[0]),0.5*(vB[1]-vA[1])],[0,0])
# transl_vB_vA = [pA[0]+vB[0]+cos(theta_ort_left)*dist_dif, pA[1]+vB[1]+sin(theta_ort_left)*dist_dif]
RVO_BA = [transl_vB_vA, bound_left, bound_right, dist_BA, 2*ROB_RAD]
RVO_BA_all.append(RVO_BA)
for hole in ws_model['circular_obstacles']:
# hole = [x, y, rad]
vB = [0, 0]
pB = hole[0:2]
transl_vB_vA = [pA[0]+vB[0], pA[1]+vB[1]]
dist_BA = distance(pA, pB)
theta_BA = atan2(pB[1]-pA[1], pB[0]-pA[0])
# over-approximation of square to circular
OVER_APPROX_C2S = 1.5
rad = hole[2]*OVER_APPROX_C2S
if (rad+ROB_RAD) > dist_BA:
dist_BA = rad+ROB_RAD
theta_BAort = asin((rad+ROB_RAD)/dist_BA)
theta_ort_left = theta_BA+theta_BAort
bound_left = [cos(theta_ort_left), sin(theta_ort_left)]
theta_ort_right = theta_BA-theta_BAort
bound_right = [cos(theta_ort_right), sin(theta_ort_right)]
RVO_BA = [transl_vB_vA, bound_left, bound_right, dist_BA, rad+ROB_RAD]
RVO_BA_all.append(RVO_BA)
vA_post = intersect(pA, V_des[i], RVO_BA_all)
V_opt[i] = vA_post[:]
return V_opt
def intersect(pA, vA, RVO_BA_all):
# print '----------------------------------------'
# print 'Start intersection test'
norm_v = distance(vA, [0, 0])
suitable_V = []
unsuitable_V = []
for theta in numpy.arange(0, 2*PI, 0.1):
for rad in numpy.arange(0.02, norm_v+0.02, norm_v/5.0):
new_v = [rad*cos(theta), rad*sin(theta)]
suit = True
for RVO_BA in RVO_BA_all:
p_0 = RVO_BA[0]
left = RVO_BA[1]
right = RVO_BA[2]
dif = [new_v[0]+pA[0]-p_0[0], new_v[1]+pA[1]-p_0[1]]
theta_dif = atan2(dif[1], dif[0])
theta_right = atan2(right[1], right[0])
theta_left = atan2(left[1], left[0])
if in_between(theta_right, theta_dif, theta_left):
suit = False
break
if suit:
suitable_V.append(new_v)
else:
unsuitable_V.append(new_v)
new_v = vA[:]
suit = True
for RVO_BA in RVO_BA_all:
p_0 = RVO_BA[0]
left = RVO_BA[1]
right = RVO_BA[2]
dif = [new_v[0]+pA[0]-p_0[0], new_v[1]+pA[1]-p_0[1]]
theta_dif = atan2(dif[1], dif[0])
theta_right = atan2(right[1], right[0])
theta_left = atan2(left[1], left[0])
if in_between(theta_right, theta_dif, theta_left):
suit = False
break
if suit:
suitable_V.append(new_v)
else:
unsuitable_V.append(new_v)
#----------------------
if suitable_V:
# print 'Suitable found'
vA_post = min(suitable_V, key = lambda v: distance(v, vA))
new_v = vA_post[:]
for RVO_BA in RVO_BA_all:
p_0 = RVO_BA[0]
left = RVO_BA[1]
right = RVO_BA[2]
dif = [new_v[0]+pA[0]-p_0[0], new_v[1]+pA[1]-p_0[1]]
theta_dif = atan2(dif[1], dif[0])
theta_right = atan2(right[1], right[0])
theta_left = atan2(left[1], left[0])
else:
# print 'Suitable not found'
tc_V = dict()
for unsuit_v in unsuitable_V:
tc_V[tuple(unsuit_v)] = 0
tc = []
for RVO_BA in RVO_BA_all:
p_0 = RVO_BA[0]
left = RVO_BA[1]
right = RVO_BA[2]
dist = RVO_BA[3]
rad = RVO_BA[4]
dif = [unsuit_v[0]+pA[0]-p_0[0], unsuit_v[1]+pA[1]-p_0[1]]
theta_dif = atan2(dif[1], dif[0])
theta_right = atan2(right[1], right[0])
theta_left = atan2(left[1], left[0])
if in_between(theta_right, theta_dif, theta_left):
small_theta = abs(theta_dif-0.5*(theta_left+theta_right))
if abs(dist*sin(small_theta)) >= rad:
rad = abs(dist*sin(small_theta))
big_theta = asin(abs(dist*sin(small_theta))/rad)
dist_tg = abs(dist*cos(small_theta))-abs(rad*cos(big_theta))
if dist_tg < 0:
dist_tg = 0
tc_v = dist_tg/distance(dif, [0,0])
tc.append(tc_v)
tc_V[tuple(unsuit_v)] = min(tc)+0.001
WT = 0.2
vA_post = min(unsuitable_V, key = lambda v: ((WT/tc_V[tuple(v)])+distance(v, vA)))
return vA_post
def in_between(theta_right, theta_dif, theta_left):
if abs(theta_right - theta_left) <= PI:
if theta_right <= theta_dif <= theta_left:
return True
else:
return False
else:
if (theta_left <0) and (theta_right >0):
theta_left += 2*PI
if theta_dif < 0:
theta_dif += 2*PI
if theta_right <= theta_dif <= theta_left:
return True
else:
return False
if (theta_left >0) and (theta_right <0):
theta_right += 2*PI
if theta_dif < 0:
theta_dif += 2*PI
if theta_left <= theta_dif <= theta_right:
return True
else:
return False
def compute_V_des(X, goal, V_max):
V_des = []
for i in range(len(X)):
dif_x = [goal[i][k]-X[i][k] for k in range(2)]
norm = distance(dif_x, [0, 0])
norm_dif_x = [dif_x[k]*V_max[k]/norm for k in range(2)]
V_des.append(norm_dif_x[:])
if reach(X[i], goal[i], 0.1):
V_des[i][0] = 0
V_des[i][1] = 0
return V_des
def reach(p1, p2, bound=0.5):
if distance(p1,p2)< bound:
return True
else:
return False