- 3.0.2 models module.
QuadrotorWithLoadFDSystem.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 
8 #include <ct/core/core.h>
9 #include <ct/rbd/rbd.h>
10 
11 namespace ct {
12 namespace rbd {
13 
14 template <class RBDDynamics, bool QUAT_INTEGRATION = false>
16  : public RBDSystem<RBDDynamics, QUAT_INTEGRATION>,
17  public core::ControlledSystem<RBDDynamics::NSTATE + QUAT_INTEGRATION, 4, typename RBDDynamics::SCALAR>
18 {
19 public:
20  using Dynamics = RBDDynamics;
21  using Kinematics = typename RBDDynamics::Kinematics_t;
22 
23  typedef typename RBDDynamics::SCALAR SCALAR;
24 
25  const static size_t STATE_DIM = RBDDynamics::NSTATE + QUAT_INTEGRATION;
26  const static size_t CONTROL_DIM = 4;
27 
29 
31 
33  : RBDSystem<RBDDynamics, QUAT_INTEGRATION>(arg),
34  core::ControlledSystem<RBDDynamics::NSTATE + QUAT_INTEGRATION, 4, typename RBDDynamics::SCALAR>(arg),
35  dynamics_(arg.dynamics_)
36  {
37  }
38 
40 
41  virtual RBDDynamics& dynamics() override { return dynamics_; }
42  virtual const RBDDynamics& dynamics() const override { return dynamics_; }
44  const core::Time& t,
47 
48  ) override
49  {
50  typename RBDDynamics::RBDState_t x = RBDStateFromVector(state);
51 
52  typename RBDDynamics::ExtLinkForces_t linkForces(Eigen::Matrix<SCALAR, 6, 1>::Zero());
53 
54  mapControlInputsToLinkForces(x, control, linkForces);
55 
56  typename RBDDynamics::RBDAcceleration_t xd;
59 
60  // introduce some light damping into the joint (friction) //! @todo tune this value
61  joint_torques = -0.0005 * x.joints().getVelocities();
62 
63  dynamics_.FloatingBaseForwardDynamics(x, joint_torques, linkForces, xd);
64 
65  derivative = toStateDerivative<QUAT_INTEGRATION>(xd, x);
66  }
67 
68 
69  void mapControlInputsToLinkForces(const typename RBDDynamics::RBDState_t& state,
71  typename RBDDynamics::ExtLinkForces_t& linkForces)
72  {
73  /* u = [overall thrust, mx, my, mz]
74  * we directly map the control inputs into quadrotor body forces
75  * */
76 
77  size_t baseLinkId = 0;
78  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(baseLinkId)].setZero();
79  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(baseLinkId)](5) =
80  control(0); // the thrust in quadrotor z-direction
81  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(baseLinkId)].template segment<3>(0) =
82  control.template segment<3>(1); // the torques
83  }
84 
85  typename RBDDynamics::RBDState_t RBDStateFromVector(const core::StateVector<STATE_DIM, SCALAR>& state)
86  {
87  return RBDStateFromVectorImpl<QUAT_INTEGRATION>(state);
88  }
89 
90  template <bool T>
91  typename RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector<STATE_DIM, SCALAR>& state,
92  typename std::enable_if<T, bool>::type = true)
93  {
94  typename RBDDynamics::RBDState_t x(RigidBodyPose::QUAT);
95  x.fromStateVectorQuaternion(state);
96  return x;
97  }
98 
99  template <bool T>
100  typename RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector<STATE_DIM, SCALAR>& state,
101  typename std::enable_if<!T, bool>::type = true)
102  {
103  typename RBDDynamics::RBDState_t x(RigidBodyPose::EULER);
104  x.fromStateVectorEulerXyz(state);
105  return x;
106  }
107 
108  template <bool T>
109  core::StateVector<STATE_DIM> toStateDerivative(const typename RBDDynamics::RBDAcceleration_t& acceleration,
110  const typename RBDDynamics::RBDState_t& state,
111  typename std::enable_if<T, bool>::type = true)
112  {
113  return acceleration.toStateUpdateVectorQuaternion(state);
114  }
115 
116  template <bool T>
117  core::StateVector<STATE_DIM> toStateDerivative(const typename RBDDynamics::RBDAcceleration_t& acceleration,
118  const typename RBDDynamics::RBDState_t& state,
119  typename std::enable_if<!T, bool>::type = true)
120  {
121  return acceleration.toStateUpdateVectorEulerXyz(state);
122  }
123 
124 
126  {
127  return new QuadrotorWithLoadFDSystem(*this);
128  }
129 
130 private:
131  RBDDynamics dynamics_;
132 };
133 }
134 }
RBDDynamics::RBDState_t RBDStateFromVector(const core::StateVector< STATE_DIM, SCALAR > &state)
Definition: QuadrotorWithLoadFDSystem.h:85
RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector< STATE_DIM, SCALAR > &state, typename std::enable_if< T, bool >::type=true)
Definition: QuadrotorWithLoadFDSystem.h:91
virtual RBDDynamics & dynamics() override
Definition: QuadrotorWithLoadFDSystem.h:41
RBDDynamics::RBDState_t RBDStateFromVectorImpl(const core::StateVector< STATE_DIM, SCALAR > &state, typename std::enable_if<!T, bool >::type=true)
Definition: QuadrotorWithLoadFDSystem.h:100
Definition: QuadrotorWithLoadFDSystem.h:15
typename RBDDynamics::Kinematics_t Kinematics
Definition: QuadrotorWithLoadFDSystem.h:21
QuadrotorWithLoadFDSystem()
Definition: QuadrotorWithLoadFDSystem.h:30
virtual ~QuadrotorWithLoadFDSystem()
Definition: QuadrotorWithLoadFDSystem.h:39
core::ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > Base
Definition: QuadrotorWithLoadFDSystem.h:28
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Definition: HyQLinearizationCodegen.cpp:14
virtual const RBDDynamics & dynamics() const override
Definition: QuadrotorWithLoadFDSystem.h:42
RBDDynamics Dynamics
Definition: QuadrotorWithLoadFDSystem.h:20
typename ct::rbd::HyA::tpl::Dynamics< SCALAR >::ExtLinkForces_t ExtLinkForces_t
Definition: HyALinearizationCodeGen.cpp:31
void computeControlledDynamics(const core::StateVector< STATE_DIM, SCALAR > &state, const core::Time &t, const core::ControlVector< CONTROL_DIM, SCALAR > &control, core::StateVector< STATE_DIM, SCALAR > &derivative) override
Definition: QuadrotorWithLoadFDSystem.h:43
ct::core::StateVector< state_dim > x
core::StateVector< STATE_DIM > toStateDerivative(const typename RBDDynamics::RBDAcceleration_t &acceleration, const typename RBDDynamics::RBDState_t &state, typename std::enable_if<!T, bool >::type=true)
Definition: QuadrotorWithLoadFDSystem.h:117
RBDDynamics::SCALAR SCALAR
Definition: QuadrotorWithLoadFDSystem.h:23
static const size_t CONTROL_DIM
Definition: QuadrotorWithLoadFDSystem.h:26
core::StateVector< STATE_DIM > toStateDerivative(const typename RBDDynamics::RBDAcceleration_t &acceleration, const typename RBDDynamics::RBDState_t &state, typename std::enable_if< T, bool >::type=true)
Definition: QuadrotorWithLoadFDSystem.h:109
virtual QuadrotorWithLoadFDSystem< RBDDynamics, QUAT_INTEGRATION > * clone() const override
Definition: QuadrotorWithLoadFDSystem.h:125
static const size_t STATE_DIM
Definition: QuadrotorWithLoadFDSystem.h:25
void mapControlInputsToLinkForces(const typename RBDDynamics::RBDState_t &state, const core::ControlVector< CONTROL_DIM, SCALAR > &control, typename RBDDynamics::ExtLinkForces_t &linkForces)
Definition: QuadrotorWithLoadFDSystem.h:69
QuadrotorWithLoadFDSystem(const QuadrotorWithLoadFDSystem &arg)
Definition: QuadrotorWithLoadFDSystem.h:32
LinkIdentifiers
Definition: declarations.h:30
double Time