CartesianNaturalPositionController.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 Raphael Grimm (raphael dot grimm at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 
26 
27 #include <VirtualRobot/IK/DifferentialIK.h>
28 #include <VirtualRobot/MathTools.h>
29 #include <VirtualRobot/Nodes/RobotNode.h>
30 #include <VirtualRobot/math/Helpers.h>
31 
34 
36 
37 namespace armarx
38 {
40  const VirtualRobot::RobotNodeSetPtr& rns,
41  const VirtualRobot::RobotNodePtr& elbow,
42  float maxPositionAcceleration,
43  float maxOrientationAcceleration,
44  float maxNullspaceAcceleration,
45  const VirtualRobot::RobotNodePtr& tcp) :
46  vcTcp(rns,
47  Eigen::VectorXf::Constant(rns->getSize(), 0.f),
48  VirtualRobot::IKSolver::CartesianSelection::All,
49  maxPositionAcceleration,
50  maxOrientationAcceleration,
51  maxNullspaceAcceleration,
52  tcp ? tcp : rns->getTCP()),
53  pcTcp(tcp ? tcp : rns->getTCP()),
54  vcElb(rns, elbow),
55  pcElb(elbow),
56  lastJointVelocity(Eigen::VectorXf::Constant(rns->getSize(), 0.f)),
57  rns(rns)
58  {
61  }
62 
63  Eigen::VectorXf
65  const std::vector<float>& maxValue)
66  {
67  if (maxValue.size() == 0)
68  {
69  return vec;
70  }
71  if (maxValue.size() != 1 && (int)maxValue.size() != vec.rows())
72  {
73  throw std::invalid_argument("maxValue.size != 1 and != maxValue.size");
74  }
75  float scale = 1;
76  for (int i = 0; i < vec.rows(); i++)
77  {
78  int j = maxValue.size() == 1 ? 0 : i;
79  if (std::abs(vec(i)) > maxValue.at(j) && maxValue.at(j) >= 0)
80  {
81  scale = std::min(scale, maxValue.at(j) / std::abs(vec(i)));
82  }
83  }
84  return vec * scale;
85  }
86 
87  const Eigen::VectorXf
89  {
90  Eigen::VectorXf jvNT = Eigen::VectorXf(rns->getSize());
91  for (size_t i = 0; i < rns->getSize(); i++)
92  {
93  if (std::isnan(nullspaceTarget(i)))
94  {
95  jvNT(i) = 0;
96  }
97  else
98  {
99  float diff = nullspaceTarget(i) - rns->getNode(i)->getJointValue();
100  if (rns->getNode(i)->isLimitless())
101  {
102  diff = ::math::Helpers::AngleModPI(diff);
103  }
104  jvNT(i) = diff;
105  }
106  }
107  return jvNT;
108  }
109 
110  const Eigen::VectorXf
113  {
114 
115  //int posLen = mode & VirtualRobot::IKSolver::Position ? 3 : 0;
116  //int oriLen = mode & VirtualRobot::IKSolver::Orientation ? 3 : 0;
117  //Eigen::Vector3f pdTcp = posLen ? pcTcp.getPositionDiff(tcpTarget) : Eigen::Vector3f::Zero();
118  //Eigen::Vector3f odTcp = oriLen ? pcTcp.getOrientationDiff(tcpTarget) : Eigen::Vector3f::Zero();
119  //Eigen::VectorXf cartesianVel(posLen + oriLen);
120  //if (posLen)
121  //{
122  // cartesianVel.block<3, 1>(0, 0) = pdTcp;
123  //}
124  //if (oriLen)
125  //{
126  // cartesianVel.block<3, 1>(posLen, 0) = odTcp;
127  //}
128  Eigen::VectorXf cartesianVelTcp = pcTcp.calculate(tcpTarget, mode);
129 
130  //Eigen::Vector3f pdElb = pcElb.getPositionDiffVec3(elbowTarget);
131  ////ARMARX_IMPORTANT << deactivateSpam(0.1) << VAROUT(pdElb) << VAROUT(elbowTarget) << VAROUT(pcElb.getTcp()->getPositionInRootFrame());
132  //Eigen::VectorXf cartesianVelElb(3);
133  //cartesianVelElb.block<3, 1>(0, 0) = pdElb;
134 
135  Eigen::VectorXf cartesianVelElb = pcElb.calculatePos(elbowTarget);
136  Eigen::VectorXf jnvClamped = Eigen::VectorXf::Zero(rns->getSize());
138  {
139  Eigen::VectorXf jnv = Eigen::VectorXf::Zero(rns->getSize());
140  Eigen::VectorXf jvElb =
142 
143  Eigen::VectorXf jvLA =
145  Eigen::VectorXf jvNT = nullspaceTargetKp * calculateNullspaceTargetDifference();
146  //ARMARX_IMPORTANT << deactivateSpam(1) << VAROUT(jvNT);
147 
148  //ARMARX_IMPORTANT << VAROUT(jvElb);
149  jnv = jvElb + jvLA + jvNT;
150  jnvClamped = LimitInfNormTo(jnv, maxNullspaceVelocity);
151  }
152 
153  //ARMARX_IMPORTANT << VAROUT(jnv);
154  if (vcTcp.getMode() != mode)
155  {
157  }
158  Eigen::VectorXf jv = vcTcp.calculate(cartesianVelTcp, jnvClamped, dt);
159  //ARMARX_IMPORTANT << VAROUT(jv);
160 
161  Eigen::VectorXf jvClamped = LimitInfNormTo(jv, maxJointVelocity);
162 
164  {
166  }
167 
168  lastJointVelocity = jvClamped;
169 
170  return jvClamped;
171  }
172 
173  void
175  const Eigen::Vector3f& elbowTarget)
176  {
177  this->tcpTarget = tcpTarget;
178  this->elbowTarget = elbowTarget;
179  }
180 
181  void
183  const Eigen::Vector6f& feedForwardVelocity)
184  {
185  this->feedForwardVelocity = feedForwardVelocity;
186  }
187 
188  void
190  const Eigen::Vector3f& feedForwardVelocityPos,
191  const Eigen::Vector3f& feedForwardVelocityOri)
192  {
193  feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
194  feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
195  }
196 
197  void
198  CartesianNaturalPositionController::setNullspaceTarget(const Eigen::VectorXf& nullspaceTarget)
199  {
200  this->nullspaceTarget = nullspaceTarget;
201  }
202 
203  void
205  const std::vector<float>& nullspaceTarget)
206  {
207  ARMARX_CHECK_EXPRESSION(rns->getSize() == nullspaceTarget.size());
208  for (size_t i = 0; i < rns->getSize(); i++)
209  {
210  this->nullspaceTarget(i) = nullspaceTarget.at(i);
211  }
212  }
213 
214  void
216  {
217  this->nullspaceTarget = Eigen::VectorXf::Constant(rns->getSize(), std::nanf(""));
218  }
219 
220  void
222  {
223  feedForwardVelocity = Eigen::Vector6f::Zero();
224  }
225 
226  float
228  {
230  }
231 
232  float
234  {
236  }
237 
238  bool
240  {
243  }
244 
245  bool
247  {
250  }
251 
252  const Eigen::Matrix4f&
254  {
255  return tcpTarget;
256  }
257 
258  const Eigen::Vector3f
260  {
261  return ::math::Helpers::GetPosition(tcpTarget);
262  }
263 
264  const Eigen::Vector3f&
266  {
267  return elbowTarget;
268  }
269 
270  const Eigen::VectorXf&
272  {
273  return nullspaceTarget;
274  }
275 
276  bool
278  {
279  for (int i = 0; i < nullspaceTarget.rows(); i++)
280  {
281  if (!std::isnan(nullspaceTarget(i)))
282  {
283  return true;
284  }
285  }
286  return false;
287  }
288 
289  void
291  {
293  }
294 
295  std::string
297  {
298  std::stringstream ss;
299  ss.precision(2);
300  ss << std::fixed << "distance: " << getPositionError() << " mm "
301  << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
302  return ss.str();
303  }
304 
305  void
307  const CartesianNaturalPositionControllerConfig& cfg)
308  {
309  jointLimitAvoidanceKp = cfg.jointLimitAvoidanceKp;
310 
311  thresholdOrientationNear = cfg.thresholdOrientationNear;
312  thresholdOrientationReached = cfg.thresholdOrientationReached;
313  thresholdPositionNear = cfg.thresholdPositionNear;
314  thresholdPositionReached = cfg.thresholdPositionReached;
315 
316  maxJointVelocity = cfg.maxJointVelocity;
317  maxNullspaceVelocity = cfg.maxNullspaceVelocity;
318 
319  nullspaceTargetKp = cfg.nullspaceTargetKp;
320 
321  pcTcp.maxPosVel = cfg.maxTcpPosVel;
322  pcTcp.maxOriVel = cfg.maxTcpOriVel;
323  pcTcp.KpOri = cfg.KpOri;
324  pcTcp.KpPos = cfg.KpPos;
325  pcElb.maxPosVel = cfg.maxElbPosVel;
326  pcElb.KpPos = cfg.elbowKp;
327 
328  vcTcp.setMaxAccelerations(cfg.maxPositionAcceleration,
329  cfg.maxOrientationAcceleration,
330  cfg.maxNullspaceAcceleration);
331  if (cfg.jointCosts.size() > 0)
332  {
333  vcTcp.controller.setJointCosts(cfg.jointCosts);
334  }
335  }
336 
337  Eigen::VectorXf
338  CartesianNaturalPositionController::actualTcpVel(const Eigen::VectorXf& jointVel)
339  {
340  return (vcTcp.controller.ik->getJacobianMatrix(VirtualRobot::IKSolver::All) * jointVel);
341  }
342 
343  Eigen::VectorXf
344  CartesianNaturalPositionController::actualElbVel(const Eigen::VectorXf& jointVel)
345  {
346  return (vcElb.jacobi * jointVel);
347  }
348 } // namespace armarx
armarx::CartesianNaturalPositionController::maxNullspaceVelocity
std::vector< float > maxNullspaceVelocity
Definition: CartesianNaturalPositionController.h:115
armarx::CartesianVelocityController::jacobi
Eigen::MatrixXf jacobi
Definition: CartesianVelocityController.h:68
Eigen
Definition: Elements.h:32
armarx::CartesianVelocityController::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityController.cpp:80
armarx::CartesianNaturalPositionController::clearFeedForwardVelocity
void clearFeedForwardVelocity()
Definition: CartesianNaturalPositionController.cpp:221
armarx::CartesianNaturalPositionController::clearNullspaceTarget
void clearNullspaceTarget()
Definition: CartesianNaturalPositionController.cpp:215
armarx::CartesianNaturalPositionController::autoClearFeedForward
bool autoClearFeedForward
Definition: CartesianNaturalPositionController.h:120
VirtualRobot
Definition: FramedPose.h:42
armarx::CartesianNaturalPositionController::pcElb
CartesianPositionController pcElb
Definition: CartesianNaturalPositionController.h:102
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::CartesianNaturalPositionController::calculate
const Eigen::VectorXf calculate(float dt, VirtualRobot::IKSolver::CartesianSelection mode=VirtualRobot::IKSolver::All)
Definition: CartesianNaturalPositionController.cpp:111
armarx::CartesianNaturalPositionController::setNullspaceTarget
void setNullspaceTarget(const Eigen::VectorXf &nullspaceTarget)
Definition: CartesianNaturalPositionController.cpp:198
armarx::CartesianNaturalPositionController::calculateNullspaceTargetDifference
const Eigen::VectorXf calculateNullspaceTargetDifference()
Definition: CartesianNaturalPositionController.cpp:88
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::CartesianNaturalPositionController::pcTcp
CartesianPositionController pcTcp
Definition: CartesianNaturalPositionController.h:100
armarx::CartesianNaturalPositionController::nullspaceTargetKp
float nullspaceTargetKp
Definition: CartesianNaturalPositionController.h:126
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:82
armarx::CartesianPositionController::calculatePos
Eigen::VectorXf calculatePos(const Eigen::Vector3f &targetPos) const
Definition: CartesianPositionController.cpp:87
armarx::CartesianNaturalPositionController::jointLimitAvoidanceKp
float jointLimitAvoidanceKp
Definition: CartesianNaturalPositionController.h:125
armarx::CartesianNaturalPositionController::thresholdPositionNear
float thresholdPositionNear
Definition: CartesianNaturalPositionController.h:111
armarx::CartesianNaturalPositionController::elbowTarget
Eigen::Vector3f elbowTarget
Definition: CartesianNaturalPositionController.h:106
armarx::CartesianNaturalPositionController::getCurrentTarget
const Eigen::Matrix4f & getCurrentTarget() const
Definition: CartesianNaturalPositionController.cpp:253
CartesianNaturalPositionController.h
armarx::CartesianNaturalPositionController::setConfig
void setConfig(const CartesianNaturalPositionControllerConfig &cfg)
Definition: CartesianNaturalPositionController.cpp:306
armarx::CartesianVelocityControllerWithRamp::setMaxAccelerations
void setMaxAccelerations(float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration)
Definition: CartesianVelocityControllerWithRamp.cpp:110
armarx::CartesianNaturalPositionController::getCurrentElbowTarget
const Eigen::Vector3f & getCurrentElbowTarget() const
Definition: CartesianNaturalPositionController.cpp:265
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:104
armarx::CartesianNaturalPositionController::thresholdOrientationNear
float thresholdOrientationNear
Definition: CartesianNaturalPositionController.h:112
armarx::CartesianNaturalPositionController::setNullSpaceControl
void setNullSpaceControl(bool enabled)
Definition: CartesianNaturalPositionController.cpp:290
armarx::CartesianNaturalPositionController::LimitInfNormTo
static Eigen::VectorXf LimitInfNormTo(const Eigen::VectorXf &vec, const std::vector< float > &maxValue)
Definition: CartesianNaturalPositionController.cpp:64
armarx::CartesianNaturalPositionController::setTarget
void setTarget(const Eigen::Matrix4f &tcpTarget, const Eigen::Vector3f &elbowTarget)
Definition: CartesianNaturalPositionController.cpp:174
armarx::CartesianVelocityControllerWithRamp::getMode
VirtualRobot::IKSolver::CartesianSelection getMode()
Definition: CartesianVelocityControllerWithRamp.cpp:83
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:281
armarx::CartesianNaturalPositionController::actualElbVel
Eigen::VectorXf actualElbVel(const Eigen::VectorXf &jointVel)
Definition: CartesianNaturalPositionController.cpp:344
armarx::CartesianVelocityControllerWithRamp::controller
CartesianVelocityController controller
Definition: CartesianVelocityControllerWithRamp.h:79
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:81
armarx::CartesianNaturalPositionController::feedForwardVelocity
Eigen::Vector6f feedForwardVelocity
Definition: CartesianNaturalPositionController.h:118
armarx::CartesianNaturalPositionController::isTargetReached
bool isTargetReached() const
Definition: CartesianNaturalPositionController.cpp:239
armarx::CartesianVelocityControllerWithRamp::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, float jointLimitAvoidanceScale, float dt)
Definition: CartesianVelocityControllerWithRamp.cpp:89
armarx::CartesianNaturalPositionController::vcElb
CartesianVelocityController vcElb
Definition: CartesianNaturalPositionController.h:101
armarx::CartesianNaturalPositionController::isTargetNear
bool isTargetNear() const
Definition: CartesianNaturalPositionController.cpp:246
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::CartesianNaturalPositionController::maxJointVelocity
std::vector< float > maxJointVelocity
Definition: CartesianNaturalPositionController.h:114
armarx::CartesianNaturalPositionController::getCurrentNullspaceTarget
const Eigen::VectorXf & getCurrentNullspaceTarget() const
Definition: CartesianNaturalPositionController.cpp:271
armarx::CartesianNaturalPositionController::getOrientationError
float getOrientationError() const
Definition: CartesianNaturalPositionController.cpp:233
armarx::CartesianNaturalPositionController::thresholdOrientationReached
float thresholdOrientationReached
Definition: CartesianNaturalPositionController.h:110
armarx::CartesianNaturalPositionController::vcTcp
CartesianVelocityControllerWithRamp vcTcp
Definition: CartesianNaturalPositionController.h:99
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:171
armarx::CartesianVelocityController::ik
VirtualRobot::DifferentialIKPtr ik
Definition: CartesianVelocityController.h:70
armarx::CartesianVelocityControllerWithRamp::switchMode
void switchMode(const Eigen::VectorXf &currentJointVelocity, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityControllerWithRamp.cpp:74
armarx::CartesianNaturalPositionController::getCurrentTargetPosition
const Eigen::Vector3f getCurrentTargetPosition() const
Definition: CartesianNaturalPositionController.cpp:259
CartesianVelocityController.h
armarx::CartesianNaturalPositionController::tcpTarget
Eigen::Matrix4f tcpTarget
Definition: CartesianNaturalPositionController.h:105
ExpressionException.h
armarx::CartesianNaturalPositionController::lastJointVelocity
Eigen::VectorXf lastJointVelocity
Definition: CartesianNaturalPositionController.h:103
armarx::CartesianNaturalPositionController::hasNullspaceTarget
bool hasNullspaceTarget() const
Definition: CartesianNaturalPositionController.cpp:277
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
TimeUtil.h
armarx::CartesianNaturalPositionController::getStatusText
std::string getStatusText()
Definition: CartesianNaturalPositionController.cpp:296
armarx::CartesianNaturalPositionController::getPositionError
float getPositionError() const
Definition: CartesianNaturalPositionController.cpp:227
armarx::CartesianNaturalPositionController::thresholdPositionReached
float thresholdPositionReached
Definition: CartesianNaturalPositionController.h:109
armarx::CartesianNaturalPositionController::CartesianNaturalPositionController
CartesianNaturalPositionController(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodePtr &elbow, float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration, const VirtualRobot::RobotNodePtr &tcp=nullptr)
Definition: CartesianNaturalPositionController.cpp:39
armarx::CartesianNaturalPositionController::setFeedForwardVelocity
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
Definition: CartesianNaturalPositionController.cpp:182
Eigen::Matrix< float, 6, 1 >
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:83
armarx::CartesianVelocityController::setJointCosts
void setJointCosts(const std::vector< float > &_jointCosts)
Definition: CartesianVelocityController.cpp:341
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:80
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
min
T min(T t1, T t2)
Definition: gdiam.h:44
armarx::CartesianNaturalPositionController::actualTcpVel
Eigen::VectorXf actualTcpVel(const Eigen::VectorXf &jointVel)
Definition: CartesianNaturalPositionController.cpp:338
armarx::CartesianNaturalPositionController::nullSpaceControlEnabled
bool nullSpaceControlEnabled
Definition: CartesianNaturalPositionController.h:123
armarx::CartesianPositionController::getOrientationError
float getOrientationError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:110
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:45
armarx::CartesianNaturalPositionController::nullspaceTarget
Eigen::VectorXf nullspaceTarget
Definition: CartesianNaturalPositionController.h:107
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:45