- 3.0.2 rigid body dynamics module.
TermTaskspacePosition.hpp
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 #ifdef CPPADCG
9 
12 
15 #include <ct/rbd/state/RBDState.h>
16 
17 
18 namespace ct {
19 namespace rbd {
20 
21 
34 template <class KINEMATICS, bool FB, size_t STATE_DIM, size_t CONTROL_DIM>
35 class TermTaskspacePosition : public optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>
36 {
37 public:
38  using SCALAR = ct::core::ADCGScalar;
39 
40  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
41 
43  TermTaskspacePosition() = delete;
44 
46  TermTaskspacePosition(size_t eeInd,
47  const Eigen::Matrix<double, 3, 3>& Q,
48  const core::StateVector<3, double>& pos_des,
49  const std::string& name = "TermTaskSpace")
50  : optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>(name),
51  eeInd_(eeInd),
52  QTaskSpace_(Q),
53  pos_ref_(pos_des)
54  {
55  // Checks whether STATE_DIM has the appropriate size.
56  // 2 * (FB * 6 + KINEMATICS::NJOINTS)) represents a floating base system with euler angles
57  // 2 * (FB * 6 + KINEMATICS::NJOINTS) + 1 representing a floating base system with quaternion angles
58  static_assert(
59  (STATE_DIM == 2 * (FB * 6 + KINEMATICS::NJOINTS)) || (STATE_DIM == 2 * (FB * 6 + KINEMATICS::NJOINTS) + 1),
60  "STATE_DIM does not have appropriate size.");
61  }
62 
64  TermTaskspacePosition(std::string& configFile, const std::string& termName, bool verbose = false)
65  {
66  loadConfigFile(configFile, termName, verbose);
67  }
68 
70  TermTaskspacePosition(const TermTaskspacePosition& arg)
71  : eeInd_(arg.eeInd_), kinematics_(KINEMATICS()), QTaskSpace_(arg.QTaskSpace_), pos_ref_(arg.pos_ref_)
72  {
73  }
74 
76  virtual ~TermTaskspacePosition() {}
78  TermTaskspacePosition<KINEMATICS, FB, STATE_DIM, CONTROL_DIM>* clone() const override
79  {
80  return new TermTaskspacePosition(*this);
81  }
82  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
83  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
84  const SCALAR& t) override
85  {
86  return evalLocal<SCALAR>(x, u, t);
87  }
88 
90  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
91  const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
92  ct::core::ADCGScalar t) override
93  {
94  return evalLocal<ct::core::ADCGScalar>(x, u, t);
95  }
96 
98  void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false) override
99  {
100  ct::optcon::loadScalarCF(filename, "eeId", eeInd_, termName);
101  ct::optcon::loadMatrixCF(filename, "Q", QTaskSpace_, termName);
102  ct::optcon::loadMatrixCF(filename, "x_des", pos_ref_, termName);
103 
104  if (verbose)
105  {
106  std::cout << "Read eeId as eeId = \n" << eeInd_ << std::endl;
107  std::cout << "Read Q as Q = \n" << QTaskSpace_ << std::endl;
108  std::cout << "Read x_des as x_des = \n" << pos_ref_.transpose() << std::endl;
109  }
110  }
111 
112 
113 private:
115  template <typename SC>
116  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t)
117  {
118  RBDState<KINEMATICS::NJOINTS, SC> rbdState = setStateFromVector<FB>(x);
119 
120  Eigen::Matrix<SC, 3, 1> xDiff =
121  kinematics_.getEEPositionInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions())
122  .toImplementation() -
123  pos_ref_.template cast<SC>();
124 
125  SC cost = (xDiff.transpose() * QTaskSpace_.template cast<SC>() * xDiff)(0, 0);
126  return cost;
127  }
128 
129 
131  template <bool T>
132  RBDState<KINEMATICS::NJOINTS, SCALAR> setStateFromVector(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
133  typename std::enable_if<T, bool>::type = true)
134  {
135  RBDState<KINEMATICS::NJOINTS, SCALAR> rbdState;
136  rbdState.fromStateVectorEulerXyz(x);
137 
138  return rbdState;
139  }
140 
142  template <bool T>
143  RBDState<KINEMATICS::NJOINTS, SCALAR> setStateFromVector(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
144  typename std::enable_if<!T, bool>::type = true)
145  {
146  RBDState<KINEMATICS::NJOINTS, SCALAR> rbdState;
147  rbdState.joints() = x;
148  return rbdState;
149  }
150 
152  size_t eeInd_;
153 
155  KINEMATICS kinematics_;
156 
158  Eigen::Matrix<double, 3, 3> QTaskSpace_;
159 
161  Eigen::Matrix<double, 3, 1> pos_ref_;
162 };
163 
164 
165 } // namespace rbd
166 } // namespace ct
167 
168 #endif
CppAD::AD< CppAD::cg::CG< double > > SCALAR
bool verbose
Definition: rbdJITtests.h:15
void loadScalarCF(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &termName="")
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")