- 3.0.2 rigid body dynamics module.
IDControllerFB.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 namespace ct {
9 namespace rbd {
10 
11 
12 template <class Dynamics>
14 {
15 public:
16  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
17 
18  typedef typename Dynamics::RBDState_t RBDState;
19 
20  IDControllerFB(std::shared_ptr<Dynamics> dynamics = std::shared_ptr<Dynamics>(new Dynamics()),
21  const RBDState& desState = RBDState(),
22  const Eigen::Matrix<double, 6, 1>& Kp = Eigen::Matrix<double, 6, 1>::Zero(),
23  const Eigen::Matrix<double, 6, 1>& Kd = Eigen::Matrix<double, 6, 1>::Zero())
24  : dynamics_(dynamics), desState_(desState), Kp_(Kp), Kd_(Kd)
25  {
26  }
27 
28  void setDesiredState(const RBDState& desState) { desState_ = desState; }
29  void setPoseGains(const Eigen::Matrix<double, 6, 1>& Kp) { Kp_ = Kp; }
30  void setTwistGains(const Eigen::Matrix<double, 6, 1>& Kd) { Kd_ = Kd; }
31  typename Dynamics::RBDAcceleration_t computeDesiredAcceleration(const RBDState& currBaseState)
32  {
33  // compute rotation error
34  kindr::RotationQuaternionD Bcurr_q_Bdes(
35  currBaseState.basePose().getRotationQuaternion().inverted() * desState_.basePose().getRotationQuaternion());
36  kindr::EulerAnglesXyzPD Bcurr_eul_Bdes(Bcurr_q_Bdes);
37 
38  // desired base acceleration
39  Eigen::Matrix<double, 6, 1> aDesEigen = Eigen::Matrix<double, 6, 1>::Zero();
40 
41  // base orientation error
42  aDesEigen.segment(0, 3) = Kp_.segment(0, 3).cwiseProduct(Bcurr_eul_Bdes.getUnique().toImplementation());
43 
44  // base position error
45  Eigen::Vector3d W_positionError =
46  (desState_.basePose().position() - currBaseState.basePose().position()).toImplementation();
47  Eigen::Vector3d W_weightedPosError = Kp_.segment(3, 3).cwiseProduct(W_positionError);
48  aDesEigen.segment(3, 3) = currBaseState.basePose().rotateInertiaToBase(W_weightedPosError);
49 
50  // base velocity error
51  aDesEigen +=
52  Kd_.cwiseProduct(desState_.base().velocities().getVector() - currBaseState.base().velocities().getVector());
53 
54  typename Dynamics::RBDAcceleration_t aDes;
55  aDes.setZero();
56 
57  aDes.base().fromVector6d(aDesEigen);
58 
59  return aDes;
60  }
61 
62  typename Dynamics::control_vector_t computeTorque(const RBDState& currState,
63  const typename Dynamics::EE_in_contact_t& eeInContact,
64  bool gravityCompensation = true)
65  {
67 
68  typename Dynamics::control_vector_t uId;
69 
70  if (gravityCompensation)
71  {
72  dynamics_->ProjectedInverseDynamics(eeInContact, currState, aDes, uId);
73  }
74  else
75  {
76  dynamics_->ProjectedInverseDynamicsNoGravity(eeInContact, currState, aDes, uId);
77  }
78 
79  return uId;
80  }
81 
82 
83 private:
84  std::shared_ptr<Dynamics> dynamics_;
85 
86  RBDState desState_;
87 
88  Eigen::Matrix<double, 6, 1> Kp_;
89  Eigen::Matrix<double, 6, 1> Kd_;
90 };
91 }
92 }
void setPoseGains(const Eigen::Matrix< double, 6, 1 > &Kp)
Definition: IDControllerFB.h:29
RigidBodyPose_t & basePose()
get base pose
Definition: RBDState.h:67
IDControllerFB(std::shared_ptr< Dynamics > dynamics=std::shared_ptr< Dynamics >(new Dynamics()), const RBDState &desState=RBDState(), const Eigen::Matrix< double, 6, 1 > &Kp=Eigen::Matrix< double, 6, 1 >::Zero(), const Eigen::Matrix< double, 6, 1 > &Kd=Eigen::Matrix< double, 6, 1 >::Zero())
Definition: IDControllerFB.h:20
Eigen::Matrix< SCALAR, NJOINTS, 1 > control_vector_t
Definition: Dynamics.h:62
RigidBodyState_t & base()
get base states
Definition: RBDState.h:63
Cored::Vector3 Vector3d
Definition: rbd.h:137
const Position3Tpl & position() const
This method returns the position of the Base frame in the inertia frame.
Definition: RigidBodyPose.h:246
Definition: IDControllerFB.h:13
void setTwistGains(const Eigen::Matrix< double, 6, 1 > &Kd)
Definition: IDControllerFB.h:30
RigidBodyVelocities< SCALAR > & velocities()
return the rigid body velocities
Definition: RigidBodyState.h:42
joint states and base states
Definition: RBDState.h:27
Vector3s rotateInertiaToBase(const Vector3s &vector) const
This methods rotates a 3D vector expressed in Inertia frame to Base Frame.
Definition: RigidBodyPose.h:339
void setDesiredState(const RBDState &desState)
Definition: IDControllerFB.h:28
Dynamics::control_vector_t computeTorque(const RBDState &currState, const typename Dynamics::EE_in_contact_t &eeInContact, bool gravityCompensation=true)
Definition: IDControllerFB.h:62
ct::rbd::Dynamics< RobCoGenContainer< SCALAR >, Utils< SCALAR >::N_EE > Dynamics
Definition: robcogenHelpers.h:773
joint acceleration and base acceleration
Definition: RBDAcceleration.h:23
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Dynamics::RBDState_t RBDState
Definition: IDControllerFB.h:18
kindr::RotationQuaternion< SCALAR > getRotationQuaternion() const
This method returns the quaternion rotation.
Definition: RigidBodyPose.h:134
void setZero()
Definition: RBDAcceleration.h:95
Dynamics::RBDAcceleration_t computeDesiredAcceleration(const RBDState &currBaseState)
Definition: IDControllerFB.h:31
RigidBodyAcceleration_t & base()
get base acceleration
Definition: RBDAcceleration.h:48
void fromVector6d(const Eigen::Matrix< SCALAR, 6, 1 > &in)
Definition: RigidBodyAcceleration.h:35