-
Notifications
You must be signed in to change notification settings - Fork 5
/
solution_cid.py
63 lines (48 loc) · 2.01 KB
/
solution_cid.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
from robots import loadTalosLegs
from scipy.optimize import fmin_slsqp
import pinocchio
from pinocchio.utils import *
from numpy.linalg import norm,inv,pinv,eig,svd
robot = loadTalosLegs()
rmodel = robot.model
rdata = robot.data
m2a = lambda m: np.array(m.flat)
a2m = lambda a: np.matrix(a).T
LOCAL = pinocchio.ReferenceFrame.LOCAL
WORLD = pinocchio.ReferenceFrame.WORLD
#robot.initDisplay(loadModel=True)
#gview = robot.viewer.gui
gview = None
class OptimProblem:
def __init__(self,rmodel,rdata,q,vq,gview=None):
self.rmodel = rmodel
self.rdata = rdata
self.idL = rmodel.getFrameId('left_sole_link') # ID of the robot object to control
self.idR = rmodel.getFrameId('right_sole_link')# ID of the robot object to control
self.jidL = rmodel.frames[self.idL].parent
self.jidR = rmodel.frames[self.idR].parent
self.q = q.copy()
self.vq = vq.copy()
def x2vars(self,x):
idx = 0
nvar = self.rmodel.nv; tauq = a2m(x[idx:idx+nvar]); idx+=nvar
nvar = 6 ; phir = a2m(x[idx:idx+nvar]); idx+=nvar
nvar = 6 ; phil = a2m(x[idx:idx+nvar]); idx+=nvar
return tauq,phir,phil
def cost(self,x):
tauq,fr,fl = self.x2vars(x)
pinocchio.computeAllTerms(self.rmodel,self.rdata,self.q,self.vq)
b = self.rdata.nle
pinocchio.updateFramePlacements(self.rmodel,self.rdata)
Jr = pinocchio.getFrameJacobian(self.rmodel,self.rdata,self.idR,LOCAL)
Jl = pinocchio.getFrameJacobian(self.rmodel,self.rdata,self.idL,LOCAL)
self.residuals = m2a(b - tauq - Jr.T*fr - Jl.T*fl)
return sum( self.residuals**2 )
# --- BLABLA -------------------------------------------------------------
def callback(self,x):
pass
pbm = OptimProblem(rmodel,rdata,q=robot.q0,vq=zero(rmodel.nv))
x0 = np.zeros(robot.model.nv+12)
res = fmin_slsqp(x0=x0,
func=pbm.cost,
epsilon=1e-7,callback=pbm.callback,iter=1000)