Cheetah Software  1.0
DynamicsSimulator< T > Class Template Reference

#include <DynamicsSimulator.h>

+ Inheritance diagram for DynamicsSimulator< T >:
+ Collaboration diagram for DynamicsSimulator< T >:

Public Member Functions

EIGEN_MAKE_ALIGNED_OPERATOR_NEW DynamicsSimulator (FloatingBaseModel< T > &model, bool useSpringDamper=false)
 
void step (T dt, const DVec< T > &tau, T kp, T kd)
 Initialize simulator with given model. More...
 
void runABA (const DVec< T > &tau)
 Simulate forward one step. More...
 
void forwardKinematics ()
 Do forward kinematics for feet. More...
 
void integrate (T dt)
 Integrate to find new _state. More...
 
void setState (const FBModelState< T > &state)
 
void setHoming (const RobotHomingInfo< T > &homing)
 
const FBModelState< T > & getState () const
 
const FBModelStateDerivative< T > & getDState () const
 
void setAllExternalForces (const vectorAligned< SVec< T >> &forces)
 
void addCollisionPlane (T mu, T rest, T height)
 
void addCollisionBox (T mu, T rest, T depth, T width, T height, const Vec3< T > &pos, const Mat3< T > &ori)
 
void addCollisionMesh (T mu, T rest, T grid_size, const Vec3< T > &left_corner_loc, const DMat< T > &height_map)
 
size_t getNumBodies ()
 
const size_t & getTotalNumGC ()
 
const Vec3< T > & getContactForce (size_t idx)
 
const FloatingBaseModel< T > & getModel ()
 

Private Member Functions

void updateCollisions (T dt, T kp, T kd)
 

Private Attributes

FBModelState< T > _state
 Update ground collision list. More...
 
FBModelStateDerivative< T > _dstate
 
FloatingBaseModel< T > & _model
 
vector< CollisionPlane< T > > _collisionPlanes
 
ContactConstraint< T > * _contact_constr
 
SVec< T > _lastBodyVelocity
 
bool _useSpringDamper
 
RobotHomingInfo< T > _homing
 

Detailed Description

template<typename T>
class DynamicsSimulator< T >

Class (containing state) for dynamics simulation of a floating-base system

Definition at line 40 of file DynamicsSimulator.h.

Constructor & Destructor Documentation

template<typename T>
DynamicsSimulator< T >::DynamicsSimulator ( FloatingBaseModel< T > &  model,
bool  useSpringDamper = false 
)

Initialize the dynamics simulator by allocating memory for ABA matrices

Definition at line 17 of file DynamicsSimulator.cpp.

References DynamicsSimulator< T >::_contact_constr, DynamicsSimulator< T >::_dstate, DynamicsSimulator< T >::_lastBodyVelocity, DynamicsSimulator< T >::_model, DynamicsSimulator< T >::_state, and DynamicsSimulator< T >::_useSpringDamper.

19  : _model(model), _useSpringDamper(useSpringDamper) {
20  if (_useSpringDamper) {
22  } else {
24  }
25 
26  _state.bodyVelocity = SVec<T>::Zero();
27  _state.bodyPosition = Vec3<T>::Zero();
28  _state.bodyOrientation = Quat<T>::Zero();
29  _state.q = DVec<T>::Zero(_model._nDof - 6);
30  _state.qd = DVec<T>::Zero(_model._nDof - 6);
31  _dstate.qdd = DVec<T>::Zero(_model._nDof - 6);
32  _lastBodyVelocity.setZero();
33 }
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
FBModelStateDerivative< T > _dstate
typename Eigen::Matrix< T, 6, 1 > SVec
Definition: cppTypes.h:62
ContactConstraint< T > * _contact_constr
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
FloatingBaseModel< T > & _model
FBModelState< T > _state
Update ground collision list.

Member Function Documentation

template<typename T>
void DynamicsSimulator< T >::addCollisionBox ( mu,
rest,
depth,
width,
height,
const Vec3< T > &  pos,
const Mat3< T > &  ori 
)
inline

