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 
27 
29 
30 #include <VirtualRobot/math/Helpers.h>
31 #include <VirtualRobot/MathTools.h>
32 
33 namespace armarx
34 {
35  PositionControllerHelper::PositionControllerHelper(const VirtualRobot::RobotNodePtr& tcp, const VelocityControllerHelperPtr& velocityControllerHelper, const Eigen::Matrix4f& target)
36  : posController(tcp), velocityControllerHelper(velocityControllerHelper), currentWaypointIndex(0)
37  {
38  waypoints.push_back(target);
39  }
40 
41  PositionControllerHelper::PositionControllerHelper(const VirtualRobot::RobotNodePtr& tcp, const VelocityControllerHelperPtr& velocityControllerHelper, const std::vector<Eigen::Matrix4f>& waypoints)
42  : posController(tcp), velocityControllerHelper(velocityControllerHelper), waypoints(waypoints), currentWaypointIndex(0)
43  {
44  }
45 
46  PositionControllerHelper::PositionControllerHelper(const VirtualRobot::RobotNodePtr& tcp, const VelocityControllerHelperPtr& velocityControllerHelper, const std::vector<PosePtr>& waypoints)
47  : posController(tcp), velocityControllerHelper(velocityControllerHelper), currentWaypointIndex(0)
48  {
49  for (const PosePtr& pose : waypoints)
50  {
51  this->waypoints.push_back(pose->toEigen());
52  }
53  }
54 
55  void PositionControllerHelper::readConfig(const CartesianPositionControllerConfigBase &config)
56  {
57  thresholdOrientationNear = config.thresholdOrientationNear;
58  thresholdOrientationReached = config.thresholdOrientationReached;
59  thresholdPositionNear = config.thresholdPositionNear;
60  thresholdPositionReached = config.thresholdPositionReached;
61  posController.KpOri = config.KpOri;
62  posController.KpPos = config.KpPos;
63  posController.maxOriVel = config.maxOriVel;
64  posController.maxPosVel = config.maxPosVel;
65  }
66 
67  void PositionControllerHelper::readConfig(const CartesianPositionControllerConfigBasePtr& config)
68  {
69  readConfig(*config);
70  }
71 
73  {
74  updateRead();
75  updateWrite();
76  }
77 
79  {
81  {
83  }
84  }
85 
87  {
88  auto target = getCurrentTarget();
89  Eigen::VectorXf cv = posController.calculate(target, VirtualRobot::IKSolver::All);
90  velocityControllerHelper->setTargetVelocity(cv + feedForwardVelocity);
92  {
94  }
95  }
96 
97  void PositionControllerHelper::setNewWaypoints(const std::vector<Eigen::Matrix4f>& waypoints)
98  {
99  this->waypoints = waypoints;
101  }
102 
103  void PositionControllerHelper::setNewWaypoints(const std::vector<PosePtr>& waypoints)
104  {
105  this->waypoints.clear();
106  for (const PosePtr& pose : waypoints)
107  {
108  this->waypoints.push_back(pose->toEigen());
109  }
111  }
112 
114  {
115  this->waypoints.push_back(waypoint);
116  }
117 
119  {
120  waypoints.clear();
121  waypoints.push_back(target);
123  }
124 
126  {
127  this->feedForwardVelocity = feedForwardVelocity;
128  }
129 
130  void PositionControllerHelper::setFeedForwardVelocity(const Eigen::Vector3f& feedForwardVelocityPos, const Eigen::Vector3f& feedForwardVelocityOri)
131  {
132  feedForwardVelocity.block<3, 1>(0, 0) = feedForwardVelocityPos;
133  feedForwardVelocity.block<3, 1>(3, 0) = feedForwardVelocityOri;
134  }
135 
137  {
138  feedForwardVelocity = Eigen::Vector6f::Zero();
139  }
140 
142  {
143  velocityControllerHelper->controller->immediateHardStop();
144  if (clearTargets)
145  {
146  setTarget(posController.getTcp()->getPoseInRootFrame());
147  }
148  }
149 
151  {
153  }
154 
156  {
158  }
159 
161  {
163  }
164 
166  {
168  }
169 
171  {
173  }
174 
176  {
177  return isLastWaypoint() && isCurrentTargetNear();
178  }
179 
181  {
182  return currentWaypointIndex + 1 >= waypoints.size();
183  }
184 
186  {
187  return waypoints.at(currentWaypointIndex);
188  }
189 
191  {
192  return math::Helpers::GetPosition(waypoints.at(currentWaypointIndex));
193  }
194 
196  {
197  float dist = FLT_MAX;
198  size_t minIndex = 0;
199  for (size_t i = 0; i < waypoints.size(); i++)
200  {
201  float posErr = posController.getPositionError(waypoints.at(i));
202  float oriErr = posController.getOrientationError(waypoints.at(i));
203  float d = posErr + oriErr * rad2mmFactor;
204  if (d < dist)
205  {
206  minIndex = i;
207  dist = d;
208  }
209  }
210  currentWaypointIndex = minIndex;
211  return currentWaypointIndex;
212  }
213 
215  {
216  velocityControllerHelper->setNullSpaceControl(enabled);
217  }
218 
220  {
221  std::stringstream ss;
222  ss.precision(2);
223  ss << std::fixed << "Waypoint: " << (currentWaypointIndex + 1) << "/" << waypoints.size() << " distance: " << getPositionError() << " mm " << VirtualRobot::MathTools::rad2deg(getOrientationError()) << " deg";
224  return ss.str();
225  }
226 
227  bool PositionControllerHelper::OptimizeNullspace(const VirtualRobot::RobotNodePtr& tcp, const VirtualRobot::RobotNodeSetPtr& rns, const Eigen::Matrix4f& target, const NullspaceOptimizationArgs& args)
228  {
229  CartesianPositionController posHelper(tcp);
230 
231  CartesianVelocityControllerPtr cartesianVelocityController;
232  cartesianVelocityController.reset(new CartesianVelocityController(rns, tcp, args.invJacMethod));
233 
234  float errorOriInitial = posHelper.getOrientationError(target);
235  float errorPosInitial = posHelper.getPositionError(target);
236 
237  float stepLength = args.stepLength;
238  float eps = args.eps;
239 
240  std::vector<float> initialJointAngles = rns->getJointValues();
241 
242  for (int i = 0; i < args.loops; i++)
243  {
244  Eigen::VectorXf tcpVelocities = posHelper.calculate(target, args.cartesianSelection);
245  Eigen::VectorXf nullspaceVel = cartesianVelocityController->calculateJointLimitAvoidance(); // calculateNullspaceVelocity(tcpVelocities, 1.0f, vrmode);
246  float nullspaceLen = nullspaceVel.norm();
247  if (nullspaceLen > stepLength)
248  {
249  nullspaceVel = nullspaceVel / nullspaceLen * stepLength;
250  }
251  Eigen::VectorXf jointVelocities = cartesianVelocityController->calculate(tcpVelocities, nullspaceVel, args.cartesianSelection);
252 
253  //jointVelocities = jointVelocities * stepLength;
254  float len = jointVelocities.norm();
255  if (len > stepLength)
256  {
257  jointVelocities = jointVelocities / len * stepLength;
258  }
259 
260  for (size_t n = 0; n < rns->getSize(); n++)
261  {
262  rns->getNode(n)->setJointValue(rns->getNode(n)->getJointValue() + jointVelocities(n));
263  }
264  if (len < eps)
265  {
266  break;
267  }
268  }
269 
270  float errorOriAfter = posHelper.getOrientationError(target);
271  float errorPosAfter = posHelper.getPositionError(target);
272  if (errorOriAfter < errorOriInitial + 1.f / 180 * M_PI && errorPosAfter < errorPosInitial + 1.f)
273  {
274  return true;
275  }
276  else
277  {
278  rns->setJointValues(initialJointAngles);
279  return false;
280  }
281  }
282 }
armarx::PositionControllerHelper::clearFeedForwardVelocity
void clearFeedForwardVelocity()
Definition: PositionControllerHelper.cpp:136
armarx::PositionControllerHelper::isCurrentTargetNear
bool isCurrentTargetNear() const
Definition: PositionControllerHelper.cpp:165
armarx::PositionControllerHelper::NullspaceOptimizationArgs::loops
int loops
Definition: PositionControllerHelper.h:97
armarx::PositionControllerHelper::getStatusText
std::string getStatusText()
Definition: PositionControllerHelper.cpp:219
armarx::PositionControllerHelper::thresholdPositionNear
float thresholdPositionNear
Definition: PositionControllerHelper.h:124
armarx::PositionControllerHelper::addWaypoint
void addWaypoint(const Eigen::Matrix4f &waypoint)
Definition: PositionControllerHelper.cpp:113
armarx::PositionControllerHelper::getCurrentTargetPosition
const Eigen::Vector3f getCurrentTargetPosition() const
Definition: PositionControllerHelper.cpp:190
armarx::PositionControllerHelper::posController
CartesianPositionController posController
Definition: PositionControllerHelper.h:116
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:76
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::PositionControllerHelper::setNullSpaceControl
void setNullSpaceControl(bool enabled)
Definition: PositionControllerHelper.cpp:214
armarx::PositionControllerHelper::isFinalTargetNear
bool isFinalTargetNear() const
Definition: PositionControllerHelper.cpp:175
armarx::PositionControllerHelper::NullspaceOptimizationArgs::stepLength
float stepLength
Definition: PositionControllerHelper.h:98
armarx::PositionControllerHelper::thresholdOrientationNear
float thresholdOrientationNear
Definition: PositionControllerHelper.h:125
armarx::CartesianVelocityController
Definition: CartesianVelocityController.h:36
armarx::CartesianVelocityControllerPtr
std::shared_ptr< CartesianVelocityController > CartesianVelocityControllerPtr
Definition: CartesianVelocityController.h:34
armarx::PositionControllerHelper::NullspaceOptimizationArgs
Definition: PositionControllerHelper.h:94
armarx::PositionControllerHelper::updateWrite
void updateWrite()
Definition: PositionControllerHelper.cpp:86
IceInternal::Handle< Pose >
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
armarx::PositionControllerHelper::immediateHardStop
void immediateHardStop(bool clearTargets=true)
Definition: PositionControllerHelper.cpp:141
armarx::PositionControllerHelper::feedForwardVelocity
Eigen::Vector6f feedForwardVelocity
Definition: PositionControllerHelper.h:126
armarx::PositionControllerHelper::isFinalTargetReached
bool isFinalTargetReached() const
Definition: PositionControllerHelper.cpp:170
armarx::PositionControllerHelper::update
void update()
Definition: PositionControllerHelper.cpp:72
armarx::PositionControllerHelper::thresholdPositionReached
float thresholdPositionReached
Definition: PositionControllerHelper.h:122
armarx::PositionControllerHelper::waypoints
std::vector< Eigen::Matrix4f > waypoints
Definition: PositionControllerHelper.h:119
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:75
armarx::PositionControllerHelper::updateRead
void updateRead()
Definition: PositionControllerHelper.cpp:78
armarx::PositionControllerHelper::NullspaceOptimizationArgs::eps
float eps
Definition: PositionControllerHelper.h:99
armarx::PositionControllerHelper::setFeedForwardVelocity
void setFeedForwardVelocity(const Eigen::Vector6f &feedForwardVelocity)
Definition: PositionControllerHelper.cpp:125
armarx::PositionControllerHelper::velocityControllerHelper
VelocityControllerHelperPtr velocityControllerHelper
Definition: PositionControllerHelper.h:117
armarx::PositionControllerHelper::autoClearFeedForward
bool autoClearFeedForward
Definition: PositionControllerHelper.h:127
armarx::PositionControllerHelper::NullspaceOptimizationArgs::invJacMethod
VirtualRobot::JacobiProvider::InverseJacobiMethod invJacMethod
Definition: PositionControllerHelper.h:103
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::PositionControllerHelper::currentWaypointIndex
size_t currentWaypointIndex
Definition: PositionControllerHelper.h:120
armarx::PositionControllerHelper::getCurrentTarget
const Eigen::Matrix4f & getCurrentTarget() const
Definition: PositionControllerHelper.cpp:185
armarx::channels::KinematicUnitObserver::jointVelocities
const KinematicUnitDatafieldCreator jointVelocities("jointVelocities")
armarx::PositionControllerHelper::skipToClosestWaypoint
size_t skipToClosestWaypoint(float rad2mmFactor)
Definition: PositionControllerHelper.cpp:195
armarx::PositionControllerHelper::PositionControllerHelper
PositionControllerHelper(const VirtualRobot::RobotNodePtr &tcp, const VelocityControllerHelperPtr &velocityControllerHelper, const Eigen::Matrix4f &target)
Definition: PositionControllerHelper.cpp:35
armarx::CartesianPositionController::getTcp
VirtualRobot::RobotNodePtr getTcp() const
Definition: CartesianPositionController.cpp:180
PositionControllerHelper.h
CartesianVelocityController.h
armarx::PositionControllerHelper::thresholdOrientationReached
float thresholdOrientationReached
Definition: PositionControllerHelper.h:123
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::PositionControllerHelper::OptimizeNullspace
static bool OptimizeNullspace(const VirtualRobot::RobotNodePtr &tcp, const VirtualRobot::RobotNodeSetPtr &rns, const Eigen::Matrix4f &target, const NullspaceOptimizationArgs &args=NullspaceOptimizationArgs())
OptimizeNullspace.
Definition: PositionControllerHelper.cpp:227
TimeUtil.h
armarx::PositionControllerHelper::getOrientationError
float getOrientationError() const
Definition: PositionControllerHelper.cpp:155
armarx::PositionControllerHelper::NullspaceOptimizationArgs::cartesianSelection
VirtualRobot::IKSolver::CartesianSelection cartesianSelection
Definition: PositionControllerHelper.h:102
armarx::PositionControllerHelper::readConfig
void readConfig(const CartesianPositionControllerConfigBase &config)
Definition: PositionControllerHelper.cpp:55
armarx::CartesianPositionController
Definition: CartesianPositionController.h:41
armarx::PositionControllerHelper::isCurrentTargetReached
bool isCurrentTargetReached() const
Definition: PositionControllerHelper.cpp:160
Eigen::Matrix< float, 6, 1 >
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:77
cv
Definition: helper.h:35
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:74
armarx::PositionControllerHelper::setTarget
void setTarget(const Eigen::Matrix4f &target)
Definition: PositionControllerHelper.cpp:118
armarx::PositionControllerHelper::isLastWaypoint
bool isLastWaypoint() const
Definition: PositionControllerHelper.cpp:180
armarx::PositionControllerHelper::getPositionError
float getPositionError() const
Definition: PositionControllerHelper.cpp:150
armarx::VelocityControllerHelperPtr
std::shared_ptr< VelocityControllerHelper > VelocityControllerHelperPtr
Definition: VelocityControllerHelper.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
armarx::PositionControllerHelper::setNewWaypoints
void setNewWaypoints(const std::vector< Eigen::Matrix4f > &waypoints)
Definition: PositionControllerHelper.cpp:97
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:37