crba.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2019 CNRS INRIA
3 //
4 
5 /*
6  * Test the CRBA algorithm. The code validates both the computation times and
7  * the value by comparing the results of the CRBA with the reconstruction of
8  * the mass matrix using the RNEA.
9  * For a strong timing benchmark, see benchmark/timings.
10  *
11  */
12 
13 #include "pinocchio/multibody/model.hpp"
14 #include "pinocchio/multibody/data.hpp"
15 #include "pinocchio/algorithm/crba.hpp"
16 #include "pinocchio/algorithm/centroidal.hpp"
17 #include "pinocchio/algorithm/rnea.hpp"
18 #include "pinocchio/algorithm/jacobian.hpp"
19 #include "pinocchio/algorithm/center-of-mass.hpp"
20 #include "pinocchio/algorithm/joint-configuration.hpp"
21 #include "pinocchio/parsers/sample-models.hpp"
22 #include "pinocchio/utils/timer.hpp"
23 
24 #include <iostream>
25 
26 #include <boost/test/unit_test.hpp>
27 #include <boost/utility/binary.hpp>
28 
29 template<typename JointModel>
32  const std::string & parent_name,
33  const std::string & name,
35  bool setRandomLimits = true)
36 {
37  using namespace pinocchio;
38  typedef typename JointModel::ConfigVector_t CV;
39  typedef typename JointModel::TangentVector_t TV;
40 
42 
43  if (setRandomLimits)
44  idx = model.addJoint(model.getJointId(parent_name),joint,
45  SE3::Random(),
46  name + "_joint",
47  TV::Random() + TV::Constant(1),
48  TV::Random() + TV::Constant(1),
49  CV::Random() - CV::Constant(1),
50  CV::Random() + CV::Constant(1)
51  );
52  else
53  idx = model.addJoint(model.getJointId(parent_name),joint,
54  placement, name + "_joint");
55 
56  model.addJointFrame(idx);
57 
59  model.addBodyFrame(name + "_body", idx);
60 }
61 
62 BOOST_AUTO_TEST_SUITE ( BOOST_TEST_MODULE )
63 
64 BOOST_AUTO_TEST_CASE ( test_crba )
65 {
68  pinocchio::Data data(model);
69 
70  #ifdef NDEBUG
71  #ifdef _INTENSE_TESTING_
72  const size_t NBT = 1000*1000;
73  #else
74  const size_t NBT = 10;
75  #endif
76 
77  Eigen::VectorXd q = Eigen::VectorXd::Zero(model.nq);
78 
79  PinocchioTicToc timer(PinocchioTicToc::US); timer.tic();
80  SMOOTH(NBT)
81  {
82  crba(model,data,q);
83  }
84  timer.toc(std::cout,NBT);
85 
86  #else
87  const size_t NBT = 1;
88  using namespace Eigen;
89  using namespace pinocchio;
90 
91  Eigen::MatrixXd M(model.nv,model.nv);
92  Eigen::VectorXd q = Eigen::VectorXd::Ones(model.nq);
93  q.segment <4> (3).normalize();
94  Eigen::VectorXd v = Eigen::VectorXd::Zero(model.nv);
95  Eigen::VectorXd a = Eigen::VectorXd::Zero(model.nv);
96  data.M.fill(0); crba(model,data,q);
97  data.M.triangularView<Eigen::StrictlyLower>() = data.M.transpose().triangularView<Eigen::StrictlyLower>();
98 
99  /* Joint inertia from iterative crba. */
100  const Eigen::VectorXd bias = rnea(model,data,q,v,a);
101  for(int i=0;i<model.nv;++i)
102  {
103  M.col(i) = rnea(model,data,q,v,Eigen::VectorXd::Unit(model.nv,i)) - bias;
104  }
105 
106  // std::cout << "Mcrb = [ " << data.M << " ];" << std::endl;
107  // std::cout << "Mrne = [ " << M << " ]; " << std::endl;
108  BOOST_CHECK(M.isApprox(data.M,1e-12));
109 
110  std::cout << "(the time score in debug mode is not relevant) " ;
111 
112  q = Eigen::VectorXd::Zero(model.nq);
113 
114  PinocchioTicToc timer(PinocchioTicToc::US); timer.tic();
115  SMOOTH(NBT)
116  {
117  crba(model,data,q);
118  }
119  timer.toc(std::cout,NBT);
120 
121  #endif // ifndef NDEBUG
122 
123 }
124 
125 BOOST_AUTO_TEST_CASE(test_minimal_crba)
126 {
129  pinocchio::Data data(model), data_ref(model);
130 
131  model.lowerPositionLimit.head<7>().fill(-1.);
132  model.upperPositionLimit.head<7>().fill(1.);
133 
135  Eigen::VectorXd v(Eigen::VectorXd::Random(model.nv));
136 
137  crba(model,data_ref,q);
138  data_ref.M.triangularView<Eigen::StrictlyLower>() = data_ref.M.transpose().triangularView<Eigen::StrictlyLower>();
139 
140  crbaMinimal(model,data,q);
141  data.M.triangularView<Eigen::StrictlyLower>() = data.M.transpose().triangularView<Eigen::StrictlyLower>();
142 
143  BOOST_CHECK(data.M.isApprox(data_ref.M));
144 
145  ccrba(model,data_ref,q,v);
146  computeJointJacobians(model,data_ref,q);
147  BOOST_CHECK(data.Ag.isApprox(data_ref.Ag));
148  BOOST_CHECK(data.J.isApprox(data_ref.J));
149 
150 }
151 
152 BOOST_AUTO_TEST_SUITE_END ()
153 
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > const Eigen::MatrixBase< TangentVectorType > & v
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...
FrameIndex addJointFrame(const JointIndex &joint_index, int previous_frame_index=-1)
Add a joint to the frame tree.
Matrix6x J
Jacobian of joint placements.
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...
static InertiaTpl Random()
ConfigVectorType lowerPositionLimit
Lower joint configuration limit.
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.
data
BOOST_AUTO_TEST_CASE(test_crba)
Definition: crba.cpp:64
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > & q
void tic()
Definition: timer.hpp:63
const DataTpl< Scalar, Options, JointCollectionTpl >::Matrix6x & ccrba(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v)
Computes the Centroidal Momentum Matrix, the Composite Ridig Body Inertia as well as the centroidal m...
JointIndex addJoint(const JointIndex parent, const JointModel &joint_model, const SE3 &joint_placement, const std::string &joint_name)
Add a joint to the kinematic tree with infinite bounds.
MatrixXs M
The joint space inertia matrix (a square matrix of dim model.nv).
const DataTpl< Scalar, Options, JointCollectionTpl >::MatrixXs & crbaMinimal(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...
pinocchio::JointIndex JointIndex
#define SMOOTH(s)
Definition: timer.hpp:38
Main pinocchio namespace.
Definition: timings.cpp:30
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...
Matrix6x Ag
Centroidal Momentum Matrix.
MotionTpl< Scalar, Options > bias(const JointDataTpl< Scalar, Options, JointCollectionTpl > &jdata)
Visit a JointDataTpl through JointBiasVisitor to get the joint bias as a dense motion.
int nv
Dimension of the velocity vector space.
JointIndex getJointId(const std::string &name) const
Return the index of a joint given by its name.
static void addJointAndBody(pinocchio::Model &model, const pinocchio::JointModelBase< JointModel > &joint, const std::string &parent_name, const std::string &name, const pinocchio::SE3 placement=pinocchio::SE3::Random(), bool setRandomLimits=true)
Definition: crba.cpp:30
list a
void normalize(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &qout)
Normalize a configuration vector.
ConfigVectorType upperPositionLimit
Upper joint configuration limit.
M
void appendBodyToJoint(const JointIndex joint_index, const Inertia &Y, const SE3 &body_placement=SE3::Identity())
Append a body to a given joint of the kinematic tree.
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
JointCollectionTpl & model
int nq
Dimension of the configuration vector representation.
FrameIndex addBodyFrame(const std::string &body_name, const JointIndex &parentJoint, const SE3 &body_placement=SE3::Identity(), int previousFrame=-1)
Add a body to the frame tree.
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)


pinocchio
Author(s):
autogenerated on Tue Jun 1 2021 02:45:02