Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trajectory player fixes #989

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ class TrajectoryPlayer
double currentDuration() const;

/**
* @brief Get the trajectory duration
* @brief Get the trajectory duration at the start state
* @return The trajectory duration
*/
double trajectoryDuration() const;
double trajectoryDurationStart() const;

/**
* @brief Get the trajectory duration at the end state
* @return The trajectory duration
*/
double trajectoryDurationEnd() const;

/**
* @brief Check if the player has the reached the end of the trajectory
Expand Down Expand Up @@ -118,7 +124,8 @@ class TrajectoryPlayer

private:
TrajectoryInterpolator::UPtr trajectory_{ nullptr };
double trajectory_duration_{ 0 };
double trajectory_duration_start_{ 0 };
double trajectory_duration_end_{ 0 };
double current_duration_{ 0 };
double scale_{ 1 };
bool loop_{ false };
Expand Down
2 changes: 1 addition & 1 deletion tesseract_visualization/src/trajectory_interpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TrajectoryInterpolator::TrajectoryInterpolator(tesseract_common::JointTrajectory
double total_time = 0;
bool overwrite_dt = false;
// Check if time is populated
if (!trajectory_.empty() && (trajectory_.back().time - trajectory_.front().time) < 1e-3)
if ((trajectory_.size() > 1) && (trajectory_.back().time - trajectory_.front().time) < 1e-3)
overwrite_dt = true;

bool initial_state = true;
Expand Down
21 changes: 12 additions & 9 deletions tesseract_visualization/src/trajectory_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void TrajectoryPlayer::setTrajectory(const tesseract_common::JointTrajectory& tr
trajectory_ = std::make_unique<TrajectoryInterpolator>(trajectory);

// Get the duration
trajectory_duration_ = trajectory_->getStateDuration(trajectory_->getStateCount() - 1);
trajectory_duration_start_ = trajectory_->getStateDuration(0);
trajectory_duration_end_ = trajectory_->getStateDuration(trajectory_->getStateCount() - 1);

// Reset state
reset();
Expand Down Expand Up @@ -74,13 +75,13 @@ tesseract_common::JointState TrajectoryPlayer::setCurrentDuration(double duratio
throw std::runtime_error("Trajectory is empty!");

finished_ = false;
if (duration > trajectory_duration_)
if (duration > trajectory_duration_end_)
{
current_duration_ = trajectory_duration_;
current_duration_ = trajectory_duration_end_;
finished_ = true;
}
else if (duration < 0)
current_duration_ = 0;
else if (duration < trajectory_duration_start_)
current_duration_ = trajectory_duration_start_;
else
current_duration_ = duration;

Expand All @@ -100,9 +101,9 @@ tesseract_common::JointState TrajectoryPlayer::getNext()
auto current_time = std::chrono::high_resolution_clock::now();
current_duration_ = (scale_ * std::chrono::duration<double>(current_time - start_time_).count());

if (current_duration_ > trajectory_duration_)
if (current_duration_ > trajectory_duration_end_)
{
current_duration_ = trajectory_duration_;
current_duration_ = trajectory_duration_end_;

// Compute the interpolated state
auto mi = trajectory_->getState(current_duration_);
Expand All @@ -126,7 +127,9 @@ tesseract_common::JointState TrajectoryPlayer::getByIndex(long index) const

double TrajectoryPlayer::currentDuration() const { return current_duration_; }

double TrajectoryPlayer::trajectoryDuration() const { return trajectory_duration_; }
double TrajectoryPlayer::trajectoryDurationStart() const { return trajectory_duration_start_; }

double TrajectoryPlayer::trajectoryDurationEnd() const { return trajectory_duration_end_; }

bool TrajectoryPlayer::isFinished() const { return finished_; }

Expand All @@ -137,7 +140,7 @@ bool TrajectoryPlayer::isLoopEnabled() const { return loop_; }
void TrajectoryPlayer::reset()
{
// Reset state associated with trajectory playback
current_duration_ = 0.0;
current_duration_ = trajectory_duration_start_;

// Get the chrono time
start_time_ = std::chrono::high_resolution_clock::now();
Expand Down
2 changes: 1 addition & 1 deletion tesseract_visualization/test/trajectory_player_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST(TesseracTrajectoryPlayerUnit, TrajectoryTest) // NOLINT
TrajectoryPlayer player;
player.setTrajectory(trajectory);

EXPECT_NEAR(player.trajectoryDuration(), 9, 1e-5);
rjoomen marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_NEAR(player.trajectoryDurationEnd(), 9, 1e-5);
EXPECT_NEAR(player.currentDuration(), 0, 1e-5);

for (long i = 0; i < 10; ++i)
Expand Down
Loading