trajectory.cpp
Go to the documentation of this file.
1 #include "trajectory.hpp"
2 
3 namespace hebi {
4 namespace trajectory {
5 
6 Trajectory::Trajectory(std::vector<HebiTrajectoryPtr> trajectories, size_t number_of_waypoints, double start_time, double end_time)
7  : trajectories_(trajectories),
8  number_of_joints_ (trajectories.size()),
9  number_of_waypoints_(number_of_waypoints),
10  start_time_(start_time),
11  end_time_(end_time)
12 {
13 }
14 
15 std::shared_ptr<Trajectory> Trajectory::createUnconstrainedQp(
16  const VectorXd& time_vector,
17  const MatrixXd& positions,
18  const MatrixXd* velocities,
19  const MatrixXd* accelerations)
20 {
21  std::shared_ptr<Trajectory> res;
22 
23  // Check argument validity
24  size_t num_joints = positions.rows();
25  size_t num_waypoints = positions.cols();
26  if (time_vector.size() != num_waypoints)
27  return res;
28  if (velocities != nullptr && (velocities->rows() != num_joints && velocities->cols() != num_waypoints))
29  return res;
30  if (accelerations != nullptr && (accelerations->rows() != num_joints && accelerations->cols() != num_waypoints))
31  return res;
32  if (num_waypoints < 2)
33  return res;
34 
35  // Put data into C-style arrays:
36  double* time_vector_c = nullptr;
37  double* positions_c = nullptr;
38  double* velocities_c = nullptr;
39  double* accelerations_c = nullptr;
40  time_vector_c = new double[num_joints * num_waypoints];
41  {
42  Map<Matrix<double, Dynamic, Dynamic, RowMajor> > tmp(time_vector_c, num_waypoints, 1);
43  tmp = time_vector;
44  }
45  positions_c = new double[num_joints * num_waypoints];
46  {
47  Map<Matrix<double, Dynamic, Dynamic, RowMajor> > tmp(positions_c, num_joints, num_waypoints);
48  tmp = positions;
49  }
50  if (velocities != nullptr)
51  {
52  velocities_c = new double[num_joints * num_waypoints];
53  Map<Matrix<double, Dynamic, Dynamic, RowMajor> > tmp(velocities_c, num_joints, num_waypoints);
54  tmp = *velocities;
55  }
56  else // Default to [0 nan .... nan 0] (can remove when C API is updated)
57  {
58  velocities_c = new double[num_joints * num_waypoints];
59  for (size_t joint = 0; joint < num_joints; ++joint)
60  {
61  velocities_c[joint * num_waypoints + 0] = 0.0f;
62  velocities_c[joint * num_waypoints + num_waypoints - 1] = 0.0f;
63  for (size_t waypoint = 1; waypoint < num_waypoints - 1; ++waypoint)
64  velocities_c[joint * num_waypoints + waypoint] = std::numeric_limits<double>::quiet_NaN();
65  }
66  }
67  if (accelerations != nullptr)
68  {
69  accelerations_c = new double[num_joints * num_waypoints];
70  Map<Matrix<double, Dynamic, Dynamic, RowMajor> > tmp(accelerations_c, num_joints, num_waypoints);
71  tmp = *accelerations;
72  }
73  else // Default to [0 nan .... nan 0] (can remove when C API is updated)
74  {
75  accelerations_c = new double[num_joints * num_waypoints];
76  for (size_t joint = 0; joint < num_joints; ++joint)
77  {
78  accelerations_c[joint * num_waypoints + 0] = 0.0f;
79  accelerations_c[joint * num_waypoints + num_waypoints - 1] = 0.0f;
80  for (size_t waypoint = 1; waypoint < num_waypoints - 1; ++waypoint)
81  accelerations_c[joint * num_waypoints + waypoint] = std::numeric_limits<double>::quiet_NaN();
82  }
83  }
84 
85  // Build C trajectory objects
86  std::vector<HebiTrajectoryPtr> trajectories(num_joints, nullptr);
87  for (size_t i = 0; i < num_joints; ++i)
88  {
89  HebiTrajectoryPtr trajectory = hebiTrajectoryCreateUnconstrainedQp(num_waypoints,
90  (positions_c + i * num_waypoints),
91  velocities_c == nullptr ? nullptr : (velocities_c + i * num_waypoints),
92  accelerations_c == nullptr ? nullptr : (accelerations_c + i * num_waypoints),
93  time_vector_c);
94  // Failure? cleanup previous trajectories
95  if (trajectory == nullptr)
96  {
97  for (size_t j = 0; j < i; ++j)
98  {
99  hebiTrajectoryRelease(trajectories[j]);
100  }
101  return res;
102  }
103  trajectories[i] = trajectory;
104  }
105 
106  delete[] positions_c;
107  if (velocities_c != nullptr)
108  delete[] velocities_c;
109  if (accelerations_c != nullptr)
110  delete[] accelerations_c;
111 
112  // Create C++ wrapper
113  return std::shared_ptr<Trajectory>(new Trajectory(trajectories, num_waypoints, time_vector[0], time_vector[time_vector.size() - 1]));
114 }
115 
117 {
118  for (HebiTrajectoryPtr traj : trajectories_)
119  hebiTrajectoryRelease(traj);
120 }
121 
123 {
124  // Note -- could use any joint here, as they all have the same time vector
126 }
127 
128 bool Trajectory::getState(double time, VectorXd* position, VectorXd* velocity, VectorXd* acceleration) const
129 {
130  double tmp_p, tmp_v, tmp_a;
131  bool success = true;
132  for (size_t i = 0; i < trajectories_.size(); ++i)
133  {
134  success = (hebiTrajectoryGetState(
135  trajectories_[i],
136  time,
137  position == nullptr ? &tmp_p : &(*position)[i],
138  velocity == nullptr ? &tmp_v : &(*velocity)[i],
139  acceleration == nullptr ? &tmp_a : &(*acceleration)[i]) == 0) && success;
140  }
141  return success;
142 }
143 
144 } // namespace trajectory
145 } // namespace hebi
146 
HebiTrajectoryPtr hebiTrajectoryCreateUnconstrainedQp(size_t num_waypoints, const double *positions, const double *velocities, const double *accelerations, const double *time_vector)
Creates a HebiTrajectory object for a single joint using the given parameters; this must be released ...
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
Trajectory(std::vector< HebiTrajectoryPtr > trajectories, size_t number_of_waypoints, double start_time, double end_time)
Definition: trajectory.cpp:6
static std::shared_ptr< Trajectory > createUnconstrainedQp(const VectorXd &time_vector, const MatrixXd &positions, const MatrixXd *velocities=nullptr, const MatrixXd *accelerations=nullptr)
Creates a smooth trajectory through a set of waypoints (position velocity and accelerations defined a...
Definition: trajectory.cpp:15
Definition: color.hpp:5
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
HebiStatusCode hebiTrajectoryGetState(HebiTrajectoryPtr trajectory, double time, double *position, double *velocity, double *acceleration)
Gets the value of the trajectory at a given time.
double hebiTrajectoryGetDuration(HebiTrajectoryPtr trajectory)
Returns the length of this trajectory (in seconds).
void hebiTrajectoryRelease(HebiTrajectoryPtr trajectory)
Frees resources created by this trajectory.
double getDuration() const
The time (in seconds) between the start and end of this trajectory.
Definition: trajectory.cpp:122
bool getState(double time, VectorXd *position, VectorXd *velocity, VectorXd *acceleration) const
Returns the position, velocity, and acceleration for a given point in time along the trajectory...
Definition: trajectory.cpp:128
struct _HebiTrajectory * HebiTrajectoryPtr
The C-style&#39;s API representation of a trajectory.
Definition: hebi.h:411
~Trajectory() noexcept
Destructor cleans up resources for trajectory.
Definition: trajectory.cpp:116
std::vector< HebiTrajectoryPtr > trajectories_
Definition: trajectory.hpp:23


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:09:39