TrajectoryController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "TrajectoryController.h"
27 
28 namespace armarx
29 {
30 
31  TrajectoryController::TrajectoryController(const TrajectoryPtr& traj, float kp, float ki, float kd, bool threadSafe, float maxIntegral) :
32  traj(traj)
33  {
34  std::vector<bool> limitless;
35  for (auto ls : traj->getLimitless())
36  {
37  limitless.push_back(ls.enabled);
38  }
39  pid.reset(new MultiDimPIDController(kp, ki, kd, std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), threadSafe, limitless));
40  pid->maxIntegral = maxIntegral;
41  pid->preallocate(traj->dim());
43  currentTimestamp = traj->begin()->getTimestamp();
44  //for (size_t i = 0; i < traj->dim(); i++)
45  //{
46  // PIDControllerPtr pid(new PIDController(kp, ki, kd, std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), traj->getLimitless().at(i).enabled));
47  // pids.push_back(pid);
48  //}
49  positions.resize(traj->dim(), 1);
50  veloctities.resize(traj->dim(), 1);
51  currentError.resize(traj->dim(), 1);
52  }
53 
54  const Eigen::VectorXf& TrajectoryController::update(double deltaT, const Eigen::VectorXf& currentPosition)
55  {
58  ARMARX_CHECK_EXPRESSION(traj->size() > 0);
59  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(currentPosition.rows()), traj->dim());
60  size_t dim = traj->dim();
62  const Trajectory& traj = *this->traj;
63  if (currentTimestamp < 0.0)
64  {
65  currentTimestamp = 0.0;
66  }
67  if (currentTimestamp <= traj.rbegin()->getTimestamp())
68  {
69  for (size_t i = 0; i < dim; ++i)
70  {
71 
72  positions(i) = traj.getState(currentTimestamp, i, 0);
73  veloctities(i) = (std::signbit(deltaT) ? -1.0 : 1.0) * traj.getState(currentTimestamp, i, 1);
74  //pids.at(i)->update(std::abs(deltaT), currentPosition(i), positions(i));
75  //veloctities(i) += pids.at(i)->getControlValue();
76  }
77  }
78  else // hold position in the end
79  {
80  for (size_t i = 0; i < dim; ++i)
81  {
82  positions(i) = traj.rbegin()->getPosition(i);
83  veloctities(i) = 0;
84  }
85  }
86  for (size_t i = 0; i < dim; ++i)
87  {
88  if (pid->limitless.size() > i && pid->limitless.at(i))
89  {
90  currentError(i) = math::MathUtils::AngleDelta(currentPosition(i), positions(i));
91  }
92  else
93  {
94  currentError(i) = positions(i) - currentPosition(i);
95  }
96 
97  }
98 
99  pid->update(std::abs(deltaT), currentPosition, positions);
100  veloctities += pid->getControlValue();
101  return veloctities;
102  }
103 
104  /*
105  const MultiDimPIDControllerPtr& TrajectoryController::getPid() const
106  {
107  return pid;
108  }
109 
110  void TrajectoryController::setPid(const MultiDimPIDControllerPtr& value)
111  {
112  pid = value;
113  }*/
114 
116  {
117  return currentTimestamp;
118  }
119 
121  {
122  return traj;
123  }
124 
126  {
127  if (traj->size() == 0)
128  {
129  return;
130  }
131  auto dim = traj->dim();
132  Ice::DoubleSeq prevPos = traj->begin()->getPositions();
133  std::vector<math::LinearizeAngularTrajectory> linTraj;
134  for (size_t i = 0; i < dim; ++i)
135  {
136  linTraj.push_back(math::LinearizeAngularTrajectory(prevPos.at(i)));
137  }
138  auto limitlessStates = traj->getLimitless();
139  for (auto& state : *traj)
140  {
141  for (size_t i = 0; i < dim; ++i)
142  {
143  //if (!limitlessStates.at(i).enabled)
144  //{
145  // continue;
146  //}
147  //double pos = state.getPosition(i);
148  //double newPos = prevPos.at(i) + math::MathUtils::angleModPI(pos - prevPos.at(i));
149  //state.getData().at(i)->at(0) = newPos;
150  //prevPos.at(i) = newPos;
151 
152  if (limitlessStates.at(i).enabled)
153  {
154  state.getData().at(i)->at(0) = linTraj.at(i).update(state.getData().at(i)->at(0));
155  }
156 
157  }
158  }
159  }
160 
162  {
163  if (traj->size() == 0)
164  {
165  return;
166  }
167  auto dim = traj->dim();
168  auto limitlessStates = traj->getLimitless();
169  ARMARX_CHECK_EQUAL(limitlessStates.size(), dim);
170  for (auto& state : *traj)
171  {
172  for (size_t i = 0; i < dim; ++i)
173  {
174  if (!limitlessStates.at(i).enabled)
175  {
176  continue;
177  }
178  double pos = state.getPosition(i);
179  double center = (limitlessStates.at(i).limitHi + limitlessStates.at(i).limitLo) * 0.5;
180  double newPos = math::MathUtils::angleModX(pos, center);
181  state.getData().at(i)->at(0) = newPos;
182  }
183  }
184  }
185 
186  const Eigen::VectorXf& TrajectoryController::getCurrentError() const
187  {
188  return currentError;
189  }
190 
191  const Eigen::VectorXf& TrajectoryController::getPositions() const
192  {
193  return positions;
194  }
195 
196 
197 } // namespace armarx
MathUtils.h
armarx::math::MathUtils::AngleDelta
static float AngleDelta(float angle1, float angle2)
Definition: MathUtils.h:176
LinearizeAngularTrajectory.h
armarx::TrajectoryController::traj
TrajectoryPtr traj
Definition: TrajectoryController.h:52
armarx::TrajectoryController::FoldLimitlessJointPositions
static void FoldLimitlessJointPositions(TrajectoryPtr traj)
Definition: TrajectoryController.cpp:161
armarx::TrajectoryController::getPositions
const Eigen::VectorXf & getPositions() const
Definition: TrajectoryController.cpp:191
armarx::math::MathUtils::angleModX
static float angleModX(float value, float center)
Definition: MathUtils.h:145
armarx::Trajectory::rbegin
ordered_view::const_reverse_iterator rbegin() const
Definition: Trajectory.cpp:1914
armarx::TrajectoryController::TrajectoryController
TrajectoryController()
Creates a new TrajectoryController and assigns a QWidget to handle.
Definition: TrajectoryController.cpp:31
armarx::TrajectoryController::currentTimestamp
double currentTimestamp
Definition: TrajectoryController.h:55
armarx::TrajectoryController::getCurrentError
const Eigen::VectorXf & getCurrentError() const
Definition: TrajectoryController.cpp:186
armarx::TrajectoryController::getCurrentTimestamp
double getCurrentTimestamp() const
Definition: TrajectoryController.cpp:115
IceInternal::Handle< Trajectory >
armarx::TrajectoryController::veloctities
Eigen::VectorXf veloctities
Definition: TrajectoryController.h:57
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::TrajectoryController::positions
Eigen::VectorXf positions
Definition: TrajectoryController.h:56
armarx::TrajectoryController::UnfoldLimitlessJointPositions
static void UnfoldLimitlessJointPositions(TrajectoryPtr traj)
Definition: TrajectoryController.cpp:125
armarx::math::LinearizeAngularTrajectory
Definition: LinearizeAngularTrajectory.h:34
armarx::TrajectoryController::pid
MultiDimPIDControllerPtr pid
Definition: TrajectoryController.h:53
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::TrajectoryController::update
const Eigen::VectorXf & update(double deltaT, const Eigen::VectorXf &currentPosition)
Definition: TrajectoryController.cpp:54
armarx::MultiDimPIDController
MultiDimPIDControllerTemplate<> MultiDimPIDController
Definition: MultiDimPIDController.h:250
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::Trajectory
The Trajectory class represents n-dimensional sampled trajectories.
Definition: Trajectory.h:76
armarx::TrajectoryController::currentError
Eigen::VectorXf currentError
Definition: TrajectoryController.h:58
armarx::TrajectoryController::getTraj
const TrajectoryPtr & getTraj() const
Definition: TrajectoryController.cpp:120
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
TrajectoryController.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28