NaturalDiffIK.cpp
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 SecondHands Demo (shdemo at armar6)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 
25 #include "NaturalDiffIK.h"
26 
29 
30 #include <cfloat>
31 
32 namespace armarx
33 {
34  Eigen::VectorXf NaturalDiffIK::LimitInfNormTo(Eigen::VectorXf vec, float maxValue)
35  {
36  float infNorm = vec.lpNorm<Eigen::Infinity>();
37  if (infNorm > maxValue)
38  {
39  vec = vec / infNorm * maxValue;
40  }
41  return vec;
42  }
43 
44  NaturalDiffIK::Result NaturalDiffIK::CalculateDiffIK(const Eigen::Matrix4f& targetPose, const Eigen::Vector3f& elbowTarget, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp, VirtualRobot::RobotNodePtr elbow, Mode setOri, Parameters params)
45  {
47 
48  CartesianVelocityController vcTcp(rns);
49  CartesianPositionController pcTcp(tcp);
50  CartesianVelocityController vcElb(rns, elbow);
51  CartesianPositionController pcElb(elbow);
52 
53  if (params.resetRnsValues)
54  {
55  for (VirtualRobot::RobotNodePtr rn : rns->getAllRobotNodes())
56  {
57  if (rn->isLimitless())
58  {
59  rn->setJointValue(0);
60  }
61  else
62  {
63  rn->setJointValue((rn->getJointLimitHi() + rn->getJointLimitLo()) / 2);
64  }
65  }
66  }
67 
68 
69  //std::stringstream ss;
70 
71  //ARMARX_IMPORTANT << "start";
72 
73  int posMet = 0;
74  int oriMet = 0;
75 
76  std::vector<IKStep> ikSteps;
77  Eigen::VectorXf currentJointValues = rns->getJointValuesEigen();
78  for (size_t i = 0; i <= params.stepsInitial + params.stepsFineTune; i++)
79  {
80  //ss << pdTcp.norm() << " ## " << odTcp.norm() << " ## " << pdElb.norm() << std::endl;
81 
82  int posLen = mode & VirtualRobot::IKSolver::Position ? 3 : 0;
83  int oriLen = mode & VirtualRobot::IKSolver::Orientation ? 3 : 0;
84  Eigen::Vector3f pdTcp = posLen ? pcTcp.getPositionDiff(targetPose) : Eigen::Vector3f::Zero();
85  Eigen::Vector3f odTcp = oriLen ? pcTcp.getOrientationDiff(targetPose) : Eigen::Vector3f::Zero();
86  Eigen::VectorXf cartesianVel(posLen + oriLen);
87  if (posLen)
88  {
89  cartesianVel.block<3, 1>(0, 0) = pdTcp;
90  }
91  if (oriLen)
92  {
93  cartesianVel.block<3, 1>(posLen, 0) = odTcp;
94  }
95 
96  Eigen::Vector3f pdElb = pcElb.getPositionDiffVec3(elbowTarget);
97  Eigen::VectorXf cartesianVelElb(3);
98  cartesianVelElb.block<3, 1>(0, 0) = pdElb;
99  Eigen::VectorXf jvElb = params.elbowKp * vcElb.calculate(cartesianVelElb, VirtualRobot::IKSolver::Position);
100  Eigen::VectorXf jvLA = params.jointLimitAvoidanceKp * vcTcp.calculateJointLimitAvoidance();
101  Eigen::VectorXf jv = vcTcp.calculate(cartesianVel, jvElb + jvLA, mode);
102 
103 
104  float stepLength = i < params.stepsInitial ? params.ikStepLengthInitial : params.ikStepLengthFineTune;
105  Eigen::VectorXf jvClamped = jv * stepLength;
106  jvClamped = LimitInfNormTo(jvClamped, params.maxJointAngleStep);
107 
108  if (params.returnIKSteps)
109  {
110  IKStep s;
111  s.pdTcp = pdTcp;
112  s.odTcp = odTcp;
113  s.pdElb = pdElb;
114  s.tcpPose = tcp->getPoseInRootFrame();
115  s.elbPose = elbow->getPoseInRootFrame();
116  s.cartesianVel = cartesianVel;
117  s.cartesianVelElb = cartesianVelElb;
118  s.jvElb = jvElb;
119  s.jvLA = jvLA;
120  s.jv = jv;
121  s.jvClamped = jvClamped;
122  ikSteps.emplace_back(s);
123  }
124 
125 
126  Eigen::VectorXf newJointValues = currentJointValues + jvClamped;
127  rns->setJointValues(newJointValues);
128  currentJointValues = newJointValues;
129 
130  if (pdTcp.norm() > params.maxPosError)
131  {
132  posMet = 0;
133  }
134  else
135  {
136  posMet++;
137  }
138  if (odTcp.norm() > params.maxOriError)
139  {
140  oriMet = 0;
141  }
142  else
143  {
144  oriMet++;
145  }
146 
147  // terminate early, if reached for at least 3 iterations
148  if (posMet > 2 && oriMet > 2)
149  {
150  break;
151  }
152  }
153 
154  //ARMARX_IMPORTANT << ss.str();
155 
156  Result result;
157  result.ikSteps = ikSteps;
158  result.jointValues = rns->getJointValuesEigen();
159  result.posDiff = pcTcp.getPositionDiff(targetPose);
160  result.oriDiff = pcTcp.getOrientationDiff(targetPose);
161  result.posError = pcTcp.getPositionError(targetPose);
162  result.oriError = pcTcp.getOrientationError(targetPose);
163  result.reached = result.posError < params.maxPosError && (setOri == Mode::Position || result.oriError < params.maxOriError);
164  result.posDiffElbow = pcElb.getPositionDiffVec3(elbowTarget);
165  result.posErrorElbow = result.posDiffElbow.norm();
166 
167  result.jointLimitMargins = Eigen::VectorXf::Zero(rns->getSize());
168  result.minimumJointLimitMargin = FLT_MAX;
169  for (size_t i = 0; i < rns->getSize(); i++)
170  {
171  VirtualRobot::RobotNodePtr rn = rns->getNode(i);
172  if (rn->isLimitless())
173  {
174  result.jointLimitMargins(i) = M_PI;
175  }
176  else
177  {
178  result.jointLimitMargins(i) = std::min(rn->getJointValue() - rn->getJointLimitLo(), rn->getJointLimitHi() - rn->getJointValue());
180  }
181  }
182 
183  return result;
184  }
185 
187  {
188  return mode == Mode::All ? VirtualRobot::IKSolver::All : VirtualRobot::IKSolver::Position;
189  }
190 
191 
192 
193 }
armarx::NaturalDiffIK::Parameters::stepsInitial
size_t stepsInitial
Definition: NaturalDiffIK.h:43
NaturalDiffIK.h
armarx::NaturalDiffIK::Parameters::maxJointAngleStep
float maxJointAngleStep
Definition: NaturalDiffIK.h:49
armarx::CartesianVelocityController::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityController.cpp:71
armarx::NaturalDiffIK::Mode::All
@ All
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:9
armarx::NaturalDiffIK::Mode::Position
@ Position
armarx::NaturalDiffIK::LimitInfNormTo
static Eigen::VectorXf LimitInfNormTo(Eigen::VectorXf vec, float maxValue)
Definition: NaturalDiffIK.cpp:34
armarx::NaturalDiffIK::Mode
Mode
Definition: NaturalDiffIK.h:36
armarx::NaturalDiffIK::Result::reached
bool reached
Definition: NaturalDiffIK.h:78
armarx::CartesianVelocityController
Definition: CartesianVelocityController.h:36
armarx::CartesianPositionController::getPositionDiff
Eigen::Vector3f getPositionDiff(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:158
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::NaturalDiffIK::Result::posDiffElbow
Eigen::Vector3f posDiffElbow
Definition: NaturalDiffIK.h:73
armarx::NaturalDiffIK::Result::oriError
float oriError
Definition: NaturalDiffIK.h:77
armarx::CartesianPositionController::getPositionDiffVec3
Eigen::Vector3f getPositionDiffVec3(const Eigen::Vector3f &targetPosition) const
Definition: CartesianPositionController.cpp:165
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::NaturalDiffIK::Parameters::maxOriError
float maxOriError
Definition: NaturalDiffIK.h:46
armarx::NaturalDiffIK::Parameters::maxPosError
float maxPosError
Definition: NaturalDiffIK.h:45
armarx::NaturalDiffIK::Result::posError
float posError
Definition: NaturalDiffIK.h:75
armarx::NaturalDiffIK::Result::posErrorElbow
float posErrorElbow
Definition: NaturalDiffIK.h:76
armarx::NaturalDiffIK::Result::oriDiff
Eigen::Vector3f oriDiff
Definition: NaturalDiffIK.h:74
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::NaturalDiffIK::Result::posDiff
Eigen::Vector3f posDiff
Definition: NaturalDiffIK.h:72
armarx::NaturalDiffIK::Result::jointValues
Eigen::VectorXf jointValues
Definition: NaturalDiffIK.h:71
armarx::NaturalDiffIK::Parameters::resetRnsValues
bool resetRnsValues
Definition: NaturalDiffIK.h:50
armarx::NaturalDiffIK::Parameters::ikStepLengthFineTune
float ikStepLengthFineTune
Definition: NaturalDiffIK.h:42
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:152
CartesianVelocityController.h
CartesianPositionController.h
armarx::NaturalDiffIK::Parameters::stepsFineTune
size_t stepsFineTune
Definition: NaturalDiffIK.h:44
armarx::CartesianPositionController::getOrientationDiff
Eigen::Vector3f getOrientationDiff(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:170
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::NaturalDiffIK::ModeToCartesianSelection
static VirtualRobot::IKSolver::CartesianSelection ModeToCartesianSelection(Mode mode)
Definition: NaturalDiffIK.cpp:186
armarx::NaturalDiffIK::Parameters::jointLimitAvoidanceKp
float jointLimitAvoidanceKp
Definition: NaturalDiffIK.h:47
armarx::NaturalDiffIK::CalculateDiffIK
static Result CalculateDiffIK(const Eigen::Matrix4f &targetPose, const Eigen::Vector3f &elbowTarget, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp, VirtualRobot::RobotNodePtr elbow, Mode setOri, Parameters params=Parameters())
Definition: NaturalDiffIK.cpp:44
armarx::CartesianPositionController
Definition: CartesianPositionController.h:41
armarx::NaturalDiffIK::Parameters
Definition: NaturalDiffIK.h:37
armarx::NaturalDiffIK::Result
Definition: NaturalDiffIK.h:69
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::NaturalDiffIK::Parameters::returnIKSteps
bool returnIKSteps
Definition: NaturalDiffIK.h:51
armarx::NaturalDiffIK::IKStep
Definition: NaturalDiffIK.h:53
armarx::NaturalDiffIK::Parameters::ikStepLengthInitial
float ikStepLengthInitial
Definition: NaturalDiffIK.h:41
armarx::NaturalDiffIK::Result::ikSteps
std::vector< IKStep > ikSteps
Definition: NaturalDiffIK.h:82
armarx::NaturalDiffIK::Parameters::elbowKp
float elbowKp
Definition: NaturalDiffIK.h:48
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::CartesianPositionController::getOrientationError
float getOrientationError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:98
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::NaturalDiffIK::Result::minimumJointLimitMargin
float minimumJointLimitMargin
Definition: NaturalDiffIK.h:80
armarx::NaturalDiffIK::Result::jointLimitMargins
Eigen::VectorXf jointLimitMargins
Definition: NaturalDiffIK.h:79