TimedTrajectoryFactory.cpp
Go to the documentation of this file.
2 
4 
5 #define TIMEDTRAJECTORY_PROBEFACTOR 100
6 
7 #define TIMEDTRAJECTORY_FPS 100
8 
9 armarx::TimedTrajectory armarx::TimedTrajectoryFactory::createTimedTrajectory(VirtualRobot::TimeOptimalTrajectory& trajectory, std::vector<std::vector<double>>& userPoints, VirtualRobot::RobotNodeSetPtr rns, double maxDeviation)
10 
11 {
12  //ARMARX_INFO << "Creating timedTrajectory";
13  //Check if the RobotNodeSet exists
14  if (rns == NULL)
15  {
16  //ARMARX_INFO << "Rns is null";
17  throw InvalidArgumentException("Rns is null");
18  }
19  //Check if the RobotNodeSet has nodes
20  if (rns->getAllRobotNodes().size() == 0)
21  {
22  //ARMARX_INFO << "Nodeset has no nodes";
23  throw InvalidArgumentException("Rns is empty");
24  }
25 
26  //Tune the timing parameter here
27  double timestep = 1 / ((double)(TIMEDTRAJECTORY_FPS));
28  //timestep may not be 0!
29  timestep = (timestep > 0) ? timestep : 0.01;
30  //If the trajectory is shorter than a second, generate as many frames as you would in a second
31  //(this guarantees that the probing is within the trajectory interval (timestep could otherwise be bigger)
32  if (trajectory.getDuration() < 1)
33  {
34  timestep = trajectory.getDuration() / TIMEDTRAJECTORY_FPS;
35  }
36 
37  std::vector<double> timestamps;
38  //std::vector<double> timestamps2;
39  //Nodedata is a vector containing the vectors of ALL positions at the respective timestamps for each joint.
40  //(Outer dimension : joints, Inner dimension: timestamp index)
41  std::vector<std::vector<double>> nodeData;
42  std::vector<std::vector<double>> nodeData2;
43 
44  //Initialize the vectors for the respective joints, fill in the first timestamp (0)
45  timestamps.push_back(0);
46  //timestamps2.push_back(0);
47  for (unsigned int i = 0; i < rns->getSize(); i++)
48  {
49  nodeData.push_back({trajectory.getPosition(0)[i]});
50  //nodeData2.push_back({trajectory.getPosition(0)[i]});
51  }
52  //Fill in the data for all other timestamps into the now existing joint values
53  for (double t = timestep; t < trajectory.getDuration(); t += timestep)
54  {
55  Eigen::VectorXd positions = trajectory.getPosition(t);
56  //Eigen::VectorXd positions2 = trajectory.getPosition(t);
57  for (unsigned int i = 0; i < positions.size(); i++)
58  {
59  nodeData[i].push_back(positions[i]);
60  //nodeData2[i].push_back(positions2[i]);
61  }
62  //ARMARX_INFO << "Time : " << t;
63  timestamps.push_back(t);
64  //timestamps2.push_back(t);
65  }
66  LimitlessStateSeq state;
67  for (VirtualRobot::RobotNodePtr node : rns->getAllRobotNodes())
68  {
69  LimitlessState current;
70  current.enabled = node->isLimitless();
71  current.limitLo = node->getJointLimitLow();
72  current.limitHi = node->getJointLimitHigh();
73  state.push_back(current);
74  }
75  //Get the correct names for the dimensions
76  Ice::StringSeq dimensionNames = rns->getNodeNames();
77  //Ice::StringSeq dimensionNames2 = rns->getNodeNames();
78  //ARMARX_WARNING << "HELLO";
79  //Construct the armarx Trajectory from the given data
80 
81  TrajectoryPtr traj = new Trajectory(nodeData, timestamps, dimensionNames);
82  traj->setLimitless(state);
83  //TrajectoryPtr original = new Trajectory(nodeData2, timestamps2, dimensionNames2);
85  /*int markedJoint = -1;
86  bool positive = false;
87  double start = 0;
88  for (double d : traj->getTimestamps())
89  {
90  Ice::DoubleSeq newStates = traj->getStates(d, 0);
91  Ice::DoubleSeq oldStates = original->getStates(d, 0);
92  int c = 0;
93  for (double f : newStates)
94  {
95  if ((f > 3.1 || f < -3.1) && rns->getAllRobotNodes()[c]->isLimitless())
96  {
97  ARMARX_ERROR << "Difference spotted " + std::to_string(f);
98 
99  markedJoint = c;
100  start = d;
101  if (f > 0)
102  {
103  positive = true;
104  }
105  }
106  if (c == markedJoint)
107  {
108  if (positive && f < -3.1 || !positive && f > 3.1)
109  {
110  traj = cutTrajectory(traj, start, d);
111  }
112  }
113  c++;
114  }
115  }*/
116  //ARMARX_ERROR << traj->toEigen();
117  int pass = 1;
118  //Arbitrary interrupt condition
119  for (pass = 1; pass < 10; pass ++)
120  {
121 
122  std::vector<double> times;
123  unsigned int i = 0;
124  //Map the user Points to the respective timestamps that their position is approximately reached within
125  for (double t = 0; t < trajectory.getDuration(); t += timestep / TIMEDTRAJECTORY_PROBEFACTOR)
126  {
127  Eigen::VectorXd jointAngles = Eigen::VectorXd::Map(&userPoints[i][0], userPoints[i].size());
128  /*for (int i = 0; i < jointAngles.rows(); i++)
129  {
130  ARMARX_INFO << "Point" << i << "jointAngles [" << i << "] : " << jointAngles[i];
131  }*/
132  Eigen::VectorXd pointJointAngles = trajectory.getPosition(t);
133  /*
134  for (int i = 0; i < pointJointAngles.rows(); i++)
135 
136  {
137  ARMARX_INFO << "Point" << i << "pointJointAngles [" << i << "] : " << pointJointAngles[i];
138  }*/
139  const double distance = (jointAngles - pointJointAngles).norm();
140  //ARMARX_INFO << "Distance " << distance;
141  //ARMARX_INFO << "MaxDeviation " << deviation;
142  //If the deviation between the userPoint and the data at the respective point is smaller than the maxDeviation, map it
143  if (distance <= maxDeviation)
144  {
145  times.push_back(t);
146  //ARMARX_INFO << std::to_string(t);
147  i++;
148  //ARMARX_INFO << "MAPPING USER POINT" << i;
149  if (i >= userPoints.size())
150  {
151  //ARMARX_INFO << "mapping last user point";
152  //For the last point, just add the final time of the trajectory as the respective timestamp
153  //this can help mitigate the resulting forward drift of timestamps caused by the inaccuracy (maxDeviation)
154  times.pop_back();
155  times.push_back(trajectory.getDuration());
156  break;
157  }
158  }
159  }
160  //ARMARX_INFO << "Userpoint mapping complete";
161  //ARMARX_INFO << "TimedTrajectory complete";
162  //If not all points were mapped, an output is created and an empty map is returned for the userPoints
163  if (times.size() < userPoints.size())
164  {
165  //Try to solve with recursively decreasing accuracy
166  //ARMARX_INFO << "Only " << times.size() << "of " << userPoints.size() << " points found";
167  //ARMARX_INFO << "Pass " << pass << " failed to find all points.";
168  maxDeviation *= 10;
169  //timestep /= 2;
170  continue;
171  }
172  //Return the result and the number of passes that were needed for the calculation
173  pass == 1 ? ARMARX_INFO << pass << " pass needed to find mapping" : ARMARX_INFO << pass << " passes needed to find mapping";
174  return TimedTrajectory(traj, times);
175  }
176  ARMARX_INFO << "More than" << pass << "passes needed for calculation";
177  //ARMARX_INFO << "Canceled calculation with maxDeviation of " << maxDeviation;
178  throw InvalidArgumentException("No TimedTrajectory could be created");
179 }
TIMEDTRAJECTORY_FPS
#define TIMEDTRAJECTORY_FPS
Definition: TimedTrajectoryFactory.cpp:7
armarx::TrajectoryController::FoldLimitlessJointPositions
static void FoldLimitlessJointPositions(TrajectoryPtr traj)
Definition: TrajectoryController.cpp:161
armarx::VariantType::Map
const VariantContainerType Map
Definition: StringValueMap.h:247
IceInternal::Handle< Trajectory >
TIMEDTRAJECTORY_PROBEFACTOR
#define TIMEDTRAJECTORY_PROBEFACTOR
Definition: TimedTrajectoryFactory.cpp:5
armarx::TimedTrajectoryFactory::createTimedTrajectory
static TimedTrajectory createTimedTrajectory(VirtualRobot::TimeOptimalTrajectory &trajectory, std::vector< std::vector< double >> &userPoints, VirtualRobot::RobotNodeSetPtr rns, double maxDeviation)
Creates a TimedTrajectory out of the TimeOptimalTrajectory and maps the parameter userPoints to it.
Definition: TimedTrajectoryFactory.cpp:9
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
TimedTrajectoryFactory.h
armarx::TimedTrajectory
A container for a Trajectory and a set of timestamps, representing the arrival of the Trajectory at u...
Definition: TimedTrajectory.h:13
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
TrajectoryController.h
norm
double norm(const Point &a)
Definition: point.hpp:94