- 3.0.2 rigid body dynamics module.
IKCostEvaluator.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 #include "IKRegularizerBase.h"
11 
12 namespace ct {
13 namespace rbd {
14 
15 
20 template <typename KINEMATICS, typename SCALAR = double>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  static const size_t NJOINTS = KINEMATICS::NJOINTS;
27 
28  IKCostEvaluator(size_t eeInd,
29  const Eigen::Matrix3d& Qpos = Eigen::Matrix3d::Identity(),
30  const Eigen::Matrix3d& Qrot = Eigen::Matrix3d::Identity())
31  : w_(nullptr),
32  goalCostTerm_(new ct::rbd::TermTaskspaceGeometricJacobian<KINEMATICS, NJOINTS, NJOINTS>(eeInd, Qpos, Qrot)),
33  jointRefTerm_(nullptr),
34  ikRegularizer_(nullptr)
35  {
36  }
37 
38  IKCostEvaluator(const std::string& costFunctionPath,
39  const std::string& termTaskspaceName,
40  const bool verbose = false)
41  : w_(nullptr),
42  goalCostTerm_(new ct::rbd::TermTaskspaceGeometricJacobian<KINEMATICS, NJOINTS, NJOINTS>(costFunctionPath,
43  termTaskspaceName,
44  verbose)),
45  jointRefTerm_(nullptr),
46  ikRegularizer_(nullptr)
47  {
48  }
49 
50  // additional constructor loading a joint position reference term
51  IKCostEvaluator(const std::string& costFunctionPath,
52  const std::string& termTaskspaceName,
53  const std::string& termJointPosName,
54  const bool verbose = false)
55  : w_(nullptr),
56  goalCostTerm_(new ct::rbd::TermTaskspaceGeometricJacobian<KINEMATICS, NJOINTS, NJOINTS>(costFunctionPath,
57  termTaskspaceName,
58  verbose)),
59  jointRefTerm_(
60  new ct::optcon::TermQuadratic<NJOINTS, NJOINTS, SCALAR>(costFunctionPath, termJointPosName, verbose)),
61  ikRegularizer_(nullptr)
62  {
63  }
64 
65  ~IKCostEvaluator() override = default;
66 
68  void setOptVector(std::shared_ptr<ct::optcon::tpl::OptVector<SCALAR>> optVector) { w_ = optVector; }
69 
70  // set an optional regularizer (if necessary)
71  void setRegularizer(std::shared_ptr<IKRegularizerBase> reg) { ikRegularizer_ = reg; }
72 
73  ct::rbd::RigidBodyPose getTargetPose() { return goalCostTerm_->getReferencePose(); }
74 
75  // set the target pose as rbd pose directly
77  {
78  goalCostTerm_->setReferencePosition(rbdPose.position().toImplementation());
79  goalCostTerm_->setReferenceOrientation(rbdPose.getRotationQuaternion().toImplementation());
80  }
81 
82  // set the target pose as separate position and quaternion
83  void setTargetPose(const core::StateVector<3>& w_pos_des, const Eigen::Quaterniond& w_q_des)
84  {
85  goalCostTerm_->setReferencePosition(w_pos_des);
86  goalCostTerm_->setReferenceOrientation(w_q_des);
87  }
88 
89  // set the gart pose as euler angles directly
90  void setTargetPose(const core::StateVector<3>& w_pos_des, const Eigen::Matrix3d& eulerXyz)
91  {
92  goalCostTerm_->setReferencePosition(w_pos_des);
93  goalCostTerm_->setReferenceOrientation(
94  Eigen::Quaterniond(Eigen::AngleAxisd(eulerXyz(0), Eigen::Vector3d::UnitX()) *
95  Eigen::AngleAxisd(eulerXyz(1), Eigen::Vector3d::UnitY()) *
96  Eigen::AngleAxisd(eulerXyz(2), Eigen::Vector3d::UnitZ())));
97  }
98 
100  virtual SCALAR eval() override
101  {
102  if (w_ != nullptr)
103  {
104  SCALAR val =
105  goalCostTerm_->evaluate(w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
106 
107  if (jointRefTerm_)
108  val +=
109  jointRefTerm_->evaluate(w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
110 
111  return val;
112  }
113  else
114  throw std::runtime_error("IKCostEvaluator: optimization vector not set.");
115  }
116 
118  virtual void evalGradient(size_t grad_length, Eigen::Map<Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>>& grad) override
119  {
120  assert(grad_length == NJOINTS);
121 
122  if (w_ != nullptr)
123  {
124  grad = goalCostTerm_->stateDerivative(
125  w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
126 
127  if (jointRefTerm_)
128  grad += jointRefTerm_->stateDerivative(
129  w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
130  }
131  else
132  throw std::runtime_error("IKCostEvaluator: optimization vector not set.");
133  }
134 
135 
136  // todo make lower triangular (when convenient)
138  virtual void getSparsityPatternHessian(Eigen::VectorXi& iRow, Eigen::VectorXi& jCol) override
139  {
140  iRow.resize(NJOINTS * NJOINTS);
141  jCol.resize(NJOINTS * NJOINTS);
142 
143  size_t count = 0;
144  for (size_t i = 0; i < NJOINTS; i++)
145  {
146  for (size_t j = 0; j < NJOINTS; j++)
147  {
148  iRow(count) = i;
149  jCol(count) = j;
150  count++;
151  }
152  }
153  }
154 
155 
156  // todo make lower triangular (when convenient)
158  void sparseHessianValues(const Eigen::VectorXd& jointAngles,
159  const Eigen::VectorXd& obj_fac,
160  Eigen::VectorXd& hes) override
161  {
162  hes.resize(NJOINTS * NJOINTS);
163 
164  if (w_ != nullptr)
165  {
166  // map hessian value-vector to matrix
167  Eigen::Map<Eigen::Matrix<SCALAR, NJOINTS, NJOINTS>> Hmat(hes.data(), NJOINTS, NJOINTS);
168 
169  Hmat = goalCostTerm_->stateSecondDerivative(
170  w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
171 
172  if (jointRefTerm_)
173  Hmat += jointRefTerm_->stateSecondDerivative(
174  w_->getOptimizationVars(), Eigen::Matrix<double, NJOINTS, 1>::Zero(), 0.0);
175 
176  if (ikRegularizer_)
177  Hmat += ikRegularizer_->computeRegularizer(w_->getOptimizationVars());
178 
179  Hmat *= obj_fac(0, 0);
180  }
181  else
182  throw std::runtime_error("IKCostEvaluator: optimization vector not set.");
183  }
184 
185 
186 private:
187  std::shared_ptr<ct::optcon::tpl::OptVector<SCALAR>> w_;
188 
189  std::shared_ptr<ct::rbd::TermTaskspaceGeometricJacobian<KINEMATICS, NJOINTS, NJOINTS>> goalCostTerm_;
190 
191  std::shared_ptr<ct::optcon::TermQuadratic<NJOINTS, NJOINTS, SCALAR>> jointRefTerm_;
192 
193  std::shared_ptr<IKRegularizerBase>
194  ikRegularizer_; // optional regularizer for Hessian matrix! (potentially goes away)
195 };
196 
197 } // namespace rbd
198 } // namespace ct
void setTargetPose(const core::StateVector< 3 > &w_pos_des, const Eigen::Quaterniond &w_q_des)
Definition: IKCostEvaluator.h:83
Inverse Kinematics cost evaluator for NLP.
Definition: IKCostEvaluator.h:21
void setTargetPose(const core::StateVector< 3 > &w_pos_des, const Eigen::Matrix3d &eulerXyz)
Definition: IKCostEvaluator.h:90
void sparseHessianValues(const Eigen::VectorXd &jointAngles, const Eigen::VectorXd &obj_fac, Eigen::VectorXd &hes) override
retrieve second order derivative
Definition: IKCostEvaluator.h:158
void setTargetPose(const ct::rbd::RigidBodyPose &rbdPose)
Definition: IKCostEvaluator.h:76
IKCostEvaluator(const std::string &costFunctionPath, const std::string &termTaskspaceName, const std::string &termJointPosName, const bool verbose=false)
Definition: IKCostEvaluator.h:51
const Position3Tpl & position() const
This method returns the position of the Base frame in the inertia frame.
Definition: RigidBodyPose.h:246
virtual void getSparsityPatternHessian(Eigen::VectorXi &iRow, Eigen::VectorXi &jCol) override
create sparsity pattern for the hessian (simply fill up completely)
Definition: IKCostEvaluator.h:138
A costfunction term that defines a cost on a task-space pose, for fix-base robots only...
Definition: TermTaskspaceGeometricJacobian.hpp:24
virtual SCALAR eval() override
evaluate cost function
Definition: IKCostEvaluator.h:100
CppAD::AD< CppAD::cg::CG< double > > SCALAR
bool verbose
Definition: rbdJITtests.h:15
for i
~IKCostEvaluator() override=default
double Qrot
Definition: rbdJITtests.h:28
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t NJOINTS
Definition: IKCostEvaluator.h:26
kindr::RotationQuaternion< SCALAR > getRotationQuaternion() const
This method returns the quaternion rotation.
Definition: RigidBodyPose.h:134
IKCostEvaluator(size_t eeInd, const Eigen::Matrix3d &Qpos=Eigen::Matrix3d::Identity(), const Eigen::Matrix3d &Qrot=Eigen::Matrix3d::Identity())
Definition: IKCostEvaluator.h:28
void setRegularizer(std::shared_ptr< IKRegularizerBase > reg)
Definition: IKCostEvaluator.h:71
const Eigen::Matrix< double, 3, 3 > Qpos
Definition: rbdJITtests.h:27
void setOptVector(std::shared_ptr< ct::optcon::tpl::OptVector< SCALAR >> optVector)
opt vector needs to be set by NLP solver
Definition: IKCostEvaluator.h:68
ct::rbd::RigidBodyPose getTargetPose()
Definition: IKCostEvaluator.h:73
IKCostEvaluator(const std::string &costFunctionPath, const std::string &termTaskspaceName, const bool verbose=false)
Definition: IKCostEvaluator.h:38
virtual void evalGradient(size_t grad_length, Eigen::Map< Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 >> &grad) override
retrieve first-order derivative
Definition: IKCostEvaluator.h:118