pinocchio_gravity_compensation_dynamics_solver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020, University of Oxford
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 #include <pinocchio/algorithm/rnea.hpp>
35 
36 namespace exotica
37 {
39 {
40  // Obtain torque to compensate gravity and dynamic effects (Coriolis)
42 
43  // Commanded torque is u_nle_ + u
44  u_command_.noalias() = u_nle_ + u;
45 
49  return xdot_analytic_;
50 }
51 
53 {
54  if (x_1.size() != num_positions_ + num_velocities_ || x_2.size() != num_positions_ + num_velocities_)
55  {
56  ThrowPretty("x_1 or x_2 do not have correct size, x1=" << x_1.size() << " x2=" << x_2.size() << " expected " << num_positions_ + num_velocities_);
57  }
58 
59  Eigen::VectorXd dx(2 * num_velocities_);
61  dx.tail(num_velocities_) = x_1.tail(num_velocities_) - x_2.tail(num_velocities_);
62  return dx;
63 }
64 
65 Eigen::MatrixXd PinocchioDynamicsSolverWithGravityCompensation::dStateDelta(const StateVector& x_1, const StateVector& x_2, const ArgumentPosition first_or_second)
66 {
67  if (x_1.size() != num_positions_ + num_velocities_ || x_2.size() != num_positions_ + num_velocities_)
68  {
69  ThrowPretty("x_1 or x_2 do not have correct size, x1=" << x_1.size() << " x2=" << x_2.size() << " expected " << num_positions_ + num_velocities_);
70  }
71 
72  if (first_or_second != ArgumentPosition::ARG0 && first_or_second != ArgumentPosition::ARG1)
73  {
74  ThrowPretty("Can only take derivative w.r.t. x_1 or x_2, i.e., ARG0 or ARG1. Provided: " << first_or_second);
75  }
76 
77  Eigen::MatrixXd J = Eigen::MatrixXd::Identity(2 * num_velocities_, 2 * num_velocities_);
78 
79  if (first_or_second == ArgumentPosition::ARG0)
80  {
81  pinocchio::dDifference(model_, x_2.head(num_positions_), x_1.head(num_positions_), J.topLeftCorner(num_velocities_, num_velocities_), pinocchio::ArgumentPosition::ARG1);
82  }
83  else
84  {
85  pinocchio::dDifference(model_, x_2.head(num_positions_), x_1.head(num_positions_), J.topLeftCorner(num_velocities_, num_velocities_), pinocchio::ArgumentPosition::ARG0);
86  J.bottomRightCorner(num_velocities_, num_velocities_) *= -1.0;
87  }
88 
89  return J;
90 }
91 
93 {
94  // TODO: Create switch based on base type and normalize if the state contains a quaternion.
95 
96  const Eigen::VectorBlock<const Eigen::VectorXd> q = x.head(num_positions_);
97  const Eigen::VectorBlock<const Eigen::VectorXd> v = x.tail(num_velocities_);
98  const Eigen::VectorBlock<const Eigen::VectorXd> a = dx.tail(num_velocities_);
99 
100  switch (integrator_)
101  {
102  // Forward Euler (RK1)
103  case Integrator::RK1:
104  {
105  Eigen::VectorXd dx_times_dt = dt * dx;
106  pinocchio::integrate(model_, q, dx_times_dt.head(num_velocities_), xout.head(num_positions_));
107  xout.tail(num_velocities_) = v + dx_times_dt.tail(num_velocities_);
108  }
109  break;
110 
111  // Semi-implicit Euler
112  case Integrator::SymplecticEuler:
113  {
114  Eigen::VectorXd dx_new(get_num_state_derivative());
115  dx_new.head(num_velocities_).noalias() = dt * v + (dt * dt) * a; // v * dt + a * dt^2
116  dx_new.tail(num_velocities_).noalias() = dt * a; // a * dt
117 
118  pinocchio::integrate(model_, q, dx_new.head(num_velocities_), xout.head(num_positions_));
119  xout.tail(num_velocities_) = v + dx_new.tail(num_velocities_);
120  }
121  break;
122 
123  default:
124  ThrowPretty("Not implemented!");
125  };
126 }
127 
128 } // 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 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)
StateVector StateDelta(const StateVector &x_1, const StateVector &x_2) override
Eigen::MatrixXd dStateDelta(const StateVector &x_1, const StateVector &x_2, const ArgumentPosition first_or_second) override
void difference(const LieGroupGenericTpl< LieGroupCollection > &lg, const Eigen::MatrixBase< ConfigL_t > &q0, const Eigen::MatrixBase< ConfigR_t > &q1, const Eigen::MatrixBase< Tangent_t > &v)
void Integrate(const StateVector &x, const StateVector &dx, const double dt, StateVector &xout) override
Eigen::Matrix< T, NU, 1 > ControlVector
StateVector f(const StateVector &x, const ControlVector &u) override
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)
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & nonLinearEffects(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v)
Eigen::Matrix< T, NX, 1 > StateVector


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