pinocchio  2.1.3
energy.hpp
1 //
2 // Copyright (c) 2016-2018 CNRS
3 //
4 
5 #ifndef __pinocchio_energy_hpp__
6 #define __pinocchio_energy_hpp__
7 
8 #include "pinocchio/multibody/model.hpp"
9 #include "pinocchio/multibody/data.hpp"
10 #include "pinocchio/algorithm/kinematics.hpp"
11 #include "pinocchio/algorithm/check.hpp"
12 
13 namespace pinocchio {
14 
30  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ConfigVectorType, typename TangentVectorType>
31  inline Scalar
32  kineticEnergy(const ModelTpl<Scalar,Options,JointCollectionTpl> & model,
33  DataTpl<Scalar,Options,JointCollectionTpl> & data,
34  const Eigen::MatrixBase<ConfigVectorType> & q,
35  const Eigen::MatrixBase<TangentVectorType> & v,
36  const bool update_kinematics = true);
37 
51  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ConfigVectorType>
52  inline Scalar
53  potentialEnergy(const ModelTpl<Scalar,Options,JointCollectionTpl> & model,
54  DataTpl<Scalar,Options,JointCollectionTpl> & data,
55  const Eigen::MatrixBase<ConfigVectorType> & q,
56  const bool update_kinematics = true);
57 }
58 
59 /* --- Details -------------------------------------------------------------------- */
60 /* --- Details -------------------------------------------------------------------- */
61 /* --- Details -------------------------------------------------------------------- */
62 namespace pinocchio
63 {
64 
65  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ConfigVectorType, typename TangentVectorType>
66  inline Scalar
69  const Eigen::MatrixBase<ConfigVectorType> & q,
70  const Eigen::MatrixBase<TangentVectorType> & v,
71  const bool update_kinematics)
72  {
73  assert(model.check(data) && "data is not consistent with model.");
74  assert(q.size() == model.nq && "The configuration vector is not of right size");
75  assert(v.size() == model.nv && "The velocity vector is not of right size");
76 
78  typedef typename Model::JointIndex JointIndex;
79 
80  data.kinetic_energy = Scalar(0);
81 
82  if (update_kinematics)
83  forwardKinematics(model,data,q.derived(),v.derived());
84 
85  for(JointIndex i=1; i<(JointIndex)(model.njoints); ++i)
86  data.kinetic_energy += model.inertias[i].vtiv(data.v[i]);
87 
88  data.kinetic_energy *= .5;
89 
90  return data.kinetic_energy;
91  }
92 
93  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ConfigVectorType>
94  inline Scalar
97  const Eigen::MatrixBase<ConfigVectorType> & q,
98  const bool update_kinematics)
99  {
100  assert(model.check(data) && "data is not consistent with model.");
101  assert(q.size() == model.nq && "The configuration vector is not of right size");
102 
104  typedef typename Model::JointIndex JointIndex;
105  typedef typename Model::Motion Motion;
106 
107  data.potential_energy = Scalar(0);
108  const typename Motion::ConstLinearType & g = model.gravity.linear();
109 
110  if (update_kinematics)
111  forwardKinematics(model,data,q);
112 
113  typename Data::Vector3 com_global; // tmp variable
114  for(JointIndex i=1; i<(JointIndex)(model.njoints); ++i)
115  {
116  com_global.noalias() = data.oMi[i].translation() + data.oMi[i].rotation() * model.inertias[i].lever();
117  data.potential_energy -= model.inertias[i].mass() * com_global.dot(g);
118  }
119 
120  return data.potential_energy;
121  }
122 }
123 #endif // __pinocchio_energy_hpp__
Scalar kineticEnergy(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v, const bool update_kinematics=true)
Computes the kinetic energy of the system. The result is accessible through data.kinetic_energy.
Definition: energy.hpp:67
int njoints
Number of joints.
int nv
Dimension of the velocity vector space.
container::aligned_vector< SE3 > oMi
Vector of absolute joint placements (wrt the world).
Definition: data.hpp:111
Motion gravity
Spatial gravity of the model.
void forwardKinematics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Update the joint placements according to the current joint configuration.
bool check(const AlgorithmCheckerBase< D > &checker=AlgorithmCheckerBase< D >()) const
container::aligned_vector< Motion > v
Vector of joint velocities expressed at the centers of the joints.
Definition: data.hpp:91
Main pinocchio namespace.
Definition: treeview.dox:24
container::aligned_vector< Inertia > inertias
Spatial inertias of the body i expressed in the supporting joint frame i.
Scalar kinetic_energy
Kinetic energy of the model.
Definition: data.hpp:300
int nq
Dimension of the configuration vector representation.
Scalar potentialEnergy(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &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:95
Scalar potential_energy
Potential energy of the model.
Definition: data.hpp:303