Skip to content

findWayPointIndicesForDurationAfterStart will return index which is out of bound #3612

Closed

Description

@XingxinHE

Description

See the code at

void RobotTrajectory::findWayPointIndicesForDurationAfterStart(double duration, int& before, int& after,
double& blend) const
{
if (duration < 0.0)
{
before = 0;
after = 0;
blend = 0;
return;
}
// Find indices
std::size_t index = 0, num_points = waypoints_.size();
double running_duration = 0.0;
for (; index < num_points; ++index)
{
running_duration += duration_from_previous_[index];
if (running_duration >= duration)
break;
}
before = std::max<int>(index - 1, 0);
after = std::min<int>(index, num_points - 1);
// Compute duration blend
double before_time = running_duration - duration_from_previous_[index];
if (after == before)
{
blend = 1.0;
}
else
{
blend = (duration - before_time) / duration_from_previous_[index];
}
}

If the duration is bigger than the duration of the trajectory, the index will be index==num_points making this line double before_time = running_duration - duration_from_previous_[index]; invalid and also output a double blend not in the [0, 1] inclusive according to

* @param The progress (0 to 1) between the two waypoints, based on time (not based on joint distances).

ROS Distro

Rolling

OS and version

Ubuntu 24.04

Source or binary build?

Source

If binary, which release version?

No response

If source, which branch?

main, jazzy, humble

Which RMW are you using?

None

Steps to Reproduce

Just give a duration bigger than the trajectory duration to the findWayPointIndicesForDurationAfterStart().

Expected behavior

It should be [0, 1] and no error.

Actual behavior

Error and also blend is not in [0, 1]`

Backtrace or Console output

No response

Activity

  1. added
    bug
    on Nov 18, 2025
  2. XingxinHE commented on Nov 18, 2025

    @XingxinHE
    ContributorAuthor

    I propose the following fix and I am happy to open a PR to support OSS.

    void RobotTrajectory::findWayPointIndicesForDurationAfterStart(double duration, int& before, int& after,
                                                                   double& blend) const
    {
      if (duration < 0.0)
      {
        before = 0;
        after = 0;
        blend = 0;
        return;
      }
    
      std::size_t index = 0, num_points = waypoints_.size();
      double running_duration = 0.0;
      for (; index < num_points; ++index)
      {
        running_duration += duration_from_previous_[index];
        if (running_duration >= duration)
          break;
      }
    
      // early return
      if (index >= num_points)
      {
        before = num_points - 1;
        after = num_points - 1;
        blend = 1.0;
        return;
      }
    
      before = std::max<int>(index - 1, 0);
      after = std::min<int>(index, num_points - 1);
    
    
      double before_time = running_duration - duration_from_previous_[index];
      if (after == before)
      {
        blend = 1.0;
      }
      else
      {
        blend = (duration - before_time) / duration_from_previous_[index];
        // clamp for numerical error
       blend = std::max(0.0, std::min(1.0, blend));
      }
    }
  3. nbbrooks commented on Nov 26, 2025

    @nbbrooks
    Collaborator

    I agree - I think a shorter adjustment is to just move the double before_time = running_duration - duration_from_previous_[index]; to inside the else statement (the only place where it is used anyway).

    Could you make the PR and ideally add a unit test to https://github.com/moveit/moveit2/blob/main/moveit_core/robot_trajectory/test/test_robot_trajectory.cpp ?

  4. XingxinHE commented on Nov 27, 2025

    @XingxinHE
    ContributorAuthor

    Oh. Good one. Will do the PR with a unit test!

  5. nbbrooks commented on Sep 22, 2026

    @nbbrooks
    Collaborator

    Closed by #3626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions