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

Off by one in getAverageSegmentDuration #1079

Merged
merged 8 commits into from
Mar 14, 2022
13 changes: 11 additions & 2 deletions moveit_core/robot_trajectory/src/robot_trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,17 @@ double RobotTrajectory::getDuration() const

double RobotTrajectory::getAverageSegmentDuration() const
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{
const bool has_initial_segment{ !duration_from_previous_.empty() && duration_from_previous_.at(0) != 0.0 };

if (duration_from_previous_.empty())
return 0.0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about restoring these two lines as an initial error check?

if (duration_from_previous_.empty())
{
  RCLCPP_WARN(rclcpp::get_logger("RobotTrajectory"), "Too few waypoints to calculate a duration. Returning 0.");
  return 0.0;
}

// If the initial segment has a duration of 0, exclude it from the average calculation
if (duration_from_previous_[0] == 0)
{
if (duration_from_previous_.size() <= 1)
{
RCLCPP_WARN(rclcpp::get_logger("RobotTrajectory"), "Too few waypoints to calculate a duration. Returning 0.");
return 0.0;
}
else
return getDuration() / static_cast<double>(duration_from_previous_.size() - 1);
}
else
return getDuration() / static_cast<double>(duration_from_previous_.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (duration_from_previous_[0] == 0)
{
if (duration_from_previous_.size() <= 1)
{
RCLCPP_WARN(rclcpp::get_logger("RobotTrajectory"), "Too few waypoints to calculate a duration. Returning 0.");
return 0.0;
}
else
return getDuration() / static_cast<double>(duration_from_previous_.size() - 1);
}
else
return getDuration() / static_cast<double>(duration_from_previous_.size());
if (has_initial_segment)
return getDuration() / static_cast<double>(duration_from_previous_.size());
else
return getDuration() / static_cast<double>(duration_from_previous_.size() - 1);

}
Expand Down