Cheetah Software  1.0
ContactSpringDamper.cpp
Go to the documentation of this file.
1 
7 
12 template <typename T>
14  CC::_nContact = CC::_CheckContact();
15  for (size_t i(0); i < _nGC; ++i) {
16  // first assume there's no contact, so the ground "springs back"
17  deflectionRate[i] = (-K / D) * _tangentialDeflections[i];
18  }
19  if (CC::_nContact > 0) {
20  _groundContactWithOffset(K, D);
21  for (size_t i(0); i < CC::_nContact; ++i) {
22  CC::_model->_externalForces.at(CC::_model->_gcParent[CC::_idx_list[i]]) +=
23  forceToSpatialForce(CC::_cp_force_list[CC::_idx_list[i]],
24  CC::_cp_pos_list[i]);
25  }
26  static int count(0);
27  ++count;
28  if (count > 5) {
29  // exit(0);
30  }
31  }
32 
33  for (size_t i(0); i < _nGC; ++i) {
34  _tangentialDeflections[i] += dt * deflectionRate[i];
35  }
36 }
37 
41 template <typename T>
43  for (size_t i = 0; i < CC::_nContact; i++) {
44  Vec3<T> v =
45  CC::_cp_frame_list[i].transpose() *
46  CC::_model->_vGC[CC::_idx_list[i]]; // velocity in plane coordinates
47  T z = CC::_cp_penetration_list[i]; // the penetration into the ground
48  T zd = v[2]; // the penetration velocity
49  T zr = std::sqrt(std::max(T(0), -z)); // sqrt penetration into the ground,
50  // or zero if we aren't penetrating
51  T normalForce =
52  zr *
53  (-K * z - D * zd); // normal force is spring-damper * sqrt(penetration)
54 
55  // set output force to zero for now.
56  CC::_cp_local_force_list[i][0] = 0;
57  CC::_cp_local_force_list[i][1] = 0;
58  CC::_cp_local_force_list[i][2] = 0;
59 
60  if (normalForce > 0) {
61  CC::_cp_local_force_list[i][2] =
62  normalForce; // set the normal force. This is in the plane's
63  // coordinates for now
64 
65  // first, assume sticking
66  // this means the tangential deformation happens at the speed of the foot.
67  deflectionRate[CC::_idx_list[i]] = v.template topLeftCorner<2, 1>();
68  Vec2<T> tangentialSpringForce =
69  K * zr *
70  _tangentialDeflections[CC::_idx_list[i]]; // tangential force due to
71  // "spring"
72  Vec2<T> tangentialForce =
73  -tangentialSpringForce -
74  D * zr * deflectionRate[CC::_idx_list[i]]; // add damping to get
75  // total tangential
76 
77  // check for slipping:
78  T slipForce = CC::_cp_mu_list[i] *
79  normalForce; // maximum force magnitude without slipping
80  T tangentialForceMagnitude =
81  tangentialForce
82  .norm(); // actual force magnitude if we assume sticking
83  T r = tangentialForceMagnitude / slipForce; // ratio of force/max_force
84 
85  if (r > 1) {
86  // we are slipping.
87  tangentialForce =
88  tangentialForce / r; // adjust tangential force to avoid slipping
89  deflectionRate[CC::_idx_list[i]] =
90  -(tangentialForce + tangentialSpringForce) / (D * zr);
91  }
92  // set forces
93  CC::_cp_local_force_list[i][0] = tangentialForce[0];
94  CC::_cp_local_force_list[i][1] = tangentialForce[1];
95  }
96  // move back into robot frame
97  CC::_cp_force_list[CC::_idx_list[i]] =
98  CC::_cp_frame_list[i] * CC::_cp_local_force_list[i];
99  }
100 }
101 
102 template class ContactSpringDamper<double>;
103 template class ContactSpringDamper<float>;
auto forceToSpatialForce(const Eigen::MatrixBase< T > &f, const Eigen::MatrixBase< T2 > &p)
Definition: spatial.h:348
virtual void UpdateExternalForces(T K, T D, T dt)
Spring Damper based Contact Computation logic.
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
typename Eigen::Matrix< T, 2, 1 > Vec2
Definition: cppTypes.h:22
void _groundContactWithOffset(T K, T D)
Implementation of Rigid Body Floating Base model data structure.