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,
7  double end_time)
8  : trajectories_(trajectories),
9  number_of_joints_(trajectories.size()),
10  number_of_waypoints_(number_of_waypoints),
11  start_time_(start_time),
12  end_time_(end_time) {}
13 
14 std::shared_ptr<Trajectory> Trajectory::createUnconstrainedQp(const VectorXd& time_vector, const MatrixXd& positions,
15  const MatrixXd* velocities,
16  const MatrixXd* accelerations) {
17  std::shared_ptr<Trajectory> res;
18 
19  // Check argument validity
20  size_t num_joints = positions.rows();
21  size_t num_waypoints = positions.cols();
22  if (time_vector.size() != num_waypoints)
23  return res;
24  if (velocities != nullptr && (velocities->rows() != num_joints && velocities->cols() != num_waypoints))
25  return res;
26  if (accelerations != nullptr && (accelerations->rows() != num_joints && accelerations->cols() != num_waypoints))
27  return res;
28  if (num_waypoints < 2)
29  return res;
30 
31  // Put data into C-style arrays:
32  double* time_vector_c = nullptr;
33  double* positions_c = nullptr;
34  double* velocities_c = nullptr;
35  double* accelerations_c = nullptr;
36  time_vector_c = new double[num_joints * num_waypoints];
37  {
38  Map<Matrix<double, Dynamic, Dynamic, RowMajor>> tmp(time_vector_c, num_waypoints, 1);
39  tmp = time_vector;
40  }
41  positions_c = new double[num_joints * num_waypoints];
42  {
43  Map<Matrix<double, Dynamic, Dynamic, RowMajor>> tmp(positions_c, num_joints, num_waypoints);
44  tmp = positions;
45  }
46  if (velocities != nullptr) {
47  velocities_c = new double[num_joints * num_waypoints];
48  Map<Matrix<double, Dynamic, Dynamic, RowMajor>> tmp(velocities_c, num_joints, num_waypoints);
49  tmp = *velocities;
50  }
51  if (accelerations != nullptr) {
52  accelerations_c = new double[num_joints * num_waypoints];
53  Map<Matrix<double, Dynamic, Dynamic, RowMajor>> tmp(accelerations_c, num_joints, num_waypoints);
54  tmp = *accelerations;
55  }
56 
57  // Build C trajectory objects
58  std::vector<HebiTrajectoryPtr> trajectories(num_joints, nullptr);
59  for (size_t i = 0; i < num_joints; ++i) {
61  num_waypoints, (positions_c + i * num_waypoints),
62  velocities_c == nullptr ? nullptr : (velocities_c + i * num_waypoints),
63  accelerations_c == nullptr ? nullptr : (accelerations_c + i * num_waypoints), time_vector_c);
64  // Failure? cleanup previous trajectories
65  if (trajectory == nullptr) {
66  for (size_t j = 0; j < i; ++j) {
67  hebiTrajectoryRelease(trajectories[j]);
68  }
69  return res;
70  }
71  trajectories[i] = trajectory;
72  }
73 
74  delete[] positions_c;
75  if (velocities_c != nullptr)
76  delete[] velocities_c;
77  if (accelerations_c != nullptr)
78  delete[] accelerations_c;
79 
80  // Create C++ wrapper
81  return std::shared_ptr<Trajectory>(
82  new Trajectory(trajectories, num_waypoints, time_vector[0], time_vector[time_vector.size() - 1]));
83 }
84 
86  for (HebiTrajectoryPtr traj : trajectories_)
88 }
89 
90 double Trajectory::getDuration() const {
91  // Note -- could use any joint here, as they all have the same time vector
93 }
94 
95 bool Trajectory::getState(double time, VectorXd* position, VectorXd* velocity, VectorXd* acceleration) const {
96  double tmp_p, tmp_v, tmp_a;
97  bool success = true;
98  for (size_t i = 0; i < trajectories_.size(); ++i) {
99  success = (hebiTrajectoryGetState(trajectories_[i], time, position == nullptr ? &tmp_p : &(*position)[i],
100  velocity == nullptr ? &tmp_v : &(*velocity)[i],
101  acceleration == nullptr ? &tmp_a : &(*acceleration)[i]) == 0) &&
102  success;
103  }
104  return success;
105 }
106 
107 } // namespace trajectory
108 } // namespace hebi
HebiTrajectoryPtr hebiTrajectoryCreateUnconstrainedQp(size_t num_waypoints, const double *positions, const double *velocities, const double *accelerations, const double *time_vector)
Trajectory API.
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:14
Definition: arm.cpp:5
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.
struct HebiTrajectory_ * HebiTrajectoryPtr
The C-style&#39;s API representation of a trajectory.
Definition: hebi.h:549
double getDuration() const
The time (in seconds) between the start and end of this trajectory.
Definition: trajectory.cpp:90
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:95
~Trajectory() noexcept
Destructor cleans up resources for trajectory.
Definition: trajectory.cpp:85
std::vector< HebiTrajectoryPtr > trajectories_
Definition: trajectory.hpp:24


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Thu May 28 2020 03:14:45