Nav2 Navigation Stack - rolling  main
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 "tf2/LinearMath/Quaternion.hpp"
40 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
41 
42 #include "rclcpp/duration.hpp"
43 
44 #include "dwb_core/exceptions.hpp"
45 
46 namespace dwb_core
47 {
48 const geometry_msgs::msg::Pose & getClosestPose(
49  const dwb_msgs::msg::Trajectory2D & trajectory,
50  const double time_offset)
51 {
52  rclcpp::Duration goal_time = rclcpp::Duration::from_seconds(time_offset);
53  const unsigned int num_poses = trajectory.poses.size();
54  if (num_poses == 0) {
55  throw nav2_core::InvalidPath("Cannot call getClosestPose on empty trajectory.");
56  }
57  unsigned int closest_index = num_poses;
58  double closest_diff = 0.0;
59  for (unsigned int i = 0; i < num_poses; ++i) {
60  double diff = std::fabs((rclcpp::Duration(trajectory.time_offsets[i]) - goal_time).seconds());
61  if (closest_index == num_poses || diff < closest_diff) {
62  closest_index = i;
63  closest_diff = diff;
64  }
65  if (goal_time < rclcpp::Duration(trajectory.time_offsets[i])) {
66  break;
67  }
68  }
69  return trajectory.poses[closest_index];
70 }
71 
72 geometry_msgs::msg::Pose projectPose(
73  const dwb_msgs::msg::Trajectory2D & trajectory,
74  const double time_offset)
75 {
76  rclcpp::Duration goal_time = rclcpp::Duration::from_seconds(time_offset);
77  const unsigned int num_poses = trajectory.poses.size();
78  if (num_poses == 0) {
79  throw nav2_core::InvalidPath("Cannot call projectPose on empty trajectory.");
80  }
81  if (goal_time <= (trajectory.time_offsets[0])) {
82  return trajectory.poses[0];
83  } else if (goal_time >= rclcpp::Duration(trajectory.time_offsets[num_poses - 1])) {
84  return trajectory.poses[num_poses - 1];
85  }
86 
87  for (unsigned int i = 0; i < num_poses - 1; ++i) {
88  if (goal_time >= rclcpp::Duration(trajectory.time_offsets[i]) &&
89  goal_time < rclcpp::Duration(trajectory.time_offsets[i + 1]))
90  {
91  double time_diff =
92  (rclcpp::Duration(trajectory.time_offsets[i + 1]) -
93  rclcpp::Duration(trajectory.time_offsets[i])).seconds();
94  double ratio = (goal_time - rclcpp::Duration(trajectory.time_offsets[i])).seconds() /
95  time_diff;
96  double inv_ratio = 1.0 - ratio;
97  const auto & pose_a = trajectory.poses[i];
98  const auto & pose_b = trajectory.poses[i + 1];
99 
100  geometry_msgs::msg::Pose projected;
101  projected.position.x = pose_a.position.x * inv_ratio + pose_b.position.x * ratio;
102  projected.position.y = pose_a.position.y * inv_ratio + pose_b.position.y * ratio;
103  projected.position.z = pose_a.position.z * inv_ratio + pose_b.position.z * ratio;
104 
105  // Interpolate orientation using slerp
106  tf2::Quaternion q1, q2;
107  tf2::fromMsg(pose_a.orientation, q1);
108  tf2::fromMsg(pose_b.orientation, q2);
109  projected.orientation = tf2::toMsg(q1.slerp(q2, ratio));
110 
111  return projected;
112  }
113  }
114 
115  // Should not reach this point
116  return trajectory.poses[num_poses - 1];
117 }
118 
119 
120 } // namespace dwb_core