DesignerTrajectory.cpp
Go to the documentation of this file.
1 #include "DesignerTrajectory.h"
2 #include <iostream>
3 using namespace std;
4 
5 using namespace armarx;
6 
7 VirtualRobot::RobotNodeSetPtr DesignerTrajectory::getRns()
8 {
9  return rns;
10 }
11 
12 void DesignerTrajectory::setRns(const VirtualRobot::RobotNodeSetPtr& value)
13 {
14  if (value != nullptr)
15  {
16  rns = value;
17  }
18  else
19  {
20  throw InvalidArgumentException("Can not set VirtualRobot::RobotNodeSetPtr rns = nullptr");
21  }
22 }
23 
24 std::vector<TrajectoryPtr> DesignerTrajectory::getInterBreakpointTrajectories()
25 {
26  return interBreakpointTrajectories;
27 }
28 
29 void DesignerTrajectory::setInterBreakpointTrajectories(
30  const std::vector<TrajectoryPtr>& value)
31 {
32  if (value.size() != 0)
33  {
34  interBreakpointTrajectories = value;
35  }
36  else
37  {
38  throw InvalidArgumentException(
39  "Can not set std::vector<TrajectoryPtr> interBreakpointTrajectories with empty vector ");
40  }
41 }
42 
43 
45  VirtualRobot::RobotNodeSetPtr newRns)
46 {
47  if (newRns != nullptr)
48  {
49  rns = newRns;
50  }
51  else
52  {
53  throw InvalidArgumentException("Can not construct DesignerTrajectory with VirtualRobot::RobotNodeSetPtr rns = nullptr");
54  }
55  if (firstPoint != nullptr)
56  {
57  addFirstUserWaypoint(firstPoint);
58  }
59  else
60  {
61  throw InvalidArgumentException("Can not construct DesignerTrajectory with UserWaypointPtr firstPoint = nullptr");
62  }
63 
64  std::vector<Ice::DoubleSeq> data;
65  std::vector<double> jointAngles = firstPoint->getJointAngles();
66  if (jointAngles.size() != 0)
67  {
68  //add jointAngles to every dimension
69  for (unsigned int i = 0; i < jointAngles.size(); i++)
70  {
71  data.push_back({jointAngles[i]});
72  }
73  }
74  else
75  {
76  throw InvalidArgumentException("firstPoint of a DesignerTrajectory must have jointAngles");
77  }
78  //firstPoint has eveytime timestamp 0
79  interBreakpointTrajectories.push_back(TrajectoryPtr(new Trajectory(data,
80  {0}, rns->getNodeNames())));
81 }
82 
83 DesignerTrajectory::DesignerTrajectory(const DesignerTrajectory& source) :
84  rns(source.rns)
85 {
86  std::vector<TrajectoryPtr> trajectoriesTmp;
87  std::vector<UserWaypointPtr> userWaypointsTmp;
88  std::vector<TransitionPtr> transitionsTmp;
89 
90  for (TrajectoryPtr t : source.interBreakpointTrajectories)
91  {
92  trajectoriesTmp.push_back(TrajectoryPtr(new Trajectory(*t)));
93  }
94 
95 
96  for (UserWaypointPtr w : source.userWaypoints)
97  {
98  userWaypointsTmp.push_back(UserWaypointPtr(new UserWaypoint(*w)));
99  }
100 
101  for (unsigned int i = 0; i < userWaypointsTmp.size() - 1; i++)
102  {
103  UserWaypointPtr start = userWaypointsTmp[i];
104  UserWaypointPtr end = userWaypointsTmp[i + 1];
105  TransitionPtr trans = source.transitions[i];
106 
107  transitionsTmp.push_back(TransitionPtr(new Transition(*trans, start, end)));
108  }
109 
110  userWaypoints = userWaypointsTmp;
111  interBreakpointTrajectories = trajectoriesTmp;
112  transitions = transitionsTmp;
113 }
114 
116 {
117  if (point != nullptr)
118  {
119  userWaypoints.insert(userWaypoints.begin(), point);
120 
121  if (userWaypoints.size() > 1)
122  {
123  transitions.insert(transitions.begin(), TransitionPtr(
124  new Transition(userWaypoints[0], userWaypoints[1])));
125 
126  }
127  }
128  else
129  {
130  throw InvalidArgumentException("Can not add UserWaypoint with point = nullptr");
131  }
132 
133 }
134 
136 {
137  if (point != nullptr)
138  {
139  userWaypoints.push_back(point);
140  if (userWaypoints.size() > 1)
141  {
142  transitions.push_back(TransitionPtr(new Transition(
143  userWaypoints[userWaypoints.size() - 2],
144  userWaypoints[userWaypoints.size() - 1])));
145  }
146  }
147  else
148  {
149  throw InvalidArgumentException("Can not add UserWaypoint with point = nullptr");
150  }
151 }
152 
154  UserWaypointPtr& point, unsigned int index)
155 {
156  if (point != nullptr)
157  {
158  if (index != 0 && index < userWaypoints.size())
159  {
160  userWaypoints.insert(userWaypoints.begin() + index, point);
161 
162  if (userWaypoints.size() > 1)
163  {
164  transitions.insert(transitions.begin() + index - 1,
166  new Transition(userWaypoints[index - 1],
167  userWaypoints[index])));
168  transitions[index]->setStart(userWaypoints[index]);
169  }
170  }
171  else
172  {
173  if (index == 0)
174  {
175  addFirstUserWaypoint(point);
176  }
177  else
178  {
179  throw IndexOutOfBoundsException("insertUserWaypoint");
180  }
181  }
182  }
183  else
184  {
185  throw InvalidArgumentException("Can not add UserWaypoint with point = nullptr");
186  }
187 
188 }
189 
191 {
192  if (index < userWaypoints.size())
193  {
194  userWaypoints.erase(userWaypoints.begin() + index);
195  //delete first waypoint
196  if (index == 0)
197  {
198  transitions.erase(transitions.begin());
199  }
200  //delete last Waypoint
201  else if (index == transitions.size())
202  {
203  transitions.erase(transitions.begin() + index - 1);
204  }
205  else
206  {
207  transitions.erase(transitions.begin() + index);
208  transitions.erase(transitions.begin() + index - 1);
209  transitions.insert(transitions.begin() + index - 1,
210  std::shared_ptr<Transition>(
211  new Transition(userWaypoints[index - 1],
212  userWaypoints[index])));
213  }
214  }
215  else
216  {
217  throw IndexOutOfBoundsException("deleteUserWaypoint");
218  }
219 }
220 
221 
223 {
224  return userWaypoints.size();
225 }
226 
228 {
229  if (index < userWaypoints.size())
230  {
231  return userWaypoints[index];
232  }
233  else
234  {
235  throw IndexOutOfBoundsException("getUserWaypoint");
236  }
237 
238 }
239 
241 {
242  if (index < transitions.size())
243  {
244  return transitions[index];
245  }
246  else
247  {
248  throw IndexOutOfBoundsException();
249  }
250 
251 }
252 
254 {
255  std::vector<Ice::DoubleSeq> dimensionDatas = getDimensionDatas();
256  Ice::DoubleSeq timestamps = getAllTimestamps();
257  TrajectoryPtr tmp = TrajectoryPtr(new Trajectory(dimensionDatas, timestamps,
258  interBreakpointTrajectories[0]->getDimensionNames()));
259  setLimitless(tmp, rns);
260 
261  return tmp;
262 }
263 
265 {
266  if (index < interBreakpointTrajectories.size())
267  {
268  return interBreakpointTrajectories[index];
269  }
270  else
271  {
272  throw IndexOutOfBoundsException();
273  }
274 
275 }
276 
277 std::vector<UserWaypointPtr> DesignerTrajectory::getAllUserWaypoints() const
278 {
279  std::vector <UserWaypointPtr> tmp;
280 
281  for (unsigned int i = 0; i < userWaypoints.size(); i++)
282  {
283  tmp.push_back(UserWaypointPtr(new UserWaypoint(*userWaypoints[i])));
284  }
285  return tmp;
286 }
287 
288 std::vector<UserWaypointPtr> DesignerTrajectory::getAllUserWaypoints()
289 {
290  return userWaypoints;
291 }
292 
294 {
295  if (userWaypoints.size() > 1)
296  {
297 
298  std::vector<Ice::DoubleSeq> dimensionDatas = getDimensionDatas();
299  std::vector<double> timestamps = getAllTimestamps();
300 
301  double tmpDif = 0;
302  unsigned int count = 0;
303  for (unsigned int i = 0; i < transitions.size() - 1; i++)
304  {
305  TransitionPtr t = transitions[i];
306  double stretch = t->getUserDuration() / t->getTimeOptimalDuration();
307  unsigned int start = count;
308  //search for the index which represents the end of the transition
309  double endTime = t->getEnd()->getTimeOptimalTimestamp();
310  while (count + 1 < timestamps.size() && endTime > timestamps[count + 1])
311  {
312  count++;
313  }
314  unsigned int end = count;
315  for (unsigned int k = start; k < end; k++)
316  {
317  timestamps[k + 1] = timestamps[k + 1] + tmpDif;
318  double oldDuration = timestamps[k + 1] - timestamps[k];
319  double newDuration = oldDuration * stretch;
320  tmpDif = tmpDif + newDuration - oldDuration;
321  timestamps[k + 1] = timestamps[k] + newDuration;
322  }
323  }
324  //last transition
325  TransitionPtr t = transitions.back();
326  double stretch = t->getUserDuration() / t->getTimeOptimalDuration();
327  unsigned int start = count;
328  unsigned int end = timestamps.size() - 1;
329  for (unsigned int k = start; k < end; k++)
330  {
331  timestamps[k + 1] = timestamps[k + 1] + tmpDif;
332  double oldDuration = timestamps[k + 1] - timestamps[k];
333  double newDuration = (timestamps[k + 1] - timestamps[k]) * stretch;
334  tmpDif = tmpDif + newDuration - oldDuration;
335  timestamps[k + 1] = timestamps[k] + newDuration;
336  }
337  TrajectoryPtr traj = TrajectoryPtr(new Trajectory(dimensionDatas,
338  timestamps,
339  interBreakpointTrajectories[0]->getDimensionNames()));
340  setLimitless(traj, rns);
341  return traj;
342 
343  }
344  else
345  {
346  return interBreakpointTrajectories[0];
347  }
348 }
349 
350 
351 //////////////private/////////////////////////////////////////////////////////////////////
352 std::vector<std::vector<double>> DesignerTrajectory::getDimensionDatas()
353 {
354  std::vector<Ice::DoubleSeq> dimensionDatas;
355 
356  if (interBreakpointTrajectories.size() != 0)
357  {
358  //just for the first point
359  for (unsigned int i = 0; i < interBreakpointTrajectories[0]->dim(); i++)
360  {
361  //get the JointAngle of the first point at current dimension
362  dimensionDatas.push_back({interBreakpointTrajectories[0]->getDimensionData(i)[0]});
363  }
364 
365  //through all trajectories
366  for (const TrajectoryPtr& t : interBreakpointTrajectories)
367  {
368  //through all dimensions
369  unsigned int dimension = t->dim();
370  for (unsigned int i = 0; i < dimension; i++)
371  {
372  //get all jointAngles
373  std::vector<double> newDatas = t->getDimensionData(i);
374  dimensionDatas[i].insert(dimensionDatas[i].end(),
375  newDatas.begin() + 1,
376  newDatas.end());
377  }
378  }
379  return dimensionDatas;
380  }
381  else
382  {
383  throw LogicError("No inter breakpoint trajectories");
384  }
385 }
386 
387 
388 std::vector<double> DesignerTrajectory::getAllTimestamps()
389 {
390  std::vector<double> timestamps;
391 
392  if (interBreakpointTrajectories.size() != 0)
393  {
394  //first timestamp
395  timestamps.push_back(interBreakpointTrajectories[0]->getTimestamps()[0]);
396  //through all trajectories
397  for (const TrajectoryPtr& t : interBreakpointTrajectories)
398  {
399  double d = timestamps.back();
400  std::vector<double> newTimestamps = t->getTimestamps();
401  for (unsigned int i = 1; i < newTimestamps.size(); i++)
402  {
403  timestamps.push_back(newTimestamps[i] + d);
404  }
405  }
406  return timestamps;
407  }
408  else
409  {
410  throw LogicError("No inter breakpoint trajectories");
411  }
412 }
413 
414 void DesignerTrajectory::setLimitless(TrajectoryPtr traj, VirtualRobot::RobotNodeSetPtr rns)
415 {
416  //set Limitless state for smooth interpolation
417  LimitlessStateSeq states;
418  for (VirtualRobot::RobotNodePtr node : rns->getAllRobotNodes())
419  {
420  LimitlessState state;
421  state.enabled = node->isLimitless();
422  state.limitLo = node->getJointLimitLow();
423  state.limitHi = node->getJointLimitHigh();
424  states.push_back(state);
425  }
426  traj->setLimitless(states);
427 }
428 
armarx::UserWaypointPtr
std::shared_ptr< UserWaypoint > UserWaypointPtr
Definition: UserWaypoint.h:137
armarx::TrajectoryPtr
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition: Trajectory.h:52
armarx::DesignerTrajectory::getNrOfUserWaypoints
unsigned int getNrOfUserWaypoints() const
get the number of the userwaypoints
Definition: DesignerTrajectory.cpp:222
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::DesignerTrajectory::getAllUserWaypoints
std::vector< UserWaypointPtr > getAllUserWaypoints() const
get a copy of all userwaypoints
Definition: DesignerTrajectory.cpp:277
armarx::DesignerTrajectory::addFirstUserWaypoint
void addFirstUserWaypoint(UserWaypointPtr &point)
add a new first userWaypoint
Definition: DesignerTrajectory.cpp:115
armarx::UserWaypoint
The UserWaypoint class represents a waypoint of the trajectory.
Definition: UserWaypoint.h:42
armarx::DesignerTrajectory
Definition: DesignerTrajectory.h:37
armarx::DesignerTrajectory::getTimeOptimalTrajectory
TrajectoryPtr getTimeOptimalTrajectory()
get the time optimal trajectory.
Definition: DesignerTrajectory.cpp:253
DesignerTrajectory.h
IceInternal::Handle< Trajectory >
armarx::DesignerTrajectory::deleteUserWaypoint
void deleteUserWaypoint(unsigned int index)
delete the userwaypoint and remove all transitions including the userwaypoint.
Definition: DesignerTrajectory.cpp:190
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DesignerTrajectory::getTransition
TransitionPtr getTransition(unsigned int index)
get the transition
Definition: DesignerTrajectory.cpp:240
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::DesignerTrajectory::DesignerTrajectory
DesignerTrajectory(UserWaypointPtr &firstPoint, VirtualRobot::RobotNodeSetPtr newRns)
Definition: DesignerTrajectory.cpp:44
armarx::TransitionPtr
std::shared_ptr< Transition > TransitionPtr
Definition: Transition.h:141
armarx::Transition
Definition: Transition.h:36
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::DesignerTrajectory::getFinalTrajectory
TrajectoryPtr getFinalTrajectory()
get the final trajectory with the right durations
Definition: DesignerTrajectory.cpp:293
armarx::DesignerTrajectory::getUserWaypoint
UserWaypointPtr getUserWaypoint(unsigned int index)
get the userWaypoint
Definition: DesignerTrajectory.cpp:227
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::DesignerTrajectory::getTrajectorySegment
TrajectoryPtr getTrajectorySegment(unsigned int index)
get one interBreakPoint trajectory
Definition: DesignerTrajectory.cpp:264
std
Definition: Application.h:66
armarx::DesignerTrajectory::addLastUserWaypoint
void addLastUserWaypoint(UserWaypointPtr &point)
add new last userWaypoint
Definition: DesignerTrajectory.cpp:135
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DesignerTrajectory::insertUserWaypoint
void insertUserWaypoint(UserWaypointPtr &point, unsigned int index)
insert userwaypoint before index
Definition: DesignerTrajectory.cpp:153