-
Notifications
You must be signed in to change notification settings - Fork 0
/
reachability.py
392 lines (289 loc) · 11.4 KB
/
reachability.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import math
import numpy as np
import matplotlib.pyplot as plt
import json
import os
from tqdm import tqdm
from compas.datastructures import Mesh
from compas_fab.backends import RosClient
from compas_fab.backends import RosValidationError
from compas_fab.robots import PlanningScene
from compas_fab.robots import CollisionMesh
from compas.geometry import Point
from compas.geometry import Frame
from compas.geometry import Plane
#################
# I/O functions
#################
def get_directory(b, c):
a = os.getcwd()
path = os.path.join(a, b, c)
return path
def save_JsonFile(path, name, vec, result_dict):
filepath = os.path.join(path, "{}.json".format(name))
try:
# if file exists
with open(filepath, "w") as f:
v = list(vec)
data = {}
data["vector"] = v
data["results"] = result_dict
json.dump(data, f, indent=4)
f.close()
except TypeError:
print("write to file not working")
def append_JsonFiles(in_filepaths, out_filepath):
"""concatenate several JSON files"""
data_combined = {}
with open(in_filepaths[0], "r") as f:
data_combined = json.load(f)
f.close()
for in_filepath in in_filepaths[1:]:
with open(in_filepath, "r") as f:
data = json.load(f)
data_combined["results"].update(data["results"])
f.close()
with open(out_filepath, "w") as f:
json.dump(data_combined, f, indent=4)
f.close()
def combine_JsonFiles(in_filepaths, out_filepath):
"""combine data from several JSON files
add up how many total TRUE values each point has
"""
data_combined = {}
# initialize the counting dictionary
with open(in_filepaths[0], "r") as f:
print("loading {}".format(in_filepaths[0]))
data_combined = json.load(f)
f.close()
data_combined["vector"] = 1
# replace True/False with 1/0
for key, value in data_combined["results"].items():
if value:
data_combined["results"][key] = 1
else:
data_combined["results"][key] = 0
# read in rest of data files
for in_filepath in in_filepaths[1:]:
with open(in_filepath, "r") as f:
print("loading {}".format(in_filepath))
data = json.load(f)
f.close()
data_combined["vector"] += 1
# increment anywhere there is True with 1
for key, value in data["results"].items():
if value:
data_combined["results"][key] += 1
with open(out_filepath, "w") as f:
json.dump(data_combined, f, indent=4)
f.close()
def generate_filepaths_append(path, filenames, rob_num, idx):
filepaths = []
for filename in filenames:
filename = filename.format(rob_num, idx)
filepaths.append(os.path.join(path, filename))
return filepaths
def generate_filepaths_combined(path, filename, rob_num, merge_range):
filepaths = []
for i in merge_range:
filename_i = filename.format(rob_num, i)
filepaths.append(os.path.join(path, filename_i))
return filepaths
def plot_dots(idx_soln, idx_no_soln):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(projection="3d")
for p in idx_soln:
ax.scatter(p.x, p.y, p.z, color="blue")
for p in idx_no_soln:
ax.scatter(p.x, p.y, p.z, color="red")
ax.set_xlabel("$X$", fontsize=20)
ax.set_ylabel("$Y$")
ax.set_zlabel("$Z$")
plt.show()
#################
# Path Planning Functions
#################
def connect_and_scene():
"""ROS connection, adding collision meshes"""
ros_client = RosClient("localhost")
ros_client.run()
robot = ros_client.load_robot()
scene = PlanningScene(robot)
mesh = Mesh.from_stl("./3dm/ground.stl")
cm = CollisionMesh(mesh, "ground")
scene.add_collision_mesh(cm)
return robot
def robot_config(robot, rob_num):
"""Starting configuration for each robot to be used in each IK run"""
config_scaled = robot.zero_configuration()
if rob_num == "rob1":
config1 = [-90.0, 0.0, 0.0, 0.0, 90.0, 180, 1950]
config2 = [90.0, 0.0, 0.0, 0.0, 90.0, 180, 100]
config3 = [180.0, 0.0, 0.0, 0.0, 90.0, 180.0]
elif rob_num == "rob2":
config1 = [90.0, 0.0, 0.0, 0.0, 90.0, 180, 3800]
config2 = [90.0, 0.0, 0.0, 0.0, 90.0, 180, 1950]
config3 = [180.0, 0.0, 0.0, 0.0, 90.0, 180.0]
elif rob_num == "rob3":
config1 = [-90.0, 0.0, 0.0, 0.0, 90.0, 180, 3800]
config2 = [90.0, 0.0, 0.0, 0.0, 90.0, 180, 100]
config3 = [180.0, 0.0, 0.0, 0.0, 90.0, 180.0]
for i in range(6):
config_scaled["r1_joint_{}".format(i + 1)] = math.radians(config1[i])
config_scaled["r2_joint_{}".format(i + 1)] = math.radians(config2[i])
config_scaled["r3_joint_{}".format(i + 1)] = math.radians(config3[i])
# mm --> m
config_scaled["r1_cart_joint"] = config1[6] / 1000
config_scaled["r2_cart_joint"] = config2[6] / 1000
return config_scaled
def ik_calc(robot, frame, start_config, planning_group):
"""single IK calcultion, given failure timeout"""
set_timeout = 0.05 # important variable, controls overall runtime
# catch the error if timeout reached = no IK solution
return robot.inverse_kinematics(
frame, start_config, planning_group, options={"timeout": set_timeout}
)
#################
# Geometry Functions
#################
def points_ranges(rob_num, n):
"""define the grid of points to search for each robot in each run"""
if rob_num == "rob1":
ranges = {
"i": np.arange(-3.2, 6.01, 0.1),
"j": np.arange(0.3, 4.91, 0.1),
"k": np.arange(-0.4, 3.41, 0.1),
"name": "{}_vec{:0>3}".format(rob_num, n),
}
elif rob_num == "rob2":
ranges = {
"i": np.arange(-3.2, 6.01, 0.1),
"j": np.arange(-1.5, 3.11, 0.1),
"k": np.arange(-0.4, 3.41, 0.1),
"name": "{}_vec{:0>3}".format(rob_num, n),
}
elif rob_num == "rob3":
ranges = {
"i": np.arange(1.7, 8.01, 0.1),
"j": np.arange(-1.5, 4.91, 0.1),
"k": np.arange(-0.4, 3.41, 0.1),
"name": "{}_vec{:0>3}".format(rob_num, n),
}
num_points = len(ranges["i"]) * len(ranges["j"]) * len(ranges["k"])
return ranges, num_points
def frame_gen(axis, ranges):
"""generator function for a frame at each point, given the axis for this run
up to accuracy of 0.1m (based on round)
"""
p = Point(0.0, 0.0, 0.0) # if want to offset (for some reason?)
for i in ranges["i"]:
for j in ranges["j"]:
for k in ranges["k"]:
x = round(i, 1) + p.x
y = round(j, 1) + p.y
z = round(k, 1) + p.z
plane = Plane((x, y, z), axis)
f = Frame.from_plane(plane)
yield [Frame(f.point, f.xaxis, f.yaxis)]
def axis_gen(samples=100):
"""generator function for an evenly spaced set of vectors in on a sphere
https://stackoverflow.com/questions/9600801/evenly-distributing-n-points-on-a-sphere
"""
phi = math.pi * (3.0 - math.sqrt(5.0)) # golden angle in radians
for i in range(samples):
y = 1 - (i / float(samples - 1)) * 2 # y goes from 1 to -1
radius = math.sqrt(1 - y * y) # radius at y
theta = phi * i # golden angle increment
x = math.cos(theta) * radius
z = math.sin(theta) * radius
vec = np.array([x, y, z])
vec[abs(vec) < 1e-14] = 0.0
vec = vec / np.linalg.norm(vec)
yield vec
def main_calc(robot, rob_num, planning_group, path, analysis_rng):
start_config = robot_config(robot, rob_num)
# loop based on vector, go through each point with the same vector
for n, vec in enumerate(axis_gen()):
if (n + 1) in analysis_rng:
# initialize variables to save data to for this run
idx_no_soln = []
idx_soln = []
result_dict = {}
ranges, total_points = points_ranges(rob_num, n + 1)
bar = tqdm(
frame_gen(vec, ranges),
bar_format=(
"{desc}{postfix} | {n_fmt}/{total_fmt} | {percentage:3.0f}%|{bar}|"
" {elapsed}/{remaining}"
),
total=total_points,
desc="Progress Bar",
)
for frames in bar:
for frame in frames:
bar.set_postfix(
{
"Vector": "{}/{}: {}".format(n + 1, 100, vec),
"Point": frame.point,
"success": len(idx_soln),
"failure": len(idx_no_soln),
}
)
try:
_ = ik_calc(robot, frame, start_config, planning_group)
success = 1
except RosValidationError:
success = 0
if success:
idx_soln.append(frame.point)
aa = str([round(x + 0, 2) for x in frame.point]).strip(
"[]"
) # +0 to avoid -0.0
result_dict[aa] = True
else:
idx_no_soln.append(frame.point)
aa = str([round(x + 0, 2) for x in frame.point]).strip(
"[]"
) # +0 to avoid -0.0
result_dict[aa] = False
# After all points checked for particular vector
save_JsonFile(path, ranges["name"], vec, result_dict)
# plot_dots(idx_soln, idx_no_soln)
if __name__ == "__main__":
calc = True
append = False
combine = False
rob_nums = [
# "rob1",
"rob2",
# "rob3"
]
planning_groups = [
# "robot1_notrack_gripper",
"robot2_notrack_gripper",
# "robot3_gripper"
]
operations_range = range(1, 101)
data_folder = "_data"
for rob_num, planning_group in zip(rob_nums, planning_groups):
path = get_directory(data_folder, rob_num)
# perform calculation
if calc:
analysis_index = operations_range # which steps to perform calculation
robot = connect_and_scene()
main_calc(robot, rob_num, planning_group, path, analysis_index)
# append/merge separate JSON files
if append:
append_index = operations_range # for which steps to do a merge
append_files = ["{}_vec{:0>3}.json", "{}_vec{:0>3}_2.json"]
for i in append_index:
in_fps = generate_filepaths_append(path, append_files, rob_num, i)
out_fp = os.path.join(path, "{}_vec{:0>3}__combined.json".format(rob_num, i))
append_JsonFiles(in_fps, out_fp)
# Combine a range of files into a single file, counting TRUE values
if combine:
combine_index = operations_range # for which steps combine all the data
filename = name = "{}_vec{:0>3}.json"
in_fps = generate_filepaths_combined(path, filename, rob_num, combine_index)
out_fp = os.path.join(path, "_{}_TOTAL.json".format(rob_num))
combine_JsonFiles(in_fps, out_fp)