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 
26 
27 #include <cfloat>
28 
29 #include <VirtualRobot/MathTools.h>
30 #include <VirtualRobot/Robot.h>
31 #include <VirtualRobot/math/Helpers.h>
32 
36 
38 
39 namespace armarx
40 {
42  const VirtualRobot::RobotNodeSetPtr& rns,
43  const Eigen::VectorXf& currentJointVelocity,
44  float maxPositionAcceleration,
45  float maxOrientationAcceleration,
46  float maxNullspaceAcceleration,
47  const VirtualRobot::RobotNodePtr& tcp,
48  const VirtualRobot::RobotNodePtr& referenceFrame) :
49  ctrlCartesianVelWithRamps{rns,
50  currentJointVelocity,
51  VirtualRobot::IKSolver::CartesianSelection::All,
52  maxPositionAcceleration,
53  maxOrientationAcceleration,
54  maxNullspaceAcceleration,
55  tcp ? tcp : rns->getTCP()},
56  ctrlCartesianPos2Vel(tcp ? tcp : rns->getTCP(), referenceFrame),
57  currentWaypointIndex(0)
58  {
60  _out = Eigen::VectorXf::Zero(rns->getSize());
61  _jnv = Eigen::VectorXf::Zero(rns->getSize());
62  }
63 
64  const Eigen::VectorXf&
66  {
68  //calculate cartesian velocity + some management stuff
70  {
73  }
75  ctrlCartesianPos2Vel.calculate(getCurrentTarget(), VirtualRobot::IKSolver::All) +
77 
79  {
82  }
83 
84  //calculate joint velocity
86  {
88  //avoid joint limits
89  _jnv = KpJointLimitAvoidance *
92  cartesianVelocity, jointLimitAvoidanceScale, VirtualRobot::IKSolver::All);
93  }
94  else
95  {
97  //don't avoid joint limits
98  _jnv *= 0;
99  }
100  ARMARX_TRACE;
102  return _out;
103  }
104 
105  void
106  CartesianWaypointController::setWaypoints(const std::vector<Eigen::Matrix4f>& waypoints)
107  {
108  this->waypoints = waypoints;
110  }
111 
112  void
113  CartesianWaypointController::swapWaypoints(std::vector<Eigen::Matrix4f>& waypoints)
114  {
115  std::swap(this->waypoints, waypoints);
117  }
118 
119  void
121  {
122  this->waypoints.push_back(waypoint);
123  }
124 
125  void
127  {
128  waypoints.clear();
129  waypoints.push_back(target);
131  }
132 
133  void
135  {
136  this->feedForwardVelocity = feedForwardVelocity;
137  }
138 
139  void
141  const Eigen::Vector3f& feedForwardVelocityPos,
142  const Eigen::Vector3f& feedForwardVelocityOri)
143  {
144  feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
145  feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
146  }
147 
148  void
150  {
151  feedForwardVelocity = Eigen::Vector6f::Zero();
152  }
153 
154  float
156  {
158  }
159 
160  float
162  {
164  }
165 
166  bool
168  {
171  }
172 
173  bool
175  {
178  }
179 
180  bool
182  {
184  }
185 
186  bool
188  {
189  return isLastWaypoint() && isCurrentTargetNear();
190  }
191 
192  bool
194  {
195  return currentWaypointIndex + 1 >= waypoints.size();
196  }
197 
198  const Eigen::Matrix4f&
200  {
201  return waypoints.at(currentWaypointIndex);
202  }
203 
204  const Eigen::Vector3f
206  {
207  ARMARX_TRACE;
208  return ::math::Helpers::GetPosition(waypoints.at(currentWaypointIndex));
209  }
210 
211  size_t
213  {
214  ARMARX_TRACE;
215  float dist = FLT_MAX;
216  size_t minIndex = 0;
217  for (size_t i = 0; i < waypoints.size(); i++)
218  {
219  float posErr = ctrlCartesianPos2Vel.getPositionError(waypoints.at(i));
220  float oriErr = ctrlCartesianPos2Vel.getOrientationError(waypoints.at(i));
221  float d = posErr + oriErr * rad2mmFactor;
222  if (d < dist)
223  {
224  minIndex = i;
225  dist = d;
226  }
227  }
228  currentWaypointIndex = minIndex;
229  return currentWaypointIndex;
230  }
231 
232  void
234  {
236  }
237 
238  std::string
240  {
241  std::stringstream ss;
242  ss.precision(2);
243  ss << std::fixed << "Waypoint: " << (currentWaypointIndex + 1) << "/" << waypoints.size()
244  << " distance: " << getPositionError() << " mm "
245  << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
246  return ss.str();
247  }
248 
249  void
250  CartesianWaypointController::setConfig(const CartesianWaypointControllerConfig& cfg)
251  {
252  KpJointLimitAvoidance = cfg.kpJointLimitAvoidance;
253  jointLimitAvoidanceScale = cfg.jointLimitAvoidanceScale;
254 
255  thresholdOrientationNear = cfg.thresholdOrientationNear;
256  thresholdOrientationReached = cfg.thresholdOrientationReached;
257  thresholdPositionNear = cfg.thresholdPositionNear;
258  thresholdPositionReached = cfg.thresholdPositionReached;
259 
260  ctrlCartesianPos2Vel.maxOriVel = cfg.maxOriVel;
261  ctrlCartesianPos2Vel.maxPosVel = cfg.maxPosVel;
262  ctrlCartesianPos2Vel.KpOri = cfg.kpOri;
263  ctrlCartesianPos2Vel.KpPos = cfg.kpPos;
264 
265  ctrlCartesianVelWithRamps.setMaxAccelerations(cfg.maxPositionAcceleration,
266  cfg.maxOrientationAcceleration,
267  cfg.maxNullspaceAcceleration);
268  }
269 
270  void
272  const Eigen::Ref<const Eigen::VectorXf>& currentJointVelocity)
273  {
274 #pragma GCC diagnostic push
275 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
277 #pragma GCC diagnostic pop
278  }
279 } // namespace armarx
armarx::CartesianWaypointController::getOrientationError
float getOrientationError() const
Definition: CartesianWaypointController.cpp:161
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:233
CartesianWaypointController.h
armarx::CartesianWaypointController::isCurrentTargetNear
bool isCurrentTargetNear() const
Definition: CartesianWaypointController.cpp:174
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::CartesianWaypointController::setCurrentJointVelocity
void setCurrentJointVelocity(const Eigen::Ref< const Eigen::VectorXf > &currentJointVelocity)
Definition: CartesianWaypointController.cpp:271
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:205
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:82
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:668
trace.h
armarx::CartesianWaypointController::nullSpaceControlEnabled
bool nullSpaceControlEnabled
Definition: CartesianWaypointController.h:113
armarx::CartesianWaypointController::skipToClosestWaypoint
size_t skipToClosestWaypoint(float rad2mmFactor)
Definition: CartesianWaypointController.cpp:212
armarx::CartesianWaypointController::isLastWaypoint
bool isLastWaypoint() const
Definition: CartesianWaypointController.cpp:193
armarx::CartesianWaypointController::getPositionError
float getPositionError() const
Definition: CartesianWaypointController.cpp:155
armarx::CartesianWaypointController::clearFeedForwardVelocity
void clearFeedForwardVelocity()
Definition: CartesianWaypointController.cpp:149
armarx::CartesianWaypointController::swapWaypoints
void swapWaypoints(std::vector< Eigen::Matrix4f > &waypoints)
Definition: CartesianWaypointController.cpp:113
armarx::CartesianVelocityControllerWithRamp::setMaxAccelerations
void setMaxAccelerations(float maxPositionAcceleration, float maxOrientationAcceleration, float maxNullspaceAcceleration)
Definition: CartesianVelocityControllerWithRamp.cpp:110
armarx::CartesianWaypointController::setFeedForwardVelocity
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
Definition: CartesianWaypointController.cpp:134
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:104
armarx::armem::client::util::swap
void swap(SubscriptionHandle &first, SubscriptionHandle &second)
Definition: SubscriptionHandle.cpp:66
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
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:41
armarx::CartesianVelocityControllerWithRamp::controller
CartesianVelocityController controller
Definition: CartesianVelocityControllerWithRamp.h:79
armarx::CartesianWaypointController::getCurrentTarget
const Eigen::Matrix4f & getCurrentTarget() const
Definition: CartesianWaypointController.cpp:199
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:81
armarx::CartesianVelocityControllerWithRamp::calculate
Eigen::VectorXf calculate(const Eigen::VectorXf &cartesianVel, float jointLimitAvoidanceScale, float dt)
Definition: CartesianVelocityControllerWithRamp.cpp:89
armarx::CartesianWaypointController::isFinalTargetNear
bool isFinalTargetNear() const
Definition: CartesianWaypointController.cpp:187
armarx::CartesianWaypointController::isCurrentTargetReached
bool isCurrentTargetReached() const
Definition: CartesianWaypointController.cpp:167
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:65
armarx::CartesianVelocityControllerWithRamp::setCurrentJointVelocity
void setCurrentJointVelocity(const Eigen::Ref< const Eigen::VectorXf > &currentJointVelocity)
Definition: CartesianVelocityControllerWithRamp.cpp:53
armarx::CartesianWaypointController::currentWaypointIndex
size_t currentWaypointIndex
Definition: CartesianWaypointController.h:101
armarx::CartesianVelocityController::calculateJointLimitAvoidance
Eigen::VectorXf calculateJointLimitAvoidance()
Definition: CartesianVelocityController.cpp:171
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:120
armarx::CartesianWaypointController::setTarget
void setTarget(const Eigen::Matrix4f &target)
Definition: CartesianWaypointController.cpp:126
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:181
armarx::CartesianWaypointController::setConfig
void setConfig(const CartesianWaypointControllerConfig &cfg)
Definition: CartesianWaypointController.cpp:250
Eigen::Matrix< float, 6, 1 >
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:83
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:80
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:223
armarx::CartesianWaypointController::thresholdOrientationNear
float thresholdOrientationNear
Definition: CartesianWaypointController.h:106
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::CartesianWaypointController::ctrlCartesianVelWithRamps
CartesianVelocityControllerWithRamp ctrlCartesianVelWithRamps
Definition: CartesianWaypointController.h:97
armarx::CartesianWaypointController::getStatusText
std::string getStatusText()
Definition: CartesianWaypointController.cpp:239
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:45