- 3.0.2 rigid body dynamics module.
EEContactModel.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
9 
10 #pragma GCC diagnostic push // include IIT headers and disable warnings
11 #pragma GCC diagnostic ignored "-Wunused-parameter"
12 #pragma GCC diagnostic ignored "-Wunused-value"
13 #include <iit/rbd/rbd.h>
16 #pragma GCC diagnostic pop
17 
18 namespace ct {
19 namespace rbd {
20 
21 
60 template <class Kinematics>
62 {
63 public:
64  static const size_t NUM_EE = Kinematics::NUM_EE;
65  static const size_t NJOINTS = Kinematics::NJOINTS;
66 
67  typedef typename Kinematics::SCALAR SCALAR;
68 
70 
71  typedef std::array<bool, NUM_EE> ActiveMap;
73  typedef std::array<EEForceLinear, NUM_EE> EEForcesLinear;
74 
75  typedef Eigen::Matrix<SCALAR, 3, 1> Vector3s;
76  typedef kindr::Position<SCALAR, 3> Position3S;
77  typedef kindr::Velocity<SCALAR, 3> Velocity3S;
78 
79 
84  {
85  NONE = 0,
86  SIGMOID = 1,
87  TANH = 2,
88  ABS = 3
89  };
90 
101  EEContactModel(const SCALAR& k = SCALAR(5000),
102  const SCALAR& d = SCALAR(500.0),
103  const SCALAR& alpha = SCALAR(100.0),
104  const SCALAR& alpha_n = SCALAR(-1.0),
105  const SCALAR& zOffset = SCALAR(0.0),
107  const std::shared_ptr<Kinematics>& kinematics = std::shared_ptr<Kinematics>(new Kinematics()))
108  : kinematics_(kinematics),
109  smoothing_(smoothing),
110  k_(k),
111  d_(d),
112  alpha_(alpha),
113  alpha_n_(alpha_n),
114  zOffset_(zOffset)
115  {
116  for (size_t i = 0; i < NUM_EE; i++)
117  EEactive_[i] = true;
118  }
119 
125  : kinematics_(other.kinematics_->clone()),
126  smoothing_(other.smoothing_),
127  k_(other.k_),
128  d_(other.d_),
129  alpha_(other.alpha_),
130  alpha_n_(other.alpha_n_),
131  zOffset_(other.zOffset_),
132  EEactive_(other.EEactive_)
133  {
134  }
135 
136  EEContactModel* clone() const { return new EEContactModel(*this); }
141  void setActiveEE(const ActiveMap& activeMap) { EEactive_ = activeMap; }
147  EEForcesLinear computeContactForces(const RBDState<NJOINTS, SCALAR>& state)
148  {
149  EEForcesLinear eeForces;
150 
151  for (size_t i = 0; i < NUM_EE; i++)
152  {
153  if (EEactive_[i])
154  {
155  Vector3s eePenetration = computePenetration(i, state.basePose(), state.jointPositions());
156 
157  if (eeInContact(eePenetration))
158  {
159  Velocity3S eeVelocity = kinematics_->getEEVelocityInWorld(i, state);
160  eeForces[i] = computeEEForce(eePenetration, eeVelocity);
161  }
162  else
163  {
164  eeForces[i].setZero();
165  }
166  }
167  }
168 
169  return eeForces;
170  }
171 
172  SCALAR& alpha() { return alpha_; }
173  SCALAR& alpha_n() { return alpha_n_; }
174  SCALAR& k() { return k_; }
175  SCALAR& d() { return d_; }
176  SCALAR& zOffset() { return zOffset_; }
177  VELOCITY_SMOOTHING& smoothing() { return smoothing_; }
178 
179 private:
185  bool eeInContact(const Vector3s& eePenetration)
186  {
187  if (smoothing_ == NONE && eePenetration(2) > 0.0)
188  return false;
189  else
190  return true;
191  }
192 
193 
201  Vector3s computePenetration(const size_t& eeId,
202  const tpl::RigidBodyPose<SCALAR>& basePose,
203  const typename JointState<NJOINTS, SCALAR>::Position& jointPosition)
204  {
205  Position3S pos = kinematics_->getEEPositionInWorld(eeId, basePose, jointPosition);
206 
207  // we currently assume flat ground at height zero penetration is only z height
208  Vector3s penetration;
209  penetration << SCALAR(0.0), SCALAR(0.0), pos.z();
210 
211  return penetration;
212  }
213 
220  EEForceLinear computeEEForce(const Vector3s& eePenetration, const Velocity3S& eeVelocity)
221  {
222  EEForceLinear eeForce;
223 
224  computeDamperForce(eeForce, eePenetration, eeVelocity);
225 
226  smoothEEForce(eeForce, eePenetration);
227 
228  computeNormalSpring(eeForce, eePenetration(2) - zOffset_, eeVelocity.toImplementation()(2));
229 
230  return eeForce;
231  }
232 
238  void smoothEEForce(EEForceLinear& eeForce, const Vector3s& eePenetration)
239  {
240  switch (smoothing_)
241  {
242  case NONE:
243  return;
244  case SIGMOID:
245  eeForce *= 1. / (1. + TRAIT::exp(eePenetration(2) * alpha_));
246  return;
247  case TANH:
248  // same as sigmoid, maybe cheaper / more expensive to compute?
249  eeForce *= 0.5 * TRAIT::tanh(-0.5 * eePenetration(2) * alpha_) + 0.5;
250  return;
251  case ABS:
252  eeForce *= 0.5 * -eePenetration(2) * alpha_ / (1. + TRAIT::fabs(-eePenetration(2) * alpha_)) + 0.5;
253  return;
254  default:
255  throw std::runtime_error("undefined smoothing function");
256  }
257  }
258 
265  void computeDamperForce(EEForceLinear& force, const Vector3s& eePenetration, const Velocity3S& eeVelocity)
266  {
267  force = -d_ * eeVelocity.toImplementation();
268  }
269 
270  void computeNormalSpring(EEForceLinear& force, const SCALAR& p_N, const SCALAR& p_dot_N)
271  {
272  if (alpha_n_ > SCALAR(0))
273  {
274  force(2) += k_ * TRAIT::exp(-alpha_n_ * p_N);
275  }
276  else if (p_N <= SCALAR(0))
277  {
278  force(2) -= k_ * p_N;
279  }
280  }
281 
282 
284  std::shared_ptr<Kinematics> kinematics_;
285 
287  VELOCITY_SMOOTHING smoothing_;
288 
289  SCALAR k_;
290  SCALAR d_;
291  SCALAR alpha_;
292  SCALAR alpha_n_;
293  SCALAR zOffset_;
294 
295  ActiveMap EEactive_;
296 };
297 } // namespace rbd
298 } // namespace ct
typename ROBCOGEN::SCALAR SCALAR
Definition: Kinematics.h:53
Kinematics::SCALAR SCALAR
Definition: EEContactModel.h:67
RigidBodyPose_t & basePose()
get base pose
Definition: RBDState.h:67
JointState< NJOINTS, SCALAR >::JointPositionBlock jointPositions()
get joint positions
Definition: RBDState.h:99
size_t eeId
Definition: rbdJITtests.h:24
Pose of a single rigid body.
Definition: RigidBodyPose.h:29
joint state and joint velocity
Definition: JointState.h:21
VELOCITY_SMOOTHING & smoothing()
Definition: EEContactModel.h:177
ct::rbd::Kinematics< RobCoGenContainer< SCALAR >, Utils< SCALAR >::N_EE > Kinematics
Definition: robcogenHelpers.h:770
joint states and base states
Definition: RBDState.h:27
SCALAR & zOffset()
Definition: EEContactModel.h:176
A soft contact model that only uses end-effector positions/velocities to compute the contact force...
Definition: EEContactModel.h:61
EEContactModel * clone() const
Definition: EEContactModel.h:136
EEContactModel(const EEContactModel &other)
Clone operator.
Definition: EEContactModel.h:124
EEForcesLinear computeContactForces(const RBDState< NJOINTS, SCALAR > &state)
Computes the contact forces given a state of the robot. Returns forces expressed in the world frame...
Definition: EEContactModel.h:147
none
Definition: EEContactModel.h:85
Vector3Tpl EEForceLinear
Definition: Kinematics.h:67
kindr::Position< SCALAR, 3 > Position3S
Definition: EEContactModel.h:76
Eigen::Matrix< SCALAR, 3, 1 > Vector3s
Definition: EEContactModel.h:75
for i
SCALAR & k()
Definition: EEContactModel.h:174
SCALAR & alpha_n()
Definition: EEContactModel.h:173
static const size_t NJOINTS
Definition: Kinematics.h:47
static const size_t NJOINTS
Definition: EEContactModel.h:65
VELOCITY_SMOOTHING
the type of velcity smoothing
Definition: EEContactModel.h:83
SCALAR & alpha()
Definition: EEContactModel.h:172
EEContactModel(const SCALAR &k=SCALAR(5000), const SCALAR &d=SCALAR(500.0), const SCALAR &alpha=SCALAR(100.0), const SCALAR &alpha_n=SCALAR(-1.0), const SCALAR &zOffset=SCALAR(0.0), const VELOCITY_SMOOTHING &smoothing=NONE, const std::shared_ptr< Kinematics > &kinematics=std::shared_ptr< Kinematics >(new Kinematics()))
Default constructor.
Definition: EEContactModel.h:101
Kinematics::EEForceLinear EEForceLinear
Definition: EEContactModel.h:72
iit::rbd::tpl::TraitSelector< SCALAR >::Trait TRAIT
Definition: EEContactModel.h:69
void setActiveEE(const ActiveMap &activeMap)
Sets which end-effectors can have forces excerted on them.
Definition: EEContactModel.h:141
static const size_t NUM_EE
Definition: EEContactModel.h:64
SCALAR & d()
Definition: EEContactModel.h:175
std::array< EEForceLinear, NUM_EE > EEForcesLinear
Definition: EEContactModel.h:73
kindr::Velocity< SCALAR, 3 > Velocity3S
Definition: EEContactModel.h:77
sigmoid
Definition: EEContactModel.h:86
std::array< bool, NUM_EE > ActiveMap
Definition: EEContactModel.h:71
abs
Definition: EEContactModel.h:88
tanh
Definition: EEContactModel.h:87
static const size_t NUM_EE
Definition: Kinematics.h:46
Definition: TraitSelector.h:16