Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
trajectory_utils.cpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <dwb_core/trajectory_utils.hpp>
36 
37 #include <cmath>
38 
39 #include "rclcpp/duration.hpp"
40 
41 #include "dwb_core/exceptions.hpp"
42 
43 namespace dwb_core
44 {
45 const geometry_msgs::msg::Pose2D & getClosestPose(
46  const dwb_msgs::msg::Trajectory2D & trajectory,
47  const double time_offset)
48 {
49  rclcpp::Duration goal_time = rclcpp::Duration::from_seconds(time_offset);
50  const unsigned int num_poses = trajectory.poses.size();
51  if (num_poses == 0) {
52  throw nav2_core::PlannerException("Cannot call getClosestPose on empty trajectory.");
53  }
54  unsigned int closest_index = num_poses;
55  double closest_diff = 0.0;
56  for (unsigned int i = 0; i < num_poses; ++i) {
57  double diff = std::fabs((rclcpp::Duration(trajectory.time_offsets[i]) - goal_time).seconds());
58  if (closest_index == num_poses || diff < closest_diff) {
59  closest_index = i;
60  closest_diff = diff;
61  }
62  if (goal_time < rclcpp::Duration(trajectory.time_offsets[i])) {
63  break;
64  }
65  }
66  return trajectory.poses[closest_index];
67 }
68 
69 geometry_msgs::msg::Pose2D projectPose(
70  const dwb_msgs::msg::Trajectory2D & trajectory,
71  const double time_offset)
72 {
73  rclcpp::Duration goal_time = rclcpp::Duration::from_seconds(time_offset);
74  const unsigned int num_poses = trajectory.poses.size();
75  if (num_poses == 0) {
76  throw nav2_core::PlannerException("Cannot call projectPose on empty trajectory.");
77  }
78  if (goal_time <= (trajectory.time_offsets[0])) {
79  return trajectory.poses[0];
80  } else if (goal_time >= rclcpp::Duration(trajectory.time_offsets[num_poses - 1])) {
81  return trajectory.poses[num_poses - 1];
82  }
83 
84  for (unsigned int i = 0; i < num_poses - 1; ++i) {
85  if (goal_time >= rclcpp::Duration(trajectory.time_offsets[i]) &&
86  goal_time < rclcpp::Duration(trajectory.time_offsets[i + 1]))
87  {
88  double time_diff =
89  (rclcpp::Duration(trajectory.time_offsets[i + 1]) -
90  rclcpp::Duration(trajectory.time_offsets[i])).seconds();
91  double ratio = (goal_time - rclcpp::Duration(trajectory.time_offsets[i])).seconds() /
92  time_diff;
93  double inv_ratio = 1.0 - ratio;
94  const geometry_msgs::msg::Pose2D & pose_a = trajectory.poses[i];
95  const geometry_msgs::msg::Pose2D & pose_b = trajectory.poses[i + 1];
96  geometry_msgs::msg::Pose2D projected;
97  projected.x = pose_a.x * inv_ratio + pose_b.x * ratio;
98  projected.y = pose_a.y * inv_ratio + pose_b.y * ratio;
99  projected.theta = pose_a.theta * inv_ratio + pose_b.theta * ratio;
100  return projected;
101  }
102  }
103 
104  // Should not reach this point
105  return trajectory.poses[num_poses - 1];
106 }
107 
108 
109 } // namespace dwb_core