-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrobotposeopt.py
170 lines (151 loc) · 5.39 KB
/
robotposeopt.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
from __future__ import print_function,division
from klampt import *
from klampt.math import vectorops,so3,se3
from klampt.model.trajectory import SE3Trajectory
from klampt.io import resource
from klampt import vis
import numpy as np
import sys
import time
from semiinfinite import geometryopt
from semiinfinite.sip import SemiInfiniteOptimizationSettings
gridres = 0.02
pcres = 0.02
#gridres = 0.1
#pcres = 0.05
geometryopt.TEST_PYCCD = False
DUMP_SDF = False
DRAW_GRID_AND_PC = False
VERBOSE = 0
world = WorldModel()
if len(sys.argv) > 1:
for fn in sys.argv[1:]:
world.readFile(fn)
else:
res = resource.load('WorldModel')
if res is not None:
fn,world = res
else:
exit(1)
if world.numRobots() == 0:
print("Must specify a robot")
exit(1)
robot = world.robot(0)
obstacles = []
for i in xrange(1,world.numRobots()):
for j in xrange(world.robot(i).numLinks()):
obstacles.append(world.robot(i).link(j))
for i in xrange(world.numRigidObjects()):
obstacles.append(world.rigidObject(i))
#for i in xrange(world.numTerrains()):
# obstacles.append(world.terrain(i))
print("%d robots, %d rigid objects, %d terrains"%(world.numRobots(),world.numRigidObjects(),world.numTerrains()))
assert len(obstacles) > 0
constraints,pairs = geometryopt.makeCollisionConstraints(robot,obstacles,gridres,pcres)
print("Created",len(constraints),"constraints")
vis.add("world",world)
movableObjects = []
for i in xrange(world.numRigidObjects()):
vis.edit(("world",world.rigidObject(i).getName()))
movableObjects.append(("world",world.rigidObject(i).getName()))
#extract geometries from constraints
linkgeoms = [None]*robot.numLinks()
obstaclegeoms = [None]*len(obstacles)
for c,(link,obj) in zip(constraints,pairs):
for i,obs in enumerate(obstacles):
if obj is obs:
obstaclegeoms[i] = c.env
linkgeoms[link.index] = c.robot.geometry[link.index]
#assert all(o is not None for o in linkgeoms),"Hm... couldn't find link geometries?"
assert all(o is not None for o in obstaclegeoms),"Hm... couldn't find obstacle geometries?"
#this recreates the geometries too much
#obstaclegeoms = [PenetrationDepthGeometry(obs.geometry(),gridres,pcres) for obs in obstacles]
if DUMP_SDF:
for i in xrange(robot.numLinks()):
fn = 'output/'+ robot.link(i).getName()+'.mat'
print("Saving SDF to",fn)
geometryopt.dump_grid_mat(linkgeoms[i].grid,fn)
qinit = robot.getConfig()
vis.add("qsoln",qinit)
vis.setColor("qsoln",0,1,0,0.5)
#edit configuration as target
vis.edit(("world",robot.getName()))
#should we draw?
if DRAW_GRID_AND_PC:
for i,g in enumerate(constraints[0].robot.geometry):
if g is not None:
vis.add("grid "+str(i),g.grid)
vis.setColor("grid "+str(i),0,1,0,0.5)
vis.hideLabel("grid "+str(i))
#vis.add("pc "+str(i),g.pc)
#vis.setColor("pc "+str(i),1,1,0,0.5)
for i,obs in enumerate(obstaclegeoms):
vis.add("envpc "+str(i),obs.pc)
vis.setColor("envpc "+str(i),1,1,0,0.5)
vis.hideLabel("envpc "+str(i))
settings = None
settings = SemiInfiniteOptimizationSettings()
#if you use qinit = random-collision-free, you'll want to set this higher
settings.max_iters = 5
settings.minimum_constraint_value = -0.02
vis.addPlot("timing")
vis.show()
oldcps = []
while vis.shown():
vis.lock()
t0 = time.time()
for path in movableObjects:
q = vis.getItemConfig(path)
T = (q[:9],q[9:])
for c,p in zip(constraints,pairs):
if p[1].getName() == path[1]:
c.env.setTransform(T)
q0 = robot.getConfig()
robot.setConfig(qinit)
#qcollfree,trace,cps = geometryopt.optimizeCollFreeRobot(robot,obstacles,constraints=constraints,qinit='random-collision-free',qdes=q0,verbose=VERBOSE,settings=settings,
# want_trace=True,want_times=False,want_constraints=True)
qcollfree,trace,cps = geometryopt.optimizeCollFreeRobot(robot,obstacles,constraints=constraints,qinit=None,qdes=q0,verbose=VERBOSE,settings=settings,
want_trace=True,want_times=False,want_constraints=True)
#vis.add("transformTrace",trace)
assert len(qcollfree) == robot.numLinks()
#robot.setConfig(qcollfree)
vis.setItemConfig("qsoln",qcollfree)
if geometryopt.TEST_PYCCD:
gx = [c(qcollfree) for c in constraints]
else:
gx = [c.eval_minimum(qcollfree) for c in constraints]
feasible = all([v >= 0 for v in gx])
if feasible:
vis.setColor("qsoln",0,1,0,0.5)
else:
vis.setColor("qsoln",1,0,0,0.5)
#initialize the next step from the last solved configuration
qinit = qcollfree
"""
#debug printing
for c in constraints:
c.setx(qcollfree)
distances = [c.minvalue(qcollfree) for c in constraints]
for c in constraints:
c.clearx()
print("Distances",distances)
"""
for i in oldcps:
vis.hide(i)
oldcps = []
for i in xrange(len(cps)):
for j in xrange(len(cps[i])):
name = "cp(%d,%d)"%(i,j)
vis.add(name,cps[i][j][:3])
vis.setColor(name,0,0,0,1)
vis.hideLabel(name)
oldcps.append(name)
#let the goal wander around
#q0 = qcollfree
#let the seed wander around
#qinit = qcollfree
vis.unlock()
t1 = time.time()
vis.logPlot("timing","opt",t1-t0)
time.sleep(max(0.001,0.05-(t1-t0)))
vis.kill()