bindings_com_velocity_derivatives.py
Go to the documentation of this file.
1 import unittest
2 import pinocchio as pin
3 from pinocchio.utils import *
4 from numpy.linalg import norm
5 
6 def df_dq(model,func,q,h=1e-9):
7  """ Perform df/dq by num_diff. q is in the lie manifold.
8  :params func: function to differentiate f : np.matrix -> np.matrix
9  :params q: configuration value at which f is differentiated. type np.matrix
10  :params h: eps
11 
12  :returns df/dq
13  """
14  dq = zero(model.nv)
15  f0 = func(q)
16  res = zero([len(f0),model.nv])
17  for iq in range(model.nv):
18  dq[iq] = h
19  res[:,iq] = (func(pin.integrate(model,q,dq)) - f0)/h
20  dq[iq] = 0
21  return res
22 
23 class TestVComDerivativesBindings(unittest.TestCase):
24  def setUp(self):
25  self.rmodel = rmodel = pin.buildSampleModelHumanoid()
26  self.rdata = rmodel.createData()
27  self.rdata_fd = rmodel.createData()
28 
29  self.rmodel.lowerPositionLimit[:3] = -1.
30  self.rmodel.upperPositionLimit[:3] = -1.
31  self.q = pin.randomConfiguration(rmodel)
32  self.vq = rand(rmodel.nv)*2-1
33 
34  self.precision = 1e-8
35 
36  def test_numdiff(self):
37  rmodel,rdata = self.rmodel,self.rdata
38  rdata_fd = self.rdata_fd
39  q,vq = self.q,self.vq
40 
41  #### Compute d/dq VCOM with the algo.
42  pin.computeAllTerms(rmodel,rdata,q,vq)
43  dvc_dq = pin.getCenterOfMassVelocityDerivatives(rmodel,rdata)
44 
45  #### Approximate d/dq VCOM by finite diff.
46  def calc_vc(q,vq):
47  """ Compute COM velocity """
48  pin.centerOfMass(rmodel,rdata_fd,q,vq)
49  return rdata_fd.vcom[0].copy()
50  dvc_dqn = df_dq(rmodel,lambda _q: calc_vc(_q,vq),q)
51 
52  self.assertTrue(np.allclose(dvc_dq,dvc_dqn,atol=np.sqrt(self.precision)))
53 
54 if __name__ == '__main__':
55  unittest.main()
56 
void copy(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &origin, DataTpl< Scalar, Options, JointCollectionTpl > &dest, KinematicLevel kinematic_level)
Copy part of the data from origin to dest. Template parameter can be used to select at which differen...
Definition: copy.hpp:52


pinocchio
Author(s):
autogenerated on Tue Jun 1 2021 02:45:02