DesignerTrajectoryCalculator.cpp
Go to the documentation of this file.
3 #include "PathFactory.h"
4 #include <VirtualRobot/TimeOptimalTrajectory/Path.h>
6 
7 #define TRAJECTORYCALCULATION_TIMESTEP 0.001
9 {
10 
11 }
12 
13 armarx::TimedTrajectory armarx::DesignerTrajectoryCalculator::calculateTimeOptimalTrajectory(std::vector<std::vector<double> >& trajectory, std::vector<std::vector<double> >& userPoints, VirtualRobot::RobotNodeSetPtr rns, double maxDeviation, double accuracyFactor)
14 {
15  //ARMARX_INFO << "Entering calculation";
16  //ARMARX_INFO << trajectory.size() << " jointangles in trajectory";
17  //Check if the Robot has the used RobotNodeSet
18  if (!environment->getRobot()->hasRobotNodeSet(rns))
19  {
20  throw InvalidArgumentException("Robot does not have the supplied robotnodeset!");
21  }
22  //Create a Simox VirtualRobot Path. This is later used to calculate the TimeOptimalTrajectory
23  VirtualRobot::Path path = armarx::PathFactory::createPath(trajectory, maxDeviation);
24  //If all points are equal, create a trajectory with minimal duration and the respective start and end points
25  //ARMARX_INFO << "Path length is " << path.getLength();
26  if (path.getLength() == 0)
27  {
28  //If the path length is 0, create a pseudo trajectory containing the start position once for every userPoint
29  std::vector<double> timestamps;
30  //ARMARX_INFO << "Filling timestamps";
31  for (unsigned int i = 0; i < userPoints.size(); i ++)
32  {
33  timestamps.push_back(0.01 * i);
34  }
35 
36  //add first point
37  std::vector<std::vector<double>> nodeData;
38  for (unsigned int i = 0; i < userPoints[0].size(); i++)
39  {
40  nodeData.push_back({userPoints[0][i]});
41 
42  }
43  for (unsigned int i = 1; i < userPoints.size(); i++)
44  {
45  for (unsigned int j = 0; j < userPoints[0].size(); j++)
46  {
47  nodeData[j].push_back(userPoints[i][j]);
48  }
49  }
50 
51  Ice::StringSeq dimensionNames = rns->getNodeNames();
52  TrajectoryPtr traj = new Trajectory(nodeData, timestamps, dimensionNames);
53  return TimedTrajectory(traj, timestamps);
54  }
55  //Get the maxVelocity and maxAcceleration for every joint
56  //If the path length is 0, create a pseudo trajectory containing the start position once for every userPoint
57 
58 
59  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60  /// CREATE TRAJECTORY TO UNFOLD
61  ///
62  ///
63  std::vector<double> timestamps = std::vector<double>();
64  for (unsigned int j = 0; j < trajectory.size(); j++)
65  {
66  timestamps.push_back(j * 0.01);
67  }
68  std::vector<std::vector<double>> nodeData = std::vector<std::vector<double>>();
69  for (unsigned int i = 0; i < rns->getAllRobotNodes().size(); i++)
70  {
71  std::vector<double> node = std::vector<double>();
72  for (unsigned int j = 0; j < trajectory.size(); j++)
73  {
74  node.push_back(trajectory[j][i]);
75  }
76  nodeData.push_back(node);
77  }
78  //ARMARX_INFO << trajectory;
79  Ice::StringSeq dimensionNames = rns->getNodeNames();
80  TrajectoryPtr traj = new Trajectory(nodeData, timestamps, dimensionNames);
81  //ARMARX_INFO << traj;
82  //ARMARX_INFO << std::to_string(traj->getLength());
83  LimitlessStateSeq state;
84  for (VirtualRobot::RobotNodePtr node : rns->getAllRobotNodes())
85  {
86  LimitlessState current;
87  current.enabled = node->isLimitless();
88  current.limitLo = node->getJointLimitLow();
89  current.limitHi = node->getJointLimitHigh();
90  state.push_back(current);
91  }
92  traj->setLimitless(state);
94  //ARMARX_INFO << "UNFOLDED";
95  //TURN TRAJECTORY BACK TO VECTOR
96  std::vector<std::vector<double>> newPathVector = std::vector<std::vector<double>>();
97  //ARMARX_INFO << std::to_string(traj->getLength());
98  //ARMARX_ERROR << traj->toEigen();
99  Eigen::MatrixXd trajMatrix = traj->toEigen();
100  for (int i = 0; i < trajMatrix.rows(); i++)
101  {
102  //ARMARX_INFO << "Trying to get state at " + std::to_string(i * 0.01);
103  std::vector<double> jA = std::vector<double>();
104  for (int j = 0; j < trajMatrix.cols(); j++)
105  {
106  jA.push_back(trajMatrix(i, j));
107  }
108  //ARMARX_INFO << jA;
109  newPathVector.push_back(jA);
110 
111  }
112  //ARMARX_INFO << newPathVector;
113  VirtualRobot::Path unfoldedPath = armarx::PathFactory::createPath(newPathVector, maxDeviation);
114  //ARMARX_INFO << "FINISHED";
115  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116  std::vector<double> mVelocity;
117  std::vector<double> mAcceleration;
118  for (VirtualRobot::RobotNodePtr rn : rns->getAllRobotNodes())
119  {
120  if (rn->getMaxVelocity() == -1.0)
121  {
122  mVelocity.push_back(10);
123  }
124  else
125  {
126  mVelocity.push_back(rn->getMaxVelocity());
127  }
128  if (rn->getMaxAcceleration() == -1.0)
129  {
130  mAcceleration.push_back(10);
131  }
132  else
133  {
134  mAcceleration.push_back(rn->getMaxAcceleration());
135  }
136  }
137  const Eigen::VectorXd maxVelocity = Eigen::VectorXd::Map(&mVelocity[0], mVelocity.size());
138  const Eigen::VectorXd maxAcceleration = Eigen::VectorXd::Map(&mAcceleration[0], mAcceleration.size());
139  //Set the timestep to the trajectorycalculation timestep constant for now
140  double timeStep = TRAJECTORYCALCULATION_TIMESTEP;
141 
142  //Calculate the TimeOptimalTrajectory using Simox, and bring the accuracyFactor into account
143  //ARMARX_INFO << "Calling Simox TimeOptimalTrajectory";
144  VirtualRobot::TimeOptimalTrajectory timeOptimalTrajectory = VirtualRobot::TimeOptimalTrajectory(unfoldedPath, maxVelocity, maxAcceleration, timeStep / accuracyFactor);
145  //If simox failed finding a trajectory, signal this
146  if (timeOptimalTrajectory.isValid() == 0)
147  {
148  //ARMARX_INFO << "Invalid trajectory";
149  throw InvalidArgumentException("Input trajectory can not generate a continuous path");
150  }
151  //As simox sometimes returns valid on trajectories with nonreal or negative duration, check this too
152  if (timeOptimalTrajectory.getDuration() <= 0 || std::isnan(timeOptimalTrajectory.getDuration()) || timeOptimalTrajectory.getDuration() >= HUGE_VAL)
153  {
154  //ARMARX_INFO << "Invalid trajectory duration";
155  throw InvalidArgumentException("Input trajectory can not generate a continuous path with positive real duration");
156  }
157  //ARMARX_INFO << "TimeOptimalTrajectory has duration of " << timeOptimalTrajectory.getDuration();
158  //Return the result
159  return armarx::TimedTrajectoryFactory::createTimedTrajectory(timeOptimalTrajectory, userPoints, rns, maxDeviation);
160 }
TRAJECTORYCALCULATION_TIMESTEP
#define TRAJECTORYCALCULATION_TIMESTEP
Definition: DesignerTrajectoryCalculator.cpp:7
armarx::VariantType::Map
const VariantContainerType Map
Definition: StringValueMap.h:247
armarx::DesignerTrajectoryCalculator::DesignerTrajectoryCalculator
DesignerTrajectoryCalculator(EnvironmentPtr environment)
Definition: DesignerTrajectoryCalculator.cpp:8
PathFactory.h
IceInternal::Handle< Trajectory >
armarx::EnvironmentPtr
std::shared_ptr< Environment > EnvironmentPtr
Definition: Environment.h:29
armarx::TrajectoryController::UnfoldLimitlessJointPositions
static void UnfoldLimitlessJointPositions(TrajectoryPtr traj)
Definition: TrajectoryController.cpp:125
armarx::DesignerTrajectoryCalculator::calculateTimeOptimalTrajectory
TimedTrajectory calculateTimeOptimalTrajectory(std::vector< std::vector< double >> &trajectory, std::vector< std::vector< double >> &userPoints, VirtualRobot::RobotNodeSetPtr rns, double maxDeviation, double accuracyFactor=1.0)
Calculates a TimedTrajectory from the supplied trajectory and points set by user.
Definition: DesignerTrajectoryCalculator.cpp:13
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
DesignerTrajectoryCalculator.h
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
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
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
TrajectoryController.h
armarx::PathFactory::createPath
static VirtualRobot::Path createPath(std::vector< std::vector< double >> &nodes, double maxDeviation)
Creates a Path out of the nodes with a maximum deviation.
Definition: PathFactory.cpp:3