trajectory_interpolator.cpp
Go to the documentation of this file.
1 
27 /* Based on MoveIt code authored by: Ioan Sucan, Adam Leeper */
28 
30 
32 {
34  : trajectory_(std::move(trajectory))
35 {
36  double last_time = 0;
37  double current_time = 0;
38  double total_time = 0;
39  bool overwrite_dt = false;
40  // Check if time is populated
41  if ((trajectory_.size() > 1) && (trajectory_.back().time - trajectory_.front().time) < 1e-3)
42  overwrite_dt = true;
43 
44  bool initial_state = true;
45 
46  for (auto& state : trajectory_)
47  {
48  current_time = state.time;
49 
50  // It is possible for sub composites to start back from zero, this accounts for it
51  if (current_time < last_time)
52  last_time = 0;
53 
54  double dt = current_time - last_time;
55  if (overwrite_dt)
56  {
57  if (initial_state)
58  dt = 0;
59  else
60  dt = 0.1;
61  }
62  initial_state = false;
63  total_time += dt;
64  duration_from_previous_.push_back(dt);
65  state.time = total_time;
66  last_time = current_time;
67  }
68 }
69 
70 void TrajectoryInterpolator::findStateIndices(const double& duration, long& before, long& after, double& blend) const
71 {
72  if (duration < 0.0)
73  {
74  before = 0;
75  after = 0;
76  blend = 0;
77  return;
78  }
79 
80  // Find indices
81  std::size_t index = 0;
82  std::size_t num_points = trajectory_.size();
83  double running_duration = 0.0;
84  for (; index < num_points; ++index)
85  {
86  running_duration += duration_from_previous_[index];
87  if (running_duration >= duration)
88  break;
89  }
90  before = static_cast<int>(std::max<std::size_t>(index - 1, 0));
91  after = static_cast<int>(std::min<std::size_t>(index, num_points - 1));
92 
93  // Compute duration blend
94  double before_time = running_duration - duration_from_previous_[index];
95  if ((index == 0) || (after == before))
96  blend = 1.0;
97  else
98  blend = (duration - before_time) / duration_from_previous_[index];
99 }
100 
102 {
103  // If there are no waypoints we can't do anything
104  if (trajectory_.empty())
105  throw std::runtime_error("Invalid duration");
106 
107  long before = 0;
108  long after = 0;
109  double blend = 1.0;
110  findStateIndices(request_duration, before, after, blend);
111 
112  if (before < 0 && after < 0)
113  throw std::runtime_error("Invalid duration");
114 
115  if (before < 0 && after == 0)
116  return trajectory_[static_cast<std::size_t>(after)];
117 
118  if (before == static_cast<int>(trajectory_.size()) - 1)
119  return trajectory_[static_cast<std::size_t>(before)];
120 
121  if (before >= 0 && after > 0)
122  {
123  const tesseract_common::JointState& swp0 = trajectory_[static_cast<std::size_t>(before)];
124  const tesseract_common::JointState& swp1 = trajectory_[static_cast<std::size_t>(after)];
125  return interpolate(swp0, swp1, blend);
126  }
127 
128  throw std::runtime_error("Invalid duration");
129 }
130 
132 {
133  if (trajectory_.empty())
134  return 0.0;
135 
136  int s = static_cast<int>(trajectory_.size());
137  if (index >= s)
138  index = s - 1;
139 
140  return trajectory_[static_cast<std::size_t>(index)].time;
141 }
142 
143 long TrajectoryInterpolator::getStateCount() const { return static_cast<long>(trajectory_.size()); }
144 
146  const tesseract_common::JointState& end,
147  double f)
148 {
149  assert(!start.joint_names.empty());
150  assert(!end.joint_names.empty());
151  assert(start.position.rows() != 0);
152  assert(end.position.rows() != 0);
154  out.time = start.time + (end.time - start.time) * f;
155  out.joint_names = start.joint_names;
156  out.position.resize(static_cast<long>(out.joint_names.size()));
157 
158  for (long i = 0; i < static_cast<long>(out.joint_names.size()); ++i)
159  out.position[i] = start.position[i] + (end.position[i] - start.position[i]) * f;
160 
161  return out;
162 }
163 
165 
166 } // namespace tesseract_visualization
tesseract_visualization::TrajectoryInterpolator::getState
tesseract_common::JointState getState(double request_duration) const
Definition: trajectory_interpolator.cpp:101
tesseract_common::JointTrajectory::empty
bool empty() const
duration
std::chrono::system_clock::duration duration
tesseract_visualization::TrajectoryInterpolator::findStateIndices
void findStateIndices(const double &duration, long &before, long &after, double &blend) const
Definition: trajectory_interpolator.cpp:70
tesseract_common::JointTrajectory::back
reference back()
tesseract_common::JointTrajectory::front
reference front()
tesseract_visualization::TrajectoryInterpolator::TrajectoryInterpolator
TrajectoryInterpolator(tesseract_common::JointTrajectory trajectory)
Definition: trajectory_interpolator.cpp:33
trajectory_interpolator.h
Trajectory interpolator class.
tesseract_common::JointState
tesseract_common::JointState::joint_names
std::vector< std::string > joint_names
tesseract_visualization::TrajectoryInterpolator::duration_from_previous_
std::vector< double > duration_from_previous_
Definition: trajectory_interpolator.h:64
tesseract_common::JointTrajectory
tesseract_visualization::TrajectoryInterpolator::trajectory_
tesseract_common::JointTrajectory trajectory_
Definition: trajectory_interpolator.h:63
tesseract_visualization::TrajectoryInterpolator::getStateCount
long getStateCount() const
Definition: trajectory_interpolator.cpp:143
tesseract_visualization::TrajectoryInterpolator::getStateDuration
double getStateDuration(long index) const
Definition: trajectory_interpolator.cpp:131
tesseract_common::JointState::time
double time
tesseract_visualization::TrajectoryInterpolator::interpolate
static tesseract_common::JointState interpolate(const tesseract_common::JointState &start, const tesseract_common::JointState &end, double f)
Definition: trajectory_interpolator.cpp:145
tesseract_visualization::TrajectoryInterpolator::empty
bool empty() const
Definition: trajectory_interpolator.cpp:164
tesseract_common::JointTrajectory::size
size_type size() const
tesseract_common::JointState::position
Eigen::VectorXd position
tesseract_visualization
Definition: fwd.h:4


tesseract_visualization
Author(s): Levi Armstrong
autogenerated on Wed Apr 9 2025 03:03:25