cppad/joint-configurations.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 CNRS INRIA
3 //
4 
8 
9 #include "../utils/model-generator.hpp"
10 
11 #include <boost/test/unit_test.hpp>
12 #include <boost/utility/binary.hpp>
13 
14 using namespace pinocchio;
15 
16 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
17 
18 BOOST_AUTO_TEST_CASE(test_joint_configuration)
19 {
20  typedef double Scalar;
21  using CppAD::AD;
22  using CppAD::NearEqual;
23 
24  typedef AD<Scalar> ADScalar;
25 
27 
28  Model model;
30  Eigen::VectorXd q2 = Eigen::VectorXd::Random(model.nq);
31  Eigen::VectorXd q1 = Eigen::VectorXd::Random(model.nq);
32  normalize(model, q1);
33  normalize(model, q2);
34 
35  Eigen::VectorXd v = Eigen::VectorXd::Random(model.nv);
36  std::vector<Eigen::VectorXd> results_q(2, Eigen::VectorXd::Zero(model.nq));
37  std::vector<Eigen::VectorXd> results_v(2, Eigen::VectorXd::Zero(model.nv));
38 
39  typedef pinocchio::ModelTpl<ADScalar> ADModel;
40  typedef ADModel::ConfigVectorType ADConfigVectorType;
41  ADModel ad_model = model.cast<ADScalar>();
42  ADConfigVectorType ad_q1(model.nq);
43  ADConfigVectorType ad_q2(model.nq);
44  ADConfigVectorType ad_v(model.nv);
45 
46  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> VectorX;
47  typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> VectorXAD;
48  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
49  typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, Eigen::Dynamic> MatrixXAD;
50 
51  // Integrate
52  {
53  VectorXAD ad_x(model.nq + model.nv);
54  ad_x << q1.cast<ADScalar>(), v.cast<ADScalar>();
55  CppAD::Independent(ad_x);
56  ad_q1 = ad_x.head(model.nq);
57  ad_v = ad_x.tail(model.nv);
58 
59  VectorXAD ad_y(model.nq);
60  pinocchio::integrate(ad_model, ad_q1, ad_v, ad_y);
61  CppAD::ADFun<Scalar> ad_fun(ad_x, ad_y);
62 
63  CPPAD_TESTVECTOR(Scalar) x_eval((size_t)(ad_x.size()));
64  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, v;
65  CPPAD_TESTVECTOR(Scalar) y_eval((size_t)(ad_y.size()));
66  y_eval = ad_fun.Forward(0, x_eval);
67  results_q[0] = Eigen::Map<VectorX>(y_eval.data(), ad_y.size());
68 
69  pinocchio::integrate(model, q1, v, results_q[1]);
70  BOOST_CHECK(results_q[0].isApprox(results_q[1]));
71 
72  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, VectorX::Zero(model.nv);
73  y_eval = ad_fun.Forward(0, x_eval);
74  results_q[0] = Eigen::Map<VectorX>(y_eval.data(), ad_y.size());
75  BOOST_CHECK(results_q[0].isApprox(q1));
76  }
77 
78  // Difference
79  {
80  VectorXAD ad_x(model.nq + model.nq);
81  ad_x << q1.cast<ADScalar>(), q2.cast<ADScalar>();
82  CppAD::Independent(ad_x);
83  ad_q1 = ad_x.head(model.nq);
84  ad_q2 = ad_x.tail(model.nq);
85 
86  VectorXAD ad_y(model.nv);
87  pinocchio::difference(ad_model, ad_q1, ad_q2, ad_y);
88  CppAD::ADFun<Scalar> ad_fun(ad_x, ad_y);
89 
90  CPPAD_TESTVECTOR(Scalar) x_eval((size_t)(ad_x.size()));
91  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, q2;
92  CPPAD_TESTVECTOR(Scalar) y_eval((size_t)(ad_y.size()));
93  y_eval = ad_fun.Forward(0, x_eval);
94  results_v[0] = Eigen::Map<VectorX>(y_eval.data(), ad_y.size());
95 
96  pinocchio::difference(model, q1, q2, results_v[1]);
97  BOOST_CHECK(results_v[0].isApprox(results_v[1]));
98 
99  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, q1;
100  y_eval = ad_fun.Forward(0, x_eval);
101  results_v[0] = Eigen::Map<VectorX>(y_eval.data(), ad_y.size());
102  BOOST_CHECK(results_v[0].isZero());
103  }
104 
105  // dDifference
106  std::vector<MatrixX> results_J0(2, MatrixX::Zero(model.nv, model.nv));
107  std::vector<MatrixX> results_J1(2, MatrixX::Zero(model.nv, model.nv));
108  {
109  VectorXAD ad_x(model.nq + model.nq);
110  ad_x << q1.cast<ADScalar>(), q2.cast<ADScalar>();
111  CppAD::Independent(ad_x);
112  ad_q1 = ad_x.head(model.nq);
113  ad_q2 = ad_x.tail(model.nq);
114 
115  MatrixXAD ad_y(2 * model.nv, model.nv);
116  pinocchio::dDifference(ad_model, ad_q1, ad_q2, ad_y.topRows(model.nv), pinocchio::ARG0);
117  pinocchio::dDifference(ad_model, ad_q1, ad_q2, ad_y.bottomRows(model.nv), pinocchio::ARG1);
118  VectorXAD ad_y_flatten = Eigen::Map<VectorXAD>(ad_y.data(), ad_y.size());
119  CppAD::ADFun<Scalar> ad_fun(ad_x, ad_y_flatten);
120 
121  CPPAD_TESTVECTOR(Scalar) x_eval((size_t)(ad_x.size()));
122  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, q2;
123  CPPAD_TESTVECTOR(Scalar) y_eval((size_t)(ad_y.size()));
124  y_eval = ad_fun.Forward(0, x_eval);
125  results_J0[0] = Eigen::Map<MatrixX>(y_eval.data(), ad_y.rows(), ad_y.cols()).topRows(model.nv);
126  results_J1[0] =
127  Eigen::Map<MatrixX>(y_eval.data(), ad_y.rows(), ad_y.cols()).bottomRows(model.nv);
128 
129  // w.r.t q1
130  pinocchio::dDifference(model, q1, q2, results_J0[1], pinocchio::ARG0);
131  BOOST_CHECK(results_J0[0].isApprox(results_J0[1]));
132 
133  // w.r.t q2
134  pinocchio::dDifference(model, q1, q2, results_J1[1], pinocchio::ARG1);
135  BOOST_CHECK(results_J1[0].isApprox(results_J1[1]));
136 
137  Eigen::Map<VectorX>(x_eval.data(), ad_x.size()) << q1, q1;
138  y_eval = ad_fun.Forward(0, x_eval);
139  results_J0[0] = Eigen::Map<MatrixX>(y_eval.data(), ad_y.rows(), ad_y.cols()).topRows(model.nv);
140  results_J1[0] =
141  Eigen::Map<MatrixX>(y_eval.data(), ad_y.rows(), ad_y.cols()).bottomRows(model.nv);
142 
143  BOOST_CHECK((-results_J0[0]).isIdentity());
144  BOOST_CHECK(results_J1[0].isIdentity());
145  }
146 }
147 
148 BOOST_AUTO_TEST_SUITE_END()
pinocchio::isZero
bool isZero(const Eigen::MatrixBase< MatrixLike > &m, const typename MatrixLike::RealScalar &prec=Eigen::NumTraits< typename MatrixLike::Scalar >::dummy_precision())
Definition: math/matrix.hpp:59
model.hpp
q2
q2
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
pinocchio::difference
void difference(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorIn1 > &q0, const Eigen::MatrixBase< ConfigVectorIn2 > &q1, const Eigen::MatrixBase< ReturnType > &dvout)
Compute the tangent vector that must be integrated during one unit time to go from q0 to q1.
Definition: joint-configuration.hpp:193
pinocchio::ARG0
@ ARG0
Definition: fwd.hpp:123
joint-configuration.hpp
pinocchio::integrate
void integrate(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v, const Eigen::MatrixBase< ReturnType > &qout)
Integrate a configuration vector for the specified model for a tangent vector during one unit time.
Definition: joint-configuration.hpp:70
pinocchio::ARG1
@ ARG1
Definition: fwd.hpp:124
pinocchio::buildAllJointsModel
void buildAllJointsModel(Model &model)
Definition: model-generator.hpp:31
cppad.hpp
pinocchio::v
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > const Eigen::MatrixBase< TangentVectorType > & v
Definition: joint-configuration.hpp:1084
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_joint_configuration)
Definition: cppad/joint-configurations.cpp:18
pinocchio::q1
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorIn1 > const Eigen::MatrixBase< ConfigVectorIn2 > & q1
Definition: joint-configuration.hpp:1138
pinocchio::dDifference
void dDifference(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVector1 > &q0, const Eigen::MatrixBase< ConfigVector2 > &q1, const Eigen::MatrixBase< JacobianMatrix > &J, const ArgumentPosition arg)
Computes the Jacobian of a small variation of the configuration vector into the tangent space at iden...
Definition: joint-configuration.hpp:742
pinocchio::ModelTpl
Definition: context/generic.hpp:20
pinocchio::Model
ModelTpl< context::Scalar, context::Options > Model
Definition: multibody/fwd.hpp:33
pinocchio::normalize
void normalize(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &qout)
Normalize a configuration vector.
Definition: joint-configuration.hpp:887
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1082
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
isApprox
bool isApprox(const Box &s1, const Box &s2, const FCL_REAL tol)


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