-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvisualize_ompl.py
130 lines (97 loc) · 3.29 KB
/
visualize_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
import pickle
from pathlib import Path
from fire import Fire
import vamp
from vamp import pybullet_interface as vpb
try:
import ompl.base as ob
import ompl.geometric as og
except:
raise ImportError("Failed to import OMPL Python bindings.")
try:
import pandas as pd
except ImportError:
raise RuntimeError("pandas is not installed!")
def solve(
start,
goals,
simulator: vpb.PyBulletSimulator,
planning_time: float = 30.,
planner: str = "RRTConnect",
**kwargs
):
space = ob.RealVectorStateSpace()
for l, h in zip(simulator.lows, simulator.highs):
space.addDimension(l, h)
space.setup()
n_dims = space.getDimension()
def set_state(state, state_list):
for i in range(n_dims):
state[i] = state_list[i]
def get_state(state):
return [state[i] for i in range(n_dims)]
def valid(state):
simulator.set_joint_positions(get_state(state))
return not simulator.in_collision()
ss = og.SimpleSetup(space)
si = ss.getSpaceInformation()
ss.setStateValidityChecker(ob.StateValidityCheckerFn(valid))
si.setStateValidityCheckingResolution(0.001)
start_state = ob.State(space)
set_state(start_state, start)
ss.setStartState(start_state)
goal_states = ob.GoalStates(si)
for goal in goals:
goal_state = ob.State(space)
set_state(goal_state, goal)
goal_states.addState(goal_state)
ss.setGoal(goal_states)
planner = eval(f"og.{planner}(si)")
ss.setPlanner(planner)
for k, v in kwargs.items():
eval(f"planner.{k}({v})")
ss.setup()
result = ss.solve(planning_time)
if result and ss.haveExactSolutionPath():
ss.simplifySolution()
path = ss.getSolutionPath()
path.interpolate(100)
states = []
for state in path.getStates():
states.append(get_state(state))
return states
return None
def main(
robot: str = "panda",
dataset: str = "problems.pkl",
problem: str = "",
index: int = 1,
):
if robot not in vamp.ROBOT_JOINTS:
raise RuntimeError(f"Robot {robot} does not exist in VAMP!")
robot_dir = Path(__file__).parent.parent / 'resources' / robot
with open(robot_dir / dataset, 'rb') as f:
data = pickle.load(f)
if not problem:
problem = list(data['problems'].keys())[0]
if problem not in data['problems']:
raise RuntimeError(
f"""No problem with name {problem}!
Existing problems: {list(data['problems'].keys())}"""
)
problems = data['problems'][problem]
try:
problem_data = next(problem for problem in problems if problem['index'] == index)
except:
raise RuntimeError(f"No problem in {problem} with index {index}!")
start = problem_data['start']
goals = problem_data['goals']
sim = vpb.PyBulletSimulator(str(robot_dir / f"{robot}_spherized.urdf"), vamp.ROBOT_JOINTS[robot], True)
sim.add_environment_from_problem_dict(problem_data, False)
path = solve(start, goals, sim, planner = "RRTConnect", setRange = vamp.ROBOT_RRT_RANGES[robot])
if not path:
print("Failed to find path, visualizing start/goals!")
path = [start] + goals
sim.animate(path)
if __name__ == "__main__":
Fire(main)