TransformTrajectoryIntoTimeOptimal.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotSkillTemplates::MotionControlGroup
17 * @author Stefan Reither ( stef dot reither at web dot de )
18 * @date 2017
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <VirtualRobot/TimeOptimalTrajectory/TimeOptimalTrajectory.h>
26
27//#include <ArmarXCore/core/time/TimeUtil.h>
28//#include <ArmarXCore/observers/variant/DatafieldRef.h>
29
30using namespace armarx;
31using namespace MotionControlGroup;
32
33using NameFloatMap = std::map<std::string, float>;
34
35// DO NOT EDIT NEXT LINE
36TransformTrajectoryIntoTimeOptimal::SubClassRegistry TransformTrajectoryIntoTimeOptimal::Registry(
37 TransformTrajectoryIntoTimeOptimal::GetName(),
39
42{
43 ARMARX_INFO << t->output();
44 double length = t->getLength(0);
45 if (length == 0.0f)
46 {
47 return t;
48 }
49 ARMARX_INFO << VAROUT(length);
50 double timelength = t->getTimeLength();
51 ARMARX_INFO << VAROUT(timelength);
52 auto timestamps = t->getTimestamps();
53 ARMARX_INFO << VAROUT(timestamps);
54 Ice::DoubleSeq newTimestamps;
55 newTimestamps.push_back(0);
56 for (size_t var = 0; var < timestamps.size() - 1; ++var)
57 {
58 double tBefore = timestamps.at(var);
59 double tAfter = (timestamps.at(var + 1));
60 ARMARX_INFO << VAROUT(tBefore) << VAROUT(tAfter);
61 double partLength = t->getLength(0, tBefore, tAfter);
62 double lengthPortion = partLength / length;
63 ARMARX_INFO << VAROUT(partLength) << VAROUT(lengthPortion);
64 newTimestamps.push_back(*newTimestamps.rbegin() + timelength * lengthPortion);
65 }
66 ARMARX_INFO << VAROUT(newTimestamps);
67 TrajectoryPtr newTraj = new Trajectory();
68 for (size_t d = 0; d < t->dim(); ++d)
69 {
70 newTraj->addDimension(t->getDimensionData(d), newTimestamps, t->getDimensionName(d));
71 }
72 ARMARX_INFO << newTraj->output();
73
74 return newTraj;
75}
76
77void
81
82void
84{
85 TrajectoryPtr inputTrajectory = in.getTrajectory();
86
87 double maxDeviation = in.getMaxDeviation();
88 double timeStep = in.getTimeStep();
89 float maxJointVelocity = in.getMaxJointVelocity();
90 float maxJointAcceleration = in.getMaxJointAcceleration();
91
92 Ice::StringSeq jointNames = inputTrajectory->getDimensionNames();
93 Eigen::VectorXd maxAccelerations(jointNames.size());
94 Eigen::VectorXd maxVelocities(jointNames.size());
95 for (size_t i = 0; i < jointNames.size(); i++)
96 {
97 maxAccelerations(i) = maxJointAcceleration;
98 maxVelocities(i) = maxJointVelocity;
99 }
100
101 if (in.isMaxJointVelocitiesSet())
102 {
103 NameFloatMap maxJointVelocities = in.getMaxJointVelocities();
104 NameFloatMap::iterator itv;
105 int i = 0;
106 for (std::string name : jointNames)
107 {
108 itv = maxJointVelocities.find(name);
109 maxVelocities(i) =
110 (double)(itv != maxJointVelocities.end() ? itv->second : maxJointVelocity);
111 ++i;
112 }
113 }
114
115 if (in.isMaxJointAccelerationsSet())
116 {
117 NameFloatMap maxJointAccelerations = in.getMaxJointAccelerations();
118 NameFloatMap::iterator ita;
119 int i = 0;
120 for (std::string name : jointNames)
121 {
122 ita = maxJointAccelerations.find(name);
123 maxAccelerations(i) =
124 (double)(ita != maxJointAccelerations.end() ? ita->second : maxJointAcceleration);
125 ++i;
126 }
127 }
128 ARMARX_INFO << "Calculating time optimal trajectory: " << inputTrajectory->output();
129 auto timeOptimalTraj = inputTrajectory->calculateTimeOptimalTrajectory(
130 maxVelocities, maxAccelerations, maxDeviation, IceUtil::Time::secondsDouble(timeStep));
131 ARMARX_INFO << "Done: " << timeOptimalTraj->output();
132 out.setTimeOptimalTrajectory(timeOptimalTraj);
133 emitSuccess();
134 // std::list<Eigen::VectorXd> waypoints;
135 // Eigen::VectorXd waypoint(jointNames.size());
136
137 // std::vector<Ice::DoubleSeq> allData(jointNames.size());
138 // for (size_t i = 0; i < jointNames.size(); i++)
139 // {
140 // allData.at(i) = inputTrajectory->getDimensionData(i);
141 // }
142
143 // Ice::DoubleSeq timeStamps = inputTrajectory->getTimestamps();
144 // for (size_t var = 0; var < timeStamps.size(); var++)
145 // {
146 // for (size_t i = 0; i < jointNames.size(); i++)
147 // {
148 // waypoint(i) = allData[i][var];
149 // }
150 // waypoints.push_back(waypoint);
151 // }
152
153 // VirtualRobot::TimeOptimalTrajectory timeOptimalTraj(VirtualRobot::Path(waypoints, maxDeviation), maxVelocities, maxAccelerations, timeStep);
154
155 // if (!timeOptimalTraj.isValid())
156 // {
157 // ARMARX_WARNING << "Generation of time-optimal trajectory failed. The original trajectory is returned.";
158 // inputTrajectory = balanceTimestamps(inputTrajectory);
159 // out.setTimeOptimalTrajectory(inputTrajectory);
160 // emitFailure();
161 // }
162 // else
163 // {
164 // TrajectoryPtr newTraj = new Trajectory();
165
166 // Ice::DoubleSeq newTimestamps;
167 // double duration = timeOptimalTraj.getDuration();
168 // for (double t = 0.0; t < duration; t += timeStep)
169 // {
170 // newTimestamps.push_back(t);
171 // }
172 // newTimestamps.push_back(duration);
173
174 // for (size_t d = 0; d < jointNames.size(); d++)
175 // {
176 // Ice::DoubleSeq position;
177 // for (double t = 0.0; t < duration; t += timeStep)
178 // {
179 // position.push_back(timeOptimalTraj.getPosition(t)[d]);
180 // }
181 // position.push_back(timeOptimalTraj.getPosition(duration)[d]);
182 // newTraj->addDimension(position, newTimestamps, inputTrajectory->getDimensionName(d));
183
184 // Ice::DoubleSeq derivs;
185 // for (double t = 0.0; t < duration; t += timeStep)
186 // {
187 // derivs.clear();
188 // derivs.push_back(timeOptimalTraj.getPosition(t)[d]);
189 // derivs.push_back(timeOptimalTraj.getVelocity(t)[d]);
190 // newTraj->addDerivationsToDimension(d, t, derivs);
191 // }
192 // derivs.clear();
193 // derivs.push_back(timeOptimalTraj.getPosition(duration)[d]);
194 // derivs.push_back(timeOptimalTraj.getVelocity(duration)[d]);
195 // newTraj->addDerivationsToDimension(d, duration, derivs);
196 // }
197
198 // out.setTimeOptimalTrajectory(newTraj);
199 // emitSuccess();
200 // }
201}
202
203//void TransformTrajectoryIntoTimeOptimal::onBreak()
204//{
205// // put your user code for the breaking point here
206// // execution time should be short (<100ms)
207//}
208
209void
211{
212 // put your user code for the exit point here
213 // execution time should be short (<100ms)
214}
215
216// DO NOT EDIT NEXT FUNCTION
#define VAROUT(x)
std::map< std::string, float > NameFloatMap
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
The Trajectory class represents n-dimensional sampled trajectories.
Definition Trajectory.h:77
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64