cppad/joints.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2019 CNRS INRIA
3 //
4 
6 
10 
11 #include <iostream>
12 
13 #include <boost/test/unit_test.hpp>
14 #include <boost/utility/binary.hpp>
15 
16 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
17 
18 BOOST_AUTO_TEST_CASE(test_jointRX_motion_space)
19 {
20  using CppAD::AD;
21  using CppAD::NearEqual;
22 
23  typedef AD<double> AD_double;
24  typedef pinocchio::JointCollectionDefaultTpl<AD_double> JointCollectionAD;
25  typedef pinocchio::JointCollectionDefaultTpl<double> JointCollection;
26 
27  typedef pinocchio::SE3Tpl<AD_double> SE3AD;
28  typedef pinocchio::MotionTpl<AD_double> MotionAD;
32 
33  typedef Eigen::Matrix<AD_double, Eigen::Dynamic, 1> VectorXAD;
34  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
35 
36  typedef JointCollectionAD::JointModelRX JointModelRXAD;
37  typedef JointModelRXAD::ConfigVector_t ConfigVectorAD;
38  // typedef JointModelRXAD::TangentVector_t TangentVectorAD;
39  typedef JointCollectionAD::JointDataRX JointDataRXAD;
40 
42  typedef JointModelRX::ConfigVector_t ConfigVector;
43  typedef JointModelRX::TangentVector_t TangentVector;
45 
46  JointModelRX jmodel;
47  jmodel.setIndexes(0, 0, 0);
48  JointDataRX jdata(jmodel.createData());
49 
50  JointModelRXAD jmodel_ad = jmodel.cast<AD_double>();
51  JointDataRXAD jdata_ad(jmodel_ad.createData());
52 
53  typedef pinocchio::LieGroup<JointModelRX>::type JointOperation;
54  ConfigVector q(jmodel.nq());
55  JointOperation().random(q);
56  ConfigVectorAD q_ad(q.cast<AD_double>());
57 
58  // Zero order
59  jmodel_ad.calc(jdata_ad, q_ad);
60  jmodel.calc(jdata, q);
61 
62  SE3 M1(jdata.M);
63  SE3AD M2(jdata_ad.M);
64  BOOST_CHECK(M1.isApprox(M2.cast<double>()));
65 
66  // First order
67  TangentVector v(TangentVector::Random(jmodel.nv()));
68  VectorXAD X(jmodel_ad.nv());
69 
70  for (Eigen::DenseIndex k = 0; k < jmodel.nv(); ++k)
71  {
72  X[k] = v[k];
73  }
74  CppAD::Independent(X);
75  jmodel_ad.calc(jdata_ad, q_ad, X);
76  jmodel.calc(jdata, q, v);
77  VectorXAD Y(6);
78  MotionAD m_ad(jdata_ad.v);
79  Motion m(jdata.v);
80  JointMotionSubspaceXd Sref(jdata.S.matrix());
81 
82  for (Eigen::DenseIndex k = 0; k < 3; ++k)
83  {
84  Y[k + Motion::LINEAR] = m_ad.linear()[k];
85  Y[k + Motion::ANGULAR] = m_ad.angular()[k];
86  }
87 
88  CppAD::ADFun<double> vjoint(X, Y);
89 
90  CPPAD_TESTVECTOR(double) x((size_t)jmodel_ad.nv());
91  for (Eigen::DenseIndex k = 0; k < jmodel.nv(); ++k)
92  {
93  x[(size_t)k] = v[k];
94  }
95 
96  CPPAD_TESTVECTOR(double) jac = vjoint.Jacobian(x);
97  MatrixX S(6, jac.size() / 6);
98  S = Eigen::Map<MatrixX>(jac.data(), S.rows(), S.cols());
99 
100  BOOST_CHECK(m.isApprox(m_ad.cast<double>()));
101 
102  BOOST_CHECK(Sref.matrix().isApprox(S));
103 }
104 
105 struct TestADOnJoints
106 {
107  template<typename JointModel>
109  {
110  JointModel jmodel;
111  jmodel.setIndexes(0, 0, 0);
112 
113  test(jmodel);
114  }
115 
116  template<typename Scalar, int Options, int axis>
118  {
120  JointModel jmodel(Scalar(0.4));
121  jmodel.setIndexes(0, 0, 0);
122 
123  test(jmodel);
124  }
125 
126  template<typename Scalar, int Options>
128  {
130  typedef typename JointModel::Vector3 Vector3;
131  JointModel jmodel(Vector3::UnitX(), Vector3::UnitY());
132  jmodel.setIndexes(0, 0, 0);
133 
134  test(jmodel);
135  }
136 
137  template<typename Scalar, int Options>
139  {
141  typedef typename JointModel::Vector3 Vector3;
142  JointModel jmodel(Vector3::Random().normalized());
143  jmodel.setIndexes(0, 0, 0);
144 
145  test(jmodel);
146  }
147 
148  template<typename Scalar, int Options>
150  {
152  typedef typename JointModel::Vector3 Vector3;
153  JointModel jmodel(Vector3::Random().normalized());
154  jmodel.setIndexes(0, 0, 0);
155 
156  test(jmodel);
157  }
158 
159  template<typename Scalar, int Options>
161  {
163  typedef typename JointModel::Vector3 Vector3;
164  JointModel jmodel(Vector3::Random().normalized());
165  jmodel.setIndexes(0, 0, 0);
166 
167  test(jmodel);
168  }
169 
170  template<typename Scalar, int Options>
172  {
174  typedef typename JointModel::Vector3 Vector3;
175  JointModel jmodel(Vector3::Random().normalized());
176  jmodel.setIndexes(0, 0, 0);
177 
178  test(jmodel);
179  }
180 
181  template<typename Scalar, int Options, template<typename, int> class JointCollection>
183  {
186  JointModel jmodel((JointModelRX()));
187  jmodel.setIndexes(0, 0, 0);
188 
189  test(jmodel);
190  }
191 
192  // TODO: implement it
193  template<typename JointModel_>
195  {
196  /* do nothing */
197  }
198 
199  template<typename Scalar, int Options, template<typename, int> class JointCollection>
201  {
205  JointModel jmodel((JointModelRX()));
206  jmodel.addJoint(JointModelRY());
207  jmodel.setIndexes(0, 0, 0);
208 
209  test(jmodel);
210  }
211 
212  template<typename JointModel>
213  static void test(const pinocchio::JointModelBase<JointModel> & jmodel)
214  {
215  using CppAD::AD;
216  using CppAD::NearEqual;
217 
218  typedef typename JointModel::Scalar Scalar;
219  typedef typename JointModel::JointDataDerived JointData;
220 
221  typedef AD<Scalar> AD_scalar;
222 
223  typedef pinocchio::SE3Tpl<AD_scalar> SE3AD;
224  typedef pinocchio::MotionTpl<AD_scalar> MotionAD;
228 
229  typedef Eigen::Matrix<AD_scalar, Eigen::Dynamic, 1> VectorXAD;
230  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
231 
232  typedef typename pinocchio::CastType<AD_scalar, JointModel>::type JointModelAD;
233  typedef typename JointModelAD::JointDataDerived JointDataAD;
234 
235  typedef typename JointModelAD::ConfigVector_t ConfigVectorAD;
236 
237  typedef typename JointModel::ConfigVector_t ConfigVector;
238  typedef typename JointModel::TangentVector_t TangentVector;
239 
240  JointData jdata(jmodel.createData());
241  pinocchio::JointDataBase<JointData> & jdata_base = jdata;
242 
243  JointModelAD jmodel_ad = jmodel.template cast<AD_scalar>();
244  JointDataAD jdata_ad(jmodel_ad.createData());
245  pinocchio::JointDataBase<JointDataAD> & jdata_ad_base = jdata_ad;
246 
247  ConfigVector q(jmodel.nq());
248  ConfigVector lb(ConfigVector::Constant(jmodel.nq(), -1.));
249  ConfigVector ub(ConfigVector::Constant(jmodel.nq(), 1.));
250 
251  typedef pinocchio::RandomConfigurationStep<
252  pinocchio::LieGroupMap, ConfigVector, ConfigVector, ConfigVector>
253  RandomConfigAlgo;
254  RandomConfigAlgo::run(jmodel.derived(), typename RandomConfigAlgo::ArgsType(q, lb, ub));
255 
256  ConfigVectorAD q_ad(q.template cast<AD_scalar>());
257 
258  // Zero order
259  jmodel_ad.calc(jdata_ad, q_ad);
260  jmodel.calc(jdata, q);
261 
262  SE3 M1(jdata_base.M());
263  SE3AD M2(jdata_ad_base.M());
264  BOOST_CHECK(M1.isApprox(M2.template cast<Scalar>()));
265 
266  // First order
267  TangentVector v(TangentVector::Random(jmodel.nv()));
268  VectorXAD X(jmodel_ad.nv());
269 
270  for (Eigen::DenseIndex k = 0; k < jmodel.nv(); ++k)
271  {
272  X[k] = v[k];
273  }
274  CppAD::Independent(X);
275  jmodel_ad.calc(jdata_ad, q_ad, X);
276  jmodel.calc(jdata, q, v);
277  VectorXAD Y(6);
278  MotionAD m_ad(jdata_ad_base.v());
279  Motion m(jdata_base.v());
280  JointMotionSubspaceXd Sref(jdata_base.S().matrix());
281 
282  for (Eigen::DenseIndex k = 0; k < 3; ++k)
283  {
284  Y[k + Motion::LINEAR] = m_ad.linear()[k];
285  Y[k + Motion::ANGULAR] = m_ad.angular()[k];
286  }
287 
288  CppAD::ADFun<Scalar> vjoint(X, Y);
289 
290  CPPAD_TESTVECTOR(Scalar) x((size_t)jmodel_ad.nv());
291  for (Eigen::DenseIndex k = 0; k < jmodel.nv(); ++k)
292  {
293  x[(size_t)k] = v[k];
294  }
295 
296  CPPAD_TESTVECTOR(Scalar) jac = vjoint.Jacobian(x);
297  MatrixX S(6, jac.size() / 6);
298  S = Eigen::Map<typename PINOCCHIO_EIGEN_PLAIN_ROW_MAJOR_TYPE(MatrixX)>(
299  jac.data(), S.rows(), S.cols());
300 
301  BOOST_CHECK(m.isApprox(m_ad.template cast<Scalar>()));
302 
303  BOOST_CHECK(Sref.matrix().isApprox(S));
304  }
305 };
306 
307 BOOST_AUTO_TEST_CASE(test_all_joints)
308 {
310  boost::mpl::for_each<JointModelVariant::types>(TestADOnJoints());
311 
313 }
314 
315 BOOST_AUTO_TEST_SUITE_END()
pinocchio::JointModelUniversalTpl
Definition: multibody/joint/fwd.hpp:102
pinocchio::JointModelBase::createData
JointDataDerived createData() const
Definition: joint-model-base.hpp:91
pinocchio::JointDataBase::v
MotionTypeConstRef v() const
Definition: joint-data-base.hpp:211
pinocchio::JointModelRevoluteUnboundedUnalignedTpl
Definition: multibody/joint/fwd.hpp:46
test-cpp2pybind11.m
m
Definition: test-cpp2pybind11.py:25
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelRevoluteUnalignedTpl< Scalar, Options > &) const
Definition: cppad/joints.cpp:149
pinocchio::JointModelHelicalTpl
Definition: multibody/joint/fwd.hpp:60
pinocchio::JointModelMimic
Definition: joint-mimic.hpp:307
pinocchio::python::context::JointDataRX
JointDataRevoluteTpl< Scalar, Options, 0 > JointDataRX
Definition: bindings/python/context/generic.hpp:72
omniidl_be_python_with_docstring.run
def run(tree, args)
Definition: cmake/hpp/idl/omniidl_be_python_with_docstring.py:140
pinocchio::JointModelBase
Definition: joint-model-base.hpp:75
codegen-rnea.jac
jac
Definition: codegen-rnea.py:41
pinocchio::SE3Tpl
Definition: context/casadi.hpp:29
pinocchio::JointDataBase
Definition: joint-data-base.hpp:161
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelCompositeTpl< Scalar, Options, JointCollection > &) const
Definition: cppad/joints.cpp:200
pinocchio::JointDataBase::M
TansformTypeConstRef M() const
Definition: joint-data-base.hpp:203
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
pinocchio::python::context::Motion
MotionTpl< Scalar, Options > Motion
Definition: bindings/python/context/generic.hpp:54
liegroup-algo.hpp
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelHelicalTpl< Scalar, Options, axis > &) const
Definition: cppad/joints.cpp:117
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelPrismaticUnalignedTpl< Scalar, Options > &) const
Definition: cppad/joints.cpp:171
pinocchio::python::context::JointModel
JointModelTpl< Scalar, Options > JointModel
Definition: bindings/python/context/generic.hpp:69
simulation-contact-dynamics.S
S
Definition: simulation-contact-dynamics.py:80
pinocchio::JointDataBase::S
ConstraintTypeConstRef S() const
Definition: joint-data-base.hpp:195
pinocchio::LieGroupMap
Definition: liegroup.hpp:17
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_jointRX_motion_space)
Definition: cppad/joints.cpp:18
TestADOnJoints
Definition: casadi/joints.cpp:287
collision-with-point-clouds.X
X
Definition: collision-with-point-clouds.py:35
joint-generic.hpp
pinocchio::JointModelVariant
JointCollectionDefault::JointModelVariant JointModelVariant
Definition: joint-collection.hpp:205
pinocchio::JointModelTpl
Definition: multibody/joint/fwd.hpp:155
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelUniversalTpl< Scalar, Options > &) const
Definition: cppad/joints.cpp:127
cartpole.v
v
Definition: cartpole.py:153
pinocchio::JointModelHelicalUnalignedTpl
Definition: multibody/joint/fwd.hpp:65
x
x
pinocchio::context::Vector3
Eigen::Matrix< Scalar, 3, 1, Options > Vector3
Definition: context/generic.hpp:53
q
q
pinocchio::JointModel
JointModelTpl< context::Scalar > JointModel
Definition: multibody/joint/fwd.hpp:155
pinocchio::JointModelBase::nq
int nq() const
Definition: joint-model-base.hpp:145
pinocchio::JointModelRevoluteTpl
Definition: multibody/joint/fwd.hpp:33
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelTpl< Scalar, Options, JointCollection > &) const
Definition: cppad/joints.cpp:182
cppad.hpp
pinocchio::JointModelRevoluteUnalignedTpl
Definition: multibody/joint/fwd.hpp:38
pinocchio::JointModelPrismaticUnalignedTpl
Definition: multibody/joint/fwd.hpp:94
pinocchio::JointModelCompositeTpl
Definition: multibody/joint/fwd.hpp:141
TestADOnJoints::test
static void test(const pinocchio::JointModelBase< JointModel > &jmodel)
Definition: cppad/joints.cpp:213
pinocchio::python::context::JointModelRY
JointModelRevoluteTpl< Scalar, Options, 1 > JointModelRY
Definition: bindings/python/context/generic.hpp:76
liegroup.hpp
pinocchio::JointModelBase::calc
void calc(JointDataDerived &data, const Eigen::MatrixBase< ConfigVectorType > &qs) const
Definition: joint-model-base.hpp:107
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelMimic< JointModel_ > &) const
Definition: cppad/joints.cpp:194
Y
Y
pinocchio::python::context::JointData
JointDataTpl< Scalar, Options > JointData
Definition: bindings/python/context/generic.hpp:70
pinocchio::VectorSpaceOperationTpl
Definition: vector-space.hpp:16
pinocchio::JointModelBase::nv
int nv() const
Definition: joint-model-base.hpp:141
pinocchio::python::context::SE3
SE3Tpl< Scalar, Options > SE3
Definition: bindings/python/context/generic.hpp:53
pinocchio::JointCollectionDefaultTpl
Definition: context/generic.hpp:15
pinocchio::MotionTpl
Definition: context/casadi.hpp:27
pinocchio::CastType
Type of the cast of a class C templated by Scalar and Options, to a new NewScalar type....
Definition: fwd.hpp:99
TestADOnJoints::test
static void test(const pinocchio::JointModelMimic< JointModel_ > &)
Definition: casadi/joints.cpp:388
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelRevoluteUnboundedUnalignedTpl< Scalar, Options > &) const
Definition: cppad/joints.cpp:160
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelHelicalUnalignedTpl< Scalar, Options > &) const
Definition: cppad/joints.cpp:138
X
pinocchio::JointCollectionDefaultTpl::JointModelVariant
boost::variant< JointModelRX, JointModelRY, JointModelRZ, JointModelMimicRX, JointModelMimicRY, JointModelMimicRZ, JointModelFreeFlyer, JointModelPlanar, JointModelRevoluteUnaligned, JointModelSpherical, JointModelSphericalZYX, JointModelPX, JointModelPY, JointModelPZ, JointModelPrismaticUnaligned, JointModelTranslation, JointModelRUBX, JointModelRUBY, JointModelRUBZ, JointModelRevoluteUnboundedUnaligned, JointModelHx, JointModelHy, JointModelHz, JointModelHelicalUnaligned, JointModelUniversal, boost::recursive_wrapper< JointModelComposite > > JointModelVariant
Definition: joint-collection.hpp:113
pinocchio::JointMotionSubspaceTpl
Definition: joint-motion-subspace-generic.hpp:48
pinocchio::JointModelBase::derived
JointModelDerived & derived()
Definition: joint-model-base.hpp:82
pinocchio::JointMotionSubspaceXd
JointMotionSubspaceTpl< Eigen::Dynamic, context::Scalar, context::Options > JointMotionSubspaceXd
Definition: joint-motion-subspace.hpp:20
TestADOnJoints::operator()
void operator()(const pinocchio::JointModelBase< JointModel > &) const
Definition: cppad/joints.cpp:108
pinocchio::python::context::JointModelRX
JointModelRevoluteTpl< Scalar, Options, 0 > JointModelRX
Definition: bindings/python/context/generic.hpp:73


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