SimpleDiffIK.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 #include "SimpleDiffIK.h"
25 
28 
29 #include <cfloat>
30 
31 namespace armarx
32 {
33  SimpleDiffIK::Result SimpleDiffIK::CalculateDiffIK(const Eigen::Matrix4f targetPose, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp, Parameters params)
34  {
35 
36  tcp = tcp ? tcp : rns->getTCP();
37  CartesianVelocityController velocityController(rns);
38  CartesianPositionController positionController(tcp);
39 
40  if (params.resetRnsValues)
41  {
42  for (VirtualRobot::RobotNodePtr rn : rns->getAllRobotNodes())
43  {
44  if (rn->isLimitless())
45  {
46  rn->setJointValue(0);
47  }
48  else
49  {
50  rn->setJointValue((rn->getJointLimitHi() + rn->getJointLimitLo()) / 2);
51  }
52  }
53  }
54 
55  std::vector<IKStep> ikSteps;
56  Eigen::VectorXf currentJointValues = rns->getJointValuesEigen();
57  for (size_t i = 0; i <= params.stepsInitial + params.stepsFineTune; i++)
58  {
59  Eigen::Vector3f posDiff = positionController.getPositionDiff(targetPose);
60  Eigen::Vector3f oriDiff = positionController.getOrientationDiff(targetPose);
61 
62  //ARMARX_IMPORTANT << VAROUT(posDiff) << VAROUT(oriDiff);
63 
64  Eigen::VectorXf cartesianVel(6);
65  cartesianVel << posDiff(0), posDiff(1), posDiff(2), oriDiff(0), oriDiff(1), oriDiff(2);
66  const Eigen::VectorXf jnv = params.jointLimitAvoidanceKp * velocityController.calculateJointLimitAvoidance();
67  const Eigen::VectorXf jv = velocityController.calculate(cartesianVel, jnv, VirtualRobot::IKSolver::All);
68 
69 
70 
71  float stepLength = i < params.stepsInitial ? params.ikStepLengthInitial : params.ikStepLengthFineTune;
72  Eigen::VectorXf jvClamped = jv * stepLength;
73 
74  float infNorm = jvClamped.lpNorm<Eigen::Infinity>();
75  if (infNorm > params.maxJointAngleStep)
76  {
77  jvClamped = jvClamped / infNorm * params.maxJointAngleStep;
78  }
79 
80  if (params.returnIKSteps)
81  {
82  IKStep s;
83  s.posDiff = posDiff;
84  s.oriDiff = oriDiff;
85  s.cartesianVel = cartesianVel;
86  s.jnv = jnv;
87  s.jv = jv;
88  s.infNorm = infNorm;
89  s.jvClamped = jvClamped;
90  ikSteps.emplace_back(s);
91  }
92 
93 
94  Eigen::VectorXf newJointValues = currentJointValues + jvClamped;
95  rns->setJointValues(newJointValues);
96  currentJointValues = newJointValues;
97  }
98 
99  Result result;
100  result.ikSteps = ikSteps;
101  result.jointValues = rns->getJointValuesEigen();
102  result.posDiff = positionController.getPositionDiff(targetPose);
103  result.oriDiff = positionController.getOrientationDiff(targetPose);
104  result.posError = positionController.getPositionError(targetPose);
105  result.oriError = positionController.getOrientationError(targetPose);
106  result.reached = result.posError < params.maxPosError && result.oriError < params.maxOriError;
107 
108  result.jointLimitMargins = Eigen::VectorXf::Zero(rns->getSize());
109  result.minimumJointLimitMargin = FLT_MAX;
110  for (size_t i = 0; i < rns->getSize(); i++)
111  {
112  VirtualRobot::RobotNodePtr rn = rns->getNode(i);
113  if (rn->isLimitless())
114  {
115  result.jointLimitMargins(i) = M_PI;
116  }
117  else
118  {
119  result.jointLimitMargins(i) = std::min(rn->getJointValue() - rn->getJointLimitLo(), rn->getJointLimitHi() - rn->getJointValue());
121  }
122  }
123 
124  return result;
125  }
126 
127  SimpleDiffIK::Reachability SimpleDiffIK::CalculateReachability(const std::vector<Eigen::Matrix4f> targets, const Eigen::VectorXf& initialJV, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp, SimpleDiffIK::Parameters params)
128  {
129  Reachability r;
130  rns->setJointValues(initialJV);
131  for (const Eigen::Matrix4f& target : targets)
132  {
133  Result res = CalculateDiffIK(target, rns, tcp, params);
134  r.aggregate(res);
135  }
136  if (!r.reachable)
137  {
139  }
140  return r;
141  }
142 
143  SimpleDiffIKProvider::SimpleDiffIKProvider(VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp, SimpleDiffIK::Parameters params)
144  : rns(rns), tcp(tcp), params(params)
145  {
146  }
147 
149  {
150  params.resetRnsValues = true;
151  SimpleDiffIK::Result result = SimpleDiffIK::CalculateDiffIK(targetPose, rns, tcp, params);
152  DiffIKResult r;
153  r.jointValues = rns->getJointValuesEigen();
154  r.oriError = result.oriError;
155  r.posError = result.posError;
156  r.reachable = result.reached;
157  return r;
158 
159  }
160 
161  DiffIKResult SimpleDiffIKProvider::SolveRelative(const Eigen::Matrix4f& targetPose, const Eigen::VectorXf& startJointValues)
162  {
163  params.resetRnsValues = false;
164  rns->setJointValues(startJointValues);
165  SimpleDiffIK::Result result = SimpleDiffIK::CalculateDiffIK(targetPose, rns, tcp, params);
166  DiffIKResult r;
167  r.jointValues = rns->getJointValuesEigen();
168  r.oriError = result.oriError;
169  r.posError = result.posError;
170  r.reachable = result.reached;
171  return r;
172  }
173 
174 }
armarx::SimpleDiffIK::Result::posDiff
Eigen::Vector3f posDiff
Definition: SimpleDiffIK.h:70
armarx::SimpleDiffIK::Parameters::ikStepLengthFineTune
float ikStepLengthFineTune
Definition: SimpleDiffIK.h:45
armarx::SimpleDiffIK::Parameters::stepsInitial
size_t stepsInitial
Definition: SimpleDiffIK.h:46
armarx::CartesianVelocityController::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityController.cpp:71
armarx::SimpleDiffIK::Parameters::returnIKSteps
bool returnIKSteps
Definition: SimpleDiffIK.h:52
armarx::DiffIKResult
Definition: DiffIKProvider.h:34
armarx::SimpleDiffIK::Reachability::aggregate
void aggregate(const Result &result)
Definition: SimpleDiffIK.h:91
armarx::SimpleDiffIK::Parameters
Definition: SimpleDiffIK.h:40
armarx::SimpleDiffIK::Reachability::reachable
bool reachable
Definition: SimpleDiffIK.h:84
armarx::SimpleDiffIK::Result::reached
bool reached
Definition: SimpleDiffIK.h:74
armarx::SimpleDiffIK::Reachability::minimumJointLimitMargin
float minimumJointLimitMargin
Definition: SimpleDiffIK.h:85
armarx::SimpleDiffIK::CalculateDiffIK
static Result CalculateDiffIK(const Eigen::Matrix4f targetPose, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), Parameters params=Parameters())
Definition: SimpleDiffIK.cpp:33
armarx::SimpleDiffIK::Reachability
Definition: SimpleDiffIK.h:81
armarx::DiffIKResult::posError
float posError
Definition: DiffIKProvider.h:37
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::DiffIKResult::jointValues
Eigen::VectorXf jointValues
Definition: DiffIKProvider.h:39
armarx::CartesianVelocityController
Definition: CartesianVelocityController.h:36
armarx::SimpleDiffIK::Result::minimumJointLimitMargin
float minimumJointLimitMargin
Definition: SimpleDiffIK.h:76
armarx::CartesianPositionController::getPositionDiff
Eigen::Vector3f getPositionDiff(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:158
armarx::SimpleDiffIK::Result::jointLimitMargins
Eigen::VectorXf jointLimitMargins
Definition: SimpleDiffIK.h:75
armarx::SimpleDiffIK::Result::jointValues
Eigen::VectorXf jointValues
Definition: SimpleDiffIK.h:69
armarx::SimpleDiffIK::Parameters::jointLimitAvoidanceKp
float jointLimitAvoidanceKp
Definition: SimpleDiffIK.h:50
armarx::SimpleDiffIK::Parameters::maxOriError
float maxOriError
Definition: SimpleDiffIK.h:49
armarx::DiffIKResult::oriError
float oriError
Definition: DiffIKProvider.h:38
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::SimpleDiffIKProvider::SolveRelative
DiffIKResult SolveRelative(const Eigen::Matrix4f &targetPose, const Eigen::VectorXf &startJointValues)
Definition: SimpleDiffIK.cpp:161
armarx::SimpleDiffIK::Result::posError
float posError
Definition: SimpleDiffIK.h:72
M_PI
#define M_PI
Definition: MathTools.h:17
SimpleDiffIK.h
armarx::SimpleDiffIK::Parameters::maxJointAngleStep
float maxJointAngleStep
Definition: SimpleDiffIK.h:51
armarx::SimpleDiffIKProvider::SimpleDiffIKProvider
SimpleDiffIKProvider(VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), SimpleDiffIK::Parameters params=SimpleDiffIK::Parameters())
Definition: SimpleDiffIK.cpp:143
armarx::SimpleDiffIK::Parameters::ikStepLengthInitial
float ikStepLengthInitial
Definition: SimpleDiffIK.h:44
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:152
armarx::SimpleDiffIK::Result::oriError
float oriError
Definition: SimpleDiffIK.h:73
CartesianVelocityController.h
CartesianPositionController.h
armarx::SimpleDiffIK::Result::ikSteps
std::vector< IKStep > ikSteps
Definition: SimpleDiffIK.h:77
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::SimpleDiffIK::Result
Definition: SimpleDiffIK.h:67
armarx::SimpleDiffIK::CalculateReachability
static Reachability CalculateReachability(const std::vector< Eigen::Matrix4f > targets, const Eigen::VectorXf &initialJV, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), Parameters params=Parameters())
Use this to check a trajectory of waypoints.
Definition: SimpleDiffIK.cpp:127
armarx::SimpleDiffIK::Parameters::maxPosError
float maxPosError
Definition: SimpleDiffIK.h:48
armarx::SimpleDiffIK::Result::oriDiff
Eigen::Vector3f oriDiff
Definition: SimpleDiffIK.h:71
armarx::CartesianPositionController
Definition: CartesianPositionController.h:41
armarx::SimpleDiffIKProvider::SolveAbsolute
DiffIKResult SolveAbsolute(const Eigen::Matrix4f &targetPose)
Definition: SimpleDiffIK.cpp:148
armarx::DiffIKResult::reachable
bool reachable
Definition: DiffIKProvider.h:36
armarx::SimpleDiffIK::IKStep
Definition: SimpleDiffIK.h:55
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::SimpleDiffIK::Parameters::stepsFineTune
size_t stepsFineTune
Definition: SimpleDiffIK.h:47
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::SimpleDiffIK::Parameters::resetRnsValues
bool resetRnsValues
Definition: SimpleDiffIK.h:53