PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ct_rbd/include/ct/rbd/robot/costfunction/TermTaskspacePosition.hpp

https://bitbucket.org/a_sadjadi/ct
C++ Header | 165 lines | 100 code | 30 blank | 35 comment | 4 complexity | ac38d639a1e58f2b577508b2eaa21654 MD5 | raw file
Possible License(s): EPL-1.0
  1. /**********************************************************************************************************************
  2. This file is part of the Control Toolbox (https://adrlab.bitbucket.io/ct), copyright by ETH Zurich, Google Inc.
  3. Authors: Michael Neunert, Markus Giftthaler, Markus Stäuble, Diego Pardo, Farbod Farshidian
  4. Licensed under Apache2 license (see LICENSE file in main directory)
  5. **********************************************************************************************************************/
  6. #pragma once
  7. #include <ct/optcon/costfunction/term/TermBase.hpp>
  8. #include <ct/optcon/costfunction/utility/utilities.hpp>
  9. #include <iit/rbd/traits/TraitSelector.h>
  10. #include <ct/rbd/robot/Kinematics.h>
  11. #include <ct/rbd/state/RBDState.h>
  12. namespace ct {
  13. namespace rbd {
  14. /*!
  15. * \brief A costfunction term that defines a cost on a taskspace position.
  16. * \warning This term does not consider orientations. For a full pose cost function, see TermTaskspacePose.h
  17. *
  18. * This cost function adds a quadratic penalty on the position offset of an endeffector to a desired position
  19. * @todo add velocity to term
  20. *
  21. * \tparam KINEMATICS kinematics of the system
  22. * \tparam FB true if system is a floating base robot
  23. * \tparam STATE_DIM state dimensionality of the system
  24. * \tparam CONTROL_DIM control dimensionality of the system
  25. */
  26. template <class KINEMATICS, bool FB, size_t STATE_DIM, size_t CONTROL_DIM>
  27. class TermTaskspacePosition : public optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>
  28. {
  29. public:
  30. using SCALAR = ct::core::ADCGScalar;
  31. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  32. //! the trivial constructor is explicitly forbidden
  33. TermTaskspacePosition() = delete;
  34. //! constructor
  35. TermTaskspacePosition(size_t eeInd,
  36. const Eigen::Matrix<double, 3, 3>& Q,
  37. const core::StateVector<3, double>& pos_des,
  38. const std::string& name = "TermTaskSpace")
  39. : optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>(name),
  40. eeInd_(eeInd),
  41. QTaskSpace_(Q),
  42. pos_ref_(pos_des)
  43. {
  44. // Checks whether STATE_DIM has the appropriate size.
  45. // 2 * (FB * 6 + KINEMATICS::NJOINTS)) represents a floating base system with euler angles
  46. // 2 * (FB * 6 + KINEMATICS::NJOINTS) + 1 representing a floating base system with quaternion angles
  47. static_assert(
  48. (STATE_DIM == 2 * (FB * 6 + KINEMATICS::NJOINTS)) || (STATE_DIM == 2 * (FB * 6 + KINEMATICS::NJOINTS) + 1),
  49. "STATE_DIM does not have appropriate size.");
  50. }
  51. //! construct this term with info loaded from a configuration file
  52. TermTaskspacePosition(std::string& configFile, const std::string& termName, bool verbose = false)
  53. {
  54. loadConfigFile(configFile, termName, verbose);
  55. }
  56. //! copy constructor
  57. TermTaskspacePosition(const TermTaskspacePosition& arg)
  58. : eeInd_(arg.eeInd_), kinematics_(KINEMATICS()), QTaskSpace_(arg.QTaskSpace_), pos_ref_(arg.pos_ref_)
  59. {
  60. }
  61. //! destructor
  62. virtual ~TermTaskspacePosition() {}
  63. //! deep cloning
  64. TermTaskspacePosition<KINEMATICS, FB, STATE_DIM, CONTROL_DIM>* clone() const override
  65. {
  66. return new TermTaskspacePosition(*this);
  67. }
  68. virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
  69. const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
  70. const SCALAR& t) override
  71. {
  72. return evalLocal<SCALAR>(x, u, t);
  73. }
  74. //! we overload the evaluateCppadCg method
  75. virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
  76. const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
  77. ct::core::ADCGScalar t) override
  78. {
  79. return evalLocal<ct::core::ADCGScalar>(x, u, t);
  80. }
  81. //! load term information from configuration file (stores data in member variables)
  82. void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false) override
  83. {
  84. ct::optcon::loadScalarCF(filename, "eeId", eeInd_, termName);
  85. ct::optcon::loadMatrixCF(filename, "Q", QTaskSpace_, termName);
  86. ct::optcon::loadMatrixCF(filename, "x_des", pos_ref_, termName);
  87. if (verbose)
  88. {
  89. std::cout << "Read eeId as eeId = \n" << eeInd_ << std::endl;
  90. std::cout << "Read Q as Q = \n" << QTaskSpace_ << std::endl;
  91. std::cout << "Read x_des as x_des = \n" << pos_ref_.transpose() << std::endl;
  92. }
  93. }
  94. private:
  95. //! a templated evaluate() method
  96. template <typename SC>
  97. SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t)
  98. {
  99. tpl::RBDState<KINEMATICS::NJOINTS, SC> rbdState = setStateFromVector<FB>(x);
  100. Eigen::Matrix<SC, 3, 1> xDiff =
  101. kinematics_.getEEPositionInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions())
  102. .toImplementation() -
  103. pos_ref_.template cast<SC>();
  104. SC cost = (xDiff.transpose() * QTaskSpace_.template cast<SC>() * xDiff)(0, 0);
  105. return cost;
  106. }
  107. //! computes RBDState in case the user supplied a floating-base robot
  108. template <bool T>
  109. tpl::RBDState<KINEMATICS::NJOINTS, SCALAR> setStateFromVector(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
  110. typename std::enable_if<T, bool>::type = true)
  111. {
  112. tpl::RBDState<KINEMATICS::NJOINTS, SCALAR> rbdState;
  113. rbdState.fromStateVectorEulerXyz(x);
  114. return rbdState;
  115. }
  116. //! computes RBDState in case the user supplied a fixed-base robot
  117. template <bool T>
  118. tpl::RBDState<KINEMATICS::NJOINTS, SCALAR> setStateFromVector(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
  119. typename std::enable_if<!T, bool>::type = true)
  120. {
  121. tpl::RBDState<KINEMATICS::NJOINTS, SCALAR> rbdState;
  122. rbdState.joints() = x;
  123. return rbdState;
  124. }
  125. //! index of the end-effector in question
  126. size_t eeInd_;
  127. //! the robot kinematics
  128. KINEMATICS kinematics_;
  129. //! weighting matrix for the task-space position
  130. Eigen::Matrix<double, 3, 3> QTaskSpace_;
  131. //! reference position in task-space
  132. Eigen::Matrix<double, 3, 1> pos_ref_;
  133. };
  134. } // namespace rbd
  135. } // namespace ct