CompositeDiffIK.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #pragma once
25 #include <SimoxUtility/math/distance/delta_angle.h>
26 
27 #include <VirtualRobot/Nodes/RobotNode.h>
28 #include <VirtualRobot/RobotNodeSet.h>
29 #include <VirtualRobot/IK/DifferentialIK.h>
32 
33 #include <memory>
34 
35 
36 namespace armarx
37 {
38 
39 
40  typedef std::shared_ptr<class CompositeDiffIK> CompositeDiffIKPtr;
42  {
43  public:
44  struct Parameters
45  {
47  // IK params
48  size_t steps = 40;
49 
50  float maxJointAngleStep = 0.2f;
51  float stepSize = 0.5f;
52  bool resetRnsValues = true;
53  bool returnIKSteps = false;
56  };
57 
59  {
60  public:
61  virtual void init(Parameters& params) = 0;
62  virtual Eigen::VectorXf getGradient(Parameters& params) = 0;
63  virtual ~NullspaceGradient() = default;
64  float kP = 1;
65  };
66  typedef std::shared_ptr<class NullspaceGradient> NullspaceGradientPtr;
67 
69  {
70  Eigen::Vector3f posDiff;
71  Eigen::Vector3f oriDiff;
73  Eigen::VectorXf cartesianVel;
74  Eigen::VectorXf jointVel;
75  };
77  {
78  public:
79  NullspaceTarget(const VirtualRobot::RobotNodeSetPtr& rns, const VirtualRobot::RobotNodePtr& tcp, const Eigen::Matrix4f& target, VirtualRobot::IKSolver::CartesianSelection mode);
80 
81  VirtualRobot::RobotNodePtr tcp;
86 
87  void init(Parameters&) override;
88  Eigen::VectorXf getGradient(Parameters& params) override;
89 
90  std::vector<NullspaceTargetStep> ikSteps;
91  };
92  typedef std::shared_ptr<class NullspaceTarget> NullspaceTargetPtr;
93 
95  {
96  public:
97  NullspaceJointTarget(const VirtualRobot::RobotNodeSetPtr& rns);
98  NullspaceJointTarget(const VirtualRobot::RobotNodeSetPtr& rns, const Eigen::VectorXf& target, const Eigen::VectorXf& weight);
99  void set(int index, float target, float weight);
100  void set(const std::string& jointName, float target, float weight);
101  void set(const VirtualRobot::RobotNodePtr& rn, float target, float weight);
102 
103  VirtualRobot::RobotNodeSetPtr rns;
104  Eigen::VectorXf target;
105  Eigen::VectorXf weight;
106 
107  void init(Parameters&) override;
108  Eigen::VectorXf getGradient(Parameters& params) override;
109  };
110  typedef std::shared_ptr<class NullspaceJointTarget> NullspaceJointTargetPtr;
111 
113  {
114  public:
115  NullspaceJointLimitAvoidance(const VirtualRobot::RobotNodeSetPtr& rns);
116  NullspaceJointLimitAvoidance(const VirtualRobot::RobotNodeSetPtr& rns, const Eigen::VectorXf& weight);
117  void setWeight(int index, float weight);
118  void setWeight(const std::string& jointName, float weight);
119  void setWeight(const VirtualRobot::RobotNodePtr& rn, float weight);
120  void setWeights(const VirtualRobot::RobotNodeSetPtr& rns, float weight);
121 
122 
123  VirtualRobot::RobotNodeSetPtr rns;
124  Eigen::VectorXf weight;
125 
126  void init(Parameters&) override;
127  Eigen::VectorXf getGradient(Parameters& params) override;
128  };
129  typedef std::shared_ptr<class NullspaceJointLimitAvoidance> NullspaceJointLimitAvoidancePtr;
130 
131  struct TargetStep
132  {
133  Eigen::Vector3f posDiff;
134  Eigen::Vector3f oriDiff;
136  Eigen::VectorXf cartesianVel;
137  };
138 
139  class Target
140  {
141  public:
142  Target(const VirtualRobot::RobotNodeSetPtr& rns, const VirtualRobot::RobotNodePtr& tcp, const Eigen::Matrix4f& target, VirtualRobot::IKSolver::CartesianSelection mode);
143  VirtualRobot::RobotNodePtr tcp;
146  Eigen::MatrixXf jacobi;
148  float maxPosError = 10.f;
149  float maxOriError = 0.05f;
150 
151  std::vector<TargetStep> ikSteps;
152 
154  {
155  return tcp->getPoseInRootFrame().inverse() * target;
156  }
157  Eigen::Vector3f getPosError() const
158  {
159  return (tcp->getPoseInRootFrame().inverse() * target).topRightCorner<3, 1>();
160  }
161  auto getOriError() const
162  {
163  return simox::math::delta_angle(tcp->getPoseInRootFrame(), target);
164  }
165  };
166  typedef std::shared_ptr<Target> TargetPtr;
167 
168 
169 
170 
171  struct Result
172  {
173  Eigen::VectorXf jointValues;
174  Eigen::Vector3f posDiff;
175  Eigen::Vector3f posDiffElbow;
176  Eigen::Vector3f oriDiff;
177  float posError;
179  float oriError;
180  bool reached;
181  Eigen::VectorXf jointLimitMargins;
183  Eigen::Vector3f elbowPosDiff;
184  };
185 
186  struct SolveState
187  {
188  int rows = 0;
189  int cols = 0;
190  Eigen::VectorXf jointRegularization;
191  Eigen::VectorXf cartesianRegularization;
192  Eigen::VectorXf jointValues;
193  Eigen::MatrixXf jacobi;
194  Eigen::MatrixXf invJac;
195  Eigen::MatrixXf nullspace;
196 
197  };
198 
199  static Eigen::VectorXf LimitInfNormTo(Eigen::VectorXf vec, float maxValue);
200 
201  CompositeDiffIK(const VirtualRobot::RobotNodeSetPtr& rns);
202 
203  void addTarget(const TargetPtr& target);
204  TargetPtr addTarget(const VirtualRobot::RobotNodePtr& tcp, const Eigen::Matrix4f& target, VirtualRobot::IKSolver::CartesianSelection mode);
205 
206  void addNullspaceGradient(const NullspaceGradientPtr& gradient);
207  NullspaceTargetPtr addNullspacePositionTarget(const VirtualRobot::RobotNodePtr& tcp, const Eigen::Vector3f& target);
208 
209  Result solve(Parameters params);
210  static Eigen::MatrixXf CalculateNullspaceSVD(const Eigen::Matrix4f& jacobi);
211  static Eigen::MatrixXf CalculateNullspaceLU(const Eigen::Matrix4f& jacobi);
212  void step(Parameters& params, SolveState& s, int stepNr);
213 
215 
216 
217 
218  private:
219  VirtualRobot::RobotNodeSetPtr rns;
220  std::vector<TargetPtr> targets;
221  std::vector<NullspaceGradientPtr> nullspaceGradients;
222  VirtualRobot::DifferentialIKPtr ik;
223 
224  };
225 }
armarx::CompositeDiffIK::TargetStep
Definition: CompositeDiffIK.h:131
armarx::CompositeDiffIK::Target::Target
Target(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodePtr &tcp, const Eigen::Matrix4f &target, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CompositeDiffIK.cpp:300
armarx::CompositeDiffIK::NullspaceGradient
Definition: CompositeDiffIK.h:58
armarx::CompositeDiffIK::NullspaceJointTarget::rns
VirtualRobot::RobotNodeSetPtr rns
Definition: CompositeDiffIK.h:103
armarx::CompositeDiffIK::NullspaceTarget::vCtrl
CartesianVelocityController vCtrl
Definition: CompositeDiffIK.h:85
armarx::CompositeDiffIK::Target::getPosError
Eigen::Vector3f getPosError() const
Definition: CompositeDiffIK.h:157
armarx::CompositeDiffIK::Target::mode
VirtualRobot::IKSolver::CartesianSelection mode
Definition: CompositeDiffIK.h:145
armarx::CompositeDiffIK::SolveState::jointValues
Eigen::VectorXf jointValues
Definition: CompositeDiffIK.h:192
armarx::CompositeDiffIK::Parameters::resetRnsValues
bool resetRnsValues
Definition: CompositeDiffIK.h:52
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::CompositeDiffIK::Result::reached
bool reached
Definition: CompositeDiffIK.h:180
armarx::CompositeDiffIK::NullspaceTarget::ikSteps
std::vector< NullspaceTargetStep > ikSteps
Definition: CompositeDiffIK.h:90
armarx::CompositeDiffIK::Target::maxPosError
float maxPosError
Definition: CompositeDiffIK.h:148
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::init
void init(Parameters &) override
Definition: CompositeDiffIK.cpp:437
armarx::CompositeDiffIK::Parameters::jointRegularizationTranslation
float jointRegularizationTranslation
Definition: CompositeDiffIK.h:54
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::getGradient
Eigen::VectorXf getGradient(Parameters &params) override
Definition: CompositeDiffIK.cpp:442
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::CompositeDiffIK::NullspaceJointLimitAvoidancePtr
std::shared_ptr< class NullspaceJointLimitAvoidance > NullspaceJointLimitAvoidancePtr
Definition: CompositeDiffIK.h:129
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::setWeight
void setWeight(int index, float weight)
Definition: CompositeDiffIK.cpp:404
armarx::CompositeDiffIK::NullspaceTargetStep::tcpPose
Eigen::Matrix4f tcpPose
Definition: CompositeDiffIK.h:72
armarx::CompositeDiffIK::Parameters::stepSize
float stepSize
Definition: CompositeDiffIK.h:51
armarx::CompositeDiffIKPtr
std::shared_ptr< class CompositeDiffIK > CompositeDiffIKPtr
Definition: CompositeDiffIK.h:40
armarx::CompositeDiffIK::NullspaceGradient::kP
float kP
Definition: CompositeDiffIK.h:64
armarx::CompositeDiffIK::TargetStep::oriDiff
Eigen::Vector3f oriDiff
Definition: CompositeDiffIK.h:134
armarx::CompositeDiffIK::NullspaceJointTarget::getGradient
Eigen::VectorXf getGradient(Parameters &params) override
Definition: CompositeDiffIK.cpp:387
armarx::CompositeDiffIK::NullspaceJointTarget::set
void set(int index, float target, float weight)
Definition: CompositeDiffIK.cpp:356
armarx::CartesianVelocityController
Definition: CartesianVelocityController.h:36
armarx::CompositeDiffIK::NullspaceTargetStep::oriDiff
Eigen::Vector3f oriDiff
Definition: CompositeDiffIK.h:71
armarx::CompositeDiffIK::SolveState
Definition: CompositeDiffIK.h:186
armarx::CompositeDiffIK::NullspaceTarget::NullspaceTarget
NullspaceTarget(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodePtr &tcp, const Eigen::Matrix4f &target, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CompositeDiffIK.cpp:306
armarx::CompositeDiffIK::Parameters::steps
size_t steps
Definition: CompositeDiffIK.h:48
armarx::CompositeDiffIK::Result::posErrorElbow
float posErrorElbow
Definition: CompositeDiffIK.h:178
armarx::CompositeDiffIK::NullspaceJointTarget::weight
Eigen::VectorXf weight
Definition: CompositeDiffIK.h:105
armarx::CompositeDiffIK
Definition: CompositeDiffIK.h:41
armarx::CompositeDiffIK::NullspaceGradient::init
virtual void init(Parameters &params)=0
armarx::CompositeDiffIK::solve
Result solve(Parameters params)
Definition: CompositeDiffIK.cpp:67
armarx::CompositeDiffIK::NullspaceTarget::mode
VirtualRobot::IKSolver::CartesianSelection mode
Definition: CompositeDiffIK.h:83
armarx::CompositeDiffIK::NullspaceTargetPtr
std::shared_ptr< class NullspaceTarget > NullspaceTargetPtr
Definition: CompositeDiffIK.h:92
armarx::CompositeDiffIK::NullspaceJointTargetPtr
std::shared_ptr< class NullspaceJointTarget > NullspaceJointTargetPtr
Definition: CompositeDiffIK.h:110
armarx::CompositeDiffIK::addTarget
void addTarget(const TargetPtr &target)
Definition: CompositeDiffIK.cpp:40
armarx::CompositeDiffIK::NullspaceJointTarget::init
void init(Parameters &) override
Definition: CompositeDiffIK.cpp:382
armarx::CompositeDiffIK::NullspaceJointTarget
Definition: CompositeDiffIK.h:94
armarx::CompositeDiffIK::Target
Definition: CompositeDiffIK.h:139
armarx::CompositeDiffIK::SolveState::jacobi
Eigen::MatrixXf jacobi
Definition: CompositeDiffIK.h:193
armarx::CompositeDiffIK::Target::jacobi
Eigen::MatrixXf jacobi
Definition: CompositeDiffIK.h:146
armarx::CompositeDiffIK::SolveState::invJac
Eigen::MatrixXf invJac
Definition: CompositeDiffIK.h:194
armarx::CompositeDiffIK::NullspaceGradient::getGradient
virtual Eigen::VectorXf getGradient(Parameters &params)=0
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::CompositeDiffIK::step
void step(Parameters &params, SolveState &s, int stepNr)
Definition: CompositeDiffIK.cpp:182
armarx::CompositeDiffIK::TargetPtr
std::shared_ptr< Target > TargetPtr
Definition: CompositeDiffIK.h:166
armarx::CompositeDiffIK::CalculateNullspaceLU
static Eigen::MatrixXf CalculateNullspaceLU(const Eigen::Matrix4f &jacobi)
Definition: CompositeDiffIK.cpp:165
armarx::CompositeDiffIK::SolveState::jointRegularization
Eigen::VectorXf jointRegularization
Definition: CompositeDiffIK.h:190
armarx::CompositeDiffIK::Result::posDiff
Eigen::Vector3f posDiff
Definition: CompositeDiffIK.h:174
armarx::CompositeDiffIK::Target::getPoseError
Eigen::Matrix4f getPoseError() const
Definition: CompositeDiffIK.h:153
armarx::CompositeDiffIK::NullspaceTarget
Definition: CompositeDiffIK.h:76
armarx::CompositeDiffIK::Target::tcp
VirtualRobot::RobotNodePtr tcp
Definition: CompositeDiffIK.h:143
armarx::CompositeDiffIK::TargetStep::posDiff
Eigen::Vector3f posDiff
Definition: CompositeDiffIK.h:133
armarx::CompositeDiffIK::Result::oriDiff
Eigen::Vector3f oriDiff
Definition: CompositeDiffIK.h:176
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::NullspaceJointLimitAvoidance
NullspaceJointLimitAvoidance(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: CompositeDiffIK.cpp:392
armarx::CompositeDiffIK::Result::jointLimitMargins
Eigen::VectorXf jointLimitMargins
Definition: CompositeDiffIK.h:181
armarx::CompositeDiffIK::Target::target
Eigen::Matrix4f target
Definition: CompositeDiffIK.h:144
armarx::CompositeDiffIK::NullspaceJointTarget::target
Eigen::VectorXf target
Definition: CompositeDiffIK.h:104
armarx::CompositeDiffIK::NullspaceTargetStep::cartesianVel
Eigen::VectorXf cartesianVel
Definition: CompositeDiffIK.h:73
armarx::CompositeDiffIK::Result::posDiffElbow
Eigen::Vector3f posDiffElbow
Definition: CompositeDiffIK.h:175
armarx::CompositeDiffIK::SolveState::cols
int cols
Definition: CompositeDiffIK.h:189
armarx::CompositeDiffIK::Result::posError
float posError
Definition: CompositeDiffIK.h:177
armarx::CompositeDiffIK::SolveState::nullspace
Eigen::MatrixXf nullspace
Definition: CompositeDiffIK.h:195
armarx::CompositeDiffIK::LimitInfNormTo
static Eigen::VectorXf LimitInfNormTo(Eigen::VectorXf vec, float maxValue)
Definition: CompositeDiffIK.cpp:334
armarx::CompositeDiffIK::NullspaceGradientPtr
std::shared_ptr< class NullspaceGradient > NullspaceGradientPtr
Definition: CompositeDiffIK.h:66
armarx::CompositeDiffIK::Result::minimumJointLimitMargin
float minimumJointLimitMargin
Definition: CompositeDiffIK.h:182
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::rns
VirtualRobot::RobotNodeSetPtr rns
Definition: CompositeDiffIK.h:123
CartesianVelocityController.h
CartesianPositionController.h
armarx::CompositeDiffIK::SolveState::rows
int rows
Definition: CompositeDiffIK.h:188
armarx::CompositeDiffIK::CalculateNullspaceSVD
static Eigen::MatrixXf CalculateNullspaceSVD(const Eigen::Matrix4f &jacobi)
Definition: CompositeDiffIK.cpp:158
armarx::CompositeDiffIK::NullspaceTargetStep::posDiff
Eigen::Vector3f posDiff
Definition: CompositeDiffIK.h:70
armarx::CompositeDiffIK::Result::oriError
float oriError
Definition: CompositeDiffIK.h:179
armarx::CompositeDiffIK::NullspaceTarget::getGradient
Eigen::VectorXf getGradient(Parameters &params) override
Definition: CompositeDiffIK.cpp:317
armarx::CompositeDiffIK::Target::ikSteps
std::vector< TargetStep > ikSteps
Definition: CompositeDiffIK.h:151
armarx::CompositeDiffIK::NullspaceTarget::target
Eigen::Matrix4f target
Definition: CompositeDiffIK.h:82
armarx::CompositeDiffIK::NullspaceTarget::init
void init(Parameters &) override
Definition: CompositeDiffIK.cpp:312
armarx::CompositeDiffIK::NullspaceTargetStep::jointVel
Eigen::VectorXf jointVel
Definition: CompositeDiffIK.h:74
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::CompositeDiffIK::Parameters::Parameters
Parameters()
Definition: CompositeDiffIK.h:46
armarx::CompositeDiffIK::Result::elbowPosDiff
Eigen::Vector3f elbowPosDiff
Definition: CompositeDiffIK.h:183
armarx::CompositeDiffIK::Result
Definition: CompositeDiffIK.h:171
armarx::CompositeDiffIK::Target::pCtrl
CartesianPositionController pCtrl
Definition: CompositeDiffIK.h:147
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance
Definition: CompositeDiffIK.h:112
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::weight
Eigen::VectorXf weight
Definition: CompositeDiffIK.h:124
armarx::CompositeDiffIK::TargetStep::cartesianVel
Eigen::VectorXf cartesianVel
Definition: CompositeDiffIK.h:136
armarx::CompositeDiffIK::CartesianSelectionToSize
int CartesianSelectionToSize(VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CompositeDiffIK.cpp:284
armarx::CompositeDiffIK::NullspaceGradient::~NullspaceGradient
virtual ~NullspaceGradient()=default
armarx::CartesianPositionController
Definition: CartesianPositionController.h:41
armarx::CompositeDiffIK::NullspaceTarget::tcp
VirtualRobot::RobotNodePtr tcp
Definition: CompositeDiffIK.h:81
armarx::CompositeDiffIK::Parameters::jointRegularizationRotation
float jointRegularizationRotation
Definition: CompositeDiffIK.h:55
armarx::CompositeDiffIK::NullspaceTarget::pCtrl
CartesianPositionController pCtrl
Definition: CompositeDiffIK.h:84
armarx::CompositeDiffIK::Parameters
Definition: CompositeDiffIK.h:44
armarx::CompositeDiffIK::Parameters::returnIKSteps
bool returnIKSteps
Definition: CompositeDiffIK.h:53
armarx::CompositeDiffIK::Parameters::maxJointAngleStep
float maxJointAngleStep
Definition: CompositeDiffIK.h:50
armarx::CompositeDiffIK::Result::jointValues
Eigen::VectorXf jointValues
Definition: CompositeDiffIK.h:173
armarx::CompositeDiffIK::SolveState::cartesianRegularization
Eigen::VectorXf cartesianRegularization
Definition: CompositeDiffIK.h:191
armarx::CompositeDiffIK::NullspaceJointLimitAvoidance::setWeights
void setWeights(const VirtualRobot::RobotNodeSetPtr &rns, float weight)
Definition: CompositeDiffIK.cpp:429
armarx::CompositeDiffIK::addNullspacePositionTarget
NullspaceTargetPtr addNullspacePositionTarget(const VirtualRobot::RobotNodePtr &tcp, const Eigen::Vector3f &target)
Definition: CompositeDiffIK.cpp:57
armarx::CompositeDiffIK::NullspaceTargetStep
Definition: CompositeDiffIK.h:68
armarx::CompositeDiffIK::CompositeDiffIK
CompositeDiffIK(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: CompositeDiffIK.cpp:34
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::CompositeDiffIK::Target::maxOriError
float maxOriError
Definition: CompositeDiffIK.h:149
armarx::CompositeDiffIK::addNullspaceGradient
void addNullspaceGradient(const NullspaceGradientPtr &gradient)
Definition: CompositeDiffIK.cpp:52
armarx::CompositeDiffIK::TargetStep::tcpPose
Eigen::Matrix4f tcpPose
Definition: CompositeDiffIK.h:135
armarx::CompositeDiffIK::NullspaceJointTarget::NullspaceJointTarget
NullspaceJointTarget(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: CompositeDiffIK.cpp:344
armarx::CompositeDiffIK::Target::getOriError
auto getOriError() const
Definition: CompositeDiffIK.h:161