finite-differences.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2019 CNRS INRIA
3 //
4 
11 
12 #include <iostream>
13 #include <boost/test/unit_test.hpp>
14 #include <boost/utility/binary.hpp>
15 
16 using namespace pinocchio;
17 using namespace Eigen;
18 
19 template<bool local>
21  const Model & model, Data & data, const Eigen::VectorXd & q, const Model::JointIndex joint_id)
22 {
23  Data::Matrix6x res(6, model.nv);
24  res.setZero();
25  VectorXd q_integrate(model.nq);
26  VectorXd v_integrate(model.nv);
27  v_integrate.setZero();
28 
30  const SE3 oMi_ref = data.oMi[joint_id];
31 
32  double eps = 1e-8;
33  for (int k = 0; k < model.nv; ++k)
34  {
35  // Integrate along kth direction
36  v_integrate[k] = eps;
37  q_integrate = integrate(model, q, v_integrate);
38 
39  forwardKinematics(model, data, q_integrate);
40  const SE3 & oMi = data.oMi[joint_id];
41 
42  if (local)
43  res.col(k) = log6(oMi_ref.inverse() * oMi).toVector();
44  else
45  res.col(k) = oMi_ref.act(log6(oMi_ref.inverse() * oMi)).toVector();
46 
47  res.col(k) /= eps;
48 
49  v_integrate[k] = 0.;
50  }
51 
52  return res;
53 }
54 
55 template<typename Matrix>
56 void filterValue(MatrixBase<Matrix> & mat, typename Matrix::Scalar value)
57 {
58  for (int k = 0; k < mat.size(); ++k)
59  mat.derived().data()[k] =
60  math::fabs(mat.derived().data()[k]) <= value ? 0 : mat.derived().data()[k];
61 }
62 
63 template<typename JointModel_>
64 struct init;
65 
66 template<typename JointModel_>
67 struct init
68 {
69  static JointModel_ run()
70  {
71  JointModel_ jmodel;
72  jmodel.setIndexes(0, 0, 0);
73  return jmodel;
74  }
75 };
76 
77 template<typename Scalar, int Options>
78 struct init<pinocchio::JointModelRevoluteUnalignedTpl<Scalar, Options>>
79 {
81 
82  static JointModel run()
83  {
84  typedef typename JointModel::Vector3 Vector3;
85  JointModel jmodel(Vector3::Random().normalized());
86 
87  jmodel.setIndexes(0, 0, 0);
88  return jmodel;
89  }
90 };
91 
92 template<typename Scalar, int Options>
94 {
96 
97  static JointModel run()
98  {
99  typedef typename JointModel::Vector3 Vector3;
100  JointModel jmodel(Vector3::Random().normalized());
101 
102  jmodel.setIndexes(0, 0, 0);
103  return jmodel;
104  }
105 };
106 
107 template<typename Scalar, int Options>
108 struct init<pinocchio::JointModelPrismaticUnalignedTpl<Scalar, Options>>
109 {
111 
112  static JointModel run()
113  {
114  typedef typename JointModel::Vector3 Vector3;
115  JointModel jmodel(Vector3::Random().normalized());
116 
117  jmodel.setIndexes(0, 0, 0);
118  return jmodel;
119  }
120 };
121 
122 template<typename Scalar, int Options>
123 struct init<pinocchio::JointModelHelicalUnalignedTpl<Scalar, Options>>
124 {
126 
127  static JointModel run()
128  {
129  typedef typename JointModel::Vector3 Vector3;
130  JointModel jmodel(Vector3::Random().normalized());
131 
132  jmodel.setIndexes(0, 0, 0);
133  return jmodel;
134  }
135 };
136 
137 template<typename Scalar, int Options>
138 struct init<pinocchio::JointModelUniversalTpl<Scalar, Options>>
139 {
141 
142  static JointModel run()
143  {
144  typedef typename JointModel::Vector3 Vector3;
146 
147  jmodel.setIndexes(0, 0, 0);
148  return jmodel;
149  }
150 };
151 
152 template<typename Scalar, int Options, int axis>
153 struct init<pinocchio::JointModelHelicalTpl<Scalar, Options, axis>>
154 {
156 
157  static JointModel run()
158  {
159  JointModel jmodel(static_cast<Scalar>(0.5));
160 
161  jmodel.setIndexes(0, 0, 0);
162  return jmodel;
163  }
164 };
165 
166 template<typename Scalar, int Options, template<typename, int> class JointCollection>
167 struct init<pinocchio::JointModelTpl<Scalar, Options, JointCollection>>
168 {
170 
171  static JointModel run()
172  {
174  JointModel jmodel((JointModelRX()));
175 
176  jmodel.setIndexes(0, 0, 0);
177  return jmodel;
178  }
179 };
180 
181 template<typename Scalar, int Options, template<typename, int> class JointCollection>
182 struct init<pinocchio::JointModelCompositeTpl<Scalar, Options, JointCollection>>
183 {
185 
186  static JointModel run()
187  {
190  JointModel jmodel((JointModelRX()));
191  jmodel.addJoint(JointModelRY());
192 
193  jmodel.setIndexes(0, 0, 0);
194  return jmodel;
195  }
196 };
197 
198 template<typename JointModel_>
199 struct init<pinocchio::JointModelMimic<JointModel_>>
200 {
202 
203  static JointModel run()
204  {
205  JointModel_ jmodel_ref = init<JointModel_>::run();
206 
207  JointModel jmodel(jmodel_ref, 1., 0.);
208  jmodel.setIndexes(0, 0, 0);
209 
210  return jmodel;
211  }
212 };
213 
215 {
216  void operator()(JointModelComposite & /*jmodel*/) const
217  {
218  }
219 
220  template<typename JointModel>
221  void operator()(JointModelBase<JointModel> & /*jmodel*/) const
222  {
223  typedef typename JointModel::ConfigVector_t CV;
224  typedef typename JointModel::TangentVector_t TV;
225  typedef typename LieGroup<JointModel>::type LieGroupType;
226 
228  std::cout << "name: " << jmodel.classname() << std::endl;
229 
230  typename JointModel::JointDataDerived jdata_ = jmodel.createData();
232  DataBaseType & jdata = static_cast<DataBaseType &>(jdata_);
233 
234  CV q = LieGroupType().random();
235  jmodel.calc(jdata.derived(), q);
236  SE3 M_ref(jdata.M());
237 
238  CV q_int(q);
239  const Eigen::DenseIndex nv = jdata.S().nv();
240  TV v(nv);
241  v.setZero();
242  double eps = 1e-8;
243 
244  Eigen::Matrix<double, 6, JointModel::NV> S(6, nv), S_ref(jdata.S().matrix());
245 
246  for (int k = 0; k < nv; ++k)
247  {
248  v[k] = eps;
249  q_int = LieGroupType().integrate(q, v);
250  jmodel.calc(jdata.derived(), q_int);
251  SE3 M_int = jdata.M();
252 
253  S.col(k) = log6(M_ref.inverse() * M_int).toVector();
254  S.col(k) /= eps;
255 
256  v[k] = 0.;
257  }
258 
259  BOOST_CHECK(S.isApprox(S_ref, eps * 1e1));
260  std::cout << "S_ref:\n" << S_ref << std::endl;
261  std::cout << "S:\n" << S << std::endl;
262  }
263 };
264 
265 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
266 
267 BOOST_AUTO_TEST_CASE(test_S_finit_diff)
268 {
269  boost::mpl::for_each<JointModelVariant::types>(FiniteDiffJoint());
270 }
271 
272 BOOST_AUTO_TEST_CASE(test_jacobian_vs_finit_diff)
273 {
277 
278  VectorXd q = VectorXd::Ones(model.nq);
279  q.segment<4>(3).normalize();
281 
282  Model::Index idx =
283  model.existJointName("rarm2") ? model.getJointId("rarm2") : (Model::Index)(model.njoints - 1);
284  Data::Matrix6x Jrh(6, model.nv);
285  Jrh.fill(0);
286 
287  getJointJacobian(model, data, idx, WORLD, Jrh);
288  Data::Matrix6x Jrh_finite_diff = finiteDiffJacobian<false>(model, data, q, idx);
289  BOOST_CHECK(Jrh_finite_diff.isApprox(Jrh, sqrt(1e-8)));
290 
291  getJointJacobian(model, data, idx, LOCAL, Jrh);
292  Jrh_finite_diff = finiteDiffJacobian<true>(model, data, q, idx);
293  BOOST_CHECK(Jrh_finite_diff.isApprox(Jrh, sqrt(1e-8)));
294 }
295 
296 BOOST_AUTO_TEST_SUITE_END()
pinocchio::JointModelUniversalTpl
Definition: multibody/joint/fwd.hpp:102
pinocchio::JointModelRevoluteUnalignedTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
pinocchio::WORLD
@ WORLD
Definition: multibody/fwd.hpp:48
init< pinocchio::JointModelPrismaticUnalignedTpl< Scalar, Options > >::JointModel
pinocchio::JointModelPrismaticUnalignedTpl< Scalar, Options > JointModel
Definition: finite-differences.cpp:110
Eigen
pinocchio::JointModelRevoluteUnboundedUnalignedTpl
Definition: multibody/joint/fwd.hpp:46
pinocchio::JointModelHelicalTpl
Definition: multibody/joint/fwd.hpp:60
pinocchio::DataTpl
Definition: context/generic.hpp:25
init< pinocchio::JointModelCompositeTpl< Scalar, Options, JointCollection > >::JointModel
pinocchio::JointModelCompositeTpl< Scalar, Options, JointCollection > JointModel
Definition: finite-differences.cpp:184
pinocchio::SE3Tpl::inverse
SE3Tpl inverse() const
aXb = bXa.inverse()
Definition: spatial/se3-tpl.hpp:149
pinocchio::JointModelMimic
Definition: joint-mimic.hpp:307
pinocchio::forwardKinematics
void forwardKinematics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Update the joint placements according to the current joint configuration.
pinocchio::JointModelRevoluteUnboundedUnalignedTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
FiniteDiffJoint::operator()
void operator()(JointModelBase< JointModel > &) const
Definition: finite-differences.cpp:221
init< pinocchio::JointModelMimic< JointModel_ > >::JointModel
pinocchio::JointModelMimic< JointModel_ > JointModel
Definition: finite-differences.cpp:201
pinocchio::JointModelBase
Definition: joint-model-base.hpp:75
filterValue
void filterValue(MatrixBase< Matrix > &mat, typename Matrix::Scalar value)
Definition: finite-differences.cpp:56
pinocchio::nv
int nv(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNvVisitor to get the dimension of the joint tangent space.
pinocchio::SE3Tpl< context::Scalar, context::Options >
pinocchio::JointModelTpl::classname
static std::string classname()
Definition: joint-generic.hpp:386
pinocchio::JointDataBase
Definition: joint-data-base.hpp:161
kinematics.hpp
model.hpp
pinocchio::JointModelRX
JointModelRevoluteTpl< context::Scalar, context::Options, 0 > JointModelRX
Definition: joint-revolute.hpp:873
setup.data
data
Definition: cmake/cython/setup.in.py:48
init< pinocchio::JointModelTpl< Scalar, Options, JointCollection > >::run
static JointModel run()
Definition: finite-differences.cpp:171
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
pinocchio::buildModels::humanoidRandom
void humanoidRandom(ModelTpl< Scalar, Options, JointCollectionTpl > &model, bool usingFF=true)
Create a humanoid kinematic tree with 6-DOF limbs and random joint placements.
pinocchio::JointModelUniversalTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
pinocchio::res
ReturnType res
Definition: spatial/classic-acceleration.hpp:57
pinocchio::computeJointJacobians
const DataTpl< Scalar, Options, JointCollectionTpl >::Matrix6x & computeJointJacobians(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Computes the full model Jacobian, i.e. the stack of all motion subspace expressed in the world frame....
pinocchio::JointModelCompositeTpl::addJoint
JointModelDerived & addJoint(const JointModelBase< JointModel > &jmodel, const SE3 &placement=SE3::Identity())
Add a joint to the vector of joints.
Definition: joint-composite.hpp:278
pinocchio::CartesianAxis::vector
static Eigen::Matrix< Scalar, 3, 1 > vector()
Definition: cartesian-axis.hpp:79
init< pinocchio::JointModelMimic< JointModel_ > >::run
static JointModel run()
Definition: finite-differences.cpp:203
pinocchio::JointModelRY
JointModelRevoluteTpl< context::Scalar, context::Options, 1 > JointModelRY
Definition: joint-revolute.hpp:877
pinocchio::ModelTpl::JointIndex
pinocchio::JointIndex JointIndex
Definition: multibody/model.hpp:67
pinocchio::JointModelTpl::setIndexes
void setIndexes(JointIndex id, int nq, int nv)
Definition: joint-generic.hpp:414
FiniteDiffJoint::operator()
void operator()(JointModelComposite &) const
Definition: finite-differences.cpp:216
init< pinocchio::JointModelRevoluteUnboundedUnalignedTpl< Scalar, Options > >::run
static JointModel run()
Definition: finite-differences.cpp:97
joint-configuration.hpp
pinocchio::JointModelHelicalTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
init< pinocchio::JointModelHelicalUnalignedTpl< Scalar, Options > >::JointModel
pinocchio::JointModelHelicalUnalignedTpl< Scalar, Options > JointModel
Definition: finite-differences.cpp:125
simulation-contact-dynamics.S
S
Definition: simulation-contact-dynamics.py:80
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
init< pinocchio::JointModelHelicalUnalignedTpl< Scalar, Options > >::run
static JointModel run()
Definition: finite-differences.cpp:127
pinocchio::JointModelHelicalUnalignedTpl::Vector3
Eigen::Matrix< Scalar, 3, 1, _Options > Vector3
Definition: joint-helical-unaligned.hpp:635
pinocchio::getJointJacobian
void getJointJacobian(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &data, const JointIndex joint_id, const ReferenceFrame reference_frame, const Eigen::MatrixBase< Matrix6Like > &J)
Computes the Jacobian of a specific joint frame expressed in one of the pinocchio::ReferenceFrame opt...
pinocchio::JointModelTpl::createData
JointDataDerived createData() const
Definition: joint-generic.hpp:317
pinocchio::JointModelRevoluteUnboundedUnalignedTpl::Vector3
Eigen::Matrix< Scalar, 3, 1, Options > Vector3
Definition: joint-revolute-unbounded-unaligned.hpp:138
pinocchio::JointModelTpl
Definition: multibody/joint/fwd.hpp:155
mat
mat
data.hpp
pinocchio::JointModelHelicalUnalignedTpl
Definition: multibody/joint/fwd.hpp:65
value
float value
init< pinocchio::JointModelRevoluteUnboundedUnalignedTpl< Scalar, Options > >::JointModel
pinocchio::JointModelRevoluteUnboundedUnalignedTpl< Scalar, Options > JointModel
Definition: finite-differences.cpp:95
init< pinocchio::JointModelHelicalTpl< Scalar, Options, axis > >::JointModel
pinocchio::JointModelHelicalTpl< Scalar, Options, axis > JointModel
Definition: finite-differences.cpp:155
pinocchio::JointModelPrismaticUnalignedTpl::Vector3
Eigen::Matrix< Scalar, 3, 1, _Options > Vector3
Definition: joint-prismatic-unaligned.hpp:596
init< pinocchio::JointModelTpl< Scalar, Options, JointCollection > >::JointModel
pinocchio::JointModelTpl< Scalar, Options, JointCollection > JointModel
Definition: finite-differences.cpp:169
init< pinocchio::JointModelUniversalTpl< Scalar, Options > >::JointModel
pinocchio::JointModelUniversalTpl< Scalar, Options > JointModel
Definition: finite-differences.cpp:140
pinocchio::context::Vector3
Eigen::Matrix< Scalar, 3, 1, Options > Vector3
Definition: context/generic.hpp:53
init< pinocchio::JointModelPrismaticUnalignedTpl< Scalar, Options > >::run
static JointModel run()
Definition: finite-differences.cpp:112
pinocchio::JointModelRevoluteTpl
Definition: multibody/joint/fwd.hpp:33
pinocchio::q
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > & q
Definition: joint-configuration.hpp:1083
init< pinocchio::JointModelRevoluteUnalignedTpl< Scalar, Options > >::JointModel
pinocchio::JointModelRevoluteUnalignedTpl< Scalar, Options > JointModel
Definition: finite-differences.cpp:80
pinocchio::JointModelHelicalUnalignedTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
pinocchio::v
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > const Eigen::MatrixBase< TangentVectorType > & v
Definition: joint-configuration.hpp:1084
pinocchio::JointModelRevoluteUnalignedTpl
Definition: multibody/joint/fwd.hpp:38
pinocchio::JointModelPrismaticUnalignedTpl
Definition: multibody/joint/fwd.hpp:94
pinocchio::JointModelCompositeTpl
Definition: multibody/joint/fwd.hpp:141
pinocchio::ModelTpl::Index
pinocchio::Index Index
Definition: multibody/model.hpp:66
pinocchio::log6
MotionTpl< Scalar, Options > log6(const SE3Tpl< Scalar, Options > &M)
Log: SE3 -> se3.
Definition: spatial/explog.hpp:435
init::run
static JointModel_ run()
Definition: finite-differences.cpp:69
pinocchio::JointModelRevoluteUnalignedTpl::Vector3
Eigen::Matrix< Scalar, 3, 1, _Options > Vector3
Definition: joint-revolute-unaligned.hpp:614
pinocchio::JointModelPrismaticUnalignedTpl::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
dcrba.eps
int eps
Definition: dcrba.py:458
pinocchio::DataTpl::Matrix6x
Eigen::Matrix< Scalar, 6, Eigen::Dynamic, Options > Matrix6x
The 6d jacobian type (temporary)
Definition: multibody/data.hpp:92
pinocchio::VectorSpaceOperationTpl
Definition: vector-space.hpp:16
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_S_finit_diff)
Definition: finite-differences.cpp:267
sample-models.hpp
append-urdf-model-with-another-model.joint_id
joint_id
Definition: append-urdf-model-with-another-model.py:34
jacobian.hpp
pinocchio::ModelTpl
Definition: context/generic.hpp:20
finiteDiffJacobian
Data::Matrix6x finiteDiffJacobian(const Model &model, Data &data, const Eigen::VectorXd &q, const Model::JointIndex joint_id)
Definition: finite-differences.cpp:20
init
Definition: all-joints.cpp:20
pinocchio::JointModelMimic::setIndexes
void setIndexes(JointIndex id, int q, int v)
Definition: joint-model-base.hpp:186
init< pinocchio::JointModelHelicalTpl< Scalar, Options, axis > >::run
static JointModel run()
Definition: finite-differences.cpp:157
pinocchio::JointModelUniversalTpl::Vector3
Eigen::Matrix< Scalar, 3, 1, _Options > Vector3
Definition: joint-universal.hpp:409
FiniteDiffJoint
Definition: finite-differences.cpp:214
init< pinocchio::JointModelRevoluteUnalignedTpl< Scalar, Options > >::run
static JointModel run()
Definition: finite-differences.cpp:82
pinocchio::normalize
void normalize(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &qout)
Normalize a configuration vector.
Definition: joint-configuration.hpp:887
init< pinocchio::JointModelCompositeTpl< Scalar, Options, JointCollection > >::run
static JointModel run()
Definition: finite-differences.cpp:186
init< pinocchio::JointModelUniversalTpl< Scalar, Options > >::run
static JointModel run()
Definition: finite-differences.cpp:142
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1082
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
pinocchio::JointModelTpl::calc
void calc(JointDataDerived &data, const Eigen::MatrixBase< ConfigVector > &q) const
Definition: joint-generic.hpp:350
pinocchio::LOCAL
@ LOCAL
Definition: multibody/fwd.hpp:50


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