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 
27 
29 
31 
32 #include <VirtualRobot/math/Helpers.h>
33 #include <VirtualRobot/MathTools.h>
34 
35 namespace armarx
36 {
37  CartesianNaturalPositionController::CartesianNaturalPositionController(const VirtualRobot::RobotNodeSetPtr& rns, const VirtualRobot::RobotNodePtr& elbow,
38  float maxPositionAcceleration,
39  float maxOrientationAcceleration,
40  float maxNullspaceAcceleration,
41  const VirtualRobot::RobotNodePtr& tcp)
42  :
43  vcTcp(rns,
44  Eigen::VectorXf::Constant(rns->getSize(), 0.f),
45  VirtualRobot::IKSolver::CartesianSelection::All,
46  maxPositionAcceleration,
47  maxOrientationAcceleration,
48  maxNullspaceAcceleration,
49  tcp ? tcp : rns->getTCP()),
50  pcTcp(tcp ? tcp : rns->getTCP()),
51  vcElb(rns, elbow),
52  pcElb(elbow),
53  lastJointVelocity(Eigen::VectorXf::Constant(rns->getSize(), 0.f)),
54  rns(rns)
55  {
58  }
59 
60  Eigen::VectorXf CartesianNaturalPositionController::LimitInfNormTo(const Eigen::VectorXf& vec, const std::vector<float>& maxValue)
61  {
62  if (maxValue.size() == 0)
63  {
64  return vec;
65  }
66  if (maxValue.size() != 1 && (int)maxValue.size() != vec.rows())
67  {
68  throw std::invalid_argument("maxValue.size != 1 and != maxValue.size");
69  }
70  float scale = 1;
71  for (int i = 0; i < vec.rows(); i++)
72  {
73  int j = maxValue.size() == 1 ? 0 : i;
74  if (std::abs(vec(i)) > maxValue.at(j) && maxValue.at(j) >= 0)
75  {
76  scale = std::min(scale, maxValue.at(j) / std::abs(vec(i)));
77  }
78  }
79  return vec * scale;
80  }
81 
83  {
84  Eigen::VectorXf jvNT = Eigen::VectorXf(rns->getSize());
85  for (size_t i = 0; i < rns->getSize(); i++)
86  {
87  if (std::isnan(nullspaceTarget(i)))
88  {
89  jvNT(i) = 0;
90  }
91  else
92  {
93  float diff = nullspaceTarget(i) - rns->getNode(i)->getJointValue();
94  if (rns->getNode(i)->isLimitless())
95  {
96  diff = ::math::Helpers::AngleModPI(diff);
97  }
98  jvNT(i) = diff;
99  }
100  }
101  return jvNT;
102  }
103 
105  {
106 
107  //int posLen = mode & VirtualRobot::IKSolver::Position ? 3 : 0;
108  //int oriLen = mode & VirtualRobot::IKSolver::Orientation ? 3 : 0;
109  //Eigen::Vector3f pdTcp = posLen ? pcTcp.getPositionDiff(tcpTarget) : Eigen::Vector3f::Zero();
110  //Eigen::Vector3f odTcp = oriLen ? pcTcp.getOrientationDiff(tcpTarget) : Eigen::Vector3f::Zero();
111  //Eigen::VectorXf cartesianVel(posLen + oriLen);
112  //if (posLen)
113  //{
114  // cartesianVel.block<3, 1>(0, 0) = pdTcp;
115  //}
116  //if (oriLen)
117  //{
118  // cartesianVel.block<3, 1>(posLen, 0) = odTcp;
119  //}
120  Eigen::VectorXf cartesianVelTcp = pcTcp.calculate(tcpTarget, mode);
121 
122  //Eigen::Vector3f pdElb = pcElb.getPositionDiffVec3(elbowTarget);
123  ////ARMARX_IMPORTANT << deactivateSpam(0.1) << VAROUT(pdElb) << VAROUT(elbowTarget) << VAROUT(pcElb.getTcp()->getPositionInRootFrame());
124  //Eigen::VectorXf cartesianVelElb(3);
125  //cartesianVelElb.block<3, 1>(0, 0) = pdElb;
126 
127  Eigen::VectorXf cartesianVelElb = pcElb.calculatePos(elbowTarget);
128  Eigen::VectorXf jnvClamped = Eigen::VectorXf::Zero(rns->getSize());
130  {
131  Eigen::VectorXf jnv = Eigen::VectorXf::Zero(rns->getSize());
132  Eigen::VectorXf jvElb = vcElb.calculate(cartesianVelElb, VirtualRobot::IKSolver::Position);
133 
135  Eigen::VectorXf jvNT = nullspaceTargetKp * calculateNullspaceTargetDifference();
136  //ARMARX_IMPORTANT << deactivateSpam(1) << VAROUT(jvNT);
137 
138  //ARMARX_IMPORTANT << VAROUT(jvElb);
139  jnv = jvElb + jvLA + jvNT;
140  jnvClamped = LimitInfNormTo(jnv, maxNullspaceVelocity);
141  }
142 
143  //ARMARX_IMPORTANT << VAROUT(jnv);
144  if (vcTcp.getMode() != mode)
145  {
147  }
148  Eigen::VectorXf jv = vcTcp.calculate(cartesianVelTcp, jnvClamped, dt);
149  //ARMARX_IMPORTANT << VAROUT(jv);
150 
151  Eigen::VectorXf jvClamped = LimitInfNormTo(jv, maxJointVelocity);
152 
154  {
156  }
157 
158  lastJointVelocity = jvClamped;
159 
160  return jvClamped;
161  }
162 
163 
164  void CartesianNaturalPositionController::setTarget(const Eigen::Matrix4f& tcpTarget, const Eigen::Vector3f& elbowTarget)
165  {
166  this->tcpTarget = tcpTarget;
167  this->elbowTarget = elbowTarget;
168  }
169 
171  {
172  this->feedForwardVelocity = feedForwardVelocity;
173  }
174 
175  void CartesianNaturalPositionController::setFeedForwardVelocity(const Eigen::Vector3f& feedForwardVelocityPos, const Eigen::Vector3f& feedForwardVelocityOri)
176  {
177  feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
178  feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
179  }
180 
181  void CartesianNaturalPositionController::setNullspaceTarget(const Eigen::VectorXf& nullspaceTarget)
182  {
183  this->nullspaceTarget = nullspaceTarget;
184  }
185 
186  void CartesianNaturalPositionController::setNullspaceTarget(const std::vector<float>& nullspaceTarget)
187  {
188  ARMARX_CHECK_EXPRESSION(rns->getSize() == nullspaceTarget.size());
189  for (size_t i = 0; i < rns->getSize(); i++)
190  {
191  this->nullspaceTarget(i) = nullspaceTarget.at(i);
192  }
193  }
194 
196  {
197  this->nullspaceTarget = Eigen::VectorXf::Constant(rns->getSize(), std::nanf(""));
198  }
199 
201  {
202  feedForwardVelocity = Eigen::Vector6f::Zero();
203  }
204 
206  {
208  }
209 
211  {
213  }
214 
216  {
218  }
219 
221  {
223  }
224 
226  {
227  return tcpTarget;
228  }
229 
231  {
232  return ::math::Helpers::GetPosition(tcpTarget);
233  }
234 
236  {
237  return elbowTarget;
238  }
239 
241  {
242  return nullspaceTarget;
243  }
244 
246  {
247  for (int i = 0; i < nullspaceTarget.rows(); i++)
248  {
249  if (!std::isnan(nullspaceTarget(i)))
250  {
251  return true;
252  }
253  }
254  return false;
255  }
256 
258  {
260  }
261 
263  {
264  std::stringstream ss;
265  ss.precision(2);
266  ss << std::fixed << "distance: " << getPositionError() << " mm " << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
267  return ss.str();
268  }
269 
270  void CartesianNaturalPositionController::setConfig(const CartesianNaturalPositionControllerConfig& cfg)
271  {
272  jointLimitAvoidanceKp = cfg.jointLimitAvoidanceKp;
273 
274  thresholdOrientationNear = cfg.thresholdOrientationNear;
275  thresholdOrientationReached = cfg.thresholdOrientationReached;
276  thresholdPositionNear = cfg.thresholdPositionNear;
277  thresholdPositionReached = cfg.thresholdPositionReached;
278 
279  maxJointVelocity = cfg.maxJointVelocity;
280  maxNullspaceVelocity = cfg.maxNullspaceVelocity;
281 
282  nullspaceTargetKp = cfg.nullspaceTargetKp;
283 
284  pcTcp.maxPosVel = cfg.maxTcpPosVel;
285  pcTcp.maxOriVel = cfg.maxTcpOriVel;
286  pcTcp.KpOri = cfg.KpOri;
287  pcTcp.KpPos = cfg.KpPos;
288  pcElb.maxPosVel = cfg.maxElbPosVel;
289  pcElb.KpPos = cfg.elbowKp;
290 
292  cfg.maxPositionAcceleration,
293  cfg.maxOrientationAcceleration,
294  cfg.maxNullspaceAcceleration
295  );
296  if (cfg.jointCosts.size() > 0)
297  {
298  vcTcp.controller.setJointCosts(cfg.jointCosts);
299  }
300  }
301 
302  Eigen::VectorXf CartesianNaturalPositionController::actualTcpVel(const Eigen::VectorXf& jointVel)
303  {
304  return (vcTcp.controller.ik->getJacobianMatrix(VirtualRobot::IKSolver::All) * jointVel);
305  }
306 
307  Eigen::VectorXf CartesianNaturalPositionController::actualElbVel(const Eigen::VectorXf& jointVel)
308  {
309  return (vcElb.jacobi * jointVel);
310  }
311 }
armarx::CartesianNaturalPositionController::maxNullspaceVelocity
std::vector< float > maxNullspaceVelocity
Definition: CartesianNaturalPositionController.h:112
armarx::CartesianVelocityController::jacobi
Eigen::MatrixXf jacobi
Definition: CartesianVelocityController.h:58
Eigen
Definition: Elements.h:36
armarx::CartesianVelocityController::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityController.cpp:71
armarx::CartesianNaturalPositionController::clearFeedForwardVelocity
void clearFeedForwardVelocity()
Definition: CartesianNaturalPositionController.cpp:200
armarx::CartesianNaturalPositionController::clearNullspaceTarget
void clearNullspaceTarget()
Definition: CartesianNaturalPositionController.cpp:195
armarx::CartesianNaturalPositionController::autoClearFeedForward
bool autoClearFeedForward
Definition: CartesianNaturalPositionController.h:117
VirtualRobot
Definition: FramedPose.h:43
armarx::CartesianNaturalPositionController::pcElb
CartesianPositionController pcElb
Definition: CartesianNaturalPositionController.h:99
armarx::CartesianNaturalPositionController::calculate
const Eigen::VectorXf calculate(float dt, VirtualRobot::IKSolver::CartesianSelection mode=VirtualRobot::IKSolver::All)
Definition: CartesianNaturalPositionController.cpp:104
armarx::CartesianNaturalPositionController::setNullspaceTarget
void setNullspaceTarget(const Eigen::VectorXf &nullspaceTarget)
Definition: CartesianNaturalPositionController.cpp:181
armarx::CartesianNaturalPositionController::calculateNullspaceTargetDifference
const Eigen::VectorXf calculateNullspaceTargetDifference()
Definition: CartesianNaturalPositionController.cpp:82
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:97
armarx::CartesianNaturalPositionController::nullspaceTargetKp
float nullspaceTargetKp
Definition: CartesianNaturalPositionController.h:123
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:76
armarx::CartesianPositionController::calculatePos
Eigen::VectorXf calculatePos(const Eigen::Vector3f &targetPos) const
Definition: CartesianPositionController.cpp:77
armarx::CartesianNaturalPositionController::jointLimitAvoidanceKp
float jointLimitAvoidanceKp
Definition: CartesianNaturalPositionController.h:122
armarx::CartesianNaturalPositionController::thresholdPositionNear
float thresholdPositionNear
Definition: CartesianNaturalPositionController.h:108
armarx::CartesianNaturalPositionController::elbowTarget
Eigen::Vector3f elbowTarget
Definition: CartesianNaturalPositionController.h:103
armarx::CartesianNaturalPositionController::getCurrentTarget
const Eigen::Matrix4f & getCurrentTarget() const
Definition: CartesianNaturalPositionController.cpp:225
CartesianNaturalPositionController.h
armarx::CartesianNaturalPositionController::setConfig
void setConfig(const CartesianNaturalPositionControllerConfig &cfg)
Definition: CartesianNaturalPositionController.cpp:270
armarx::CartesianVelocityControllerWithRamp::setMaxAccelerations
void setMaxAccelerations(float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration)
Definition: CartesianVelocityControllerWithRamp.cpp:84
armarx::CartesianNaturalPositionController::getCurrentElbowTarget
const Eigen::Vector3f & getCurrentElbowTarget() const
Definition: CartesianNaturalPositionController.cpp:235
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::CartesianNaturalPositionController::thresholdOrientationNear
float thresholdOrientationNear
Definition: CartesianNaturalPositionController.h:109
armarx::CartesianNaturalPositionController::setNullSpaceControl
void setNullSpaceControl(bool enabled)
Definition: CartesianNaturalPositionController.cpp:257
armarx::CartesianNaturalPositionController::LimitInfNormTo
static Eigen::VectorXf LimitInfNormTo(const Eigen::VectorXf &vec, const std::vector< float > &maxValue)
Definition: CartesianNaturalPositionController.cpp:60
armarx::CartesianNaturalPositionController::setTarget
void setTarget(const Eigen::Matrix4f &tcpTarget, const Eigen::Vector3f &elbowTarget)
Definition: CartesianNaturalPositionController.cpp:164
armarx::CartesianVelocityControllerWithRamp::getMode
VirtualRobot::IKSolver::CartesianSelection getMode()
Definition: CartesianVelocityControllerWithRamp.cpp:67
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::CartesianNaturalPositionController::actualElbVel
Eigen::VectorXf actualElbVel(const Eigen::VectorXf &jointVel)
Definition: CartesianNaturalPositionController.cpp:307
armarx::CartesianVelocityControllerWithRamp::controller
CartesianVelocityController controller
Definition: CartesianVelocityControllerWithRamp.h:68
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:75
armarx::CartesianNaturalPositionController::feedForwardVelocity
Eigen::Vector6f feedForwardVelocity
Definition: CartesianNaturalPositionController.h:115
armarx::CartesianNaturalPositionController::isTargetReached
bool isTargetReached() const
Definition: CartesianNaturalPositionController.cpp:215
armarx::CartesianVelocityControllerWithRamp::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, float jointLimitAvoidanceScale, float dt)
Definition: CartesianVelocityControllerWithRamp.cpp:72
armarx::CartesianNaturalPositionController::vcElb
CartesianVelocityController vcElb
Definition: CartesianNaturalPositionController.h:98
armarx::CartesianNaturalPositionController::isTargetNear
bool isTargetNear() const
Definition: CartesianNaturalPositionController.cpp:220
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:111
armarx::CartesianNaturalPositionController::getCurrentNullspaceTarget
const Eigen::VectorXf & getCurrentNullspaceTarget() const
Definition: CartesianNaturalPositionController.cpp:240
armarx::CartesianNaturalPositionController::getOrientationError
float getOrientationError() const
Definition: CartesianNaturalPositionController.cpp:210
armarx::CartesianNaturalPositionController::thresholdOrientationReached
float thresholdOrientationReached
Definition: CartesianNaturalPositionController.h:107
armarx::CartesianNaturalPositionController::vcTcp
CartesianVelocityControllerWithRamp vcTcp
Definition: CartesianNaturalPositionController.h:96
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:152
armarx::CartesianVelocityController::ik
VirtualRobot::DifferentialIKPtr ik
Definition: CartesianVelocityController.h:60
armarx::CartesianVelocityControllerWithRamp::switchMode
void switchMode(const Eigen::VectorXf &currentJointVelocity, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityControllerWithRamp.cpp:60
armarx::CartesianNaturalPositionController::getCurrentTargetPosition
const Eigen::Vector3f getCurrentTargetPosition() const
Definition: CartesianNaturalPositionController.cpp:230
CartesianVelocityController.h
armarx::CartesianNaturalPositionController::tcpTarget
Eigen::Matrix4f tcpTarget
Definition: CartesianNaturalPositionController.h:102
ExpressionException.h
armarx::CartesianNaturalPositionController::lastJointVelocity
Eigen::VectorXf lastJointVelocity
Definition: CartesianNaturalPositionController.h:100
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::CartesianNaturalPositionController::hasNullspaceTarget
bool hasNullspaceTarget() const
Definition: CartesianNaturalPositionController.cpp:245
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:262
armarx::CartesianNaturalPositionController::getPositionError
float getPositionError() const
Definition: CartesianNaturalPositionController.cpp:205
armarx::CartesianNaturalPositionController::thresholdPositionReached
float thresholdPositionReached
Definition: CartesianNaturalPositionController.h:106
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:37
armarx::CartesianNaturalPositionController::setFeedForwardVelocity
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
Definition: CartesianNaturalPositionController.cpp:170
Eigen::Matrix< float, 6, 1 >
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:77
armarx::CartesianVelocityController::setJointCosts
void setJointCosts(const std::vector< float > &_jointCosts)
Definition: CartesianVelocityController.cpp:301
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:74
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::CartesianNaturalPositionController::actualTcpVel
Eigen::VectorXf actualTcpVel(const Eigen::VectorXf &jointVel)
Definition: CartesianNaturalPositionController.cpp:302
armarx::CartesianNaturalPositionController::nullSpaceControlEnabled
bool nullSpaceControlEnabled
Definition: CartesianNaturalPositionController.h:120
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
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:42
armarx::CartesianNaturalPositionController::nullspaceTarget
Eigen::VectorXf nullspaceTarget
Definition: CartesianNaturalPositionController.h:104
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:37