CartesianWaypointController.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 #include <cfloat>
36 
37 namespace armarx
38 {
40  const VirtualRobot::RobotNodeSetPtr& rns,
41  const Eigen::VectorXf& currentJointVelocity,
42  float maxPositionAcceleration,
43  float maxOrientationAcceleration,
44  float maxNullspaceAcceleration,
45  const VirtualRobot::RobotNodePtr& tcp,
46  const VirtualRobot::RobotNodePtr& referenceFrame
47  ) :
48  ctrlCartesianVelWithRamps
49  {
50  rns,
51  currentJointVelocity,
52  VirtualRobot::IKSolver::CartesianSelection::All,
53  maxPositionAcceleration,
54  maxOrientationAcceleration,
55  maxNullspaceAcceleration,
56  tcp ? tcp : rns->getTCP()
57  },
58  ctrlCartesianPos2Vel(tcp ? tcp : rns->getTCP(), referenceFrame),
59  currentWaypointIndex(0)
60  {
62  _out = Eigen::VectorXf::Zero(rns->getSize());
63  _jnv = Eigen::VectorXf::Zero(rns->getSize());
64  }
65 
66  const Eigen::VectorXf& CartesianWaypointController::calculate(float dt)
67  {
69  //calculate cartesian velocity + some management stuff
71  {
74  }
76 
78  {
81  }
82 
83  //calculate joint velocity
85  {
87  //avoid joint limits
92  VirtualRobot::IKSolver::All
93  );
94  }
95  else
96  {
98  //don't avoid joint limits
99  _jnv *= 0;
100  }
101  ARMARX_TRACE;
103  return _out;
104  }
105 
106  void CartesianWaypointController::setWaypoints(const std::vector<Eigen::Matrix4f>& waypoints)
107  {
108  this->waypoints = waypoints;
110  }
111 
112  void CartesianWaypointController::swapWaypoints(std::vector<Eigen::Matrix4f>& waypoints)
113  {
114  std::swap(this->waypoints, waypoints);
116  }
117 
119  {
120  this->waypoints.push_back(waypoint);
121  }
122 
124  {
125  waypoints.clear();
126  waypoints.push_back(target);
128  }
129 
131  {
132  this->feedForwardVelocity = feedForwardVelocity;
133  }
134 
135  void CartesianWaypointController::setFeedForwardVelocity(const Eigen::Vector3f& feedForwardVelocityPos, const Eigen::Vector3f& feedForwardVelocityOri)
136  {
137  feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
138  feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
139  }
140 
142  {
143  feedForwardVelocity = Eigen::Vector6f::Zero();
144  }
145 
147  {
149  }
150 
152  {
154  }
155 
157  {
159  }
160 
162  {
164  }
165 
167  {
169  }
170 
172  {
173  return isLastWaypoint() && isCurrentTargetNear();
174  }
175 
177  {
178  return currentWaypointIndex + 1 >= waypoints.size();
179  }
180 
182  {
183  return waypoints.at(currentWaypointIndex);
184  }
185 
187  {
188  ARMARX_TRACE;
189  return ::math::Helpers::GetPosition(waypoints.at(currentWaypointIndex));
190  }
191 
193  {
194  ARMARX_TRACE;
195  float dist = FLT_MAX;
196  size_t minIndex = 0;
197  for (size_t i = 0; i < waypoints.size(); i++)
198  {
199  float posErr = ctrlCartesianPos2Vel.getPositionError(waypoints.at(i));
200  float oriErr = ctrlCartesianPos2Vel.getOrientationError(waypoints.at(i));
201  float d = posErr + oriErr * rad2mmFactor;
202  if (d < dist)
203  {
204  minIndex = i;
205  dist = d;
206  }
207  }
208  currentWaypointIndex = minIndex;
209  return currentWaypointIndex;
210  }
211 
213  {
215  }
216 
218  {
219  std::stringstream ss;
220  ss.precision(2);
221  ss << std::fixed << "Waypoint: " << (currentWaypointIndex + 1) << "/" << waypoints.size() << " distance: " << getPositionError() << " mm " << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
222  return ss.str();
223  }
224 
225  void CartesianWaypointController::setConfig(const CartesianWaypointControllerConfig& cfg)
226  {
227  KpJointLimitAvoidance = cfg.kpJointLimitAvoidance;
228  jointLimitAvoidanceScale = cfg.jointLimitAvoidanceScale;
229 
230  thresholdOrientationNear = cfg.thresholdOrientationNear;
231  thresholdOrientationReached = cfg.thresholdOrientationReached;
232  thresholdPositionNear = cfg.thresholdPositionNear;
233  thresholdPositionReached = cfg.thresholdPositionReached;
234 
235  ctrlCartesianPos2Vel.maxOriVel = cfg.maxOriVel;
236  ctrlCartesianPos2Vel.maxPosVel = cfg.maxPosVel;
237  ctrlCartesianPos2Vel.KpOri = cfg.kpOri;
238  ctrlCartesianPos2Vel.KpPos = cfg.kpPos;
239 
241  cfg.maxPositionAcceleration,
242  cfg.maxOrientationAcceleration,
243  cfg.maxNullspaceAcceleration
244  );
245  }
246  void CartesianWaypointController::setCurrentJointVelocity(const Eigen::Ref<const Eigen::VectorXf>& currentJointVelocity)
247  {
248 #pragma GCC diagnostic push
249 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
251 #pragma GCC diagnostic pop
252  }
253 }
armarx::CartesianWaypointController::getOrientationError
float getOrientationError() const
Definition: CartesianWaypointController.cpp:151
armarx::CartesianWaypointController::thresholdOrientationReached
float thresholdOrientationReached
Definition: CartesianWaypointController.h:104
armarx::CartesianWaypointController::waypoints
std::vector< Eigen::Matrix4f > waypoints
Definition: CartesianWaypointController.h:100
armarx::CartesianWaypointController::setNullSpaceControl
void setNullSpaceControl(bool enabled)
Definition: CartesianWaypointController.cpp:212
CartesianWaypointController.h
armarx::CartesianWaypointController::isCurrentTargetNear
bool isCurrentTargetNear() const
Definition: CartesianWaypointController.cpp:161
armarx::CartesianWaypointController::setCurrentJointVelocity
void setCurrentJointVelocity(const Eigen::Ref< const Eigen::VectorXf > &currentJointVelocity)
Definition: CartesianWaypointController.cpp:246
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::CartesianWaypointController::getCurrentTargetPosition
const Eigen::Vector3f getCurrentTargetPosition() const
Definition: CartesianWaypointController.cpp:186
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:76
armarx::CartesianWaypointController::ctrlCartesianPos2Vel
CartesianPositionController ctrlCartesianPos2Vel
Definition: CartesianWaypointController.h:98
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
trace.h
armarx::CartesianWaypointController::nullSpaceControlEnabled
bool nullSpaceControlEnabled
Definition: CartesianWaypointController.h:113
armarx::CartesianWaypointController::skipToClosestWaypoint
size_t skipToClosestWaypoint(float rad2mmFactor)
Definition: CartesianWaypointController.cpp:192
armarx::CartesianWaypointController::isLastWaypoint
bool isLastWaypoint() const
Definition: CartesianWaypointController.cpp:176
armarx::CartesianWaypointController::getPositionError
float getPositionError() const
Definition: CartesianWaypointController.cpp:146
armarx::CartesianWaypointController::clearFeedForwardVelocity
void clearFeedForwardVelocity()
Definition: CartesianWaypointController.cpp:141
armarx::CartesianWaypointController::swapWaypoints
void swapWaypoints(std::vector< Eigen::Matrix4f > &waypoints)
Definition: CartesianWaypointController.cpp:112
armarx::CartesianVelocityControllerWithRamp::setMaxAccelerations
void setMaxAccelerations(float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration)
Definition: CartesianVelocityControllerWithRamp.cpp:84
armarx::CartesianWaypointController::setFeedForwardVelocity
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
Definition: CartesianWaypointController.cpp:130
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::armem::client::util::swap
void swap(SubscriptionHandle &first, SubscriptionHandle &second)
Definition: SubscriptionHandle.cpp:66
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::CartesianWaypointController::thresholdPositionNear
float thresholdPositionNear
Definition: CartesianWaypointController.h:105
armarx::CartesianWaypointController::CartesianWaypointController
CartesianWaypointController(const VirtualRobot::RobotNodeSetPtr &rns, const Eigen::VectorXf &currentJointVelocity, float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration, const VirtualRobot::RobotNodePtr &tcp=nullptr, const VirtualRobot::RobotNodePtr &referenceFrame=nullptr)
Definition: CartesianWaypointController.cpp:39
armarx::CartesianVelocityControllerWithRamp::controller
CartesianVelocityController controller
Definition: CartesianVelocityControllerWithRamp.h:68
armarx::CartesianWaypointController::getCurrentTarget
const Eigen::Matrix4f & getCurrentTarget() const
Definition: CartesianWaypointController.cpp:181
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:75
armarx::CartesianVelocityControllerWithRamp::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, float jointLimitAvoidanceScale, float dt)
Definition: CartesianVelocityControllerWithRamp.cpp:72
armarx::CartesianWaypointController::isFinalTargetNear
bool isFinalTargetNear() const
Definition: CartesianWaypointController.cpp:171
armarx::CartesianWaypointController::isCurrentTargetReached
bool isCurrentTargetReached() const
Definition: CartesianWaypointController.cpp:156
armarx::CartesianWaypointController::cartesianVelocity
Eigen::Vector6f cartesianVelocity
Definition: CartesianWaypointController.h:109
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::CartesianWaypointController::calculate
const Eigen::VectorXf & calculate(float dt)
Definition: CartesianWaypointController.cpp:66
armarx::CartesianVelocityControllerWithRamp::setCurrentJointVelocity
void setCurrentJointVelocity(const Eigen::Ref< const Eigen::VectorXf > &currentJointVelocity)
Definition: CartesianVelocityControllerWithRamp.cpp:41
armarx::CartesianWaypointController::currentWaypointIndex
size_t currentWaypointIndex
Definition: CartesianWaypointController.h:101
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:152
armarx::CartesianWaypointController::thresholdPositionReached
float thresholdPositionReached
Definition: CartesianWaypointController.h:103
CartesianVelocityController.h
armarx::CartesianWaypointController::setWaypoints
void setWaypoints(const std::vector< Eigen::Matrix4f > &waypoints)
Definition: CartesianWaypointController.cpp:106
ExpressionException.h
armarx::CartesianWaypointController::addWaypoint
void addWaypoint(const Eigen::Matrix4f &waypoint)
Definition: CartesianWaypointController.cpp:118
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::CartesianWaypointController::setTarget
void setTarget(const Eigen::Matrix4f &target)
Definition: CartesianWaypointController.cpp:123
TimeUtil.h
armarx::CartesianWaypointController::KpJointLimitAvoidance
float KpJointLimitAvoidance
Definition: CartesianWaypointController.h:115
armarx::CartesianWaypointController::jointLimitAvoidanceScale
float jointLimitAvoidanceScale
Definition: CartesianWaypointController.h:114
armarx::CartesianWaypointController::autoClearFeedForward
bool autoClearFeedForward
Definition: CartesianWaypointController.h:110
armarx::CartesianWaypointController::isFinalTargetReached
bool isFinalTargetReached() const
Definition: CartesianWaypointController.cpp:166
armarx::CartesianWaypointController::setConfig
void setConfig(const CartesianWaypointControllerConfig &cfg)
Definition: CartesianWaypointController.cpp:225
Eigen::Matrix< float, 6, 1 >
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:77
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:74
armarx::CartesianWaypointController::feedForwardVelocity
Eigen::Vector6f feedForwardVelocity
Definition: CartesianWaypointController.h:108
armarx::CartesianVelocityController::calculateNullspaceVelocity
Eigen::VectorXf calculateNullspaceVelocity(const Eigen::VectorXf &cartesianVel, float KpScale, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: CartesianVelocityController.cpp:201
armarx::CartesianWaypointController::thresholdOrientationNear
float thresholdOrientationNear
Definition: CartesianWaypointController.h:106
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::CartesianWaypointController::ctrlCartesianVelWithRamps
CartesianVelocityControllerWithRamp ctrlCartesianVelWithRamps
Definition: CartesianWaypointController.h:97
armarx::CartesianWaypointController::getStatusText
std::string getStatusText()
Definition: CartesianWaypointController.cpp:217
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:37