- 3.0.2 rigid body dynamics module.
FloatingBaseFDSystem.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 
10 
11 #include "RBDSystem.h"
12 
13 namespace ct {
14 namespace rbd {
15 
20 template <class RBDDynamics, bool QUAT_INTEGRATION = false, bool EE_ARE_CONTROL_INPUTS = false>
21 class FloatingBaseFDSystem : public RBDSystem<RBDDynamics, QUAT_INTEGRATION>,
22  public core::SymplecticSystem<RBDDynamics::NSTATE / 2 + QUAT_INTEGRATION,
23  RBDDynamics::NSTATE / 2,
24  RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * RBDDynamics::N_EE * 3,
25  typename RBDDynamics::SCALAR>
26 {
27 public:
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29 
30  using Dynamics = RBDDynamics;
32 
33  typedef typename RBDDynamics::SCALAR SCALAR;
34 
35  const static size_t N_EE = RBDDynamics::N_EE;
36  const static size_t STATE_DIM = RBDDynamics::NSTATE + QUAT_INTEGRATION;
37  const static size_t CONTROL_DIM = RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * N_EE * 3;
38 
41 
42  typedef core::
43  SymplecticSystem<RBDDynamics::NSTATE / 2 + QUAT_INTEGRATION, RBDDynamics::NSTATE / 2, CONTROL_DIM, SCALAR>
45 
47 
48  FloatingBaseFDSystem() : Base(), dynamics_(), eeContactModel_(nullptr) {}
50  : Base(other), dynamics_(other.dynamics_), eeContactModel_(other.eeContactModel_->clone())
51  {
52  }
53 
54  virtual ~FloatingBaseFDSystem() {}
56  {
58  }
59 
60  virtual RBDDynamics& dynamics() override { return dynamics_; }
61  virtual const RBDDynamics& dynamics() const override { return dynamics_; }
62  void setContactModel(const std::shared_ptr<ContactModel>& contactModel) { eeContactModel_ = contactModel; }
63  virtual void computePdot(const StateVector& x,
65  const ControlVector& control,
67  {
68  StateVector xLocal = x;
69 
70  xLocal.tail(RBDDynamics::NSTATE / 2) = v;
71 
72  typename RBDDynamics::RBDState_t rbdCached = RBDStateFromVector(xLocal);
73 
74  typename RBDDynamics::RBDAcceleration_t xd;
75 
76  pDot = toStateDerivative<QUAT_INTEGRATION>(xd, rbdCached).head(RBDDynamics::NSTATE / 2 + QUAT_INTEGRATION);
77  }
78 
79  virtual void computeVdot(const StateVector& x,
81  const ControlVector& control,
83  {
84  StateVector xLocal = x;
85  xLocal.head(RBDDynamics::NSTATE / 2 + QUAT_INTEGRATION) = p;
86 
87  // Cache updated rbd state
88  typename RBDDynamics::RBDState_t rbdCached = RBDStateFromVector(xLocal);
89  typename RBDDynamics::ExtLinkForces_t linkForces(Eigen::Matrix<SCALAR, 6, 1>::Zero());
90 
91  std::array<typename Kinematics::EEForceLinear, N_EE> eeForcesW;
92  eeForcesW.fill(Kinematics::EEForceLinear::Zero());
93 
94  if (eeContactModel_)
95  eeForcesW = eeContactModel_->computeContactForces(rbdCached);
96 
97  if (EE_ARE_CONTROL_INPUTS)
98  for (size_t i = 0; i < N_EE; i++)
99  eeForcesW[i] += control.template segment<3>(RBDDynamics::NJOINTS + i * 3);
100 
101  mapEndeffectorForcesToLinkForces(rbdCached, eeForcesW, linkForces);
102 
103  typename RBDDynamics::RBDAcceleration_t xd;
104 
105  dynamics_.FloatingBaseForwardDynamics(rbdCached, control.template head<RBDDynamics::NJOINTS>(), linkForces, xd);
106 
107  vDot = toStateDerivative<QUAT_INTEGRATION>(xd, rbdCached).tail(RBDDynamics::NSTATE / 2);
108  }
109 
117  void mapEndeffectorForcesToLinkForces(const typename RBDDynamics::RBDState_t& state,
118  const std::array<typename Kinematics::EEForceLinear, N_EE>& eeForcesW,
119  typename RBDDynamics::ExtLinkForces_t& linkForces)
120  {
121  for (size_t i = 0; i < N_EE; i++)
122  {
123  auto endEffector = dynamics_.kinematics().getEndEffector(i);
124  size_t linkId = endEffector.getLinkId();
125  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(linkId)] =
126  dynamics_.kinematics().mapForceFromWorldToLink3d(
127  eeForcesW[i], state.basePose(), state.jointPositions(), i);
128  }
129  }
130 
131  typename RBDDynamics::RBDState_t RBDStateFromVector(const core::StateVector<STATE_DIM, SCALAR>& state)
132  {
133  return RBDStateFromVectorImpl<QUAT_INTEGRATION>(state);
134  }
135 
136  template <bool T>
137  typename RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector<STATE_DIM, SCALAR>& state,
138  typename std::enable_if<T, bool>::type = true)
139  {
140  typename RBDDynamics::RBDState_t x(tpl::RigidBodyPose<SCALAR>::QUAT);
141  x.fromStateVectorQuaternion(state);
142  return x;
143  }
144 
145  template <bool T>
146  typename RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector<STATE_DIM, SCALAR>& state,
147  typename std::enable_if<!T, bool>::type = true)
148  {
149  typename RBDDynamics::RBDState_t x(tpl::RigidBodyPose<SCALAR>::EULER);
150  x.fromStateVectorEulerXyz(state);
151  return x;
152  }
153 
154  template <bool T>
155  core::StateVector<STATE_DIM, SCALAR> toStateDerivative(const typename RBDDynamics::RBDAcceleration_t& acceleration,
156  const typename RBDDynamics::RBDState_t& state,
157  typename std::enable_if<T, bool>::type = true)
158  {
159  return acceleration.toStateUpdateVectorQuaternion(state);
160  }
161 
162  template <bool T>
163  core::StateVector<STATE_DIM, SCALAR> toStateDerivative(const typename RBDDynamics::RBDAcceleration_t& acceleration,
164  const typename RBDDynamics::RBDState_t& state,
165  typename std::enable_if<!T, bool>::type = true)
166  {
167  return acceleration.toStateUpdateVectorEulerXyz(state);
168  }
169 
170  template <bool T>
171  core::StateVector<STATE_DIM, SCALAR> toStateVector(const typename RBDDynamics::RBDState_t& state,
172  typename std::enable_if<T, bool>::type = true)
173  {
174  return state.toStateVectorQuaternion();
175  }
176 
177  template <bool T>
178  core::StateVector<STATE_DIM, SCALAR> toStateVector(const typename RBDDynamics::RBDState_t& state,
179  typename std::enable_if<!T, bool>::type = true)
180  {
181  return state.toStateVectorEulerXyz();
182  }
183 
184 private:
185  RBDDynamics dynamics_;
186  std::shared_ptr<ContactModel> eeContactModel_;
187 };
188 
189 } // namespace rbd
190 } // namespace ct
typename RBDDynamics::Kinematics_t Kinematics
Definition: FloatingBaseFDSystem.h:31
static const size_t CONTROL_DIM
Definition: FloatingBaseFDSystem.h:37
core::StateVector< STATE_DIM, SCALAR > toStateVector(const typename RBDDynamics::RBDState_t &state, typename std::enable_if<!T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:178
virtual FloatingBaseFDSystem< RBDDynamics, QUAT_INTEGRATION, EE_ARE_CONTROL_INPUTS > * clone() const override
Definition: FloatingBaseFDSystem.h:55
Pose of a single rigid body.
Definition: RigidBodyPose.h:29
FloatingBaseFDSystem()
Definition: FloatingBaseFDSystem.h:48
core::SymplecticSystem< RBDDynamics::NSTATE/2+QUAT_INTEGRATION, RBDDynamics::NSTATE/2, CONTROL_DIM, SCALAR > Base
Definition: FloatingBaseFDSystem.h:44
core::ControlVector< CONTROL_DIM, SCALAR > ControlVector
Definition: FloatingBaseFDSystem.h:40
RBDDynamics::SCALAR SCALAR
Definition: FloatingBaseFDSystem.h:33
void setContactModel(const std::shared_ptr< ContactModel > &contactModel)
Definition: FloatingBaseFDSystem.h:62
A soft contact model that only uses end-effector positions/velocities to compute the contact force...
Definition: EEContactModel.h:61
FloatingBaseFDSystem(const FloatingBaseFDSystem< RBDDynamics, QUAT_INTEGRATION, EE_ARE_CONTROL_INPUTS > &other)
Definition: FloatingBaseFDSystem.h:49
A floating base rigid body system that uses forward dynamics. The input vector is assumed to consist ...
Definition: FloatingBaseFDSystem.h:21
virtual void computePdot(const StateVector &x, const core::StateVector< RBDDynamics::NSTATE/2, SCALAR > &v, const ControlVector &control, core::StateVector< RBDDynamics::NSTATE/2+QUAT_INTEGRATION, SCALAR > &pDot) override
Definition: FloatingBaseFDSystem.h:63
core::StateVector< STATE_DIM, SCALAR > toStateDerivative(const typename RBDDynamics::RBDAcceleration_t &acceleration, const typename RBDDynamics::RBDState_t &state, typename std::enable_if< T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:155
ct::rbd::TestIrb4600::tpl::Kinematics< double > Kinematics_t
Definition: FixBaseIKTest.cpp:14
virtual const RBDDynamics & dynamics() const override
Definition: FloatingBaseFDSystem.h:61
LinkIdentifiers
Definition: declarations.h:40
This is a common interface class for an RBDSystem.
Definition: RBDSystem.h:15
for i
RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector< STATE_DIM, SCALAR > &state, typename std::enable_if<!T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:146
ct::core::StateVector< state_dim > x
virtual ~FloatingBaseFDSystem()
Definition: FloatingBaseFDSystem.h:54
static const size_t STATE_DIM
Definition: FloatingBaseFDSystem.h:36
RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector< STATE_DIM, SCALAR > &state, typename std::enable_if< T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:137
core::StateVector< STATE_DIM, SCALAR > toStateDerivative(const typename RBDDynamics::RBDAcceleration_t &acceleration, const typename RBDDynamics::RBDState_t &state, typename std::enable_if<!T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:163
RBDDynamics Dynamics
Definition: FloatingBaseFDSystem.h:30
virtual RBDDynamics & dynamics() override
Definition: FloatingBaseFDSystem.h:60
virtual void computeVdot(const StateVector &x, const core::StateVector< RBDDynamics::NSTATE/2+QUAT_INTEGRATION, SCALAR > &p, const ControlVector &control, core::StateVector< RBDDynamics::NSTATE/2, SCALAR > &vDot) override
Definition: FloatingBaseFDSystem.h:79
core::StateVector< STATE_DIM, SCALAR > toStateVector(const typename RBDDynamics::RBDState_t &state, typename std::enable_if< T, bool >::type=true)
Definition: FloatingBaseFDSystem.h:171
core::StateVector< STATE_DIM, SCALAR > StateVector
Definition: FloatingBaseFDSystem.h:39
static const size_t N_EE
Definition: FloatingBaseFDSystem.h:35
void mapEndeffectorForcesToLinkForces(const typename RBDDynamics::RBDState_t &state, const std::array< typename Kinematics::EEForceLinear, N_EE > &eeForcesW, typename RBDDynamics::ExtLinkForces_t &linkForces)
Definition: FloatingBaseFDSystem.h:117
RBDDynamics::RBDState_t RBDStateFromVector(const core::StateVector< STATE_DIM, SCALAR > &state)
Definition: FloatingBaseFDSystem.h:131