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 */
25
28
29namespace armarx
30{
31
33 float kp,
34 float ki,
35 float kd,
36 bool threadSafe,
37 float maxIntegral) :
38 traj(traj)
39 {
40 std::vector<bool> limitless;
41 for (auto ls : traj->getLimitless())
42 {
43 limitless.push_back(ls.enabled);
44 }
45 pid.reset(new MultiDimPIDController(kp,
46 ki,
47 kd,
48 std::numeric_limits<double>::max(),
49 std::numeric_limits<double>::max(),
50 threadSafe,
51 limitless));
52 pid->maxIntegral = maxIntegral;
53 pid->preallocate(traj->dim());
55 currentTimestamp = traj->begin()->getTimestamp();
56 //for (size_t i = 0; i < traj->dim(); i++)
57 //{
58 // PIDControllerPtr pid(new PIDController(kp, ki, kd, std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), traj->getLimitless().at(i).enabled));
59 // pids.push_back(pid);
60 //}
61 positions.resize(traj->dim(), 1);
62 veloctities.resize(traj->dim(), 1);
63 currentError.resize(traj->dim(), 1);
64 }
65
66 const Eigen::VectorXf&
67 TrajectoryController::update(double deltaT, const Eigen::VectorXf& currentPosition)
68 {
71 ARMARX_CHECK_EXPRESSION(traj->size() > 0);
72 ARMARX_CHECK_EQUAL(static_cast<std::size_t>(currentPosition.rows()), traj->dim());
73 size_t dim = traj->dim();
75 const Trajectory& traj = *this->traj;
76 if (currentTimestamp < 0.0)
77 {
78 currentTimestamp = 0.0;
79 }
80 if (currentTimestamp <= traj.rbegin()->getTimestamp())
81 {
82 for (size_t i = 0; i < dim; ++i)
83 {
84
85 positions(i) = traj.getState(currentTimestamp, i, 0);
86 veloctities(i) =
87 (std::signbit(deltaT) ? -1.0 : 1.0) * traj.getState(currentTimestamp, i, 1);
88 //pids.at(i)->update(std::abs(deltaT), currentPosition(i), positions(i));
89 //veloctities(i) += pids.at(i)->getControlValue();
90 }
91 }
92 else // hold position in the end
93 {
94 for (size_t i = 0; i < dim; ++i)
95 {
96 positions(i) = traj.rbegin()->getPosition(i);
97 veloctities(i) = 0;
98 }
99 }
100 for (size_t i = 0; i < dim; ++i)
101 {
102 if (pid->limitless.size() > i && pid->limitless.at(i))
103 {
104 currentError(i) = math::MathUtils::AngleDelta(currentPosition(i), positions(i));
105 }
106 else
107 {
108 currentError(i) = positions(i) - currentPosition(i);
109 }
110 }
111
112 pid->update(std::abs(deltaT), currentPosition, positions);
113 veloctities += pid->getControlValue();
114 return veloctities;
115 }
116
117 /*
118 const MultiDimPIDControllerPtr& TrajectoryController::getPid() const
119 {
120 return pid;
121 }
122
123 void TrajectoryController::setPid(const MultiDimPIDControllerPtr& value)
124 {
125 pid = value;
126 }*/
127
128 double
133
134 const TrajectoryPtr&
136 {
137 return traj;
138 }
139
140 void
142 {
143 if (traj->size() == 0)
144 {
145 return;
146 }
147 auto dim = traj->dim();
148 Ice::DoubleSeq prevPos = traj->begin()->getPositions();
149 std::vector<math::LinearizeAngularTrajectory> linTraj;
150 for (size_t i = 0; i < dim; ++i)
151 {
152 linTraj.push_back(math::LinearizeAngularTrajectory(prevPos.at(i)));
153 }
154 auto limitlessStates = traj->getLimitless();
155 for (auto& state : *traj)
156 {
157 for (size_t i = 0; i < dim; ++i)
158 {
159 //if (!limitlessStates.at(i).enabled)
160 //{
161 // continue;
162 //}
163 //double pos = state.getPosition(i);
164 //double newPos = prevPos.at(i) + math::MathUtils::angleModPI(pos - prevPos.at(i));
165 //state.getData().at(i)->at(0) = newPos;
166 //prevPos.at(i) = newPos;
167
168 if (limitlessStates.at(i).enabled)
169 {
170 state.getData().at(i)->at(0) =
171 linTraj.at(i).update(state.getData().at(i)->at(0));
172 }
173 }
174 }
175 }
176
177 void
179 {
180 if (traj->size() == 0)
181 {
182 return;
183 }
184 auto dim = traj->dim();
185 auto limitlessStates = traj->getLimitless();
186 ARMARX_CHECK_EQUAL(limitlessStates.size(), dim);
187 for (auto& state : *traj)
188 {
189 for (size_t i = 0; i < dim; ++i)
190 {
191 if (!limitlessStates.at(i).enabled)
192 {
193 continue;
194 }
195 double pos = state.getPosition(i);
196 double center =
197 (limitlessStates.at(i).limitHi + limitlessStates.at(i).limitLo) * 0.5;
198 double newPos = math::MathUtils::angleModX(pos, center);
199 state.getData().at(i)->at(0) = newPos;
200 }
201 }
202 }
203
204 const Eigen::VectorXf&
209
210 const Eigen::VectorXf&
212 {
213 return positions;
214 }
215
216
217} // namespace armarx
const Eigen::VectorXf & getPositions() const
TrajectoryController()
Creates a new TrajectoryController and assigns a QWidget to handle.
MultiDimPIDControllerPtr pid
static void UnfoldLimitlessJointPositions(TrajectoryPtr traj)
const TrajectoryPtr & getTraj() const
static void FoldLimitlessJointPositions(TrajectoryPtr traj)
const Eigen::VectorXf & getCurrentError() const
const Eigen::VectorXf & update(double deltaT, const Eigen::VectorXf &currentPosition)
The Trajectory class represents n-dimensional sampled trajectories.
Definition Trajectory.h:77
static float angleModX(float value, float center)
Definition MathUtils.h:179
static float AngleDelta(float angle1, float angle2)
Definition MathUtils.h:216
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52
MultiDimPIDControllerTemplate<> MultiDimPIDController