energy.hpp 3.91 KB
Newer Older
1
//
2
// Copyright (c) 2016-2017 CNRS
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// This file is part of Pinocchio
// Pinocchio is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// Pinocchio is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// Pinocchio If not, see
// <http://www.gnu.org/licenses/>.

#ifndef __se3_energy_hpp__
#define __se3_energy_hpp__

#include "pinocchio/multibody/model.hpp"
jcarpent's avatar
jcarpent committed
22
#include "pinocchio/multibody/data.hpp"
23
#include "pinocchio/algorithm/kinematics.hpp"
24
#include "pinocchio/algorithm/check.hpp"
25
26

namespace se3 {
27
28
29
  
  ///
  /// \brief Computes the kinetic energy of the system.
Valenza Florian's avatar
Valenza Florian committed
30
  ///        The result is accessible through data.kinetic_energy.
31
32
33
34
35
36
37
38
  ///
  /// \param[in] model The model structure of the rigid body system.
  /// \param[in] data The data structure of the rigid body system.
  /// \param[in] q The joint configuration vector (dim model.nq).
  /// \param[in] v The joint velocity vector (dim model.nv).
  ///
  /// \return The kinetic energy of the system in [J].
  ///
39
40
41
42
43
44
  inline double
  kineticEnergy(const Model & model,
                Data & data,
                const Eigen::VectorXd & q,
                const Eigen::VectorXd & v,
                const bool update_kinematics = true);
45
46
47
  
  ///
  /// \brief Computes the potential energy of the system, i.e. the potential energy linked to the gravity field.
Valenza Florian's avatar
Valenza Florian committed
48
  ///        The result is accessible through data.potential_energy.
49
50
51
52
53
54
55
56
57
58
59
60
  ///
  /// \param[in] model The model structure of the rigid body system.
  /// \param[in] data The data structure of the rigid body system.
  /// \param[in] q The joint configuration vector (dim model.nq).
  ///
  /// \return The potential energy of the system in [J].
  ///
  inline double
  potentialEnergy(const Model & model,
                  Data & data,
                  const Eigen::VectorXd & q,
                  const bool update_kinematics = true);
61
62
}

63
64
/* --- Details -------------------------------------------------------------------- */
/* --- Details -------------------------------------------------------------------- */
65
66
67
68
69
70
71
72
73
74
75
/* --- Details -------------------------------------------------------------------- */
namespace se3
{
   
  inline double
  kineticEnergy(const Model & model,
                Data & data,
                const Eigen::VectorXd & q,
                const Eigen::VectorXd & v,
                const bool update_kinematics)
  {
76
77
    assert(model.check(data) && "data is not consistent with model.");
    
78
79
80
    data.kinetic_energy = 0.;
    
    if (update_kinematics)
81
      forwardKinematics(model,data,q,v);
82
    
83
    for(Model::JointIndex i=1;i<(Model::JointIndex)(model.njoints);++i)
84
85
86
87
88
      data.kinetic_energy += model.inertias[i].vtiv(data.v[i]);
    
    data.kinetic_energy *= .5;
    return data.kinetic_energy;
  }
89
90
91
92
93
94
95
  
  inline double
  potentialEnergy(const Model & model,
                  Data & data,
                  const Eigen::VectorXd & q,
                  const bool update_kinematics)
  {
96
97
    assert(model.check(data) && "data is not consistent with model.");
    
98
    data.potential_energy = 0.;
99
    const Motion::ConstLinearType & g = model.gravity.linear();
100
    SE3::Vector3 com_global;
101
102
    
    if (update_kinematics)
103
      forwardKinematics(model,data,q);
104
    
105
    for(Model::JointIndex i=1;i<(Model::JointIndex)(model.njoints);++i)
106
107
    {
      com_global = data.oMi[i].translation() + data.oMi[i].rotation() * model.inertias[i].lever();
108
      data.potential_energy -= model.inertias[i].mass() * com_global.dot(g);
109
    }
110
111
112
    
    return data.potential_energy;
  }
113
114
}
#endif // __se3_energy_hpp__