Definition at line 97 of file DynamicsSimulator.h.

98  {
99  _contact_constr->AddCollision(
100  new CollisionBox<T>(mu, rest, depth, width, height, pos, ori));
101  }
ContactConstraint< T > * _contact_constr

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::addCollisionMesh ( mu,
rest,
grid_size,
const Vec3< T > &  left_corner_loc,
const DMat< T > &  height_map 
)
inline

Definition at line 104 of file DynamicsSimulator.h.

106  {
107  _contact_constr->AddCollision(
108  new CollisionMesh<T>(mu, rest, grid_size, left_corner_loc, height_map));
109  }
ContactConstraint< T > * _contact_constr

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::addCollisionPlane ( mu,
rest,
height 
)
inline

Definition at line 92 of file DynamicsSimulator.h.

92  {
93  _contact_constr->AddCollision(new CollisionPlane<T>(mu, rest, height));
94  }
ContactConstraint< T > * _contact_constr

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::forwardKinematics ( )
inline

Do forward kinematics for feet.

Definition at line 53 of file DynamicsSimulator.h.

53 { _model.forwardKinematics(); }
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
const Vec3<T>& DynamicsSimulator< T >::getContactForce ( size_t  idx)
inline

Definition at line 114 of file DynamicsSimulator.h.

114  {
115  return _contact_constr->getGCForce(idx);
116  }
ContactConstraint< T > * _contact_constr

+ Here is the caller graph for this function:

template<typename T>
const FBModelStateDerivative<T>& DynamicsSimulator< T >::getDState ( ) const
inline

Get the most recently calculated state derivative (updated by runABA)

Returns

Definition at line 80 of file DynamicsSimulator.h.

80 { return _dstate; }
FBModelStateDerivative< T > _dstate

+ Here is the caller graph for this function:

template<typename T>
const FloatingBaseModel<T>& DynamicsSimulator< T >::getModel ( )
inline

Definition at line 118 of file DynamicsSimulator.h.

118 { return _model; }
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
size_t DynamicsSimulator< T >::getNumBodies ( )
inline

Definition at line 111 of file DynamicsSimulator.h.

111 { return _model._nDof; }
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
const FBModelState<T>& DynamicsSimulator< T >::getState ( ) const
inline

Get the state of the robot

Returns

Definition at line 74 of file DynamicsSimulator.h.

74 { return _state; }
FBModelState< T > _state
Update ground collision list.

+ Here is the caller graph for this function:

template<typename T>
const size_t& DynamicsSimulator< T >::getTotalNumGC ( )
inline

Definition at line 113 of file DynamicsSimulator.h.

113 { return _model._nGroundContact; }
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::integrate ( dt)

Integrate to find new _state.

Integrate the floating base state

Parameters
dttimestep

Definition at line 87 of file DynamicsSimulator.cpp.

References DynamicsSimulator< T >::_contact_constr, DynamicsSimulator< T >::_dstate, DynamicsSimulator< T >::_lastBodyVelocity, DynamicsSimulator< T >::_state, DynamicsSimulator< T >::_useSpringDamper, spatial::createSXform(), ori::integrateQuat(), ori::integrateQuatImplicit(), ori::quaternionToRotationMatrix(), spatial::rotationFromSXform(), and ori::X.

