timings-derivatives.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020 CNRS INRIA
3 //
4 
5 #include "pinocchio/algorithm/joint-configuration.hpp"
6 #include "pinocchio/algorithm/kinematics.hpp"
7 #include "pinocchio/algorithm/kinematics-derivatives.hpp"
8 #include "pinocchio/algorithm/rnea-derivatives.hpp"
9 #include "pinocchio/algorithm/rnea-second-order-derivatives.hpp"
10 #include "pinocchio/algorithm/aba-derivatives.hpp"
11 #include "pinocchio/algorithm/aba.hpp"
12 #include "pinocchio/algorithm/rnea.hpp"
13 #include "pinocchio/algorithm/crba.hpp"
14 #include "pinocchio/algorithm/cholesky.hpp"
15 #include "pinocchio/parsers/urdf.hpp"
16 #include "pinocchio/parsers/sample-models.hpp"
17 #include "pinocchio/container/aligned-vector.hpp"
18 
19 #include <iostream>
20 
21 #include "pinocchio/utils/timer.hpp"
22 
23 template<typename Matrix1, typename Matrix2, typename Matrix3>
25  const Eigen::VectorXd & q,
26  const Eigen::VectorXd & v,
27  const Eigen::VectorXd & a,
28  const Eigen::MatrixBase<Matrix1> & _drnea_dq,
29  const Eigen::MatrixBase<Matrix2> & _drnea_dv,
30  const Eigen::MatrixBase<Matrix3> & _drnea_da)
31 {
32  Matrix1 & drnea_dq = PINOCCHIO_EIGEN_CONST_CAST(Matrix1,_drnea_dq);
33  Matrix2 & drnea_dv = PINOCCHIO_EIGEN_CONST_CAST(Matrix2,_drnea_dv);
34  Matrix3 & drnea_da = PINOCCHIO_EIGEN_CONST_CAST(Matrix3,_drnea_da);
35 
36  using namespace Eigen;
37  VectorXd v_eps(VectorXd::Zero(model.nv));
38  VectorXd q_plus(model.nq);
39  VectorXd tau_plus(model.nv);
40  const double alpha = 1e-8;
41 
42  VectorXd tau0 = rnea(model,data_fd,q,v,a);
43 
44  // dRNEA/dq
45  for(int k = 0; k < model.nv; ++k)
46  {
47  v_eps[k] += alpha;
48  integrate(model,q,v_eps,q_plus);
49  tau_plus = rnea(model,data_fd,q_plus,v,a);
50 
51  drnea_dq.col(k) = (tau_plus - tau0)/alpha;
52  v_eps[k] -= alpha;
53  }
54 
55  // dRNEA/dv
56  VectorXd v_plus(v);
57  for(int k = 0; k < model.nv; ++k)
58  {
59  v_plus[k] += alpha;
60  tau_plus = rnea(model,data_fd,q,v_plus,a);
61 
62  drnea_dv.col(k) = (tau_plus - tau0)/alpha;
63  v_plus[k] -= alpha;
64  }
65 
66  // dRNEA/da
67  drnea_da = crba(model,data_fd,q);
68  drnea_da.template triangularView<Eigen::StrictlyLower>()
69  = drnea_da.transpose().template triangularView<Eigen::StrictlyLower>();
70 
71 }
72 
73 void aba_fd(const pinocchio::Model & model, pinocchio::Data & data_fd,
74  const Eigen::VectorXd & q,
75  const Eigen::VectorXd & v,
76  const Eigen::VectorXd & tau,
77  Eigen::MatrixXd & daba_dq,
78  Eigen::MatrixXd & daba_dv,
79  pinocchio::Data::RowMatrixXs & daba_dtau)
80 {
81  using namespace Eigen;
82  VectorXd v_eps(VectorXd::Zero(model.nv));
83  VectorXd q_plus(model.nq);
84  VectorXd a_plus(model.nv);
85  const double alpha = 1e-8;
86 
87  VectorXd a0 = aba(model,data_fd,q,v,tau);
88 
89  // dABA/dq
90  for(int k = 0; k < model.nv; ++k)
91  {
92  v_eps[k] += alpha;
93  integrate(model,q,v_eps,q_plus);
94  a_plus = aba(model,data_fd,q_plus,v,tau);
95 
96  daba_dq.col(k) = (a_plus - a0)/alpha;
97  v_eps[k] -= alpha;
98  }
99 
100  // dABA/dv
101  VectorXd v_plus(v);
102  for(int k = 0; k < model.nv; ++k)
103  {
104  v_plus[k] += alpha;
105  a_plus = aba(model,data_fd,q,v_plus,tau);
106 
107  daba_dv.col(k) = (a_plus - a0)/alpha;
108  v_plus[k] -= alpha;
109  }
110 
111  // dABA/dtau
112  daba_dtau = computeMinverse(model,data_fd,q);
113 }
114 
115 int main(int argc, const char ** argv)
116 {
117  using namespace Eigen;
118  using namespace pinocchio;
119 
121  #ifdef NDEBUG
122  const int NBT = 1000*100;
123  #else
124  const int NBT = 1;
125  std::cout << "(the time score in debug mode is not relevant) " << std::endl;
126  #endif
127 
128  Model model;
129 
130  std::string filename = PINOCCHIO_MODEL_DIR + std::string("/simple_humanoid.urdf");
131  if(argc>1) filename = argv[1];
132  bool with_ff = true;
133 
134  if(argc>2)
135  {
136  const std::string ff_option = argv[2];
137  if(ff_option == "-no-ff")
138  with_ff = false;
139  }
140 
141  if( filename == "HS")
142  buildModels::humanoidRandom(model,true);
143  else
144  if(with_ff)
146 // pinocchio::urdf::buildModel(filename,JointModelRX(),model);
147  else
148  pinocchio::urdf::buildModel(filename,model);
149  std::cout << "nq = " << model.nq << std::endl;
150  std::cout << "nv = " << model.nv << std::endl;
151 
152  Data data(model);
153  VectorXd qmax = Eigen::VectorXd::Ones(model.nq);
154 
159 
160  for(size_t i=0;i<NBT;++i)
161  {
162  qs[i] = randomConfiguration(model,-qmax,qmax);
163  qdots[i] = Eigen::VectorXd::Random(model.nv);
164  qddots[i] = Eigen::VectorXd::Random(model.nv);
165  taus[i] = Eigen::VectorXd::Random(model.nv);
166  }
167 
168  PINOCCHIO_EIGEN_PLAIN_ROW_MAJOR_TYPE(MatrixXd) drnea_dq(MatrixXd::Zero(model.nv,model.nv));
169  PINOCCHIO_EIGEN_PLAIN_ROW_MAJOR_TYPE(MatrixXd) drnea_dv(MatrixXd::Zero(model.nv,model.nv));
170  MatrixXd drnea_da(MatrixXd::Zero(model.nv,model.nv));
171 
172  MatrixXd daba_dq(MatrixXd::Zero(model.nv,model.nv));
173  MatrixXd daba_dv(MatrixXd::Zero(model.nv,model.nv));
174  Data::RowMatrixXs daba_dtau(Data::RowMatrixXs::Zero(model.nv,model.nv));
175 
176  typedef Data::Tensor3x Tensor3x;
177 // typedef Eigen::Tensor<double, 3, Eigen::RowMajor> Tensor3x;
178  Tensor3x dtau2_dq(model.nv, model.nv, model.nv);
179  Tensor3x dtau2_dv(model.nv, model.nv, model.nv);
180  Tensor3x dtau2_dqv(model.nv, model.nv, model.nv);
181  Tensor3x dtau_dadq(model.nv, model.nv, model.nv);
182  dtau2_dq.setZero();
183  dtau2_dv.setZero();
184  dtau2_dqv.setZero();
185  dtau_dadq.setZero();
186 
187  timer.tic();
188  SMOOTH(NBT)
189  {
190  forwardKinematics(model,data,qs[_smooth],qdots[_smooth],qddots[_smooth]);
191  }
192  std::cout << "FK= \t\t"; timer.toc(std::cout,NBT);
193 
194  timer.tic();
195  SMOOTH(NBT)
196  {
197  computeForwardKinematicsDerivatives(model,data,qs[_smooth],qdots[_smooth],qddots[_smooth]);
198  }
199  std::cout << "FK derivatives= \t\t"; timer.toc(std::cout,NBT);
200 
201  timer.tic();
202  SMOOTH(NBT)
203  {
204  rnea(model,data,qs[_smooth],qdots[_smooth],qddots[_smooth]);
205  }
206  std::cout << "RNEA= \t\t"; timer.toc(std::cout,NBT);
207 
208  timer.tic();
209  SMOOTH(NBT)
210  {
211  computeRNEADerivatives(model,data,qs[_smooth],qdots[_smooth],qddots[_smooth],
212  drnea_dq,drnea_dv,drnea_da);
213  }
214  std::cout << "RNEA derivatives= \t\t"; timer.toc(std::cout,NBT);
215 
216  timer.tic();
217  SMOOTH(NBT) {
218  ComputeRNEASecondOrderDerivatives(model, data, qs[_smooth], qdots[_smooth],
219  qddots[_smooth], dtau2_dq, dtau2_dv, dtau2_dqv,
220  dtau_dadq);
221  }
222  std::cout << "RNEA derivatives SO= \t\t";
223  timer.toc(std::cout, NBT);
224 
225  timer.tic();
226  SMOOTH(NBT/100)
227  {
228  rnea_fd(model,data,qs[_smooth],qdots[_smooth],qddots[_smooth],
229  drnea_dq,drnea_dv,drnea_da);
230  }
231  std::cout << "RNEA finite differences= \t\t"; timer.toc(std::cout,NBT/100);
232 
233  timer.tic();
234  SMOOTH(NBT)
235  {
236  aba(model,data,qs[_smooth],qdots[_smooth],taus[_smooth]);
237  }
238  std::cout << "ABA= \t\t"; timer.toc(std::cout,NBT);
239 
240  timer.tic();
241  SMOOTH(NBT)
242  {
243  computeABADerivatives(model,data,qs[_smooth],qdots[_smooth],taus[_smooth],
244  daba_dq,daba_dv,daba_dtau);
245  }
246  std::cout << "ABA derivatives= \t\t"; timer.toc(std::cout,NBT);
247 
248  timer.tic();
249  SMOOTH(NBT)
250  {
251  aba_fd(model,data,qs[_smooth],qdots[_smooth],taus[_smooth],
252  daba_dq,daba_dv,daba_dtau);
253  }
254  std::cout << "ABA finite differences= \t\t"; timer.toc(std::cout,NBT);
255 
256  timer.tic();
257  SMOOTH(NBT)
258  {
259  computeMinverse(model,data,qs[_smooth]);
260  }
261  std::cout << "M.inverse() from ABA = \t\t"; timer.toc(std::cout,NBT);
262 
263  MatrixXd Minv(model.nv,model.nv); Minv.setZero();
264  timer.tic();
265  SMOOTH(NBT)
266  {
267  crba(model,data,qs[_smooth]);
268  cholesky::decompose(model,data);
269  cholesky::computeMinv(model,data,Minv);
270  }
271  std::cout << "Minv from Cholesky = \t\t"; timer.toc(std::cout,NBT);
272 
273  std::cout << "--" << std::endl;
274  return 0;
275 }
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & aba(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &tau)
The Articulated-Body algorithm. It computes the forward dynamics, aka the joint accelerations given t...
double toc()
Definition: timer.hpp:68
const DataTpl< Scalar, Options, JointCollectionTpl >::MatrixXs & crba(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Computes the upper triangular part of the joint space inertia matrix M by using the Composite Rigid B...
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Options > RowMatrixXs
Mat & computeMinv(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< Mat > &Minv)
Computes the inverse of the joint space inertia matrix M from its Cholesky factorization.
const DataTpl< Scalar, Options, JointCollectionTpl >::RowMatrixXs & computeMinverse(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Computes the inverse of the joint space inertia matrix using Articulated Body formulation.
void rnea(const int num_threads, ModelPoolTpl< Scalar, Options, JointCollectionTpl > &pool, const Eigen::MatrixBase< ConfigVectorPool > &q, const Eigen::MatrixBase< TangentVectorPool1 > &v, const Eigen::MatrixBase< TangentVectorPool2 > &a, const Eigen::MatrixBase< TangentVectorPool3 > &tau)
The Recursive Newton-Euler algorithm. It computes the inverse dynamics, aka the joint torques accordi...
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...
const DataTpl< Scalar, Options, JointCollectionTpl >::MatrixXs & decompose(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data)
Compute the Cholesky decomposition of the joint space inertia matrix M contained in data...
int main(int argc, const char **argv)
void randomConfiguration(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorIn1 > &lowerLimits, const Eigen::MatrixBase< ConfigVectorIn2 > &upperLimits, const Eigen::MatrixBase< ReturnType > &qout)
Generate a configuration vector uniformly sampled among provided limits.
#define PINOCCHIO_MODEL_DIR
#define PINOCCHIO_EIGEN_CONST_CAST(TYPE, OBJ)
Macro for an automatic const_cast.
void tic()
Definition: timer.hpp:63
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.
bp::tuple computeRNEADerivatives(const Model &model, Data &data, const Eigen::VectorXd &q, const Eigen::VectorXd &v, const Eigen::VectorXd &a)
#define PINOCCHIO_ALIGNED_STD_VECTOR(Type)
void computeForwardKinematicsDerivatives(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &a)
Computes all the terms required to compute the derivatives of the placement, spatial velocity and acc...
#define SMOOTH(s)
Definition: timer.hpp:38
ModelTpl< Scalar, Options, JointCollectionTpl > & buildModel(const std::string &filename, const typename ModelTpl< Scalar, Options, JointCollectionTpl >::JointModel &rootJoint, ModelTpl< Scalar, Options, JointCollectionTpl > &model, const bool verbose=false)
Build the model from a URDF file with a particular joint as root of the model tree inside the model g...
Vec3f a
void aba_fd(const pinocchio::Model &model, pinocchio::Data &data_fd, const Eigen::VectorXd &q, const Eigen::VectorXd &v, const Eigen::VectorXd &tau, Eigen::MatrixXd &daba_dq, Eigen::MatrixXd &daba_dv, pinocchio::Data::RowMatrixXs &daba_dtau)
Main pinocchio namespace.
Definition: timings.cpp:28
#define PINOCCHIO_EIGEN_PLAIN_ROW_MAJOR_TYPE(D)
Similar to macro PINOCCHIO_EIGEN_PLAIN_TYPE but with guaranty to provite a row major type...
void ComputeRNEASecondOrderDerivatives(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &a, const Tensor1 &d2tau_dqdq, const Tensor2 &d2tau_dvdv, const Tensor3 &dtau_dqdv, const Tensor4 &dtau_dadq)
Computes the Second-Order partial derivatives of the Recursive Newton Euler Algorithm w...
int nv
Dimension of the velocity vector space.
void rnea_fd(const pinocchio::Model &model, pinocchio::Data &data_fd, const Eigen::VectorXd &q, const Eigen::VectorXd &v, const Eigen::VectorXd &a, const Eigen::MatrixBase< Matrix1 > &_drnea_dq, const Eigen::MatrixBase< Matrix2 > &_drnea_dv, const Eigen::MatrixBase< Matrix3 > &_drnea_da)
filename
JointModelFreeFlyerTpl< double > JointModelFreeFlyer
void computeABADerivatives(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &tau, const Eigen::MatrixBase< MatrixType1 > &aba_partial_dq, const Eigen::MatrixBase< MatrixType2 > &aba_partial_dv, const Eigen::MatrixBase< MatrixType3 > &aba_partial_dtau)
The derivatives of the Articulated-Body algorithm.
void humanoidRandom(ModelTpl< Scalar, Options, JointCollectionTpl > &model, bool usingFF=true)
Create a humanoid kinematic tree with 6-DOF limbs and random joint placements.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorXd
Definition: conversions.cpp:14
int nq
Dimension of the configuration vector representation.


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:33