CartesianPositionController.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 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 
25 
28 
29 namespace armarx
30 {
31  CartesianPositionController::CartesianPositionController(const VirtualRobot::RobotNodePtr& tcp,
32  const VirtualRobot::RobotNodePtr& referenceFrame)
33  : tcp(tcp), referenceFrame(referenceFrame ? referenceFrame : tcp->getRobot()->getRootNode())
34  {
35  }
36 
38  {
40  int posLen = mode & VirtualRobot::IKSolver::Position ? 3 : 0;
41  int oriLen = mode & VirtualRobot::IKSolver::Orientation ? 3 : 0;
42  Eigen::VectorXf cartesianVel(posLen + oriLen);
43 
44  if (posLen)
45  {
47  Eigen::Vector3f targetPos = targetPose.block<3, 1>(0, 3);
48  Eigen::Vector3f currentPos = tcp->getPositionInFrame(referenceFrame);
49  Eigen::Vector3f errPos = targetPos - currentPos;
50  Eigen::Vector3f posVel = errPos * KpPos;
51  if (maxPosVel > 0)
52  {
53  posVel = math::MathUtils::LimitTo(posVel, maxPosVel);
54  }
55  cartesianVel.block<3, 1>(0, 0) = posVel;
56  }
57 
58  if (oriLen)
59  {
61  Eigen::Matrix3f targetOri = targetPose.block<3, 3>(0, 0);
62  Eigen::Matrix3f tcpOri = tcp->getPoseInFrame(referenceFrame).block<3, 3>(0, 0);
63  Eigen::Matrix3f oriDir = targetOri * tcpOri.inverse();
64  Eigen::AngleAxisf aa(oriDir);
65  Eigen::Vector3f errOri = aa.axis() * aa.angle();
66  Eigen::Vector3f oriVel = errOri * KpOri;
67 
68  if (maxOriVel > 0)
69  {
70  oriVel = math::MathUtils::LimitTo(oriVel, maxOriVel);
71  }
72  cartesianVel.block<3, 1>(posLen, 0) = oriVel;
73  }
74  return cartesianVel;
75  }
76 
77  Eigen::VectorXf CartesianPositionController::calculatePos(const Eigen::Vector3f& targetPos) const
78  {
80  Eigen::VectorXf cartesianVel(3);
81  Eigen::Vector3f currentPos = tcp->getPositionInFrame(referenceFrame);
82  Eigen::Vector3f errPos = targetPos - currentPos;
83  Eigen::Vector3f posVel = errPos * KpPos;
84  if (maxPosVel > 0)
85  {
87  posVel = math::MathUtils::LimitTo(posVel, maxPosVel);
88  }
89  cartesianVel.block<3, 1>(0, 0) = posVel;
90  return cartesianVel;
91  }
92 
94  {
95  return GetPositionError(targetPose, tcp, referenceFrame);
96  }
97 
99  {
100  return GetOrientationError(targetPose, tcp, referenceFrame);
101  }
102 
104  const VirtualRobot::RobotNodePtr& tcp,
105  const VirtualRobot::RobotNodePtr& referenceFrame)
106  {
107  ARMARX_TRACE;
108  Eigen::Vector3f targetPos = targetPose.block<3, 1>(0, 3);
109  Eigen::Vector3f tcpPos = referenceFrame ? tcp->getPositionInFrame(referenceFrame) : tcp->getPositionInRootFrame();
110  Eigen::Vector3f errPos = targetPos - tcpPos;
111  return errPos.norm();
112  }
113 
115  const VirtualRobot::RobotNodePtr& tcp,
116  const VirtualRobot::RobotNodePtr& referenceFrame)
117  {
118  ARMARX_TRACE;
119  Eigen::Matrix3f targetOri = targetPose.block<3, 3>(0, 0);
120  Eigen::Matrix4f tcpPose = referenceFrame ? tcp->getPoseInFrame(referenceFrame) : tcp->getPoseInRootFrame();
121  Eigen::Matrix3f tcpOri = tcpPose.block<3, 3>(0, 0);
122  Eigen::Matrix3f oriDir = targetOri * tcpOri.inverse();
123  Eigen::AngleAxisf aa(oriDir);
124  return aa.angle();
125  }
126 
128  const VirtualRobot::RobotNodePtr& tcp,
129  bool checkOri,
130  float thresholdPosReached,
131  float thresholdOriReached,
132  const VirtualRobot::RobotNodePtr& referenceFrame)
133  {
134  return GetPositionError(targetPose, tcp, referenceFrame) < thresholdPosReached
135  && (!checkOri || GetOrientationError(targetPose, tcp, referenceFrame) < thresholdOriReached);
136  }
137 
138  bool CartesianPositionController::reached(const Eigen::Matrix4f& targetPose, VirtualRobot::IKSolver::CartesianSelection mode, float thresholdPosReached, float thresholdOriReached)
139  {
140  ARMARX_TRACE;
142  {
143  if (GetPositionError(targetPose, tcp, referenceFrame) > thresholdPosReached)
144  {
145  return false;
146  }
147  }
149  {
150  if (GetOrientationError(targetPose, tcp, referenceFrame) > thresholdOriReached)
151  {
152  return false;
153  }
154  }
155  return true;
156  }
157 
158  Eigen::Vector3f CartesianPositionController::getPositionDiff(const Eigen::Matrix4f& targetPose) const
159  {
160  ARMARX_TRACE;
161  Eigen::Vector3f targetPos = targetPose.block<3, 1>(0, 3);
162  return targetPos - tcp->getPositionInFrame(referenceFrame);
163  }
164 
165  Eigen::Vector3f CartesianPositionController::getPositionDiffVec3(const Eigen::Vector3f& targetPosition) const
166  {
167  return targetPosition - tcp->getPositionInFrame(referenceFrame);
168  }
169 
170  Eigen::Vector3f CartesianPositionController::getOrientationDiff(const Eigen::Matrix4f& targetPose) const
171  {
172  ARMARX_TRACE;
173  Eigen::Matrix3f targetOri = targetPose.block<3, 3>(0, 0);
174  Eigen::Matrix3f tcpOri = tcp->getPoseInFrame(referenceFrame).block<3, 3>(0, 0);
175  Eigen::Matrix3f oriDir = targetOri * tcpOri.inverse();
176  Eigen::AngleAxisf aa(oriDir);
177  return aa.axis() * aa.angle();
178  }
179 
180  VirtualRobot::RobotNodePtr CartesianPositionController::getTcp() const
181  {
182  return tcp;
183  }
184 
185 
186 
187 #define SS_OUT(x) ss << #x << " = " << x << "\n"
188 #define SET_FLT(x) obj->setFloat(#x, x)
189 #define GET_FLT(x) x = obj->getFloat(#x);
190 
192  {
193  }
194 
195  std::string CartesianPositionControllerConfig::output(const Ice::Current&) const
196  {
197  std::stringstream ss;
198 
199  SS_OUT(KpPos);
200  SS_OUT(KpOri);
201  SS_OUT(maxPosVel);
202  SS_OUT(maxOriVel);
203  SS_OUT(thresholdOrientationNear);
204  SS_OUT(thresholdOrientationReached);
205  SS_OUT(thresholdPositionNear);
206  SS_OUT(thresholdPositionReached);
207 
208  return ss.str();
209  }
210 
211  void CartesianPositionControllerConfig::serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
212  {
213  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
214 
215  SET_FLT(KpPos);
216  SET_FLT(KpOri);
217  SET_FLT(maxPosVel);
218  SET_FLT(maxOriVel);
219  SET_FLT(thresholdOrientationNear);
220  SET_FLT(thresholdOrientationReached);
221  SET_FLT(thresholdPositionNear);
222  SET_FLT(thresholdPositionReached);
223 
224  }
225 
226  void CartesianPositionControllerConfig::deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
227  {
228  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
229 
230  GET_FLT(KpPos);
231  GET_FLT(KpOri);
232  GET_FLT(maxPosVel);
233  GET_FLT(maxOriVel);
234  GET_FLT(thresholdOrientationNear);
235  GET_FLT(thresholdOrientationReached);
236  GET_FLT(thresholdPositionNear);
237  GET_FLT(thresholdPositionReached);
238 
239  }
240 }
armarx::CartesianPositionController::CartesianPositionController
CartesianPositionController(const VirtualRobot::RobotNodePtr &tcp, const VirtualRobot::RobotNodePtr &referenceFrame=nullptr)
Definition: CartesianPositionController.cpp:31
MathUtils.h
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:9
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:76
armarx::CartesianPositionController::calculatePos
Eigen::VectorXf calculatePos(const Eigen::Vector3f &targetPos) const
Definition: CartesianPositionController.cpp:77
trace.h
armarx::CartesianPositionController::GetOrientationError
static float GetOrientationError(const Eigen::Matrix4f &targetPose, const VirtualRobot::RobotNodePtr &tcp, const VirtualRobot::RobotNodePtr &referenceFrame=nullptr)
Definition: CartesianPositionController.cpp:114
armarx::CartesianPositionController::getPositionDiff
Eigen::Vector3f getPositionDiff(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:158
SET_FLT
#define SET_FLT(x)
Definition: CartesianPositionController.cpp:188
armarx::CartesianPositionController::reached
bool reached(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode, float thresholdPosReached, float thresholdOriReached)
Definition: CartesianPositionController.cpp:138
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::CartesianPositionController::getPositionDiffVec3
Eigen::Vector3f getPositionDiffVec3(const Eigen::Vector3f &targetPosition) const
Definition: CartesianPositionController.cpp:165
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::CartesianPositionControllerConfig::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=::Ice::Current()) const override
Definition: CartesianPositionController.cpp:211
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:75
armarx::CartesianPositionController::GetPositionError
static float GetPositionError(const Eigen::Matrix4f &targetPose, const VirtualRobot::RobotNodePtr &tcp, const VirtualRobot::RobotNodePtr &referenceFrame=nullptr)
Definition: CartesianPositionController.cpp:103
GET_FLT
#define GET_FLT(x)
Definition: CartesianPositionController.cpp:189
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::CartesianPositionControllerConfig::CartesianPositionControllerConfig
CartesianPositionControllerConfig()
Definition: CartesianPositionController.cpp:191
armarx::CartesianPositionController::getTcp
VirtualRobot::RobotNodePtr getTcp() const
Definition: CartesianPositionController.cpp:180
CartesianPositionController.h
armarx::CartesianPositionControllerConfig::output
std::string output(const Ice::Current &c=::Ice::Current()) const override
Definition: CartesianPositionController.cpp:195
armarx::math::MathUtils::LimitTo
static double LimitTo(double value, double absThreshold)
Definition: MathUtils.h:56
armarx::CartesianPositionController::getOrientationDiff
Eigen::Vector3f getOrientationDiff(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:170
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:77
armarx::CartesianPositionController::Reached
static bool Reached(const Eigen::Matrix4f &targetPose, const VirtualRobot::RobotNodePtr &tcp, bool checkOri, float thresholdPosReached, float thresholdOriReached, const VirtualRobot::RobotNodePtr &referenceFrame=nullptr)
Definition: CartesianPositionController.cpp:127
armarx::CartesianPositionControllerConfig::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=::Ice::Current()) override
Definition: CartesianPositionController.cpp:226
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:74
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
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
SS_OUT
#define SS_OUT(x)
Definition: CartesianPositionController.cpp:187
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:37