-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_two_collisions.py
212 lines (168 loc) · 8.4 KB
/
test_two_collisions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# COLLISION AVOIDANCE MECHANISM
import numpy as np
import matplotlib.pyplot as plt
import logging
from arm import Arm
from velocity_control import linear_interpolation, find_intersection, update_velocity, adjust_arm_velocity
from arm import Arm
logger = logging.getLogger(__name__)
logging.basicConfig()
logger.setLevel(logging.INFO)
INIT_VEL = 0.16 # controls speed of paths
INC_VEL = 0.16
COLLISION_RANGE = 4
def main():
animate = False
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('X, [m]')
ax.set_ylabel('Y, [m]')
ax.set_zlabel('Z, [m]')
ax.set_xlim([0,12])
ax.set_ylim([0,12])
ax.set_zlim([0,12])
l1 = np.array([[0.0,0.0,0.0], [6.5,7.0,7.0], [9.0,1.0,2.0]])
l2 = np.array([[2.0,0.0,3.0], [4.0,6.0,6.0], [5.0,4.0,4.0], [10.0,6.0,6.0]])
# l1 = np.array([[0.0,0.0,0.0], [10.0,8.0,8.0]])
# l2 = np.array([[2.0,0.0,3.0], [4.0,6.0,6.0], [10.0,2.0,2.0]])
# initialize arms
# start at 1 pts of each lines, end at last pt of each line
arm1 = Arm(name="PSM1", position=l1[0], destination=l1[l1.shape[0]-1], velocity=INIT_VEL, home=l1[0])
arm2 = Arm(name="PSM2", position=l2[0], destination=l2[l2.shape[0]-1], velocity=INIT_VEL, home=l2[0])
plt.plot(l1[:,0], l1[:,1], l1[:,2], 'o', color='orange')
plt.plot(l2[:,0], l2[:,1], l2[:,2], 'o', color='blue')
# initialize paths
path1 = linear_interpolation(l1, INIT_VEL)
path2 = linear_interpolation(l2, INIT_VEL)
for i in range(l1.shape[0]-1):
ax.plot([l1[i,0], l1[i+1,0]], [l1[i,1], l1[i+1,1]], [l1[i,2], l1[i+1,2]], color = 'orange', linewidth=1, zorder=15)
for i in range(l2.shape[0]-1):
ax.plot([l2[i,0], l2[i+1,0]], [l2[i,1], l2[i+1,1]], [l2[i,2], l2[i+1,2]], color = 'blue', linewidth=1, zorder=15)
arm1.set_position(path1[0]) # start at pt. 0 of path1
arm2.set_position(path2[0]) # start at pt. 60 of path2
check_collision = True
arm1_collision, arm2_collision = np.empty(3), np.empty(3)
idx1 = 0
idx2 = 0
i = 0
while(path1.shape[0] != 0 or path2.shape[0] != 0):
# idx1 = i
# idx2 = i
if check_collision:
print("Checking collisions...")
# check whether any pts in paths are within threshold
# get start index for path change (current arm pos)
intersect_pts1, intersect_pts2 = find_intersection(path1, path2)
if intersect_pts1.size > 0 and intersect_pts2.size > 0:
# now that we have the intersection zones in both paths, adjust speed and animate
# update current path ONLY to first OR last collision point, keep initial path post collision pt
print("COLLISION DETECTED")
arm1_collision = intersect_pts1[0]
arm2_collision = intersect_pts2[0]
print(arm1_collision, arm2_collision)
path1_col_idx = np.where(path1 == arm1_collision)[0][0]
path2_col_idx = np.where(path2 == arm2_collision)[0][0]
# plot collision points
for col_pt in intersect_pts1:
ax.plot(col_pt[0], col_pt[1], col_pt[2], 'o', color='cyan')
for col_pt in intersect_pts2:
ax.plot(col_pt[0], col_pt[1], col_pt[2], 'o', color='cyan')
# choose whether to speed up arm nearest or furthest to goal
# update paths such that speed is inc/dec until collision point only
path1, path2 = update_velocity(p_fast=path1, p_slow=path2, vel=INC_VEL, idx_fast=path1_col_idx, idx_slow=path2_col_idx)
logger.info("INCREASED {} VELOCITY, DECREASED {} VELOCITY".format(arm1.get_name(), arm2.get_name()))
else:
print("NO COLLISION DETECTED")
check_collision = False
else:
if (path1[0] == arm1_collision).all() or (path2[0] == arm2_collision).all():
print("RESETTING VELOCITY")
plt.plot(path1[0,0], path1[0,1], path1[0,2], 'o', color='yellow', markersize=5)
path1, path2 = update_velocity(p_fast=path1, p_slow=path2, vel=INIT_VEL)
arm1_collision, arm2_collision = np.empty(3), np.empty(3)
check_collision = True
# print("PLOTTING {}, {}".format(path1[0], path2[0]))
if idx1 < path1.shape[0]:
plt.plot(path1[0,0], path1[0,1], path1[0,2], 'o', color='red', markersize=1)
arm1.set_position(path1[0]) # start at pt. 0 of path1
path1 = np.delete(path1, 0, axis=0)
if idx2 < path2.shape[0]:
plt.plot(path2[0,0], path2[0,1], path2[0,2], 'o', color='red', markersize=1)
arm2.set_position(path2[0]) # start at pt. 60 of path2
path2 = np.delete(path2, 0, axis=0)
# i += 1
plt.pause(0.0005)
# run_path(new_path1, new_path2, arm1, arm2)
logger.info("INTERSECTIONS: {}".format(intersect_pts1))
plt.show()
def euclidean_distance(point1, point2):
distance = np.linalg.norm(point1-point2)
return distance
def avoid_collision(intersect_pts1, intersect_pts2, path1, path2, arm1, arm2):
# get start index for path change (current arm pos)
idx1 = np.where(path1 == arm1.get_position())[0][0] # get start index
idx2 = np.where(path2 == arm2.get_position())[0][0] # get start index
# if collision detected, adjust path velocities
if (intersect_pts1.shape[0] != 0) and (intersect_pts2.shape[0] != 0):
logger.info("COLLISION DETECTED!")
logger.debug("INTERSECTIONS: {}, SHAPE: {}".format(intersect_pts1, intersect_pts1.shape[0]))
# temp pre-set velocities:
# NOTE: start point of new paths is arm current location!! need to iter from 0 when plotting
new_path1, new_path2 = adjust_arm_velocity(path1, path2, vel1=0.07, vel=0.08)
# set new path and last collision point
logger.info("UPDATED VELOCITY FOR COLLISION AVOIDANCE")
print("Arm1: {}, Arm2: {}".format(0.07, 0.09))
else:
# reset paths velocities if no more intersections
logger.info("NO COLLISION DETECTED!")
new_path1, new_path2 = adjust_arm_velocity(path, path2, vel=INIT_VEL)
print("Arm1: {}, Arm2: {}".format(INIT_VEL, INIT_VEL))
# arm1_sm.set_path(new_path1, np.empty(3))
# arm2_sm.set_path(new_path2, np.empty(3))
return new_path1, new_path2
def run_path(path1, path2, arm1, arm2):
# check whether any pts in paths are within threshold
# idx1 = np.where(path1 == arm1.get_position())[0][0] # get start index
# idx2 = np.where(path2 == arm2.get_position())[0][0] # get start index
idx1 = 0
idx2 = 0
# path_range = min(path1[idx1:,:].shape[0], path2[idx2:,:].shape[0]) # get minimum of both remaining paths
# print("PATH RANGES: {}, {}".format(path1[idx1:,:].shape[0], path2[idx2:,:].shape[0]))
intersect1 = []
intersect2 = []
i = 0
while(idx1 != path1[idx1:,:].shape[0] or idx2 != path2[idx2:,:].shape[0]):
# idx1 = np.where(path1 == arm1.get_position())[0][0] + i
# idx2 = np.where(path2 == arm2.get_position())[0][0] + i
idx1 = i
idx2 = i
if idx1 < path1.shape[0]:
plt.plot(path1[idx1,0], path1[idx1,1], path1[idx1,2], 'o', color='red', markersize=1)
if idx2 < path2.shape[0]:
plt.plot(path2[idx2,0], path2[idx2,1], path2[idx2,2], 'o', color='red', markersize=1)
i += 1
plt.pause(0.0005)
plt.show()
def show_fig():
animate = False
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('X, [m]')
ax.set_ylabel('Y, [m]')
ax.set_zlabel('Z, [m]')
ax.set_xlim([0,12])
ax.set_ylim([0,12])
ax.set_zlim([0,12])
# l1 = np.array([[0.0,0.0,0.0], [6.5,7.0,7.0], [9.0,1.0,2.0]])
# l2 = np.array([[2.0,0.0,3.0], [4.0,6.0,6.0], [5.0,4.0,4.0], [10.0,6.0,6.0]])
# # initialize arms
# # start at 1 pts of each lines, end at last pt of each line
# plt.plot(l1[:,0], l1[:,1], l1[:,2], 'o', color='orange')
# plt.plot(l2[:,0], l2[:,1], l2[:,2], 'o', color='blue')
# for i in range(l1.shape[0]-1):
# ax.plot([l1[i,0], l1[i+1,0]], [l1[i,1], l1[i+1,1]], [l1[i,2], l1[i+1,2]], color = 'orange', linewidth=1, zorder=15)
# for i in range(l2.shape[0]-1):
# ax.plot([l2[i,0], l2[i+1,0]], [l2[i,1], l2[i+1,1]], [l2[i,2], l2[i+1,2]], color = 'blue', linewidth=1, zorder=15)
plt.show()
if __name__ == '__main__':
main()