87  {
88  if (_useSpringDamper) {
89  Vec3<T> omegaBody = _state.bodyVelocity.template block<3, 1>(0, 0);
91  _state.bodyPosition);
93  Vec3<T> omega0 = R.transpose() * omegaBody;
94 
95  // actual integration
96  _state.qd += _dstate.qdd * dt;
97  _state.q += _state.qd * dt;
98 
99  _state.bodyVelocity += _dstate.dBodyVelocity * dt;
100  _state.bodyPosition += _dstate.dBodyPosition * dt;
101  _state.bodyOrientation = integrateQuat(_state.bodyOrientation, omega0, dt);
102  } else {
103  // actual integration
104  // Velocity Update by integrating acceleration
105  _state.qd += _dstate.qdd * dt;
106  _state.bodyVelocity += _dstate.dBodyVelocity * dt;
107 
108  // Contact Constraint Velocity Updated
109  _contact_constr->UpdateQdot(_state);
110 
111  // Prepare body velocity integration
112  RotMat<T> R_body = quaternionToRotationMatrix(_state.bodyOrientation);
113 
114  _dstate.dBodyPosition =
115  R_body.transpose() * _state.bodyVelocity.template block<3, 1>(3, 0);
116  Vec3<T> omegaBody = _state.bodyVelocity.template block<3, 1>(0, 0);
117 
118  // Position Update
119  _state.q += _state.qd * dt;
120  _state.bodyPosition += _dstate.dBodyPosition * dt;
121  _state.bodyOrientation =
122  integrateQuatImplicit(_state.bodyOrientation, omegaBody, dt);
123  _dstate.dBodyVelocity = (_state.bodyVelocity - _lastBodyVelocity) / dt;
124  _lastBodyVelocity = _state.bodyVelocity;
125  }
126 }
Quat< typename T::Scalar > integrateQuatImplicit(const Eigen::MatrixBase< T > &quat, const Eigen::MatrixBase< T2 > &omega, T3 dt)
typename Eigen::Matrix< T, 6, 6 > Mat6
Definition: cppTypes.h:70
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
FBModelStateDerivative< T > _dstate
Quat< typename T::Scalar > integrateQuat(const Eigen::MatrixBase< T > &quat, const Eigen::MatrixBase< T2 > &omega, T3 dt)
auto createSXform(const Eigen::MatrixBase< T > &R, const Eigen::MatrixBase< T2 > &r)
Definition: spatial.h:140
ContactConstraint< T > * _contact_constr
FBModelState< T > _state
Update ground collision list.
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18
auto rotationFromSXform(const Eigen::MatrixBase< T > &X)
Definition: spatial.h:157

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::runABA ( const DVec< T > &  tau)
inline

Simulate forward one step.

Find _dstate with the articulated body algorithm

Definition at line 50 of file DynamicsSimulator.h.

50 { _model.runABA(tau, _dstate); }
FBModelStateDerivative< T > _dstate
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::setAllExternalForces ( const vectorAligned< SVec< T >> &  forces)
inline

Add external forces. These are added on top of the ground reaction forces The i-th force is the spatial force applied to body i. This is cleared after each step of the simulator.

Definition at line 87 of file DynamicsSimulator.h.

87  {
88  _model._externalForces = forces;
89  }
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::setHoming ( const RobotHomingInfo< T > &  homing)
inline

Definition at line 66 of file DynamicsSimulator.h.

66  {
67  _homing = homing;
68  }
RobotHomingInfo< T > _homing

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::setState ( const FBModelState< T > &  state)
inline

Set the state of the robot being simulated

Definition at line 61 of file DynamicsSimulator.h.

61  {
62  _state = state;
63  _model.setState(state); // force recalculate dynamics
64  }
FloatingBaseModel< T > & _model
FBModelState< T > _state
Update ground collision list.

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::step ( dt,
const DVec< T > &  tau,
kp,
kd 
)

Initialize simulator with given model.

Take one simulation step

Parameters
dt: timestep duration
tau: joint torques

Definition at line 41 of file DynamicsSimulator.cpp.

References DynamicsSimulator< T >::_homing, DynamicsSimulator< T >::_model, DynamicsSimulator< T >::_state, f(), spatial::forceToSpatialForce(), DynamicsSimulator< T >::forwardKinematics(), DynamicsSimulator< T >::integrate(), ori::quatToso3(), ori::rotationMatrixToQuaternion(), ori::rpyToRotMat(), DynamicsSimulator< T >::runABA(), and DynamicsSimulator< T >::updateCollisions().

