Exotica
ik_solver.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020, University of Edinburgh, University of Oxford
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_IK_SOLVER_IK_SOLVER_H_
31 #define EXOTICA_IK_SOLVER_IK_SOLVER_H_
32 
35 
36 #include <exotica_ik_solver/ik_solver_initializer.h>
37 
38 namespace exotica
39 {
44 class IKSolver : public MotionSolver, public Instantiable<IKSolverInitializer>
45 {
46 public:
47  void Solve(Eigen::MatrixXd& solution) override;
48  void SpecifyProblem(PlanningProblemPtr pointer) override;
49 
50 private:
51  UnconstrainedEndPoseProblemPtr prob_; // Shared pointer to the planning problem.
52 
53  Eigen::MatrixXd W_inv_;
54  Eigen::VectorXd alpha_space_;
55 
56  // Pre-allocation of variables used during optimisation
57  double stop_;
58  double step_;
59  double lambda_ = 0;
60  double steplength_;
61  Eigen::VectorXd q_;
62  Eigen::VectorXd qd_;
63  Eigen::VectorXd yd_;
64  Eigen::MatrixXd cost_jacobian_;
65  Eigen::MatrixXd J_pseudo_inverse_;
66  double error_;
67  double error_prev_;
68  Eigen::LLT<Eigen::MatrixXd> J_decomposition_;
69  Eigen::MatrixXd J_tmp_;
70 
71  // Convergence thresholds
72  double th_stop_;
73 
74  // Regularization
75  double regmin_ = 1e-9;
76  double regmax_ = 1e9;
77  double regfactor_ = 10.;
78  double th_stepdec_ = 0.5;
79  double th_stepinc_ = 0.1;
80 
82  {
84  if (lambda_ > regmax_)
85  {
86  lambda_ = regmax_;
87  }
88  }
89 
91  {
93  if (lambda_ < regmin_)
94  {
95  lambda_ = regmin_;
96  }
97  }
98 
99  void PrintDebug(const int i)
100  {
101  if (i % 10 == 0 || i == 0)
102  {
103  std::cout << "iter \t cost \t stop \t grad \t reg \t step\n";
104  }
105 
106  std::cout << std::setw(4) << i << " ";
107  std::cout << std::scientific << std::setprecision(5) << error_ << " ";
108  std::cout << step_ << " " << stop_ << " ";
109  std::cout << lambda_ << " ";
110  std::cout << std::fixed << std::setprecision(4) << steplength_ << '\n';
111  }
112 
114 };
115 } // namespace exotica
116 
117 #endif // EXOTICA_IK_SOLVER_IK_SOLVER_H_
exotica::IKSolver::regmin_
double regmin_
Minimum regularization (will not decrease lower)
Definition: ik_solver.h:75
exotica::IKSolver::J_pseudo_inverse_
Eigen::MatrixXd J_pseudo_inverse_
Jacobian pseudo-inverse, used during optimisation.
Definition: ik_solver.h:65
exotica::IKSolver::th_stop_
double th_stop_
Gradient convergence threshold.
Definition: ik_solver.h:72
exotica::IKSolver::yd_
Eigen::VectorXd yd_
Task space difference/error, used during optimisation.
Definition: ik_solver.h:63
exotica::IKSolver::SpecifyProblem
void SpecifyProblem(PlanningProblemPtr pointer) override
exotica::IKSolver::ScaleToStepSize
void ScaleToStepSize(Eigen::VectorXdRef xd)
To scale to maximum step size.
exotica::IKSolver::J_tmp_
Eigen::MatrixXd J_tmp_
Temporary variable for inverse computation.
Definition: ik_solver.h:69
exotica::IKSolver::step_
double step_
Size of step: Sum of squared norm of qd_.
Definition: ik_solver.h:58
exotica::IKSolver::error_
double error_
Error, used during optimisation.
Definition: ik_solver.h:66
exotica::IKSolver::cost_jacobian_
Eigen::MatrixXd cost_jacobian_
Jacobian, used during optimisation.
Definition: ik_solver.h:64
exotica::IKSolver::alpha_space_
Eigen::VectorXd alpha_space_
Steplengths for backtracking line-search.
Definition: ik_solver.h:54
unconstrained_end_pose_problem.h
exotica::IKSolver::lambda_
double lambda_
Damping factor.
Definition: ik_solver.h:59
exotica::Instantiable
Definition: property.h:110
exotica
Definition: cartpole_dynamics_solver.h:38
exotica::IKSolver::th_stepinc_
double th_stepinc_
Step-length threshold used to increase regularization.
Definition: ik_solver.h:79
exotica::IKSolver::q_
Eigen::VectorXd q_
Joint configuration vector, used during optimisation.
Definition: ik_solver.h:61
Eigen::VectorXdRef
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:54
exotica::UnconstrainedEndPoseProblemPtr
std::shared_ptr< exotica::UnconstrainedEndPoseProblem > UnconstrainedEndPoseProblemPtr
Definition: unconstrained_end_pose_problem.h:81
exotica::IKSolver::error_prev_
double error_prev_
Error at previous iteration, used during optimisation.
Definition: ik_solver.h:67
exotica::IKSolver::PrintDebug
void PrintDebug(const int i)
Definition: ik_solver.h:99
exotica::IKSolver::qd_
Eigen::VectorXd qd_
Change in joint configuration, used during optimisation.
Definition: ik_solver.h:62
exotica::IKSolver::Solve
void Solve(Eigen::MatrixXd &solution) override
exotica::IKSolver::prob_
UnconstrainedEndPoseProblemPtr prob_
Definition: ik_solver.h:51
exotica::IKSolver::steplength_
double steplength_
Accepted steplength.
Definition: ik_solver.h:60
motion_solver.h
exotica::MotionSolver
Definition: motion_solver.h:42
exotica::PlanningProblemPtr
std::shared_ptr< PlanningProblem > PlanningProblemPtr
Definition: planning_problem.h:116
exotica::IKSolver::DecreaseRegularization
void DecreaseRegularization()
Definition: ik_solver.h:90
exotica::IKSolver::regmax_
double regmax_
Maximum regularization (to exit by divergence)
Definition: ik_solver.h:76
exotica::IKSolver::stop_
double stop_
Stop criterion: Norm of cost Jacobian.
Definition: ik_solver.h:57
exotica::IKSolver::th_stepdec_
double th_stepdec_
Step-length threshold used to decrease regularization.
Definition: ik_solver.h:78
exotica::IKSolver::W_inv_
Eigen::MatrixXd W_inv_
Joint-space weighting (inverse)
Definition: ik_solver.h:53
exotica::IKSolver
Weighted and Regularized Pseudo-Inverse Inverse Kinematics Solver The solver solves a weighted and re...
Definition: ik_solver.h:44
exotica::IKSolver::J_decomposition_
Eigen::LLT< Eigen::MatrixXd > J_decomposition_
Cholesky decomposition for the weighted pseudo-inverse.
Definition: ik_solver.h:68
exotica::IKSolver::IncreaseRegularization
void IncreaseRegularization()
Definition: ik_solver.h:81
exotica::IKSolver::regfactor_
double regfactor_
Factor by which the regularization gets increased/decreased.
Definition: ik_solver.h:77