bindings_com_velocity_derivatives.py
Go to the documentation of this file.
1 import unittest
2 
3 import numpy as np
4 import pinocchio as pin
5 from pinocchio.utils import rand
6 
7 
8 def df_dq(model, func, q, h=1e-9):
9  """Perform df/dq by num_diff. q is in the lie manifold.
10  :params func: function to differentiate f : np.array -> np.array
11  :params q: configuration value at which f is differentiated. type np.array
12  :params h: eps
13 
14  :returns df/dq
15  """
16  dq = np.zeros(model.nv)
17  f0 = func(q)
18  res = np.zeros([len(f0), model.nv])
19  for iq in range(model.nv):
20  dq[iq] = h
21  res[:, iq] = (func(pin.integrate(model, q, dq)) - f0) / h
22  dq[iq] = 0
23  return res
24 
25 
26 class TestVComDerivativesBindings(unittest.TestCase):
27  def setUp(self):
28  self.rmodel = rmodel = pin.buildSampleModelHumanoid()
29  self.rdata = rmodel.createData()
30  self.rdata_fd = rmodel.createData()
31 
32  self.rmodel.lowerPositionLimit[:3] = -1.0
33  self.rmodel.upperPositionLimit[:3] = -1.0
34  self.q = pin.randomConfiguration(rmodel)
35  self.vq = rand(rmodel.nv) * 2 - 1
36 
37  self.precision = 1e-8
38 
39  def test_numdiff(self):
40  rmodel, rdata = self.rmodel, self.rdata
41  rdata_fd = self.rdata_fd
42  q, vq = self.q, self.vq
43 
44 
45  pin.computeAllTerms(rmodel, rdata, q, vq)
46  dvc_dq = pin.getCenterOfMassVelocityDerivatives(rmodel, rdata)
47 
48 
49  def calc_vc(q, vq):
50  """Compute COM velocity"""
51  pin.centerOfMass(rmodel, rdata_fd, q, vq)
52  return rdata_fd.vcom[0].copy()
53 
54  dvc_dqn = df_dq(rmodel, lambda _q: calc_vc(_q, vq), q)
55 
56  self.assertTrue(np.allclose(dvc_dq, dvc_dqn, atol=np.sqrt(self.precision)))
57 
58 
59 if __name__ == "__main__":
60  unittest.main()
bindings_com_velocity_derivatives.TestVComDerivativesBindings.rmodel
rmodel
Definition: bindings_com_velocity_derivatives.py:28
bindings_com_velocity_derivatives.TestVComDerivativesBindings.precision
precision
Definition: bindings_com_velocity_derivatives.py:37
bindings_com_velocity_derivatives.TestVComDerivativesBindings
Definition: bindings_com_velocity_derivatives.py:26
bindings_com_velocity_derivatives.TestVComDerivativesBindings.rdata
rdata
Definition: bindings_com_velocity_derivatives.py:29
bindings_com_velocity_derivatives.TestVComDerivativesBindings.test_numdiff
def test_numdiff(self)
Definition: bindings_com_velocity_derivatives.py:39
bindings_com_velocity_derivatives.TestVComDerivativesBindings.vq
vq
Definition: bindings_com_velocity_derivatives.py:35
bindings_com_velocity_derivatives.df_dq
def df_dq(model, func, q, h=1e-9)
Definition: bindings_com_velocity_derivatives.py:8
bindings_com_velocity_derivatives.TestVComDerivativesBindings.rdata_fd
rdata_fd
Definition: bindings_com_velocity_derivatives.py:30
bindings_com_velocity_derivatives.TestVComDerivativesBindings.q
q
Definition: bindings_com_velocity_derivatives.py:34
pinocchio.utils
Definition: bindings/python/pinocchio/utils.py:1
bindings_com_velocity_derivatives.TestVComDerivativesBindings.setUp
def setUp(self)
Definition: bindings_com_velocity_derivatives.py:27
pinocchio::copy
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:42
pinocchio.utils.rand
def rand(n)
Definition: bindings/python/pinocchio/utils.py:41


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:25