41  {
42  // fwd-kin on gc points
43  // compute ground contact forces
44  // aba
45  // integrate
46  forwardKinematics(); // compute forward kinematics
47  updateCollisions(dt, kp, kd); // process collisions
48  // Process Homing
49  if( _homing.active_flag) {
50 
51  Mat3<T> R10_des = rpyToRotMat(_homing.rpy); // R10_des
52  Mat3<T> R10_act = _model.getOrientation(5).transpose(); // R10
53  Mat3<T> eR01 = R10_des.transpose()*R10_act; // eR * R01 = R01_des
54 
55  Vec4<T> equat = rotationMatrixToQuaternion(eR01.transpose());
56  Vec3<T> angle_axis = quatToso3(equat); // in world frame
57 
58  Vec3<T> p = _model.getPosition(5);
59  Vec3<T> f = _homing.kp_lin*(_homing.position - p)-_homing.kd_lin*_model.getLinearVelocity(5);
60 
61  // Note: External forces are spatial forces in the {0} frame.
62  _model._externalForces.at(5) += forceToSpatialForce(f,p);
63  _model._externalForces.at(5).head(3) += _homing.kp_ang*angle_axis - _homing.kd_ang*_model.getAngularVelocity(5);
64 
65  }
66 
67 
68  runABA(tau); // dynamics algorithm
69  integrate(dt); // step forward
70 
71  _model.setState(_state);
72  _model.resetExternalForces(); // clear external forces
73  _model.resetCalculationFlags();
74 }
auto forceToSpatialForce(const Eigen::MatrixBase< T > &f, const Eigen::MatrixBase< T2 > &p)
Definition: spatial.h:348
void forwardKinematics()
Do forward kinematics for feet.
Mat3< typename T::Scalar > rpyToRotMat(const Eigen::MatrixBase< T > &v)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
void integrate(T dt)
Integrate to find new _state.
void updateCollisions(T dt, T kp, T kd)
RobotHomingInfo< T > _homing
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
void runABA(const DVec< T > &tau)
Simulate forward one step.
Vec3< typename T::Scalar > quatToso3(const Eigen::MatrixBase< T > &q)
FloatingBaseModel< T > & _model
FBModelState< T > _state
Update ground collision list.
MX f(const MX &x, const MX &u)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T>
void DynamicsSimulator< T >::updateCollisions ( dt,
kp,
kd 
)
private

Definition at line 77 of file DynamicsSimulator.cpp.

References DynamicsSimulator< T >::_contact_constr, and DynamicsSimulator< T >::_model.

77  {
78  _model.forwardKinematics();
79  _contact_constr->UpdateExternalForces(kp, kd, dt);
80 }
ContactConstraint< T > * _contact_constr
FloatingBaseModel< T > & _model

+ Here is the caller graph for this function:

Member Data Documentation

template<typename T>
vector<CollisionPlane<T> > DynamicsSimulator< T >::_collisionPlanes
private

Definition at line 125 of file DynamicsSimulator.h.

template<typename T>
ContactConstraint<T>* DynamicsSimulator< T >::_contact_constr
private

Definition at line 126 of file DynamicsSimulator.h.

template<typename T>
FBModelStateDerivative<T> DynamicsSimulator< T >::_dstate
private

Definition at line 123 of file DynamicsSimulator.h.

template<typename T>
RobotHomingInfo<T> DynamicsSimulator< T >::_homing
private

Definition at line 130 of file DynamicsSimulator.h.

template<typename T>
SVec<T> DynamicsSimulator< T >::_lastBodyVelocity
private

Definition at line 127 of file DynamicsSimulator.h.

template<typename T>
FloatingBaseModel<T>& DynamicsSimulator< T >::_model
private

Definition at line 124 of file DynamicsSimulator.h.

template<typename T>
FBModelState<T> DynamicsSimulator< T >::_state
private

Update ground collision list.

Definition at line 122 of file DynamicsSimulator.h.

template<typename T>
bool DynamicsSimulator< T >::_useSpringDamper
private

Definition at line 128 of file DynamicsSimulator.h.


The documentation for this class was generated from the following files: