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
39namespace 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) :
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),
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
90 ctrlCartesianVelWithRamps.controller.calculateJointLimitAvoidance() +
91 ctrlCartesianVelWithRamps.controller.calculateNullspaceVelocity(
92 cartesianVelocity, jointLimitAvoidanceScale, VirtualRobot::IKSolver::All);
93 }
94 else
95 {
97 //don't avoid joint limits
98 _jnv *= 0;
99 }
101 _out = ctrlCartesianVelWithRamps.calculate(cartesianVelocity, _jnv, dt);
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
114 {
115 std::swap(this->waypoints, waypoints);
117 }
118
119 void
120 CartesianWaypointController::addWaypoint(const Eigen::Matrix4f& waypoint)
121 {
122 this->waypoints.push_back(waypoint);
123 }
124
125 void
126 CartesianWaypointController::setTarget(const Eigen::Matrix4f& target)
127 {
128 waypoints.clear();
129 waypoints.push_back(target);
131 }
132
133 void
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
153
154 float
159
160 float
162 {
163 return ctrlCartesianPos2Vel.getOrientationError(getCurrentTarget());
164 }
165
166 bool
172
173 bool
179
180 bool
185
186 bool
191
192 bool
197
198 const Eigen::Matrix4f&
203
204 const Eigen::Vector3f
206 {
208 return ::math::Helpers::GetPosition(waypoints.at(currentWaypointIndex));
209 }
210
211 size_t
213 {
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;
230 }
231
232 void
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"
276 ctrlCartesianVelWithRamps.setCurrentJointVelocity(currentJointVelocity);
277#pragma GCC diagnostic pop
278 }
279} // namespace armarx
constexpr T dt
const Eigen::VectorXf & calculate(float dt)
void swapWaypoints(std::vector< Eigen::Matrix4f > &waypoints)
void setWaypoints(const std::vector< Eigen::Matrix4f > &waypoints)
const Eigen::Matrix4f & getCurrentTarget() const
void setCurrentJointVelocity(const Eigen::Ref< const Eigen::VectorXf > &currentJointVelocity)
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)
CartesianVelocityControllerWithRamp ctrlCartesianVelWithRamps
void setTarget(const Eigen::Matrix4f &target)
const Eigen::Vector3f getCurrentTargetPosition() const
void setConfig(const CartesianWaypointControllerConfig &cfg)
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
void addWaypoint(const Eigen::Matrix4f &waypoint)
#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...
Matrix< float, 6, 1 > Vector6f
This file offers overloads of toIce() and fromIce() functions for STL container types.
#define ARMARX_TRACE
Definition trace.h:77