Cheetah Software  1.0
FloatingBaseModel.h
Go to the documentation of this file.
1 
18 #ifndef LIBBIOMIMETICS_FLOATINGBASEMODEL_H
19 #define LIBBIOMIMETICS_FLOATINGBASEMODEL_H
20 
21 #include "Math/orientation_tools.h"
22 #include "SpatialInertia.h"
23 #include "spatial.h"
24 
25 #include <eigen3/Eigen/StdVector>
26 
27 #include <string>
28 #include <vector>
29 
30 using std::vector;
31 using namespace ori;
32 using namespace spatial;
33 
37 template <typename T>
38 struct FBModelState {
39  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42  SVec<T> bodyVelocity; // body coordinates
45 
46  void print() const {
47  printf("position: %.3f %.3f %.3f\n", bodyPosition[0], bodyPosition[1],
48  bodyPosition[2]);
49  }
50 };
51 
56 template <typename T>
58  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 };
63 
68 template <typename T>
70  public:
74  FloatingBaseModel() : _gravity(0, 0, -9.81) {}
76 
80  void addBase(const SpatialInertia<T>& inertia);
81 
85  void addBase(T mass, const Vec3<T>& com, const Mat3<T>& I);
86 
95  int addGroundContactPoint(int bodyID, const Vec3<T>& location,
96  bool isFoot = false);
97 
103  void addGroundContactBoxPoints(int bodyId, const Vec3<T>& dims);
104 
117  int addBody(const SpatialInertia<T>& inertia,
118  const SpatialInertia<T>& rotorInertia, T gearRatio, int parent,
119  JointType jointType, CoordinateAxis jointAxis,
120  const Mat6<T>& Xtree, const Mat6<T>& Xrot);
121 
134  int addBody(const MassProperties<T>& inertia,
135  const MassProperties<T>& rotorInertia, T gearRatio, int parent,
136  JointType jointType, CoordinateAxis jointAxis,
137  const Mat6<T>& Xtree, const Mat6<T>& Xrot);
138 
142  void check();
143 
147  T totalRotorMass();
148 
152  T totalNonRotorMass();
153 
154  const std::vector<int>& getParentVector() { return _parents; }
155 
156  const std::vector<SpatialInertia<T>,
157  Eigen::aligned_allocator<SpatialInertia<T>>>&
159  return _Ibody;
160  }
161 
162  const std::vector<SpatialInertia<T>,
163  Eigen::aligned_allocator<SpatialInertia<T>>>&
165  return _Irot;
166  }
167 
171  void setGravity(Vec3<T>& g) { _gravity = g; }
172 
173  void setContactComputeFlag(size_t gc_index, bool flag) {
174  _compute_contact_info[gc_index] = flag;
175  }
176 
177  DMat<T> invContactInertia(const int gc_index,
178  const D6Mat<T>& force_directions);
179  T invContactInertia(const int gc_index, const Vec3<T>& force_ics_at_contact);
180 
181  T applyTestForce(const int gc_index, const Vec3<T>& force_ics_at_contact,
182  FBModelStateDerivative<T>& dstate_out);
183 
184  T applyTestForce(const int gc_index, const Vec3<T>& force_ics_at_contact,
185  DVec<T>& dstate_out);
186 
187  void addDynamicsVars(int count);
188 
189  void resizeSystemMatricies();
190 
191  void setState(const FBModelState<T>& state) {
192  _state = state;
193 
194  _biasAccelerationsUpToDate = false;
195  _compositeInertiasUpToDate = false;
196 
197  resetCalculationFlags();
198  }
200  _articulatedBodiesUpToDate = false;
201  _kinematicsUpToDate = false;
202  _forcePropagatorsUpToDate = false;
203  _qddEffectsUpToDate = false;
204  _accelerationsUpToDate = false;
205  }
206 
207  void setDState(const FBModelStateDerivative<T>& dState) {
208  _dState = dState;
209  _accelerationsUpToDate = false;
210  }
211 
212  Vec3<T> getPosition(const int link_idx, const Vec3<T> & local_pos);
213  Vec3<T> getPosition(const int link_idx);
214 
215 
216  Mat3<T> getOrientation(const int link_idx);
217  Vec3<T> getLinearVelocity(const int link_idx, const Vec3<T>& point);
218  Vec3<T> getLinearVelocity(const int link_idx);
219 
220  Vec3<T> getLinearAcceleration(const int link_idx, const Vec3<T>& point);
221  Vec3<T> getLinearAcceleration(const int link_idx);
222 
223  Vec3<T> getAngularVelocity(const int link_idx);
224  Vec3<T> getAngularAcceleration(const int link_idx);
225 
226  void forwardKinematics();
227  void biasAccelerations();
228  void compositeInertias();
229  void forwardAccelerationKinematics();
230  void contactJacobians();
231 
232  DVec<T> generalizedGravityForce();
233  DVec<T> generalizedCoriolisForce();
234  DMat<T> massMatrix();
235  DVec<T> inverseDynamics(const FBModelStateDerivative<T>& dState);
236  void runABA(const DVec<T>& tau, FBModelStateDerivative<T>& dstate);
237 
238  size_t _nDof = 0;
240  vector<int> _parents;
241  vector<T> _gearRatios;
242  vector<T> _d, _u;
243 
244  vector<JointType> _jointTypes;
245  vector<CoordinateAxis> _jointAxes;
246  vector<Mat6<T>, Eigen::aligned_allocator<Mat6<T>>> _Xtree, _Xrot;
247  vector<SpatialInertia<T>, Eigen::aligned_allocator<SpatialInertia<T>>> _Ibody,
248  _Irot;
249  vector<std::string> _bodyNames;
250 
251  size_t _nGroundContact = 0;
252  vector<size_t> _gcParent;
253  vector<Vec3<T>> _gcLocation;
254  vector<uint64_t> _footIndicesGC;
255 
256  vector<Vec3<T>> _pGC;
257  vector<Vec3<T>> _vGC;
258 
259  vector<bool> _compute_contact_info;
260 
261  const DMat<T>& getMassMatrix() const { return _H; }
262  const DVec<T>& getGravityForce() const { return _G; }
263  const DVec<T>& getCoriolisForce() const { return _Cqd; }
264 
265  // void getPositionVelocity(
266  // const int & link_idx, const Vec3<T> & local_pos,
267  // Vec3<T> & link_pos, Vec3<T> & link_vel) const ;
268 
272 
273  vectorAligned<SVec<T>> _v, _vrot, _a, _arot, _avp, _avprot, _c, _crot, _S,
274  _Srot, _fvp, _fvprot, _ag, _agrot, _f, _frot;
275 
276  vectorAligned<SVec<T>> _U, _Urot, _Utot, _pA, _pArot;
278 
280  vectorAligned<Mat6<T>> _Xup, _Xa, _Xuprot, _IA, _ChiUp;
281 
282  DMat<T> _H, _C;
283  DVec<T> _Cqd, _G;
284 
287 
290 
291  bool _kinematicsUpToDate = false;
292  bool _biasAccelerationsUpToDate = false;
293  bool _accelerationsUpToDate = false;
294 
295  bool _compositeInertiasUpToDate = false;
296 
297  void updateArticulatedBodies();
298  void updateForcePropagators();
299  void udpateQddEffects();
300 
302  for (size_t i = 0; i < _nDof; i++) {
303  _externalForces[i] = SVec<T>::Zero();
304  }
305  }
306 
307  bool _articulatedBodiesUpToDate = false;
308  bool _forcePropagatorsUpToDate = false;
309  bool _qddEffectsUpToDate = false;
310 
313  Eigen::ColPivHouseholderQR<Mat6<T>> _invIA5;
314 };
315 
316 #endif // LIBBIOMIMETICS_FLOATINGBASEMODEL_H
vector< std::string > _bodyNames
Vec3< T > bodyPosition
void setDState(const FBModelStateDerivative< T > &dState)
vector< bool > _compute_contact_info
SVec< T > bodyVelocity
vector< CoordinateAxis > _jointAxes
typename Eigen::Matrix< T, 6, Eigen::Dynamic > D6Mat
Definition: cppTypes.h:110
Eigen::ColPivHouseholderQR< Mat6< T > > _invIA5
typename Eigen::Matrix< T, 6, 6 > Mat6
Definition: cppTypes.h:70
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Vec3< T > dBodyPosition
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
const std::vector< int > & getParentVector()
vectorAligned< SpatialInertia< T > > _IC
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
vectorAligned< Vec3< T > > _Jcdqd
vector< Mat6< T >, Eigen::aligned_allocator< Mat6< T > > > _Xtree
vectorAligned< SVec< T > > _externalForces
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
void setGravity(Vec3< T > &g)
const DVec< T > & getGravityForce() const
const std::vector< SpatialInertia< T >, Eigen::aligned_allocator< SpatialInertia< T > > > & getBodyInertiaVector()
vector< Vec3< T > > _vGC
const DVec< T > & getCoriolisForce() const
FBModelStateDerivative< T > _dState
typename Eigen::Matrix< T, 6, 1 > SVec
Definition: cppTypes.h:62
vector< Vec3< T > > _gcLocation
const DMat< T > & getMassMatrix() const
void print() const
vector< int > _parents
const std::vector< SpatialInertia< T >, Eigen::aligned_allocator< SpatialInertia< T > > > & getRotorInertiaVector()
vectorAligned< SVec< T > > _Utot
vector< size_t > _gcParent
Class representing spatial inertia tensors.
vectorAligned< Mat6< T > > _Xuprot
vectorAligned< SVec< T > > _vrot
JointType
Definition: spatial.h:21
void setContactComputeFlag(size_t gc_index, bool flag)
void setState(const FBModelState< T > &state)
typename std::vector< T, Eigen::aligned_allocator< T >> vectorAligned
Definition: cppTypes.h:118
vector< Vec3< T > > _pGC
vectorAligned< D6Mat< T > > _J
vector< uint64_t > _footIndicesGC
vector< SpatialInertia< T >, Eigen::aligned_allocator< SpatialInertia< T > > > _Irot
Utility functions for 3D rotations.
vectorAligned< SVec< T > > _Jdqd
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
vectorAligned< D3Mat< T > > _Jc
typename Eigen::Matrix< T, 10, 1 > MassProperties
Definition: cppTypes.h:98
FBModelState< T > _state
BEGIN ALGORITHM SUPPORT VARIABLES.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Quat< T > bodyOrientation
Utility functions for manipulating spatial quantities.
DMat< T > _qdd_from_base_accel
vector< JointType > _jointTypes