PositionControllerHelper.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
26#include <VirtualRobot/MathTools.h>
27#include <VirtualRobot/Nodes/RobotNode.h>
28#include <VirtualRobot/Robot.h>
29#include <VirtualRobot/RobotNodeSet.h>
30#include <VirtualRobot/math/Helpers.h>
31
33
35
36namespace armarx
37{
39 const VirtualRobot::RobotNodePtr& tcp,
41 const Eigen::Matrix4f& target) :
42 posController(tcp),
45 {
46 waypoints.push_back(target);
47 }
48
50 const VirtualRobot::RobotNodePtr& tcp,
52 const std::vector<Eigen::Matrix4f>& waypoints) :
53 posController(tcp),
57 {
58 }
59
61 const VirtualRobot::RobotNodePtr& tcp,
63 const std::vector<PosePtr>& waypoints) :
64 posController(tcp),
67 {
68 for (const PosePtr& pose : waypoints)
69 {
70 this->waypoints.push_back(pose->toEigen());
71 }
72 }
73
74 void
75 PositionControllerHelper::readConfig(const CartesianPositionControllerConfigBase& config)
76 {
77 thresholdOrientationNear = config.thresholdOrientationNear;
78 thresholdOrientationReached = config.thresholdOrientationReached;
79 thresholdPositionNear = config.thresholdPositionNear;
80 thresholdPositionReached = config.thresholdPositionReached;
81 posController.KpOri = config.KpOri;
82 posController.KpPos = config.KpPos;
83 posController.maxOriVel = config.maxOriVel;
84 posController.maxPosVel = config.maxPosVel;
85 }
86
87 void
88 PositionControllerHelper::readConfig(const CartesianPositionControllerConfigBasePtr& config)
89 {
90 readConfig(*config);
91 }
92
93 void
99
100 void
108
109 void
111 {
112 auto target = getCurrentTarget();
113 Eigen::VectorXf cv = posController.calculate(target, VirtualRobot::IKSolver::All);
114 velocityControllerHelper->setTargetVelocity(cv + feedForwardVelocity);
116 {
118 }
119 }
120
121 void
122 PositionControllerHelper::setNewWaypoints(const std::vector<Eigen::Matrix4f>& waypoints)
123 {
124 this->waypoints = waypoints;
126 }
127
128 void
130 {
131 this->waypoints.clear();
132 for (const PosePtr& pose : waypoints)
133 {
134 this->waypoints.push_back(pose->toEigen());
135 }
137 }
138
139 void
140 PositionControllerHelper::addWaypoint(const Eigen::Matrix4f& waypoint)
141 {
142 this->waypoints.push_back(waypoint);
143 }
144
145 void
146 PositionControllerHelper::setTarget(const Eigen::Matrix4f& target)
147 {
148 waypoints.clear();
149 waypoints.push_back(target);
151 }
152
153 void
158
159 void
160 PositionControllerHelper::setFeedForwardVelocity(const Eigen::Vector3f& feedForwardVelocityPos,
161 const Eigen::Vector3f& feedForwardVelocityOri)
162 {
163 feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
164 feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
165 }
166
167 void
172
173 void
175 {
176 velocityControllerHelper->controller->immediateHardStop();
177 if (clearTargets)
178 {
179 setTarget(posController.getTcp()->getPoseInRootFrame());
180 }
181 }
182
183 float
185 {
186 return posController.getPositionError(getCurrentTarget());
187 }
188
189 float
191 {
192 return posController.getOrientationError(getCurrentTarget());
193 }
194
195 bool
201
202 bool
208
209 bool
214
215 bool
220
221 bool
223 {
224 return currentWaypointIndex + 1 >= waypoints.size();
225 }
226
227 const Eigen::Matrix4f&
232
233 const Eigen::Vector3f
235 {
236 return math::Helpers::GetPosition(waypoints.at(currentWaypointIndex));
237 }
238
239 size_t
241 {
242 float dist = FLT_MAX;
243 size_t minIndex = 0;
244 for (size_t i = 0; i < waypoints.size(); i++)
245 {
246 float posErr = posController.getPositionError(waypoints.at(i));
247 float oriErr = posController.getOrientationError(waypoints.at(i));
248 float d = posErr + oriErr * rad2mmFactor;
249 if (d < dist)
250 {
251 minIndex = i;
252 dist = d;
253 }
254 }
255 currentWaypointIndex = minIndex;
257 }
258
259 void
261 {
262 velocityControllerHelper->setNullSpaceControl(enabled);
263 }
264
265 std::string
267 {
268 std::stringstream ss;
269 ss.precision(2);
270 ss << std::fixed << "Waypoint: " << (currentWaypointIndex + 1) << "/" << waypoints.size()
271 << " distance: " << getPositionError() << " mm "
272 << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
273 return ss.str();
274 }
275
276 bool
277 PositionControllerHelper::OptimizeNullspace(const VirtualRobot::RobotNodePtr& tcp,
278 const VirtualRobot::RobotNodeSetPtr& rns,
279 const Eigen::Matrix4f& target,
280 const NullspaceOptimizationArgs& args)
281 {
282 CartesianPositionController posHelper(tcp);
283
284 CartesianVelocityControllerPtr cartesianVelocityController;
285 cartesianVelocityController.reset(
286 new CartesianVelocityController(rns, tcp, args.invJacMethod));
287
288 float errorOriInitial = posHelper.getOrientationError(target);
289 float errorPosInitial = posHelper.getPositionError(target);
290
291 float stepLength = args.stepLength;
292 float eps = args.eps;
293
294 std::vector<float> initialJointAngles = rns->getJointValues();
295
296 for (int i = 0; i < args.loops; i++)
297 {
298 Eigen::VectorXf tcpVelocities = posHelper.calculate(target, args.cartesianSelection);
299 Eigen::VectorXf nullspaceVel =
300 cartesianVelocityController
301 ->calculateJointLimitAvoidance(); // calculateNullspaceVelocity(tcpVelocities, 1.0f, vrmode);
302 float nullspaceLen = nullspaceVel.norm();
303 if (nullspaceLen > stepLength)
304 {
305 nullspaceVel = nullspaceVel / nullspaceLen * stepLength;
306 }
307 Eigen::VectorXf jointVelocities = cartesianVelocityController->calculate(
308 tcpVelocities, nullspaceVel, args.cartesianSelection);
309
310 //jointVelocities = jointVelocities * stepLength;
311 float len = jointVelocities.norm();
312 if (len > stepLength)
313 {
314 jointVelocities = jointVelocities / len * stepLength;
315 }
316
317 for (size_t n = 0; n < rns->getSize(); n++)
318 {
319 rns->getNode(n)->setJointValue(rns->getNode(n)->getJointValue() +
320 jointVelocities(n));
321 }
322 if (len < eps)
323 {
324 break;
325 }
326 }
327
328 float errorOriAfter = posHelper.getOrientationError(target);
329 float errorPosAfter = posHelper.getPositionError(target);
330 if (errorOriAfter < errorOriInitial + 1.f / 180 * M_PI &&
331 errorPosAfter < errorPosInitial + 1.f)
332 {
333 return true;
334 }
335 else
336 {
337 rns->setJointValues(initialJointAngles);
338 return false;
339 }
340 }
341} // namespace armarx
#define M_PI
Definition MathTools.h:17
float getPositionError(const Eigen::Matrix4f &targetPose) const
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
float getOrientationError(const Eigen::Matrix4f &targetPose) const
CartesianPositionController posController
void readConfig(const CartesianPositionControllerConfigBase &config)
static bool OptimizeNullspace(const VirtualRobot::RobotNodePtr &tcp, const VirtualRobot::RobotNodeSetPtr &rns, const Eigen::Matrix4f &target, const NullspaceOptimizationArgs &args=NullspaceOptimizationArgs())
OptimizeNullspace.
std::vector< Eigen::Matrix4f > waypoints
const Eigen::Matrix4f & getCurrentTarget() const
VelocityControllerHelperPtr velocityControllerHelper
size_t skipToClosestWaypoint(float rad2mmFactor)
void immediateHardStop(bool clearTargets=true)
void setTarget(const Eigen::Matrix4f &target)
const Eigen::Vector3f getCurrentTargetPosition() const
PositionControllerHelper(const VirtualRobot::RobotNodePtr &tcp, const VelocityControllerHelperPtr &velocityControllerHelper, const Eigen::Matrix4f &target)
void setNewWaypoints(const std::vector< Eigen::Matrix4f > &waypoints)
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
void addWaypoint(const Eigen::Matrix4f &waypoint)
Matrix< float, 6, 1 > Vector6f
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< VelocityControllerHelper > VelocityControllerHelperPtr
std::shared_ptr< CartesianVelocityController > CartesianVelocityControllerPtr
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
Definition helper.h:35
VirtualRobot::JacobiProvider::InverseJacobiMethod invJacMethod