timings-derivatives.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2021 CNRS INRIA
3 //
4 
18 
19 #include <iostream>
20 
22 
23 template<typename Matrix1, typename Matrix2, typename Matrix3>
24 void rnea_fd(
25  const pinocchio::Model & model,
26  pinocchio::Data & data_fd,
27  const Eigen::VectorXd & q,
28  const Eigen::VectorXd & v,
29  const Eigen::VectorXd & a,
30  const Eigen::MatrixBase<Matrix1> & _drnea_dq,
31  const Eigen::MatrixBase<Matrix2> & _drnea_dv,
32  const Eigen::MatrixBase<Matrix3> & _drnea_da)
33 {
34  Matrix1 & drnea_dq = PINOCCHIO_EIGEN_CONST_CAST(Matrix1, _drnea_dq);
35  Matrix2 & drnea_dv = PINOCCHIO_EIGEN_CONST_CAST(Matrix2, _drnea_dv);
36  Matrix3 & drnea_da = PINOCCHIO_EIGEN_CONST_CAST(Matrix3, _drnea_da);
37 
38  using namespace Eigen;
39  VectorXd v_eps(VectorXd::Zero(model.nv));
40  VectorXd q_plus(model.nq);
41  VectorXd tau_plus(model.nv);
42  const double alpha = 1e-8;
43 
44  VectorXd tau0 = rnea(model, data_fd, q, v, a);
45 
46  // dRNEA/dq
47  for (int k = 0; k < model.nv; ++k)
48  {
49  v_eps[k] += alpha;
50  integrate(model, q, v_eps, q_plus);
51  tau_plus = rnea(model, data_fd, q_plus, v, a);
52 
53  drnea_dq.col(k) = (tau_plus - tau0) / alpha;
54  v_eps[k] -= alpha;
55  }
56 
57  // dRNEA/dv
58  VectorXd v_plus(v);
59  for (int k = 0; k < model.nv; ++k)
60  {
61  v_plus[k] += alpha;
62  tau_plus = rnea(model, data_fd, q, v_plus, a);
63 
64  drnea_dv.col(k) = (tau_plus - tau0) / alpha;
65  v_plus[k] -= alpha;
66  }
67 
68  // dRNEA/da
69  drnea_da = pinocchio::crba(model, data_fd, q, pinocchio::Convention::WORLD);
70  drnea_da.template triangularView<Eigen::StrictlyLower>() =
71  drnea_da.transpose().template triangularView<Eigen::StrictlyLower>();
72 }
73 
74 void aba_fd(
75  const pinocchio::Model & model,
76  pinocchio::Data & data_fd,
77  const Eigen::VectorXd & q,
78  const Eigen::VectorXd & v,
79  const Eigen::VectorXd & tau,
80  Eigen::MatrixXd & daba_dq,
81  Eigen::MatrixXd & daba_dv,
82  pinocchio::Data::RowMatrixXs & daba_dtau)
83 {
84  using namespace Eigen;
85  VectorXd v_eps(VectorXd::Zero(model.nv));
86  VectorXd q_plus(model.nq);
87  VectorXd a_plus(model.nv);
88  const double alpha = 1e-8;
89 
90  VectorXd a0 = pinocchio::aba(model, data_fd, q, v, tau, pinocchio::Convention::LOCAL);
91 
92  // dABA/dq
93  for (int k = 0; k < model.nv; ++k)
94  {
95  v_eps[k] += alpha;
96  q_plus = integrate(model, q, v_eps);
97  a_plus = pinocchio::aba(model, data_fd, q_plus, v, tau, pinocchio::Convention::LOCAL);
98 
99  daba_dq.col(k) = (a_plus - a0) / alpha;
100  v_eps[k] -= alpha;
101  }
102 
103  // dABA/dv
104  VectorXd v_plus(v);
105  for (int k = 0; k < model.nv; ++k)
106  {
107  v_plus[k] += alpha;
108  a_plus = pinocchio::aba(model, data_fd, q, v_plus, tau, pinocchio::Convention::LOCAL);
109 
110  daba_dv.col(k) = (a_plus - a0) / alpha;
111  v_plus[k] -= alpha;
112  }
113 
114  // dABA/dtau
115  daba_dtau = computeMinverse(model, data_fd, q);
116 }
117 
118 int main(int argc, const char ** argv)
119 {
120  using namespace Eigen;
121  using namespace pinocchio;
122 
124 #ifdef NDEBUG
125  const int NBT = 1000 * 100;
126 #else
127  const int NBT = 1;
128  std::cout << "(the time score in debug mode is not relevant) " << std::endl;
129 #endif
130 
131  Model model;
132 
133  std::string filename = PINOCCHIO_MODEL_DIR + std::string("/simple_humanoid.urdf");
134  if (argc > 1)
135  filename = argv[1];
136  bool with_ff = true;
137 
138  if (argc > 2)
139  {
140  const std::string ff_option = argv[2];
141  if (ff_option == "-no-ff")
142  with_ff = false;
143  }
144 
145  if (filename == "HS")
147  else if (with_ff)
149  // pinocchio::urdf::buildModel(filename,JointModelRX(),model);
150  else
152  std::cout << "nq = " << model.nq << std::endl;
153  std::cout << "nv = " << model.nv << std::endl;
154  std::cout << "--" << std::endl;
155 
156  Data data(model);
157  VectorXd qmax = Eigen::VectorXd::Ones(model.nq);
158 
159  PINOCCHIO_ALIGNED_STD_VECTOR(VectorXd) qs(NBT);
160  PINOCCHIO_ALIGNED_STD_VECTOR(VectorXd) qdots(NBT);
161  PINOCCHIO_ALIGNED_STD_VECTOR(VectorXd) qddots(NBT);
162  PINOCCHIO_ALIGNED_STD_VECTOR(VectorXd) taus(NBT);
163 
164  for (size_t i = 0; i < NBT; ++i)
165  {
166  qs[i] = randomConfiguration(model, -qmax, qmax);
167  qdots[i] = Eigen::VectorXd::Random(model.nv);
168  qddots[i] = Eigen::VectorXd::Random(model.nv);
169  taus[i] = Eigen::VectorXd::Random(model.nv);
170  }
171 
172  PINOCCHIO_EIGEN_PLAIN_COLUMN_MAJOR_TYPE(MatrixXd) drnea_dq(MatrixXd::Zero(model.nv, model.nv));
173  PINOCCHIO_EIGEN_PLAIN_COLUMN_MAJOR_TYPE(MatrixXd) drnea_dv(MatrixXd::Zero(model.nv, model.nv));
174  MatrixXd drnea_da(MatrixXd::Zero(model.nv, model.nv));
175 
176  MatrixXd daba_dq(MatrixXd::Zero(model.nv, model.nv));
177  MatrixXd daba_dv(MatrixXd::Zero(model.nv, model.nv));
178  Data::RowMatrixXs daba_dtau(Data::RowMatrixXs::Zero(model.nv, model.nv));
179 
180  typedef Data::Tensor3x Tensor3x;
181  // typedef Eigen::Tensor<double, 3, Eigen::RowMajor> Tensor3x;
182  Tensor3x dtau2_dq(model.nv, model.nv, model.nv);
183  Tensor3x dtau2_dv(model.nv, model.nv, model.nv);
184  Tensor3x dtau2_dqv(model.nv, model.nv, model.nv);
185  Tensor3x dtau_dadq(model.nv, model.nv, model.nv);
186  dtau2_dq.setZero();
187  dtau2_dv.setZero();
188  dtau2_dqv.setZero();
189  dtau_dadq.setZero();
190 
191  timer.tic();
192  SMOOTH(NBT)
193  {
194  forwardKinematics(model, data, qs[_smooth], qdots[_smooth], qddots[_smooth]);
195  }
196  std::cout << "FK= \t\t\t\t";
197  timer.toc(std::cout, NBT);
198 
199  timer.tic();
200  SMOOTH(NBT)
201  {
202  computeForwardKinematicsDerivatives(model, data, qs[_smooth], qdots[_smooth], qddots[_smooth]);
203  }
204  std::cout << "FK derivatives= \t\t";
205  timer.toc(std::cout, NBT);
206 
207  timer.tic();
208  SMOOTH(NBT)
209  {
210  rnea(model, data, qs[_smooth], qdots[_smooth], qddots[_smooth]);
211  }
212  std::cout << "RNEA= \t\t\t\t";
213  timer.toc(std::cout, NBT);
214 
215  timer.tic();
216  SMOOTH(NBT)
217  {
219  model, data, qs[_smooth], qdots[_smooth], qddots[_smooth], drnea_dq, drnea_dv, drnea_da);
220  }
221  std::cout << "RNEA derivatives= \t\t";
222  timer.toc(std::cout, NBT);
223 
224  timer.tic();
225  SMOOTH(NBT)
226  {
228  model, data, qs[_smooth], qdots[_smooth], qddots[_smooth], dtau2_dq, dtau2_dv, dtau2_dqv,
229  dtau_dadq);
230  }
231  std::cout << "RNEA derivatives SO= \t\t";
232  timer.toc(std::cout, NBT);
233 
234  timer.tic();
235  SMOOTH(NBT / 100)
236  {
237  rnea_fd(
238  model, data, qs[_smooth], qdots[_smooth], qddots[_smooth], drnea_dq, drnea_dv, drnea_da);
239  }
240  std::cout << "RNEA finite differences= \t";
241  timer.toc(std::cout, NBT / 100);
242 
243  timer.tic();
244  SMOOTH(NBT)
245  {
246  aba(model, data, qs[_smooth], qdots[_smooth], taus[_smooth], Convention::LOCAL);
247  }
248  std::cout << "ABA= \t\t\t\t";
249  timer.toc(std::cout, NBT);
250 
251  timer.tic();
252  SMOOTH(NBT)
253  {
255  model, data, qs[_smooth], qdots[_smooth], taus[_smooth], daba_dq, daba_dv, daba_dtau);
256  }
257  std::cout << "ABA derivatives(q,v,tau)= \t";
258  timer.toc(std::cout, NBT);
259 
260  {
261  double total = 0;
262  SMOOTH(NBT)
263  {
264  aba(model, data, qs[_smooth], qdots[_smooth], taus[_smooth], Convention::WORLD);
265  timer.tic();
266  computeABADerivatives(model, data, daba_dq, daba_dv, daba_dtau);
267  total += timer.toc(timer.DEFAULT_UNIT);
268  }
269  std::cout << "ABA derivatives() = \t\t" << (total / NBT) << " "
270  << timer.unitName(timer.DEFAULT_UNIT) << std::endl;
271  }
272 
273  timer.tic();
274  SMOOTH(NBT / 100)
275  {
276  aba_fd(model, data, qs[_smooth], qdots[_smooth], taus[_smooth], daba_dq, daba_dv, daba_dtau);
277  }
278  std::cout << "ABA finite differences= \t";
279  timer.toc(std::cout, NBT / 100);
280 
281  timer.tic();
282  SMOOTH(NBT)
283  {
284  computeMinverse(model, data, qs[_smooth]);
285  }
286  std::cout << "M.inverse(q) = \t\t\t";
287  timer.toc(std::cout, NBT);
288 
289  {
290  double total = 0;
291  SMOOTH(NBT)
292  {
293  aba(model, data, qs[_smooth], qdots[_smooth], taus[_smooth], Convention::WORLD);
294  timer.tic();
296  total += timer.toc(timer.DEFAULT_UNIT);
297  }
298  std::cout << "M.inverse() from ABA = \t\t" << (total / NBT) << " "
299  << timer.unitName(timer.DEFAULT_UNIT) << std::endl;
300  }
301 
302  MatrixXd Minv(model.nv, model.nv);
303  Minv.setZero();
304  timer.tic();
305  SMOOTH(NBT)
306  {
307  crba(model, data, qs[_smooth], Convention::WORLD);
310  }
311  std::cout << "Minv from Cholesky = \t\t";
312  timer.toc(std::cout, NBT);
313 
314  std::cout << "--" << std::endl;
315  return 0;
316 }
PINOCCHIO_EIGEN_PLAIN_COLUMN_MAJOR_TYPE
#define PINOCCHIO_EIGEN_PLAIN_COLUMN_MAJOR_TYPE(D)
Similar to macro PINOCCHIO_EIGEN_PLAIN_TYPE but with guaranty to provite a column major type.
Definition: eigen-macros.hpp:18
pinocchio::WORLD
@ WORLD
Definition: multibody/fwd.hpp:48
PinocchioTicToc::toc
double toc()
Definition: timer.hpp:88
Eigen
pinocchio::DataTpl
Definition: context/generic.hpp:25
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.
aba_fd
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)
Definition: timings-derivatives.cpp:74
pinocchio::DataTpl::RowMatrixXs
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Options > RowMatrixXs
Definition: multibody/data.hpp:99
aligned-vector.hpp
PINOCCHIO_EIGEN_CONST_CAST
#define PINOCCHIO_EIGEN_CONST_CAST(TYPE, OBJ)
Macro for an automatic const_cast.
Definition: eigen-macros.hpp:51
pinocchio::Convention::WORLD
@ WORLD
inverse-kinematics.i
int i
Definition: inverse-kinematics.py:17
kinematics.hpp
rnea_fd
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)
Definition: timings-derivatives.cpp:24
rnea-derivatives.hpp
setup.data
data
Definition: cmake/cython/setup.in.py:48
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::python::computeRNEADerivatives
bp::tuple computeRNEADerivatives(const context::Model &model, context::Data &data, const context::VectorXs &q, const context::VectorXs &v, const context::VectorXs &a)
Definition: expose-rnea-derivatives.cpp:38
pinocchio::python::context::JointModelFreeFlyer
JointModelFreeFlyerTpl< Scalar > JointModelFreeFlyer
Definition: bindings/python/context/generic.hpp:126
pinocchio::crba
const DataTpl< Scalar, Options, JointCollectionTpl >::MatrixXs & crba(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Convention convention=Convention::LOCAL)
Computes the upper triangular part of the joint space inertia matrix M by using the Composite Rigid B...
pinocchio::aba
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, const Convention convention=Convention::LOCAL)
The Articulated-Body algorithm. It computes the forward dynamics, aka the joint accelerations given t...
PinocchioTicToc::unitName
static std::string unitName(Unit u)
Definition: timer.hpp:58
PinocchioTicToc::DEFAULT_UNIT
Unit DEFAULT_UNIT
Definition: timer.hpp:56
pinocchio::randomConfiguration
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.
Definition: joint-configuration.hpp:315
rnea.hpp
aba-derivatives.hpp
timer.hpp
pinocchio::computeMinverse
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.
aba.hpp
anymal-simulation.model
model
Definition: anymal-simulation.py:8
forward-dynamics-derivatives.tau
tau
Definition: forward-dynamics-derivatives.py:25
SMOOTH
#define SMOOTH(s)
Definition: timer.hpp:38
pinocchio::computeForwardKinematicsDerivatives
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...
joint-configuration.hpp
pinocchio::ComputeRNEASecondOrderDerivatives
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....
reachable-workspace-with-collisions.alpha
float alpha
Definition: reachable-workspace-with-collisions.py:162
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
filename
filename
urdf.hpp
cartpole.v
v
Definition: cartpole.py:153
main
int main(int argc, const char **argv)
Definition: timings-derivatives.cpp:118
pinocchio::urdf::buildModel
ModelTpl< Scalar, Options, JointCollectionTpl > & buildModel(const std::string &filename, const typename ModelTpl< Scalar, Options, JointCollectionTpl >::JointModel &rootJoint, const std::string &rootJointName, 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...
q
q
rnea-second-order-derivatives.hpp
a
Vec3f a
PinocchioTicToc::US
@ US
Definition: timer.hpp:53
cholesky.hpp
pinocchio::cholesky::decompose
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.
pinocchio::cholesky::computeMinv
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.
pinocchio::rnea
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & rnea(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)
The Recursive Newton-Euler algorithm. It computes the inverse dynamics, aka the joint torques accordi...
pinocchio::Convention::LOCAL
@ LOCAL
kinematics-derivatives.hpp
PINOCCHIO_ALIGNED_STD_VECTOR
#define PINOCCHIO_ALIGNED_STD_VECTOR(Type)
Definition: container/aligned-vector.hpp:11
pinocchio::Tensor< Scalar, 3, Options >
pinocchio::python::computeABADerivatives
bp::tuple computeABADerivatives(const context::Model &model, context::Data &data, const context::VectorXs &q, const context::VectorXs &v, const context::VectorXs &tau)
Definition: expose-aba-derivatives.cpp:20
sample-models.hpp
pinocchio::ModelTpl
Definition: context/generic.hpp:20
cartpole.a0
a0
Definition: cartpole.py:155
crba.hpp
PinocchioTicToc
Definition: timer.hpp:47
meshcat-viewer.qs
qs
Definition: meshcat-viewer.py:128
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
PINOCCHIO_MODEL_DIR
#define PINOCCHIO_MODEL_DIR
Definition: build-reduced-model.cpp:11
PinocchioTicToc::tic
void tic()
Definition: timer.hpp:82
pinocchio::LOCAL
@ LOCAL
Definition: multibody/fwd.hpp:50


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