-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_hpp.py
389 lines (355 loc) · 15.7 KB
/
script_hpp.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
# Copyright 2020 CNRS - Airbus SAS
# Author: Florent Lamiraux, Joseph Mirabel, Alexis Nicolin
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This script selectively does one of the following
#
# 1. generate n configurations where the camera looks at the left wrist
# options --N=n --arm=left,
# 2. generate n configurations where the camera looks at the right wrist
# options --N=n --arm=right,
# 3. reads configurations from file './data/all-configurations.csv')
#
# Then, it computes a path going through all these configurations.
from csv import reader, writer
import argparse, numpy as np
from CORBA import Any, TC_double, TC_long
from hpp import Transform
from hpp.corbaserver import loadServerPlugin
from hpp.corbaserver.manipulation import Constraints, ConstraintGraph, \
ProblemSolver, Rule
from hpp.corbaserver.manipulation.robot import CorbaClient, HumanoidRobot
from hpp.gepetto.manipulation import ViewerFactory
from hpp.corbaserver.manipulation.constraint_graph_factory import \
ConstraintGraphFactory
from agimus_demos.tools_hpp import RosInterface, concatenatePaths
from hpp_idl.hpp import Error as HppError
from agimus_demos.talos.tools_hpp import createGazeConstraints, \
createGripperLockedJoints, createLeftArmLockedJoints, \
createRightArmLockedJoints, defaultContext, setGaussianShooter
from common_hpp import createQuasiStaticEquilibriumConstraint, makeGraph, \
makeRobotProblemAndViewerFactory
loadServerPlugin (defaultContext, "manipulation-corba.so")
client = CorbaClient(context=defaultContext)
client.manipulation.problem.resetProblem()
# parse arguments
p = argparse.ArgumentParser (description=
'Procuce motion to calibrate arms and camera')
p.add_argument ('--arm', type=str, metavar='arm',
default=None,
help="which arm: 'right' or 'left'")
p.add_argument ('--N', type=int, metavar='N', default=0,
help="number of configurations generated")
args = p.parse_args ()
# Write configurations in a file in CSV format
def writeConfigsInFile (filename, configs):
with open (filename, "w") as f:
w = writer (f)
for q in configs:
w.writerow (q)
# Read configurations in a file in CSV format
def readConfigsInFile (filename):
with open(filename, "r") as f:
configurations = list()
r = reader (f)
for line in r:
configurations.append(map(float,line))
return configurations
# distance between configurations
def distance (ps, q0, q1) :
''' Distance between two configurations of the box'''
assert (len (q0) == ps.robot.getConfigSize ())
assert (len (q1) == ps.robot.getConfigSize ())
distance = ps.hppcorba.problem.getDistance ()
return distance.call (q0, q1)
# Check that target frame of gaze constraint is not behind the camera
def validateGazeConstraint (ps, q, whichArm):
robot = ps.robot
robot.setCurrentConfig (q)
Mcamera = Transform(robot.getLinkPosition
(ps.robot.camera_frame))
Mtarget = Transform(robot.getLinkPosition("talos/arm_" + whichArm +
"_7_link"))
z = (Mcamera.inverse () * Mtarget).translation [2]
if z < .1: return False
return True
def shootRandomConfigs (ps, graph, q0, n):
robot = ps.robot
configs = list ()
i = 0
while i < n:
q = robot.shootRandomConfig ()
res, q1, err = graph.generateTargetConfig ("Loop | f", q0, q)
if not res: continue
res = validateGazeConstraint (ps, q1, args.arm)
if not res: continue
res, msg = robot.isConfigValid (q1)
if res:
configs.append (q1)
i += 1
return configs
def buildDistanceMatrix (ps, configs):
N = len (configs)
# Build matrix of distances between box poses
dist = np.matrix (np.zeros (N*N).reshape (N,N))
for i in range (N):
for j in range (i+1,N):
dist [i,j] = distance (ps, configs [i], configs [j])
dist [j,i] = dist [i,j]
return dist
def orderConfigurations (ps, configs):
N = len (configs)
# Order configurations according to naive solution to traveler
# salesman problem
notVisited = range (1,N)
visited = range (0,1)
dist = buildDistanceMatrix (ps, configs)
while len (notVisited) > 0:
# rank of current configuration in visited
i = visited [-1]
# find closest not visited configuration
m = 1e20
closest = None
for j in notVisited:
if dist [i,j] < m:
m = dist [i,j]
closest = j
notVisited.remove (closest)
visited.append (closest)
orderedConfigs = list ()
for i in visited:
orderedConfigs.append (configs [i])
return orderedConfigs
# get indices of closest configs to config [i]
def getClosest (dist,i,n):
d = list()
for j in range (dist.shape[1]):
if j!=i:
d.append ((j,dist[i,j]))
d.sort (key=lambda x:x[1])
return zip (*d)[0][:n]
def buildRoadmap (configs):
if len(configs)==0: return
dist = buildDistanceMatrix (ps, configs)
for q in configs:
ps.addConfigToRoadmap(q)
for i, q in enumerate (configs):
ps.addConfigToRoadmap (q)
closest = getClosest (dist,i,20)
for j in closest:
if dist[i,j] != 0 and j>i:
qi=configs[i]
qj=configs[j]
res, pid, msg = ps.directPath(qi,qj,True)
if res:
ps.addEdgeToRoadmap (qi,qj,pid,True)
# clear paths
for i in range(ps.numberPaths(),0,-1):
ps.erasePath (i-1)
def visitConfigurations (ps, configs):
nOptimizers = len(ps.getSelected("PathOptimizer"))
for q_init, q_goal in zip (configs, configs [1:]):
ps.resetGoalConfigs ()
ps.setInitialConfig (q_init)
ps.addGoalConfig (q_goal)
ps.solve ()
for i in range(nOptimizers):
# remove non optimized paths
pid = ps.numberPaths () - 2
ps.erasePath (pid)
def goToContact(ri, pg, gripper, handle, q_init):
pg.gripper = gripper
q_init = ri.getCurrentConfig(q_init, 5., 'talos/leg_left_6_joint') if ri \
else q_init
res, q_init, err = pg.graph.generateTargetConfig('starting_motion', q_init,
q_init)
if not res:
raise RuntimeError('Failed to project initial configuration')
isp = pg.inStatePlanner
isp.optimizerTypes = ["RandomShortcut", "EnforceTransitionSemantic",
"SimpleTimeParameterization"]
isp.manipulationProblem.setParameter\
("SimpleTimeParameterization/maxAcceleration", Any(TC_double, 0.5))
isp.manipulationProblem.setParameter\
("SimpleTimeParameterization/safety", Any(TC_double, .9))
isp.manipulationProblem.setParameter\
("SimpleTimeParameterization/order", Any(TC_long, 2))
paths = pg.generatePathForHandle(handle, q_init, NrandomConfig=100)
# First path is already time parameterized
# Transform second and third path into PathVector instances to time
# parameterize them
isp.manipulationProblem.setParameter\
("SimpleTimeParameterization/maxAcceleration", Any(TC_double, 0.5))
isp.manipulationProblem.setParameter\
("SimpleTimeParameterization/safety", Any(TC_double, 0.04))
finalPaths = [paths[0],]
for i, p in enumerate(paths[1:]):
path = p.asVector()
for optType in isp.optimizerTypes:
optimizer = isp.wd(isp.ps.hppcorba.problem.createPathOptimizer\
(optType, isp.manipulationProblem))
optpath = optimizer.optimize(path)
# optimize can return path if it couldn't find a better one.
# In this case, we have too different client refering to the
# same servant.
# thus the following code deletes the old client, which
# triggers deletion of the servant and the new path points to
# nothing...
# path = pg.wd(optimizer.optimize(path))
from hpp.corbaserver.tools import equals
if not equals(path, optpath):
path = isp.wd(optpath)
finalPaths.append(path)
pg.ps.client.basic.problem.addPath(finalPaths[0])
pg.ps.client.basic.problem.addPath(concatenatePaths(finalPaths[1:]))
# Read configurations from csv measurement files. Compute freeflyer pose
# by keeping the same link pose of referenceJoint parameter as in configuration
# q0
def getMeasurementConfigs(robot, q0, referenceJoint, filename):
with open(filename, "r") as f:
r = reader(f, delimiter=',')
configs = list()
for i, line in enumerate(r):
if i==0:
# read joint names
assert line[0] == "joint_names"
joints = line[1:]
else:
r = line.index('joint_states')
angles = map(float, line[r+1:])
q = q0[:]
assert(len(angles) == len(joints))
for j, angle in zip(joints, angles):
r = robot.rankInConfiguration['talos/'+j]
q[r] = angle
# compute freeflyer pose
robot.setCurrentConfig(q0)
# desired pose of the reference joint
Mrj0 = Transform(robot.getJointPosition(referenceJoint))
robot.setCurrentConfig(q)
# actual pose of the reference joint
Mrj = Transform(robot.getJointPosition(referenceJoint))
# actual pose of the humanoid root joint
rk = robot.rankInConfiguration['talos/root_joint']
Mr0 = Transform(q0[rk:rk+7])
# desired pose fo the humanoid root joint
q[rk:rk+7] = list(Mrj0*Mrj.inverse()*Mr0)
configs.append(q)
return configs
initConf = [0, 0, 1.05, 0, 0, 0, 1, 0, 0, -0.37, 0.67, -0.3, 0, 0, 0, -0.37, 0.67, -0.3, 0, 0, 0.006761, 0.4, 0.24, -0.7, -1.45, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.4, -0.24, 0.7, -1.45, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
# Initial configuration in PAL simulator
# initConf = [0, 0, 1.0493761707252309, 0, 0, 0, 1, 0,
# -0.0010225974303292359, -0.36787678308396593, 0.6757922512379118,
# -0.3028653783931391, 0.0005395405932684167, -2.8927888057647416e-06,
# 0, -0.3681011200415869, 0.6761074493076881, -0.3029241643529437, 0, 0,
# 0.050319274246977824, 0.3999220584702339, 0.24002787068272383,
# -0.6002140330094962, -1.4490672794672388, 0, -0, 0.003036732533889251,
# 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, -0.3999212805713791,
# -0.2400023000505192, 0.6002203685413067, -1.4490697603203644, 0, 0,
# 0.003224000598866796, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0]
robot, ps, vf, table, objects = makeRobotProblemAndViewerFactory(None)
initConf += [.5,0,0,0,0,0,1]
ri = RosInterface(robot)
left_arm_lock = createLeftArmLockedJoints (ps)
right_arm_lock = createRightArmLockedJoints (ps)
if args.arm == 'left':
arm_locked = right_arm_lock
elif args.arm == 'right':
arm_locked = left_arm_lock
else:
arm_locked = list()
left_gripper_lock, right_gripper_lock = createGripperLockedJoints (ps, initConf)
com_constraint, foot_placement, foot_placement_complement = \
createQuasiStaticEquilibriumConstraint (ps, initConf)
look_left_hand, look_right_hand = createGazeConstraints(ps)
graph = ConstraintGraph(robot, "graph")
makeGraph(ps, table, graph)
# Add other locked joints in the edges.
for edgename, edgeid in graph.edges.items():
if edgename[:7] == "Loop | " and edgename[7] != 'f':
graph.addConstraints(
edge=edgename, constraints=Constraints(numConstraints=\
['table/root_joint',])
)
# Add gaze and and equilibrium constraints and locked grippers to each node of
# the graph
prefixLeft = 'talos/left_gripper'
prefixRight = 'talos/right_gripper'
l = len(prefixLeft)
r = len(prefixRight)
for nodename, nodeid in graph.nodes.items():
graph.addConstraints(
node=nodename, constraints=Constraints(numConstraints=\
com_constraint + foot_placement + left_gripper_lock + \
right_gripper_lock
)
)
if nodename[:l] == prefixLeft:
graph.addConstraints(
node=nodename, constraints=Constraints(numConstraints=\
[look_left_hand,]))
if nodename[:r] == prefixRight:
graph.addConstraints(
node=nodename, constraints=Constraints(numConstraints=\
[look_right_hand,]))
# add foot placement complement in each edge.
for edgename, edgeid in graph.edges.items():
graph.addConstraints(
edge=edgename,
constraints=Constraints(numConstraints=foot_placement_complement),
)
# On the real robot, the initial configuration as measured by sensors is very
# likely not in any state of the graph. State "starting_state" and transition
# "starting_motion" are aimed at coping with this issue.
graph.createNode("starting_state")
graph.createEdge("starting_state", "free", "starting_motion", isInNode="starting_state")
graph.createEdge(
"free",
"starting_state",
"go_to_starting_state",
isInNode="starting_state",
weight=0,
)
graph.addConstraints(
edge="starting_motion",
constraints=Constraints(numConstraints=['table/root_joint',]),)
graph.addConstraints(
edge="go_to_starting_state",
constraints=Constraints(numConstraints=['table/root_joint',]),
)
graph.initialize ()
ps.setParameter("SimpleTimeParameterization/safety", 0.2)
ps.setParameter("SimpleTimeParameterization/order", 2)
ps.setParameter("SimpleTimeParameterization/maxAcceleration", .1)
# ps.addPathOptimizer ("RandomShortcut")
ps.addPathOptimizer ("EnforceTransitionSemantic")
ps.addPathOptimizer ("SimpleTimeParameterization")
res, q_init, err = graph.generateTargetConfig ("starting_motion", initConf,
initConf)
if not res:
raise RuntimeError ('Failed to project initial configuration')
# Set variance to 0 in Gaussian shooter for arm that does not go into contact
varianceLeft = {'talos/root_joint': .1, 'table/root_joint': 0.}
varianceRight = {'talos/root_joint': .1, 'table/root_joint': 0.}
for i in range(1,8):
varianceLeft['talos/arm_right_{}_joint'.format(i)] = 0
varianceRight['talos/arm_left_{}_joint'.format(i)] = 0
setGaussianShooter(ps, [table], q_init, .2, varianceLeft)