diff --git a/moveit_core/robot_trajectory/src/robot_trajectory.cpp b/moveit_core/robot_trajectory/src/robot_trajectory.cpp index bfe2dae702..794ea9d78a 100644 --- a/moveit_core/robot_trajectory/src/robot_trajectory.cpp +++ b/moveit_core/robot_trajectory/src/robot_trajectory.cpp @@ -92,7 +92,22 @@ double RobotTrajectory::getDuration() const double RobotTrajectory::getAverageSegmentDuration() const { 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"), "First and only waypoint has a duration of 0."); + return 0.0; + } + else + return getDuration() / static_cast(duration_from_previous_.size() - 1); + } else return getDuration() / static_cast(duration_from_previous_.size()); }