forked from lyfkyle/pybullet_ompl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb_ompl.py
384 lines (318 loc) · 13.5 KB
/
pb_ompl.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
try:
from ompl import util as ou
from ompl import base as ob
from ompl import geometric as og
except ImportError:
# if the ompl module is not in the PYTHONPATH assume it is installed in a
# subdirectory of the parent directory called "py-bindings."
from os.path import abspath, dirname, join
import sys
sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'ompl/py-bindings'))
# sys.path.insert(0, join(dirname(abspath(__file__)), '../whole-body-motion-planning/src/ompl/py-bindings'))
print(sys.path)
from ompl import util as ou
from ompl import base as ob
from ompl import geometric as og
import pybullet as p
import utils
import time
from itertools import product
import copy
import math
import random
INTERPOLATE_NUM = 500
SIMPLIFY_NUM = 5
DEFAULT_PLANNING_TIME = 5.0
class PbOMPLRobot():
'''
To use with Pb_OMPL. You need to construct a instance of this class and pass to PbOMPL.
Note:
This parent class by default assumes that all joints are acutated and should be planned. If this is not your desired
behaviour, please write your own inheritated class that overrides respective functionalities.
'''
def __init__(self, id) -> None:
# Public attributes
self.id = id
self.x_limits = []
self.y_limits = []
# prune fixed joints
all_joint_num = p.getNumJoints(id)
all_joint_idx = list(range(all_joint_num))
joint_idx = [j for j in all_joint_idx if self._is_not_fixed(j)]
self.num_dim = len(joint_idx)
self.joint_idx = joint_idx
print(self.joint_idx)
self.joint_bounds = []
self.reset()
def _is_not_fixed(self, joint_idx):
joint_info = p.getJointInfo(self.id, joint_idx)
return joint_info[2] != p.JOINT_FIXED
def set_xy_limits(self, x_limits: tuple[float, float], y_limits: tuple[float, float]):
self.x_limits = x_limits
self.y_limits = y_limits
def get_joint_bounds(self):
'''
Get joint bounds.
By default, read from pybullet
'''
for i, joint_id in enumerate(self.joint_idx):
joint_info = p.getJointInfo(self.id, joint_id)
low = joint_info[8] # low bounds
high = joint_info[9] # high bounds
if low < high:
self.joint_bounds.append([low, high])
if self.x_limits:
self.joint_bounds[0] = self.x_limits
if self.y_limits:
self.joint_bounds[1] = self.y_limits
print("Joint bounds: {}".format(self.joint_bounds))
return self.joint_bounds
def get_cur_state(self):
return copy.deepcopy(self.state)
def set_state(self, state):
'''
Set robot state.
To faciliate collision checking
Args:
state: list[Float], joint values of robot
'''
self._set_joint_positions(self.joint_idx, state)
self.state = state
def reset(self):
'''
Reset robot state
Args:
state: list[Float], joint values of robot
'''
state = [0] * self.num_dim
self._set_joint_positions(self.joint_idx, state)
self.state = state
def _set_joint_positions(self, joints, positions):
for joint, value in zip(joints, positions):
p.resetJointState(self.id, joint, value, targetVelocity=0)
class PbStateSpace(ob.RealVectorStateSpace):
def __init__(self, num_dim) -> None:
super().__init__(num_dim)
self.num_dim = num_dim
self.state_sampler = None
def allocStateSampler(self):
'''
This will be called by the internal OMPL planner
'''
# WARN: This will cause problems if the underlying planner is multi-threaded!!!
if self.state_sampler:
return self.state_sampler
# when ompl planner calls this, we will return our sampler
return self.allocDefaultStateSampler()
def set_state_sampler(self, state_sampler):
'''
Optional, Set custom state sampler.
'''
self.state_sampler = state_sampler
class PbOMPL():
def __init__(self, robot, obstacles = []) -> None:
'''
Args
robot: A PbOMPLRobot instance.
obstacles: list of obstacle ids. Optional.
'''
self.robot = robot
self.robot_id = robot.id
self.obstacles = obstacles
print(self.obstacles)
self.space = PbStateSpace(robot.num_dim)
bounds = ob.RealVectorBounds(robot.num_dim)
joint_bounds = self.robot.get_joint_bounds()
for i, bound in enumerate(joint_bounds):
bounds.setLow(i, bound[0])
bounds.setHigh(i, bound[1])
self.space.setBounds(bounds)
self.ss = og.SimpleSetup(self.space)
self.ss.setStateValidityChecker(ob.StateValidityCheckerFn(self.is_state_valid))
self.si = self.ss.getSpaceInformation()
# self.si.setStateValidityCheckingResolution(0.005)
# self.collision_fn = pb_utils.get_collision_fn(self.robot_id, self.robot.joint_idx, self.obstacles, [], True, set(),
# custom_limits={}, max_distance=0, allow_collision_links=[])
self.set_obstacles(obstacles)
self.set_planner("RRT") # RRT by default
def set_obstacles(self, obstacles):
self.obstacles = obstacles
# update collision detection
self.setup_collision_detection(self.robot, self.obstacles)
def add_obstacles(self, obstacle_id):
self.obstacles.append(obstacle_id)
def remove_obstacles(self, obstacle_id):
self.obstacles.remove(obstacle_id)
def is_state_valid(self, state):
# satisfy bounds TODO
# Should be unecessary if joint bounds is properly set
# check self-collision
self.robot.set_state(self.state_to_list(state))
for link1, link2 in self.check_link_pairs:
if utils.pairwise_link_collision(self.robot_id, link1, self.robot_id, link2):
# print(get_body_name(body), get_link_name(body, link1), get_link_name(body, link2))
return False
# check collision against environment
for body1, body2 in self.check_body_pairs:
if utils.pairwise_collision(body1, body2):
# print('body collision', body1, body2)
# print(get_body_name(body1), get_body_name(body2))
return False
return True
def setup_collision_detection(self, robot, obstacles, self_collisions = True, allow_collision_links = []):
self.check_link_pairs = utils.get_self_link_pairs(robot.id, robot.joint_idx) if self_collisions else []
moving_links = frozenset(
[item for item in utils.get_moving_links(robot.id, robot.joint_idx) if not item in allow_collision_links])
moving_bodies = [(robot.id, moving_links)]
self.check_body_pairs = list(product(moving_bodies, obstacles))
def set_planner(self, planner_name):
'''
Note: Add your planner here!!
'''
if planner_name == "PRM":
self.planner = og.PRM(self.ss.getSpaceInformation())
elif planner_name == "RRT":
self.planner = og.RRT(self.ss.getSpaceInformation())
elif planner_name == "RRTConnect":
self.planner = og.RRTConnect(self.ss.getSpaceInformation())
elif planner_name == "RRTstar":
self.planner = og.RRTstar(self.ss.getSpaceInformation())
elif planner_name == "EST":
self.planner = og.EST(self.ss.getSpaceInformation())
elif planner_name == "FMT":
self.planner = og.FMT(self.ss.getSpaceInformation())
elif planner_name == "BITstar":
self.planner = og.BITstar(self.ss.getSpaceInformation())
elif planner_name == "AITstar":
self.planner = og.AITstar(self.ss.getSpaceInformation())
elif planner_name == "KPIECE1":
self.planner = og.KPIECE1(self.ss.getSpaceInformation())
elif planner_name == "BKPIECE1":
self.planner = og.BKPIECE1(self.ss.getSpaceInformation())
elif planner_name == "LBKPIECE1":
self.planner = og.LBKPIECE1(self.ss.getSpaceInformation())
else:
print("{} not recognized, please add it first".format(planner_name))
return
self.ss.setPlanner(self.planner)
def plan_start_goal(self, start, goal, allowed_time=DEFAULT_PLANNING_TIME, interpolate_num=INTERPOLATE_NUM, simplify_num=SIMPLIFY_NUM):
'''
plan a path to gaol from the given robot start state
'''
print("start_planning")
print(self.planner.params())
orig_robot_state = self.robot.get_cur_state()
self.ss.clear()
# set the start state;
s = ob.State(self.space)
for i in range(len(start)):
s[i] = start[i]
self.ss.setStartState(s)
# set goal
if isinstance(goal, IKGoal):
self.ss.setGoal(goal)
else:
g = ob.State(self.space)
for i in range(len(goal)):
g[i] = goal[i]
self.ss.setGoalState(g)
# attempt to solve the problem within allowed planning time
solved = self.ss.solve(allowed_time)
for _ in range(simplify_num):
self.ss.simplifySolution()
res = False
sol_path_list = []
if solved:
print("Found solution: interpolating into {} segments".format(interpolate_num))
# print the path to screen
sol_path_geometric = self.ss.getSolutionPath()
sol_path_geometric.interpolate(interpolate_num)
sol_path_states = sol_path_geometric.getStates()
sol_path_list = [self.state_to_list(state) for state in sol_path_states]
# print(len(sol_path_list))
# print(sol_path_list)
for sol_path in sol_path_list:
self.is_state_valid(sol_path)
res = True
else:
print("No solution found")
# reset robot state
self.robot.set_state(orig_robot_state)
return res, sol_path_list
def plan_fast(self, goal):
return self.plan(goal, 1.0, 5, 0)
def plan(self, goal_state, allowed_time=DEFAULT_PLANNING_TIME, interpolate_num=INTERPOLATE_NUM, simplify_num=SIMPLIFY_NUM):
'''
plan a path to gaol from current robot state
'''
start = self.robot.get_cur_state()
return self.plan_start_goal(start, goal_state, allowed_time, interpolate_num, simplify_num)
def plan_ik(self, end_effector_goal_pos, allowed_time=DEFAULT_PLANNING_TIME, interpolate_num=INTERPOLATE_NUM, simplify_num=SIMPLIFY_NUM):
'''
WARNING: This method does not work well with all planners (PRM seems to work).
Sample a goal state for a given `end_effector_goal_pos` using IK,
and plan a path to that gaol from the current robot state
'''
start = self.robot.get_cur_state()
ik_goal = IKGoal(self.si, end_effector_goal_pos, self.robot)
return self.plan_start_goal(start, ik_goal, allowed_time, interpolate_num, simplify_num)
def execute(self, path, dynamics=False):
'''
Execute a planned plan. Will visualize in pybullet.
Args:
path: list[state], a list of state
dynamics: allow dynamic simulation. If dynamics is false, this API will use robot.set_state(),
meaning that the simulator will simply reset robot's state WITHOUT any dynamics simulation. Since the
path is collision free, this is somewhat acceptable.
'''
for q in path:
if dynamics:
for i in range(self.robot.num_dim):
p.setJointMotorControl2(self.robot.id, i, p.POSITION_CONTROL, q[i],force=5 * 240.)
else:
self.robot.set_state(q)
time.sleep(0.01)
# -------------
# Configurations
# ------------
def set_state_sampler(self, state_sampler):
self.space.set_state_sampler(state_sampler)
# -------------
# Util
# ------------
def state_to_list(self, state):
return [state[i] for i in range(self.robot.num_dim)]
class IKGoal(ob.GoalSampleableRegion):
def __init__(self, si, pos, robot):
super(IKGoal, self).__init__(si)
self.pos = pos
self.ik = InverseKinematics(robot)
def maxSampleCount(self):
return 1000000
def sampleGoal(self, state):
res = self.ik.solve(self.pos)
for i in range(len(res)):
state[i] = res[i]
return state
class InverseKinematics:
def __init__(self, robot):
self.robot = robot
self.end_effector_index = 8
self.orn = p.getQuaternionFromEuler((0, math.pi, 0))
self.iterations = 300
def solve(self, pos):
original_state = self.robot.get_cur_state()
start_state = self.get_random_state()
self.robot.set_state(start_state)
state = p.calculateInverseKinematics(self.robot.id, self.end_effector_index, pos, self.orn, maxNumIterations=self.iterations)
state = list(state)
for i in range(len(state)):
state[i] = round(state[i], 5) # otherwise some states might be slightly out of joint bounds
self.robot.set_state(original_state)
return state
def get_random_state(self):
m = map(self.random_num, self.robot.joint_bounds)
return list(m)
def random_num(self, limits):
r = random.uniform(limits[0], limits[1])
return round(r, 5)