pinocchio_dynamics_solver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019, Wolfgang Merkt
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
31 
32 #include <pinocchio/algorithm/aba.hpp>
33 #include <pinocchio/algorithm/joint-configuration.hpp>
34 
36 
37 namespace exotica
38 {
39 Eigen::VectorXd PinocchioDynamicsSolver::f(const StateVector& x, const ControlVector& u)
40 {
41  // TODO: THIS DOES NOT WORK FOR A FLOATING BASE YET!!
42  pinocchio::aba(model_, *pinocchio_data_.get(), x.head(num_positions_), x.tail(num_velocities_), u);
43  xdot_analytic_.head(num_velocities_) = x.tail(num_velocities_);
44  xdot_analytic_.tail(num_velocities_) = pinocchio_data_->ddq;
45  return xdot_analytic_;
46 }
47 
48 Eigen::VectorXd PinocchioDynamicsSolver::StateDelta(const StateVector& x_1, const StateVector& x_2)
49 {
50  if (x_1.size() != num_positions_ + num_velocities_ || x_2.size() != num_positions_ + num_velocities_)
51  {
52  ThrowPretty("x_1 or x_2 do not have correct size, x1=" << x_1.size() << " x2=" << x_2.size() << " expected " << num_positions_ + num_velocities_);
53  }
54 
55  Eigen::VectorXd dx(2 * num_velocities_);
56  pinocchio::difference(model_, x_2.head(num_positions_), x_1.head(num_positions_), dx.head(num_velocities_));
57  dx.tail(num_velocities_) = x_1.tail(num_velocities_) - x_2.tail(num_velocities_);
58  return dx;
59 }
60 
61 Eigen::MatrixXd PinocchioDynamicsSolver::dStateDelta(const StateVector& x_1, const StateVector& x_2, const ArgumentPosition first_or_second)
62 {
63  if (x_1.size() != num_positions_ + num_velocities_ || x_2.size() != num_positions_ + num_velocities_)
64  {
65  ThrowPretty("x_1 or x_2 do not have correct size, x1=" << x_1.size() << " x2=" << x_2.size() << " expected " << num_positions_ + num_velocities_);
66  }
67 
68  if (first_or_second != ArgumentPosition::ARG0 && first_or_second != ArgumentPosition::ARG1)
69  {
70  ThrowPretty("Can only take derivative w.r.t. x_1 or x_2, i.e., ARG0 or ARG1. Provided: " << first_or_second);
71  }
72 
73  Eigen::MatrixXd J = Eigen::MatrixXd::Identity(2 * num_velocities_, 2 * num_velocities_);
74 
75  if (first_or_second == ArgumentPosition::ARG0)
76  {
77  pinocchio::dDifference(model_, x_2.head(num_positions_), x_1.head(num_positions_), J.topLeftCorner(num_velocities_, num_velocities_), pinocchio::ArgumentPosition::ARG1);
78  }
79  else
80  {
81  pinocchio::dDifference(model_, x_2.head(num_positions_), x_1.head(num_positions_), J.topLeftCorner(num_velocities_, num_velocities_), pinocchio::ArgumentPosition::ARG0);
82  J.bottomRightCorner(num_velocities_, num_velocities_) *= -1.0;
83  }
84 
85  return J;
86 }
87 
88 void PinocchioDynamicsSolver::Integrate(const StateVector& x, const StateVector& dx, const double dt, StateVector& xout)
89 {
90  // TODO: Create switch based on base type and normalize if the state contains a quaternion.
91 
92  const Eigen::VectorBlock<const Eigen::VectorXd> q = x.head(num_positions_);
93  const Eigen::VectorBlock<const Eigen::VectorXd> v = x.tail(num_velocities_);
94  const Eigen::VectorBlock<const Eigen::VectorXd> a = dx.tail(num_velocities_);
95 
96  switch (integrator_)
97  {
98  // Forward Euler (RK1)
99  case Integrator::RK1:
100  {
101  Eigen::VectorXd dx_times_dt = dt * dx;
102  pinocchio::integrate(model_, q, dx_times_dt.head(num_velocities_), xout.head(num_positions_));
103  xout.tail(num_velocities_) = v + dx_times_dt.tail(num_velocities_);
104  }
105  break;
106 
107  // Semi-implicit Euler
108  case Integrator::SymplecticEuler:
109  {
110  Eigen::VectorXd dx_new(get_num_state_derivative());
111  dx_new.head(num_velocities_).noalias() = dt * v + (dt * dt) * a; // v * dt + a * dt^2
112  dx_new.tail(num_velocities_).noalias() = dt * a; // a * dt
113 
114  pinocchio::integrate(model_, q, dx_new.head(num_velocities_), xout.head(num_positions_));
115  xout.tail(num_velocities_) = v + dx_new.tail(num_velocities_);
116  }
117  break;
118 
119  default:
120  ThrowPretty("Not implemented!");
121  };
122 }
123 } // namespace exotica
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)
J
q
#define REGISTER_DYNAMICS_SOLVER_TYPE(TYPE, DERIV)
#define ThrowPretty(m)
v
void integrate(const LieGroupGenericTpl< LieGroupCollection > &lg, const Eigen::MatrixBase< ConfigIn_t > &q, const Eigen::MatrixBase< Tangent_t > &v, const Eigen::MatrixBase< ConfigOut_t > &qout)
void difference(const LieGroupGenericTpl< LieGroupCollection > &lg, const Eigen::MatrixBase< ConfigL_t > &q0, const Eigen::MatrixBase< ConfigR_t > &q1, const Eigen::MatrixBase< Tangent_t > &v)
Eigen::Matrix< T, NU, 1 > ControlVector
list a
void dDifference(const LieGroupGenericTpl< LieGroupCollection > &lg, const Eigen::MatrixBase< ConfigL_t > &q0, const Eigen::MatrixBase< ConfigR_t > &q1, const Eigen::MatrixBase< JacobianOut_t > &J, const ArgumentPosition arg)
Eigen::Matrix< T, NX, 1 > StateVector


exotica_pinocchio_dynamics_solver
Author(s):
autogenerated on Sat Apr 10 2021 02:35:54