trajectory_player.cpp
Go to the documentation of this file.
1 
29 #include <memory>
31 
34 
36 {
39 
41 {
42  // Prepare the new trajectory message
43  trajectory_ = std::make_unique<TrajectoryInterpolator>(trajectory);
44 
45  // Get the duration
46  trajectory_duration_start_ = trajectory_->getStateDuration(0);
47  trajectory_duration_end_ = trajectory_->getStateDuration(trajectory_->getStateCount() - 1);
48 
49  // Reset state
50  reset();
51 }
52 
53 void TrajectoryPlayer::setScale(double scale) { scale_ = scale; }
54 
56 {
57  if (!trajectory_ || trajectory_->empty())
58  throw std::runtime_error("Trajectory is empty!");
59 
60  if (trajectory_->getStateCount() > 0)
61  {
62  if (index > 0)
63  current_duration_ = trajectory_->getStateDuration(index);
64  else
66  }
67 
68  start_time_ = std::chrono::high_resolution_clock::now() -
69  std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
70  std::chrono::duration<double>(current_duration_));
71 
72  // Compute the interpolated state
73  return trajectory_->getState(current_duration_);
74 }
75 
77 {
78  if (!trajectory_ || trajectory_->empty())
79  throw std::runtime_error("Trajectory is empty!");
80 
81  finished_ = false;
83  {
85  finished_ = true;
86  }
89  else
91 
92  start_time_ = std::chrono::high_resolution_clock::now() -
93  std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
94  std::chrono::duration<double>(current_duration_));
95 
96  // Compute the interpolated state
97  return trajectory_->getState(current_duration_);
98 }
99 
101 {
102  if (!trajectory_ || trajectory_->empty())
103  throw std::runtime_error("Trajectory is empty!");
104 
105  auto current_time = std::chrono::high_resolution_clock::now();
106  current_duration_ = (scale_ * std::chrono::duration<double>(current_time - start_time_).count());
107 
109  {
111 
112  // Compute the interpolated state
113  auto mi = trajectory_->getState(current_duration_);
114 
115  // Reset the player
116  if (loop_)
117  reset();
118  else
119  finished_ = true;
120 
121  return mi;
122  }
123 
124  return trajectory_->getState(current_duration_);
125 }
126 
128 {
129  return trajectory_->getState(trajectory_->getStateDuration(index));
130 }
131 
133 
135 
137 
138 bool TrajectoryPlayer::isFinished() const { return finished_; }
139 
140 void TrajectoryPlayer::enableLoop(bool loop) { loop_ = loop; }
141 
142 bool TrajectoryPlayer::isLoopEnabled() const { return loop_; }
143 
145 {
146  // Reset state associated with trajectory playback
148 
149  // Get the chrono time
150  start_time_ = std::chrono::high_resolution_clock::now();
151 
152  // Reset finished
153  finished_ = false;
154 }
155 
156 long TrajectoryPlayer::size() const { return (trajectory_) ? trajectory_->getStateCount() : 0; }
157 
158 } // namespace tesseract_visualization
tesseract_visualization::TrajectoryPlayer::~TrajectoryPlayer
~TrajectoryPlayer()
duration
std::chrono::system_clock::duration duration
tesseract_visualization::TrajectoryPlayer::trajectoryDurationEnd
double trajectoryDurationEnd() const
Get the trajectory duration at the end state.
Definition: trajectory_player.cpp:136
tesseract_visualization::TrajectoryPlayer::setCurrentDuration
tesseract_common::JointState setCurrentDuration(double duration)
Set the current time for the player by duration.
Definition: trajectory_player.cpp:76
tesseract_visualization::TrajectoryPlayer::current_duration_
double current_duration_
Definition: trajectory_player.h:137
tesseract_visualization::TrajectoryPlayer::enableLoop
void enableLoop(bool loop)
Enable looping playback of the trajectory.
Definition: trajectory_player.cpp:140
tesseract_visualization::TrajectoryPlayer::size
long size() const
The size of the tajectory.
Definition: trajectory_player.cpp:156
tesseract_visualization::TrajectoryPlayer::currentDuration
double currentDuration() const
Get the current duration populated by the last call to getNext()
Definition: trajectory_player.cpp:132
tesseract_visualization::TrajectoryPlayer::scale_
double scale_
Definition: trajectory_player.h:138
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
trajectory_interpolator.h
Trajectory interpolator class.
tesseract_visualization::TrajectoryPlayer::reset
void reset()
Reset the state of the trajectory player.
Definition: trajectory_player.cpp:144
tesseract_common::JointState
tesseract_visualization::TrajectoryPlayer::setTrajectory
void setTrajectory(const tesseract_common::JointTrajectory &trajectory)
Set the the trajectory for the trajectory player.
Definition: trajectory_player.cpp:40
tesseract_visualization::TrajectoryPlayer::setCurrentDurationByIndex
tesseract_common::JointState setCurrentDurationByIndex(long index)
Set the current time for the player by index of the input trajectoy.
Definition: trajectory_player.cpp:55
tesseract_common::JointTrajectory
tesseract_visualization::TrajectoryPlayer::finished_
bool finished_
Definition: trajectory_player.h:140
tesseract_visualization::TrajectoryPlayer::isFinished
bool isFinished() const
Check if the player has the reached the end of the trajectory.
Definition: trajectory_player.cpp:138
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_visualization::TrajectoryPlayer::getByIndex
tesseract_common::JointState getByIndex(long index) const
Get move instruction by index.
Definition: trajectory_player.cpp:127
tesseract_visualization::TrajectoryPlayer::loop_
bool loop_
Definition: trajectory_player.h:139
tesseract_visualization::TrajectoryPlayer::isLoopEnabled
bool isLoopEnabled() const
Get if looping playback is enabled.
Definition: trajectory_player.cpp:142
tesseract_visualization::TrajectoryPlayer::getNext
tesseract_common::JointState getNext()
Get the next move instruction from the player.
Definition: trajectory_player.cpp:100
trajectory_player.h
Trajectory player class.
macros.h
tesseract_visualization::TrajectoryPlayer::setScale
void setScale(double scale)
Set the scale factor for the play back of the trajectory.
Definition: trajectory_player.cpp:53
tesseract_visualization::TrajectoryPlayer::trajectoryDurationBegin
double trajectoryDurationBegin() const
Get the trajectory duration at the begin state.
Definition: trajectory_player.cpp:134
tesseract_visualization::TrajectoryPlayer::trajectory_duration_start_
double trajectory_duration_start_
Definition: trajectory_player.h:135
tesseract_visualization::TrajectoryPlayer::trajectory_duration_end_
double trajectory_duration_end_
Definition: trajectory_player.h:136
tesseract_visualization::TrajectoryPlayer::trajectory_
std::unique_ptr< TrajectoryInterpolator > trajectory_
Definition: trajectory_player.h:134
tesseract_visualization::TrajectoryPlayer::start_time_
std::chrono::time_point< std::chrono::high_resolution_clock > start_time_
Definition: trajectory_player.h:142
tesseract_visualization
Definition: fwd.h:4
tesseract_visualization::TrajectoryPlayer::TrajectoryPlayer
TrajectoryPlayer()


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