bindings_dynamics.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, zero
6 from test_case import PinocchioTestCase as TestCase
7 
8 # common quantities for all tests.
9 # They correspond to the default values of the arguments, and they need to stay this way
10 r_coeff = 0.0
11 inv_damping = 0.0
12 
13 
14 class TestDynamicsBindings(TestCase):
15  def setUp(self):
16  self.model = pin.buildSampleModelHumanoidRandom()
17  self.data = self.model.createData()
18 
19  qmax = np.full((self.model.nq, 1), np.pi)
20  self.q = pin.randomConfiguration(self.model, -qmax, qmax)
21  self.v = rand(self.model.nv)
22  self.tau = rand(self.model.nv)
23 
24  self.v0 = zero(self.model.nv)
25  self.tau0 = zero(self.model.nv)
26  self.tolerance = 1e-9
27 
28  # we compute J on a different self.data
29  self.J = pin.computeJointJacobian(
30  self.model,
31  self.model.createData(),
32  self.q,
33  self.model.getJointId("lleg6_joint"),
34  )
35  self.gamma = zero(6)
36 
38  data_no_q = self.model.createData()
39 
40  self.model.gravity = pin.Motion.Zero()
41  ddq = pin.forwardDynamics(
42  self.model, self.data, self.q, self.v0, self.tau0, self.J, self.gamma
43  )
44  self.assertLess(np.linalg.norm(ddq), self.tolerance)
45 
46  KKT_inverse = pin.getKKTContactDynamicMatrixInverse(
47  self.model, self.data, self.J
48  )
49  M = pin.crba(self.model, self.model.createData(), self.q)
50 
51  self.assertApprox(
52  M, np.linalg.inv(KKT_inverse)[: self.model.nv, : self.model.nv]
53  )
54 
55  pin.computeAllTerms(self.model, data_no_q, self.q, self.v0)
56  ddq_no_q = pin.forwardDynamics(
57  self.model, data_no_q, self.tau0, self.J, self.gamma
58  )
59  self.assertLess(np.linalg.norm(ddq_no_q), self.tolerance)
60 
61  self.assertApprox(ddq, ddq_no_q)
62 
64  model = self.model
65  data = model.createData()
66  data_ref = model.createData()
67 
68  q = self.q
69  v = self.v
70  tau = self.tau0
71  J = self.J
72  gamma = self.gamma
73 
74  pin.forwardDynamics(model, data_ref, q, v, tau, J, gamma)
75  KKT_inverse_ref = pin.getKKTContactDynamicMatrixInverse(model, data_ref, J)
76 
77  KKT_inverse = pin.computeKKTContactDynamicMatrixInverse(model, data, q, J)
78  KKT_inverse2 = pin.computeKKTContactDynamicMatrixInverse(model, data, q, J, 0.0)
79  self.assertApprox(KKT_inverse, KKT_inverse_ref)
80  self.assertApprox(KKT_inverse2, KKT_inverse_ref)
81 
83  data_no_q = self.model.createData()
84 
85  self.model.gravity = pin.Motion.Zero()
86  ddq = pin.forwardDynamics(
87  self.model,
88  self.data,
89  self.q,
90  self.v0,
91  self.tau0,
92  self.J,
93  self.gamma,
94  r_coeff,
95  )
96  self.assertLess(np.linalg.norm(ddq), self.tolerance)
97 
98  pin.computeAllTerms(self.model, data_no_q, self.q, self.v0)
99  ddq_no_q = pin.forwardDynamics(
100  self.model, data_no_q, self.tau0, self.J, self.gamma, r_coeff
101  )
102  self.assertLess(np.linalg.norm(ddq_no_q), self.tolerance)
103 
104  self.assertApprox(ddq, ddq_no_q)
105 
107  data7 = self.data
108  data8 = self.model.createData()
109  _data9_deprecated = self.model.createData()
110  ddq7 = pin.forwardDynamics(
111  self.model, data7, self.q, self.v, self.tau, self.J, self.gamma
112  )
113  ddq8 = pin.forwardDynamics(
114  self.model, data8, self.q, self.v, self.tau, self.J, self.gamma, r_coeff
115  )
116 
117  self.assertTrue((ddq7 == ddq8).all())
118 
120  data5 = self.data
121  data6 = self.model.createData()
122  data9_deprecated = self.model.createData()
123  pin.computeAllTerms(self.model, data5, self.q, self.v0)
124  pin.computeAllTerms(self.model, data6, self.q, self.v0)
125  pin.computeAllTerms(self.model, data9_deprecated, self.q, self.v0)
126  ddq5 = pin.forwardDynamics(self.model, data5, self.tau, self.J, self.gamma)
127  ddq6 = pin.forwardDynamics(
128  self.model, data6, self.tau, self.J, self.gamma, r_coeff
129  )
130  self.assertTrue((ddq5 == ddq6).all())
131 
133  data_no_q = self.model.createData()
134 
135  vnext = pin.impulseDynamics(self.model, self.data, self.q, self.v0, self.J)
136  self.assertLess(np.linalg.norm(vnext), self.tolerance)
137 
138  pin.crba(self.model, data_no_q, self.q)
139  vnext_no_q = pin.impulseDynamics(self.model, data_no_q, self.v0, self.J)
140  self.assertLess(np.linalg.norm(vnext_no_q), self.tolerance)
141 
142  self.assertApprox(vnext, vnext_no_q)
143 
145  data_no_q = self.model.createData()
146 
147  vnext = pin.impulseDynamics(
148  self.model, self.data, self.q, self.v0, self.J, r_coeff
149  )
150  self.assertLess(np.linalg.norm(vnext), self.tolerance)
151 
152  pin.crba(self.model, data_no_q, self.q)
153  vnext_no_q = pin.impulseDynamics(
154  self.model, data_no_q, self.v0, self.J, r_coeff
155  )
156  self.assertLess(np.linalg.norm(vnext_no_q), self.tolerance)
157 
158  self.assertApprox(vnext, vnext_no_q)
159 
161  data_no_q = self.model.createData()
162 
163  vnext = pin.impulseDynamics(
164  self.model, self.data, self.q, self.v0, self.J, r_coeff, inv_damping
165  )
166  self.assertLess(np.linalg.norm(vnext), self.tolerance)
167 
168  pin.crba(self.model, data_no_q, self.q)
169  vnext_no_q = pin.impulseDynamics(
170  self.model, data_no_q, self.v0, self.J, r_coeff, inv_damping
171  )
172  self.assertLess(np.linalg.norm(vnext_no_q), self.tolerance)
173 
174  self.assertApprox(vnext, vnext_no_q)
175 
177  data5 = self.data
178  data6 = self.model.createData()
179  data7 = self.model.createData()
180  _data7_deprecated = self.model.createData()
181  vnext5 = pin.impulseDynamics(self.model, data5, self.q, self.v, self.J)
182  vnext6 = pin.impulseDynamics(self.model, data6, self.q, self.v, self.J, r_coeff)
183  vnext7 = pin.impulseDynamics(
184  self.model, data7, self.q, self.v, self.J, r_coeff, inv_damping
185  )
186  self.assertTrue((vnext5 == vnext6).all())
187  self.assertTrue((vnext5 == vnext7).all())
188  self.assertTrue((vnext6 == vnext7).all())
189 
191  data4 = self.data
192  data5 = self.model.createData()
193  data6 = self.model.createData()
194  data7_deprecated = self.model.createData()
195  pin.crba(self.model, data4, self.q)
196  pin.crba(self.model, data5, self.q)
197  pin.crba(self.model, data6, self.q)
198  pin.crba(self.model, data7_deprecated, self.q)
199  vnext4 = pin.impulseDynamics(self.model, data4, self.v, self.J)
200  vnext5 = pin.impulseDynamics(self.model, data5, self.v, self.J, r_coeff)
201  vnext6 = pin.impulseDynamics(
202  self.model, data6, self.v, self.J, r_coeff, inv_damping
203  )
204 
205  self.assertTrue((vnext4 == vnext5).all())
206  self.assertTrue((vnext4 == vnext6).all())
207  self.assertTrue((vnext5 == vnext6).all())
208 
209 
210 if __name__ == "__main__":
211  unittest.main()
bindings_dynamics.TestDynamicsBindings.v0
v0
Definition: bindings_dynamics.py:24
bindings_dynamics.TestDynamicsBindings.gamma
gamma
Definition: bindings_dynamics.py:35
bindings_dynamics.TestDynamicsBindings.test_impulseDynamics_default
def test_impulseDynamics_default(self)
Definition: bindings_dynamics.py:132
bindings_dynamics.TestDynamicsBindings.data
data
Definition: bindings_dynamics.py:17
bindings_dynamics.TestDynamicsBindings.setUp
def setUp(self)
Definition: bindings_dynamics.py:15
bindings_dynamics.TestDynamicsBindings.test_impulseDynamics_r
def test_impulseDynamics_r(self)
Definition: bindings_dynamics.py:144
bindings_dynamics.TestDynamicsBindings.test_forwardDynamics_default
def test_forwardDynamics_default(self)
Definition: bindings_dynamics.py:37
bindings_dynamics.TestDynamicsBindings.test_impulseDynamics_rd
def test_impulseDynamics_rd(self)
Definition: bindings_dynamics.py:160
bindings_dynamics.TestDynamicsBindings.test_computeKKTMatrix
def test_computeKKTMatrix(self)
Definition: bindings_dynamics.py:63
bindings_dynamics.TestDynamicsBindings.tolerance
tolerance
Definition: bindings_dynamics.py:26
bindings_dynamics.TestDynamicsBindings.q
q
Definition: bindings_dynamics.py:20
pinocchio.utils
Definition: bindings/python/pinocchio/utils.py:1
pinocchio::createData
ConstraintDataTpl< Scalar, Options, ConstraintCollectionTpl > createData(const ConstraintModelTpl< Scalar, Options, ConstraintCollectionTpl > &cmodel)
Definition: constraint-model-visitor.hpp:239
bindings_dynamics.TestDynamicsBindings.tau0
tau0
Definition: bindings_dynamics.py:25
bindings_dynamics.TestDynamicsBindings.test_forwardDynamics_q
def test_forwardDynamics_q(self)
Definition: bindings_dynamics.py:106
bindings_dynamics.TestDynamicsBindings.J
J
Definition: bindings_dynamics.py:29
bindings_dynamics.TestDynamicsBindings
Definition: bindings_dynamics.py:14
bindings_dynamics.TestDynamicsBindings.model
model
Definition: bindings_dynamics.py:16
bindings_dynamics.TestDynamicsBindings.test_forwardDynamics_rcoeff
def test_forwardDynamics_rcoeff(self)
Definition: bindings_dynamics.py:82
bindings_dynamics.TestDynamicsBindings.tau
tau
Definition: bindings_dynamics.py:22
pinocchio.utils.rand
def rand(n)
Definition: bindings/python/pinocchio/utils.py:41
bindings_dynamics.TestDynamicsBindings.test_forwardDynamics_no_q
def test_forwardDynamics_no_q(self)
Definition: bindings_dynamics.py:119
bindings_dynamics.TestDynamicsBindings.v
v
Definition: bindings_dynamics.py:21
pinocchio.utils.zero
def zero(n)
Definition: bindings/python/pinocchio/utils.py:37
bindings_dynamics.TestDynamicsBindings.test_impulseDynamics_q
def test_impulseDynamics_q(self)
Definition: bindings_dynamics.py:176
bindings_dynamics.TestDynamicsBindings.test_impulseDynamics_no_q
def test_impulseDynamics_no_q(self)
Definition: bindings_dynamics.py:190


pinocchio
Author(s):
autogenerated on Fri Nov 1 2024 02:41:38