- 3.0.2 rigid body dynamics module.
IKNLP.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/optcon/nlp/Nlp>
9 
10 #include "IKCostEvaluator.h"
11 #include "IKConstraintContainer.h"
12 
13 namespace ct {
14 namespace rbd {
15 
16 template <typename KINEMATICS, typename SCALAR = double>
17 class IKNLP final : public ct::optcon::tpl::Nlp<SCALAR>
18 {
19 public:
20  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21 
22  using Scalar_t = SCALAR;
23  using Kinematics_t = KINEMATICS;
24 
29 
31  IKNLP(std::shared_ptr<ct::rbd::IKCostEvaluator<KINEMATICS, SCALAR>> costEvaluator,
32  const JointPosition_t& lowerBound,
33  const JointPosition_t& upperBound)
34  {
35  // the number of optimization variables is the number of robot joints
36  this->optVariables_ = std::shared_ptr<ct::optcon::tpl::OptVector<SCALAR>>(
37  new ct::optcon::tpl::OptVector<SCALAR>(KINEMATICS::NJOINTS));
38  // trivial initial guess
39  this->optVariables_->setZero();
40 
41  // set the cost evaluator
42  this->costEvaluator_ = costEvaluator;
43  std::static_pointer_cast<ct::rbd::IKCostEvaluator<KINEMATICS, SCALAR>>(this->costEvaluator_)
44  ->setOptVector(this->optVariables_);
45 
46  this->constraints_ = std::shared_ptr<IKConstraintsContainer<KINEMATICS, SCALAR>>(
47  new IKConstraintsContainer<KINEMATICS, SCALAR>(this->optVariables_, lowerBound, upperBound));
48  }
49 
50  ~IKNLP() override = default;
51 
52  void updateProblem() override { /* do nothing */}
53 
54  JointPosition_t getSolution() { return this->optVariables_->getOptimizationVars(); }
56  void printSolution() const
57  {
58  std::cout << "IKNLP Solution: " << std::endl
59  << this->optVariables_->getOptimizationVars().transpose() << std::endl;
60  }
61 
62  void setInitialGuess(const JointPosition_t& q_init) { this->optVariables_->setInitialGuess(q_init); }
63  std::shared_ptr<ct::rbd::IKCostEvaluator<KINEMATICS, SCALAR>> getIKCostEvaluator()
64  {
65  return std::static_pointer_cast<ct::rbd::IKCostEvaluator<KINEMATICS, SCALAR>>(this->costEvaluator_);
66  }
67 
68  std::shared_ptr<ct::rbd::IKConstraintsContainer<KINEMATICS, SCALAR>> getIKConstraintContainer()
69  {
70  return std::static_pointer_cast<ct::rbd::IKConstraintsContainer<KINEMATICS, SCALAR>>(this->constraints_);
71  }
72 };
73 
74 } // rbd
75 } // ct
Inverse Kinematics cost evaluator for NLP.
Definition: IKCostEvaluator.h:21
JointPosition_t getSolution()
Definition: IKNLP.h:54
KINEMATICS Kinematics_t
Definition: IKNLP.h:23
typename InverseKinematicsBase::JointPosition_t JointPosition_t
Definition: IKNLP.h:26
std::shared_ptr< ct::rbd::IKCostEvaluator< KINEMATICS, SCALAR > > getIKCostEvaluator()
Definition: IKNLP.h:63
std::shared_ptr< ct::rbd::IKConstraintsContainer< KINEMATICS, SCALAR > > getIKConstraintContainer()
Definition: IKNLP.h:68
void printSolution() const
print the solution to command line
Definition: IKNLP.h:56
void setInitialGuess(const JointPosition_t &q_init)
Definition: IKNLP.h:62
std::shared_ptr< DiscreteCostEvaluatorBase< SCALAR > > costEvaluator_
~IKNLP() override=default
std::shared_ptr< OptVector< SCALAR > > optVariables_
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Definition: IKNLP.h:17
Definition: InverseKinematicsBase.h:25
typename InverseKinematicsBase::RigidBodyPoseTpl RigidBodyPoseTpl
Definition: IKNLP.h:28
SCALAR Scalar_t
Definition: IKNLP.h:22
Inverse Kinematics constraint container.
Definition: IKConstraintContainer.h:20
typename JointState< NJOINTS, SCALAR >::Position JointPosition_t
Definition: InverseKinematicsBase.h:30
typename InverseKinematicsBase::JointPositionsVector_t JointPositionsVector_t
Definition: IKNLP.h:27
tpl::RigidBodyPose< SCALAR > RigidBodyPoseTpl
Definition: InverseKinematicsBase.h:32
IKNLP(std::shared_ptr< ct::rbd::IKCostEvaluator< KINEMATICS, SCALAR >> costEvaluator, const JointPosition_t &lowerBound, const JointPosition_t &upperBound)
constructor
Definition: IKNLP.h:31
void updateProblem() override
Definition: IKNLP.h:52
std::shared_ptr< DiscreteConstraintContainerBase< SCALAR > > constraints_
std::vector< JointPosition_t, Eigen::aligned_allocator< JointPosition_t > > JointPositionsVector_t
Definition: InverseKinematicsBase.h:31