pinocchio  UNKNOWN
energy.hpp
1 //
2 // Copyright (c) 2016-2017 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_energy_hpp__
19 #define __se3_energy_hpp__
20 
21 #include "pinocchio/multibody/model.hpp"
22 #include "pinocchio/multibody/data.hpp"
23 #include "pinocchio/algorithm/kinematics.hpp"
24 #include "pinocchio/algorithm/check.hpp"
25 
26 namespace se3 {
27 
39  inline double
40  kineticEnergy(const Model & model,
41  Data & data,
42  const Eigen::VectorXd & q,
43  const Eigen::VectorXd & v,
44  const bool update_kinematics = true);
45 
56  inline double
57  potentialEnergy(const Model & model,
58  Data & data,
59  const Eigen::VectorXd & q,
60  const bool update_kinematics = true);
61 }
62 
63 /* --- Details -------------------------------------------------------------------- */
64 /* --- Details -------------------------------------------------------------------- */
65 /* --- Details -------------------------------------------------------------------- */
66 namespace se3
67 {
68 
69  inline double
70  kineticEnergy(const Model & model,
71  Data & data,
72  const Eigen::VectorXd & q,
73  const Eigen::VectorXd & v,
74  const bool update_kinematics)
75  {
76  assert(model.check(data) && "data is not consistent with model.");
77 
78  data.kinetic_energy = 0.;
79 
80  if (update_kinematics)
81  forwardKinematics(model,data,q,v);
82 
83  for(Model::JointIndex i=1;i<(Model::JointIndex)(model.njoints);++i)
84  data.kinetic_energy += model.inertias[i].vtiv(data.v[i]);
85 
86  data.kinetic_energy *= .5;
87  return data.kinetic_energy;
88  }
89 
90  inline double
91  potentialEnergy(const Model & model,
92  Data & data,
93  const Eigen::VectorXd & q,
94  const bool update_kinematics)
95  {
96  assert(model.check(data) && "data is not consistent with model.");
97 
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  return data.potential_energy;
112  }
113 }
114 #endif // __se3_energy_hpp__
double potential_energy
Potential energy of the model.
Definition: data.hpp:267
double potentialEnergy(const Model &model, Data &data, const Eigen::VectorXd &q, const bool update_kinematics=true)
Computes the potential energy of the system, i.e. the potential energy linked to the gravity field...
Definition: energy.hpp:91
double kineticEnergy(const Model &model, Data &data, const Eigen::VectorXd &q, const Eigen::VectorXd &v, const bool update_kinematics=true)
Computes the kinetic energy of the system. The result is accessible through data.kinetic_energy.
Definition: energy.hpp:70
double kinetic_energy
Kinetic energy of the model.
Definition: data.hpp:264
Motion gravity
Spatial gravity of the model.
Definition: model.hpp:106
void forwardKinematics(const Model &model, Data &data, const Eigen::VectorXd &q)
Update the joint placements according to the current joint configuration.
Definition: kinematics.hxx:106
container::aligned_vector< SE3 > oMi
Vector of absolute joint placements (wrt the world).
Definition: data.hpp:87
bool check(const AlgorithmCheckerBase< D > &checker=AlgorithmCheckerBase< D >()) const
Definition: model.hpp:331
container::aligned_vector< Inertia > inertias
Spatial inertias of the body i expressed in the supporting joint frame i.
Definition: model.hpp:64
container::aligned_vector< Motion > v
Vector of joint velocities expressed at the centers of the joints.
Definition: data.hpp:67
int njoints
Number of joints.
Definition: model.hpp:55