pinocchio  UNKNOWN
dynamics.hpp
1 //
2 // Copyright (c) 2016-2018 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_dynamics_hpp__
19 #define __se3_dynamics_hpp__
20 
21 #include "pinocchio/multibody/model.hpp"
22 #include "pinocchio/multibody/data.hpp"
23 #include "pinocchio/algorithm/compute-all-terms.hpp"
24 #include "pinocchio/algorithm/cholesky.hpp"
25 #include "pinocchio/algorithm/crba.hpp"
26 #include "pinocchio/algorithm/check.hpp"
27 
28 #include <Eigen/Cholesky>
29 
30 namespace se3
31 {
32 
55  inline const Eigen::VectorXd & forwardDynamics(const Model & model,
56  Data & data,
57  const Eigen::VectorXd & q,
58  const Eigen::VectorXd & v,
59  const Eigen::VectorXd & tau,
60  const Eigen::MatrixXd & J,
61  const Eigen::VectorXd & gamma,
62  const double inv_damping = 0.,
63  const bool updateKinematics = true
64  )
65  {
66  assert(q.size() == model.nq);
67  assert(v.size() == model.nv);
68  assert(tau.size() == model.nv);
69  assert(J.cols() == model.nv);
70  assert(J.rows() == gamma.size());
71  assert(model.check(data) && "data is not consistent with model.");
72 
73  Eigen::VectorXd & a = data.ddq;
74  Eigen::VectorXd & lambda_c = data.lambda_c;
75 
76  if (updateKinematics)
77  computeAllTerms(model, data, q, v);
78 
79  // Compute the UDUt decomposition of data.M
80  cholesky::decompose(model, data);
81 
82  // Compute the dynamic drift (control - nle)
83  data.torque_residual = tau - data.nle;
84  cholesky::solve(model, data, data.torque_residual);
85 
86  data.sDUiJt = J.transpose();
87  // Compute U^-1 * J.T
88  cholesky::Uiv(model, data, data.sDUiJt);
89  for(int k=0;k<model.nv;++k) data.sDUiJt.row(k) /= sqrt(data.D[k]);
90 
91  data.JMinvJt.noalias() = data.sDUiJt.transpose() * data.sDUiJt;
92 
93  data.JMinvJt.diagonal().array() += inv_damping;
94  data.llt_JMinvJt.compute(data.JMinvJt);
95 
96  // Compute the Lagrange Multipliers
97  lambda_c = -gamma -J*data.torque_residual;
98  data.llt_JMinvJt.solveInPlace (lambda_c);
99 
100  // Compute the joint acceleration
101  a = J.transpose() * lambda_c;
102  cholesky::solve (model, data, a);
103  a += data.torque_residual;
104 
105  return a;
106  }
107 
126  inline const Eigen::VectorXd & impulseDynamics(const Model & model,
127  Data & data,
128  const Eigen::VectorXd & q,
129  const Eigen::VectorXd & v_before,
130  const Eigen::MatrixXd & J,
131  const double r_coeff = 0.,
132  const bool updateKinematics = true
133  )
134  {
135  assert(q.size() == model.nq);
136  assert(v_before.size() == model.nv);
137  assert(J.cols() == model.nv);
138  assert(model.check(data) && "data is not consistent with model.");
139 
140  Eigen::VectorXd & impulse_c = data.impulse_c;
141  Eigen::VectorXd & dq_after = data.dq_after;
142 
143  // Compute the mass matrix
144  if (updateKinematics)
145  crba(model, data, q);
146 
147  // Compute the UDUt decomposition of data.M
148  cholesky::decompose(model, data);
149 
150  data.sDUiJt = J.transpose();
151  // Compute U^-1 * J.T
152  cholesky::Uiv(model, data, data.sDUiJt);
153  for(int k=0;k<model.nv;++k) data.sDUiJt.row(k) /= sqrt(data.D[k]);
154 
155  data.JMinvJt.noalias() = data.sDUiJt.transpose() * data.sDUiJt;
156  data.llt_JMinvJt.compute(data.JMinvJt);
157 
158  // Compute the Lagrange Multipliers related to the contact impulses
159  impulse_c = (-r_coeff - 1.) * (J * v_before);
160  data.llt_JMinvJt.solveInPlace (impulse_c);
161 
162  // Compute the joint velocity after impacts
163  dq_after = J.transpose() * impulse_c;
164  cholesky::solve (model, data, dq_after);
165  dq_after += v_before;
166 
167  return dq_after;
168  }
169 } // namespace se3
170 
171 
172 #endif // ifndef __se3_dynamics_hpp__
int nq
Dimension of the configuration vector representation.
Definition: model.hpp:49
int nv
Dimension of the velocity vector space.
Definition: model.hpp:52
void computeAllTerms(const Model &model, Data &data, const Eigen::VectorXd &q, const Eigen::VectorXd &v)
Computes efficiently all the terms needed for dynamic simulation. It is equivalent to the call at the...
const Eigen::MatrixXd & crba(const Model &model, Data &data, const Eigen::VectorXd &q)
Computes the upper triangular part of the joint space inertia matrix M by using the Composite Rigid B...
Definition: crba.hxx:168
Eigen::LLT< Eigen::MatrixXd > llt_JMinvJt
Cholesky decompostion of .
Definition: data.hpp:275
Eigen::VectorXd lambda_c
Lagrange Multipliers corresponding to the contact forces in se3::forwardDynamics. ...
Definition: data.hpp:278
Eigen::VectorXd D
Diagonal of the joint space intertia matrix obtained by a Cholesky Decomposition. ...
Definition: data.hpp:201
Eigen::VectorXd ddq
The joint accelerations computed from ABA.
Definition: data.hpp:161
Eigen::VectorXd torque_residual
Temporary corresponding to the residual torque .
Definition: data.hpp:284
Eigen::VectorXd impulse_c
Lagrange Multipliers corresponding to the contact impulses in se3::impulseDynamics.
Definition: data.hpp:290
Eigen::MatrixXd JMinvJt
Inverse of the operational-space inertia matrix.
Definition: data.hpp:272
Eigen::VectorXd nle
Vector of Non Linear Effects (dim model.nv). It corresponds to concatenation of the Coriolis...
Definition: data.hpp:98
const Eigen::VectorXd & forwardDynamics(const Model &model, Data &data, const Eigen::VectorXd &q, const Eigen::VectorXd &v, const Eigen::VectorXd &tau, const Eigen::MatrixXd &J, const Eigen::VectorXd &gamma, const double inv_damping=0., const bool updateKinematics=true)
Compute the forward dynamics with contact constraints.
Definition: dynamics.hpp:55
Eigen::VectorXd dq_after
Generalized velocity after impact.
Definition: data.hpp:287
const Eigen::VectorXd & impulseDynamics(const Model &model, Data &data, const Eigen::VectorXd &q, const Eigen::VectorXd &v_before, const Eigen::MatrixXd &J, const double r_coeff=0., const bool updateKinematics=true)
Compute the impulse dynamics with contact constraints.
Definition: dynamics.hpp:126
bool check(const AlgorithmCheckerBase< D > &checker=AlgorithmCheckerBase< D >()) const
Definition: model.hpp:331
Eigen::MatrixXd sDUiJt
Temporary corresponding to .
Definition: data.hpp:281