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 #include <VirtualRobot/TimeOptimalTrajectory/TimeOptimalTrajectory.h>
25 
26 //#include <ArmarXCore/core/time/TimeUtil.h>
27 //#include <ArmarXCore/observers/variant/DatafieldRef.h>
28 
29 using namespace armarx;
30 using namespace MotionControlGroup;
31 
32 using NameFloatMap = std::map<std::string, float>;
33 
34 // DO NOT EDIT NEXT LINE
35 TransformTrajectoryIntoTimeOptimal::SubClassRegistry TransformTrajectoryIntoTimeOptimal::Registry(TransformTrajectoryIntoTimeOptimal::GetName(), &TransformTrajectoryIntoTimeOptimal::CreateInstance);
36 
38 {
39  ARMARX_INFO << t->output();
40  double length = t->getLength(0);
41  if (length == 0.0f)
42  {
43  return t;
44  }
45  ARMARX_INFO << VAROUT(length);
46  double timelength = t->getTimeLength();
47  ARMARX_INFO << VAROUT(timelength);
48  auto timestamps = t->getTimestamps();
49  ARMARX_INFO << VAROUT(timestamps);
50  Ice::DoubleSeq newTimestamps;
51  newTimestamps.push_back(0);
52  for (size_t var = 0; var < timestamps.size() - 1; ++var)
53  {
54  double tBefore = timestamps.at(var);
55  double tAfter = (timestamps.at(var + 1));
56  ARMARX_INFO << VAROUT(tBefore) << VAROUT(tAfter);
57  double partLength = t->getLength(0, tBefore, tAfter);
58  double lengthPortion = partLength / length;
59  ARMARX_INFO << VAROUT(partLength) << VAROUT(lengthPortion);
60  newTimestamps.push_back(*newTimestamps.rbegin() + timelength * lengthPortion);
61  }
62  ARMARX_INFO << VAROUT(newTimestamps);
63  TrajectoryPtr newTraj = new Trajectory();
64  for (size_t d = 0; d < t->dim(); ++d)
65  {
66  newTraj->addDimension(t->getDimensionData(d), newTimestamps, t->getDimensionName(d));
67  }
68  ARMARX_INFO << newTraj->output();
69 
70  return newTraj;
71 }
72 
74 {
75 
76 }
77 
79 {
80  TrajectoryPtr inputTrajectory = in.getTrajectory();
81 
82  double maxDeviation = in.getMaxDeviation();
83  double timeStep = in.getTimeStep();
84  float maxJointVelocity = in.getMaxJointVelocity();
85  float maxJointAcceleration = in.getMaxJointAcceleration();
86 
87  Ice::StringSeq jointNames = inputTrajectory->getDimensionNames();
88  Eigen::VectorXd maxAccelerations(jointNames.size());
89  Eigen::VectorXd maxVelocities(jointNames.size());
90  for (size_t i = 0; i < jointNames.size(); i++)
91  {
92  maxAccelerations(i) = maxJointAcceleration;
93  maxVelocities(i) = maxJointVelocity;
94  }
95 
96  if (in.isMaxJointVelocitiesSet())
97  {
98  NameFloatMap maxJointVelocities = in.getMaxJointVelocities();
99  NameFloatMap::iterator itv;
100  int i = 0;
101  for (std::string name : jointNames)
102  {
103  itv = maxJointVelocities.find(name);
104  maxVelocities(i) = (double)(itv != maxJointVelocities.end() ? itv->second : maxJointVelocity);
105  ++i;
106  }
107  }
108 
109  if (in.isMaxJointAccelerationsSet())
110  {
111  NameFloatMap maxJointAccelerations = in.getMaxJointAccelerations();
112  NameFloatMap::iterator ita;
113  int i = 0;
114  for (std::string name : jointNames)
115  {
116  ita = maxJointAccelerations.find(name);
117  maxAccelerations(i) = (double)(ita != maxJointAccelerations.end() ? ita->second : maxJointAcceleration);
118  ++i;
119  }
120  }
121  ARMARX_INFO << "Calculating time optimal trajectory: " << inputTrajectory->output();
122  auto timeOptimalTraj = inputTrajectory->calculateTimeOptimalTrajectory(maxVelocities, maxAccelerations, maxDeviation, IceUtil::Time::secondsDouble(timeStep));
123  ARMARX_INFO << "Done: " << timeOptimalTraj->output();
124  out.setTimeOptimalTrajectory(timeOptimalTraj);
125  emitSuccess();
126  // std::list<Eigen::VectorXd> waypoints;
127  // Eigen::VectorXd waypoint(jointNames.size());
128 
129  // std::vector<Ice::DoubleSeq> allData(jointNames.size());
130  // for (size_t i = 0; i < jointNames.size(); i++)
131  // {
132  // allData.at(i) = inputTrajectory->getDimensionData(i);
133  // }
134 
135  // Ice::DoubleSeq timeStamps = inputTrajectory->getTimestamps();
136  // for (size_t var = 0; var < timeStamps.size(); var++)
137  // {
138  // for (size_t i = 0; i < jointNames.size(); i++)
139  // {
140  // waypoint(i) = allData[i][var];
141  // }
142  // waypoints.push_back(waypoint);
143  // }
144 
145  // VirtualRobot::TimeOptimalTrajectory timeOptimalTraj(VirtualRobot::Path(waypoints, maxDeviation), maxVelocities, maxAccelerations, timeStep);
146 
147  // if (!timeOptimalTraj.isValid())
148  // {
149  // ARMARX_WARNING << "Generation of time-optimal trajectory failed. The original trajectory is returned.";
150  // inputTrajectory = balanceTimestamps(inputTrajectory);
151  // out.setTimeOptimalTrajectory(inputTrajectory);
152  // emitFailure();
153  // }
154  // else
155  // {
156  // TrajectoryPtr newTraj = new Trajectory();
157 
158  // Ice::DoubleSeq newTimestamps;
159  // double duration = timeOptimalTraj.getDuration();
160  // for (double t = 0.0; t < duration; t += timeStep)
161  // {
162  // newTimestamps.push_back(t);
163  // }
164  // newTimestamps.push_back(duration);
165 
166  // for (size_t d = 0; d < jointNames.size(); d++)
167  // {
168  // Ice::DoubleSeq position;
169  // for (double t = 0.0; t < duration; t += timeStep)
170  // {
171  // position.push_back(timeOptimalTraj.getPosition(t)[d]);
172  // }
173  // position.push_back(timeOptimalTraj.getPosition(duration)[d]);
174  // newTraj->addDimension(position, newTimestamps, inputTrajectory->getDimensionName(d));
175 
176  // Ice::DoubleSeq derivs;
177  // for (double t = 0.0; t < duration; t += timeStep)
178  // {
179  // derivs.clear();
180  // derivs.push_back(timeOptimalTraj.getPosition(t)[d]);
181  // derivs.push_back(timeOptimalTraj.getVelocity(t)[d]);
182  // newTraj->addDerivationsToDimension(d, t, derivs);
183  // }
184  // derivs.clear();
185  // derivs.push_back(timeOptimalTraj.getPosition(duration)[d]);
186  // derivs.push_back(timeOptimalTraj.getVelocity(duration)[d]);
187  // newTraj->addDerivationsToDimension(d, duration, derivs);
188  // }
189 
190  // out.setTimeOptimalTrajectory(newTraj);
191  // emitSuccess();
192  // }
193 }
194 
195 //void TransformTrajectoryIntoTimeOptimal::onBreak()
196 //{
197 // // put your user code for the breaking point here
198 // // execution time should be short (<100ms)
199 //}
200 
202 {
203  // put your user code for the exit point here
204  // execution time should be short (<100ms)
205 }
206 
207 
208 // DO NOT EDIT NEXT FUNCTION
210 {
212 }
213 
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::Registry
static SubClassRegistry Registry
Definition: TransformTrajectoryIntoTimeOptimal.h:49
TransformTrajectoryIntoTimeOptimal.h
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::run
void run() override
Definition: TransformTrajectoryIntoTimeOptimal.cpp:78
IceInternal::Handle< Trajectory >
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::onEnter
void onEnter() override
Definition: TransformTrajectoryIntoTimeOptimal.cpp:73
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::balanceTimestamps
TrajectoryPtr balanceTimestamps(TrajectoryPtr t)
Definition: TransformTrajectoryIntoTimeOptimal.cpp:37
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: TransformTrajectoryIntoTimeOptimal.cpp:209
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::TransformTrajectoryIntoTimeOptimal
TransformTrajectoryIntoTimeOptimal(const XMLStateConstructorParams &stateData)
Definition: TransformTrajectoryIntoTimeOptimal.h:33
armarx::MotionControlGroup::TransformTrajectoryIntoTimeOptimal::onExit
void onExit() override
Definition: TransformTrajectoryIntoTimeOptimal.cpp:201
NameFloatMap
std::map< std::string, float > NameFloatMap
Definition: TransformTrajectoryIntoTimeOptimal.cpp:32
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28